LXC - Guide to building a LXC Lab

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
if you're in the tech sector then having a good lab environment is critical to help you develop your skills being able to create and manage a multi-server lab environment gives you the ability to learn practically any technology out there in my home lab i run lxc and i'm able to create new virtual machines in seconds in case you're wondering lxc stands for linux containers but it's different than something like docker whereas docker is meant to run a single task in a container lxc functions more like a virtual machine all you need to get started is a single host running linux my recommendation is to go with ubuntu if you can but it is supported on many linux kernels i have my lxc environment running on a raspberry pi this gives me an always-on lab environment but feel free to install it on your desktop or laptop machine to get started just run the lxc install command applicable to your operating system once the installation is complete you're going to run the init command this is going to take you through a guided installation for the most part you can just accept the defaults for my installation i just accepted the defaults for every option available after you've run the initialization you're ready to get started let's hop into the terminal and i'll show you all the commands you need to know to manage your virtual machines okay so with lxc if you want to see the machines that you're currently running just run a lxc list and this is going to return information on your current machines that you're running so you can see that i have five machines here you can see node one two three four and then this one that just has a randomly generated name now by default the lxc list gives some pretty good information you can see the name the state and the ip addresses but i usually use my own version of the lxc list command that gives a little bit better information so that command is this one right here you can see lxc list and then i'm specifying my own columns so i'm doing the name and the state the ipv4 address and then the image description so i find this way is a little bit cleaner and you can alias this to something else if you want but yeah basically i find it's pretty important to know the image name of your actual containers and virtual machines now to create your own machine is very easy all you need to do that is do an lxc launch and then specify the image repository and then the version so i'm going to use the image repository of ubuntu and i'm going to say 18.04 and then after that i'm going to specify a container name so i'll go ubuntu one and now it's creating my virtual machine ubuntu one and now it's up and running so now if i do an lxc list command you can see that it's up and it's on this ubuntu 18.04 image now if you want to search what images are out there the best command for that is lxc image alias and then specify the image repository the two big ones are ubuntu so you can specify it like this and then it's going to search for everything in the ubuntu repository and i actually need to say list here so let's fix that and it should come back pretty quickly here and return you all the results so you can see all the ubuntu images now if you were just looking for one of them in particular you just take the same command and then add at the end here what you're looking for so i could go 18.04 and you can see that this returned all the images with the alias 18.04 in it now the other repository is not ubuntu it's actually just images and for this one you can basically put any linux container image so i'll just search for alpine and it'll just take a minute as it searches but it should return all the alpine images and you can see yeah there's a bunch of alpine images now if i want to launch one basically just take my command that i did before the lxc launch and then i'm going to say images and let's grab this one right here and i'll rename it alpine one and now it's creating the alpine virtual machine and if i run my list command again you can see that it was created as well so a very simple way to get virtual machines up and running very quickly now i'm going to go over a few more commands that i find helpful when using lxc so i like the lxc info command and then you specify the container name and basically you can see it gives you a dump of information it gives you the name the architecture when it was created and then ip address and network information so pretty good stuff now if you actually want to run a command on one of these containers what you can do is do the lxc exec and then specify the container name so i'll just go hostname and you can see it returned its hostname now if you actually want to go into one of these containers you can go lxcx specify the container name again and then just specify shell environment so i'll say bash and now i'm in ubuntu one now if i want to do this for my alpine image alpine doesn't actually have bash installed so you can see that doesn't work so you just actually go sh and that'll bring you in there so that's a good way to hop in and out of your containers now if you want to stop one of your containers you can go lxc stop and then specify the container name if i go lxc list you can see alpine one is stopped and you can delete it very easily as well and it's gone now let's say i created one of these machines and i set up a bunch of settings and now i basically want to clone it you can do this very easily with the lxc copy command so to do that just go lxc copy and then just specify the machine name and then the destination name so i'll go ubuntu 1 copy it to ubuntu 2. and now when i do an lxc list you can see that i got both containers running it looks like the new one doesn't start by default so let's start it up and now it's up now one thing you can do with these containers is push and pull files from them so one thing i commonly do when i set up a machine is i push over my ssh authorized keys and this allows me to ssh into the machines so my ssh keys are located under dot ssh and then let's grab this so and then list this out so i want to copy over this file to my ubuntu machine so to do that i go lxc file push and let's say this one and then authorize keys and then the destination should be root slash dot ssh and then we should specify the file name as well so this should push it over and we can see it failed and the reason behind that is you actually need to specify the container name so it knows where to push the files to so you go ubuntu one so the container name of ubuntu one and then the file location so let's do that now we should be able to ssh to it so though ssh root at and then since there's no name resolution right now we'll just throw in the ip address and connect and you can accept this and it should be able to connect yep we can see that if we tried to do this to ubuntu 2 oh it doesn't have an ip address yet but if we did if we tried it to ubuntu two it would fail because it doesn't have that authorized key file to it so you can see we try it says permission denied so let's push it over to ubuntu two and now when we connect it should work i'm hoping yep so it connected fine so that's a really easy way to push over your authorized keys file so you can ssh into them now if i wanted to pull a file it's basically the same command except for i would reverse these so i'd go ubuntu one and then you would do a pull so this would be a way to pull a file from it now if you didn't want to push or pull files but you wanted to edit it you could do that as well so let's take this out and we'll say lxe file edit and then we'll go ubuntu one and you can see that i'm connected to it now another great feature with lxc is what we call snapshots and basically this allows you to make a restore point on your machines and then revert to that snapshot if you ever need to so if we go lxc list you can see none of my containers have a snapshot but if i want to take a snapshot i can go lxc snapshot and we'll go ubuntu1 and then we'll just hit enter and it'll take the snapshot now if i do lxc list you can see ubuntu 1 has a snapshot and if i want to learn more about that snapshot i could go lxc info and then the container name and there you can see that it has that one snapshot now if you wanted to restore to that snapshot all you need to do is uh lxc restore and then container name so this is going to return me to the restore point of that snapshot so obviously you would probably do this before you make any major changes to your container or virtual machine you take the snapshot go in there make your changes and if something went wrong you could always just go back to that snapshot so the container has been restored and if we go lxce list you can see that it is running it still has that snapshot so you can always revert to that snapshot whenever you want now snapshots do take up some memory so you don't really want to leave a bunch of snapshots hanging there on containers or virtual machines especially if you're going to be keeping them for a while so if you want to delete them you just run the lxc delete and then make sure to put the container name and then slash the snapshot so that's how you delete the snapshot and you can see it's gone there now one thing that i like to do for this is make everything accessible on the lan you can see nodes one to four are on this 10.1.1 subnet and then these other ones are on 10.211. so that's basically just like a default subnet that lxe creates and connects the containers to by default but if you want to get your containers on your lan what you can do is map them to a bridged adapter so to do that let's clear our screen what you need to do is create a bridged adapter now this might be done differently depending on the operating system that you're running lxc from i'm on uh ubuntu 20 i think yeah i'm on ubuntu 20.10 and the way i do this is through a netplan file so i'm going to pull up that file right now it's at this location right here and these are my settings and i have this in the description below for anyone interested but as you can see i have an ethernet adapter it's just using dhcp and then this was what i actually had to add so i have f0 and then br0 so basically what i need to do is map my containers to this bridged adapter and this is going to bring them onto my lan so they can pull dhcp addresses from my router so this is a pretty big command so i'm just going to copy and paste it in and go over it so you can see it's lxc config device add the container name f0 this is the container nick name so f0 is what you're going to use as well nick the neck type should be bridged and then parent is going to be your bridged adapter so i called mine br0 so i'm going to map it to that one put that in and you can see that ethernet adapter has been added to ubuntu 1. if you do lxc list you can see there's no more ip address you actually need to stop and start the container for this to work so i'll go lxc stop ubuntu one and then we're going to start it up again and now if i do a lxc list you can see that it's in this 10.1.1 subnet range which is on my lan so this is a routable subnet on my network so i'm able to access it from any device in my house so very convenient if you're running a home lab i highly recommend connecting your containers to a bridged adapter anyways i hope this video was helpful if you have any questions please leave them below and i'll try to get back to them and if you're interested in learning more about linux and creating your home lab so you can further your career in devops and it then go ahead and check out the other videos on my channel thanks so much for watching and i hope to see you all in the next video
Info
Channel: DevOps Journey
Views: 3,599
Rating: 5 out of 5
Keywords: linux containers, how to, linux container, lxc ubuntu, cgroups ubuntu, containers on ubuntu, lxc, lxd, lxd hypervisor, homelab, setting up homelab, devops, devops homelab
Id: cqOtksmsxfg
Channel Id: undefined
Length: 15min 31sec (931 seconds)
Published: Thu Mar 04 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.