{"id":1085,"date":"2023-02-18T21:11:52","date_gmt":"2023-02-18T21:11:52","guid":{"rendered":"https:\/\/labiol.xyz\/?p=1085"},"modified":"2023-02-19T06:20:04","modified_gmt":"2023-02-19T06:20:04","slug":"lxc-container-for-lab-environment","status":"publish","type":"post","link":"https:\/\/www.labiol.xyz\/index.php\/2023\/02\/18\/lxc-container-for-lab-environment\/","title":{"rendered":"LXC container for lab environment."},"content":{"rendered":"\n<p>Power of the containers (mainly based on docker) has been proved many, many times. But what in the scenario you have very, very limited resources in your virtual lab and lets say, small debian installation with very few GB of RAM and 1 CPU. With LXC you can use this resources in very convenient and efficient way, to build few separated environment that will serve services for you like DNS, DHCP or web server to provide easy file access in your VMware lab environment. All done in a way you like, as LXC provides full Linux OS in those separated environment. For every of them you can use apt to install and then configure application whatever you like.  <\/p>\n\n\n\n<p>I have found such very nice to use, especially with VMware environment, as such one VM (with many containers) can be with little configuration change cloned for many environments and adjusted in a way that is needed &#8211; in that sense, it can be duplicated and used several times without huge time effort.<\/p>\n\n\n\n<p> At home lab, my favorite distribution to use is Debian, and so this configuration example is based on it.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Download and install Debian. I have found that net installation iso is very nice approach as it speeds up task to create USB bootable drive (350MB). Also installation time is short as Debian installer needs to download only small amount of packages (choose just CLI environment and SSH service, no GUI like gnome we do not really need)<\/li>\n\n\n\n<li>Hopefully installed system has working IP address (DHCP). At this point, do not modify the network, just use it download necessary packages plus your favorite one. <br>Packages you will need for Debian 11: <code>apt-get install lxc libvirt0 libpam-cgfs bridge-utils uidmap libvirt-clients libvirt-daemon-system iptables ebtables dnsmasq-base libxml2-utils iproute2<\/code> &#8212; not necessary all needed, but better to be on safe site.<\/li>\n\n\n\n<li>Lets configure bridge interface for communication containers with the word.<br>Use ip a command to check network interfaces name (<code>eno0 <\/code>in this example)<br> then edit:<br><code>\/etc\/network\/interfaces<\/code><br>and set the file accordingly:<br><\/li>\n<\/ul>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-bash\" data-lang=\"Bash\"><code>l@host:~$ cat \/etc\/network\/interfaces\n# This file describes the network interfaces available on your system\n# and how to activate them. For more information, see interfaces(5).\n\nsource \/etc\/network\/interfaces.d\/*\n\n# The loopback network interface\nauto lo\niface lo inet loopback\n\n#auto eno0\n#iface eno0 inet static\n#\taddress 192.168.1.19\n#\tnetmask 255.255.255.0\n#\tgateway 192.168.1.1\n\nauto br0\niface br0 inet static\n      bridge_ports eno0\n      bridge_fd 0\n      address 192.168.1.19\n      netmask 255.255.255.0\n      network 192.168.1.0\n      broadcast 192.168.1.255\n      gateway 192.168.1.1\n      # dns-* options are implemented by the resolvconf package, if installed\n      #dns-nameservers 8.8.8.8 1.1.1.1\n      #dns-search your.search.domain.here\n<\/code><\/pre><\/div>\n\n\n\n<p>Change \/etc\/default\/lxc-net to disable lxc bridge as we don&#8217;t really need it:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-bash\" data-lang=\"Bash\"><code>#USE_LXC_BRIDGE=&quot;true&quot;\nUSE_LXC_BRIDGE=&quot;false&quot;<\/code><\/pre><\/div>\n\n\n\n<p>LXC can create own bridge to use simply use NAT for containers.<\/p>\n\n\n\n<p>Also edit \/etc\/lxc\/default.conf, with the following:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-bash\" data-lang=\"Bash\"><code>cat \/etc\/lxc\/default.conf \nlxc.net.0.type = veth\n#lxc.net.0.link = lxcbr0\nlxc.net.0.link = virbr0\nlxc.net.0.flags = up\n\nlxc.apparmor.profile = generated\nlxc.apparmor.allow_nesting = 1<\/code><\/pre><\/div>\n\n\n\n<p>Restart network (<code>\/etc\/init.d\/networking restart<\/code>). While you are doing that, it would be good to have console access to VM, there are many changes and it is likely that network won&#8217;t work at the first try. It would be good also to restart whole server and verify how it is working after.<\/p>\n\n\n\n<p>Verify if bridge has created with command <code>ip a<\/code><br>Try to ping gateway, other networks and verify also if br0 interface is reachable.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>create first container and configure it:<\/li>\n<\/ul>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-bash\" data-lang=\"Bash\"><code>lxc-create -n lxdeb1 -t debian -- -r stretch<\/code><\/pre><\/div>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-bash\" data-lang=\"Bash\"><code>lxc-start -n lxdeb1\nlxc-attach lxdeb1  \/\/check if you have access and if it is working fine; verify the network<\/code><\/pre><\/div>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-bash\" data-lang=\"Bash\"><code>cat \/etc\/network\/interfaces\nauto lo\niface lo inet loopback\n\n#auto eth0\n#iface eth0 inet dhcp<\/code><\/pre><\/div>\n\n\n\n<p>network\/interfaces we are changing inside the container as we don&#8217;t really need that configuration, network interface will be configured by the container configuration (see below)<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-bash\" data-lang=\"Bash\"><code>lxc-stop lxdeb1<\/code><\/pre><\/div>\n\n\n\n<p>Reconfigure container with our new br0 interface, file: <code><strong>\/var\/lib\/lxc\/lxdeb1\/config<\/strong><\/code>: \/\/modify lxdeb1 with yours container name.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-bash\" data-lang=\"Bash\"><code>## Network\nlxc.uts.name = lxdeb1\nlxc.net.0.type = veth\nlxc.net.0.flags = up\n\n## Network\nlxc.utsname = containershostname\nlxc.network.type = veth\nlxc.network.flags = up\n\n# that&#39;s the interface defined above in host&#39;s interfaces file\nlxc.network.link = br0\n\n# name of network device inside the container,\n# defaults to eth0, you could choose a name freely\n# lxc.network.name = lxcnet0 \n\nlxc.network.hwaddr = 00:FF:AA:00:00:01\n\n# the ip may be set to 0.0.0.0\/24 or skip this line\n# if you like to use a dhcp client inside the container\nlxc.network.ipv4 = 192.168.1.110\/24\n\n# define a gateway to have access to the internet\nlxc.network.ipv4.gateway = 192.168.1.1\n\nlxc.apparmor.profile = generated\nlxc.apparmor.allow_nesting = 1\nlxc.rootfs.path = dir:\/var\/lib\/lxc\/unfiy\/rootfs\n\n# Common configuration\nlxc.include = \/usr\/share\/lxc\/config\/debian.common.conf\n\n# Container specific configuration\nlxc.tty.max = 4\nlxc.uts.name = unfiy\nlxc.arch = amd64\nlxc.pty.max = 1024\n\n<\/code><\/pre><\/div>\n\n\n\n<p>Above example from: <a href=\"https:\/\/wiki.debian.org\/LXC\/SimpleBridge#Host_device_as_bridge\">https:\/\/wiki.debian.org\/LXC\/SimpleBridge#Host_device_as_bridge<\/a><\/p>\n\n\n\n<p>Update config for our container<\/p>\n\n\n\n<p><code>sudo lxc-update-config -c \/var\/lib\/lxc\/lxdeb1\/config<\/code><\/p>\n\n\n\n<p>If not error here, you can start container, attach to it and verify your network configuration, verify if container is reachable from your lab network.<\/p>\n\n\n\n<p>That&#8217;s it. Now you can create few more containers and use it in a way you need in your lab.<\/p>\n\n\n\n<p>Other necessary command:<\/p>\n\n\n\n<div class=\"wp-block-group is-vertical is-layout-flex wp-container-core-group-is-layout-8cf370e7 wp-block-group-is-layout-flex\">\n<p>sudo lxc-info lxdeb1<\/p>\n\n\n\n<p>lxc-autostart lxdeb1<\/p>\n<\/div>\n\n\n\n<p>If autostart won&#8217;t work for you as expected (didn&#8217;t work for me) then just add to the container config file the following: <code>lxc.start.auto = 1<\/code><\/p>\n\n\n\n<p>Images list can be found here: <a href=\"https:\/\/uk.lxd.images.canonical.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/uk.lxd.images.canonical.com\/<\/a><\/p>\n\n\n\n<p>Hope it was helpful, comments are very welcome.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Power of the containers (mainly based on docker) has been proved many, many times. But what in the scenario you have very, very limited resources in your virtual lab and lets say, small debian installation with very few GB of RAM and 1 CPU. With LXC you can use this &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,2],"tags":[],"class_list":["post-1085","post","type-post","status-publish","format-standard","hentry","category-linux","category-vmware"],"_links":{"self":[{"href":"https:\/\/www.labiol.xyz\/index.php\/wp-json\/wp\/v2\/posts\/1085","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.labiol.xyz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.labiol.xyz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.labiol.xyz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.labiol.xyz\/index.php\/wp-json\/wp\/v2\/comments?post=1085"}],"version-history":[{"count":23,"href":"https:\/\/www.labiol.xyz\/index.php\/wp-json\/wp\/v2\/posts\/1085\/revisions"}],"predecessor-version":[{"id":1109,"href":"https:\/\/www.labiol.xyz\/index.php\/wp-json\/wp\/v2\/posts\/1085\/revisions\/1109"}],"wp:attachment":[{"href":"https:\/\/www.labiol.xyz\/index.php\/wp-json\/wp\/v2\/media?parent=1085"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.labiol.xyz\/index.php\/wp-json\/wp\/v2\/categories?post=1085"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.labiol.xyz\/index.php\/wp-json\/wp\/v2\/tags?post=1085"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}