diff options
author | luxagraf <sng@luxagraf.net> | 2019-05-06 09:10:11 -0500 |
---|---|---|
committer | luxagraf <sng@luxagraf.net> | 2019-05-06 09:10:11 -0500 |
commit | 7f6fc23adcb42a6f25e8ab93fad9cbaac689a742 (patch) | |
tree | 2cea2d93d9655983b13406647f6d62ac9bf291ba /switching-to-lxc-lxd-for-django-dev-work.txt | |
parent | 41d5367448cd69158a61cf4e022baf803b157af7 (diff) |
added recent posts to archive and some edits of old posts
Diffstat (limited to 'switching-to-lxc-lxd-for-django-dev-work.txt')
-rw-r--r-- | switching-to-lxc-lxd-for-django-dev-work.txt | 188 |
1 files changed, 0 insertions, 188 deletions
diff --git a/switching-to-lxc-lxd-for-django-dev-work.txt b/switching-to-lxc-lxd-for-django-dev-work.txt deleted file mode 100644 index fa856d8..0000000 --- a/switching-to-lxc-lxd-for-django-dev-work.txt +++ /dev/null @@ -1,188 +0,0 @@ -I've used Vagrant to manage my local development environment for quite some time. The developers I used to work with used it, and everyone seemed happy with it. While I have no particular love for it, it works well enough.Eventually I got comfortable enough with Vagrant that I started using it in my own projects. I even wrote about [setting up a custom Debian 9 Vagrant box]() to mirror the server running this site. - -Despite that, I've never really liked Vagrant. Using Virtualbox as a provider -- pretty much the only option when your team uses Linux, Windows, and Mac -- means running a huge VM that gobbles a ton of memory. - -My laptop only has 8GB of RAM. The internet being as bloated as it is, my browser is always taking about 2GB, throwing in two Vagrant machines and I'm pretty much maxed out. Plus Django's dev server is painfully slow to reload any changes. - -Recently I was talking with one of Canonical's [MAAS](https://maas.io/) developers and topic of containers came up. When I mentioned I really didn't like Docker, he nodded sagely and told me a needed to use LXD. This stuck in my head the way things sometimes do, and later that day I began to look into LXD and LXC. The more I read on the [LinuxContainers](https://linuxcontainers.org/) site, the more I liked the idea. Since I like to tinker, I dove right in and now, a few days later, there's not a Vagrant machine left on my laptop. - -To be fair, you can use Vagrant to manage LXC containers, but I don't know why you'd bother. LXD's management tools and config system works great (and I say this as someone very familiar with Vagrant's tools), why add another tool to the mix?[^1] - -LXC/LXD is blazing fast, lightweight, and dead simple. To quote, Canonical's [Michael Iatrou](https://blog.ubuntu.com/2018/01/26/lxd-5-easy-pieces), LXC "liberates your laptop from the tyranny of heavyweight virtualization and simplifies experimentation." - -Here's how I'm using it for Django development on Arch Linux. I've also included instruction for Ubuntu since I set it up there as well. - -### What's the difference between LXC, LXD and `lxc` - -I wrote this guide in part because I've been hearing about LXC for ages -- I've even mentioned it in Ubuntu reviews when it got significant updates -- but part of what stopped me from using it is that it sounded overwhelming and confusing, too enterprisey you might say. It's really not though, in fact I found it easier to understand than Vagrant or Docker. - -So what is a LXC container, what's LXD, and how are either different than say a VM or for that matter Docker? - -* LXC - low-level tools and a library to create and manage containers, powerful, but complicated. -* LXD - is a daemon which provides a REST API to drive LXC containers, much more user-friendly -* `lxc` - the command line client for LXD. - -In LXC parlance a container is essentially a virtual machine, if you want to get pedantic, see Stéphane Graber's post on the [various terms and components that make up LXD](https://stgraber.org/2016/03/11/lxd-2-0-introduction-to-lxd-112/). For the most part though, interacting with an LXC container is like interacting with a VM. You say ssh, LXD says socket, potato, potahto. Mostly. - -An LXC container is not a container in the same sense that Docker talks about containers. Think of it more as a VM that only uses the resources it needs to do whatever it's doing. Running this site in an LXC container uses very little RAM. Running it in Vagrant uses 2GB of RAM because that's what I allocated to the VM -- that's what it uses even if it doesn't need it. LXC is much smarter than that. - -Now what about LXD? LXC is the low level tool, you don't really need to go there. If you're doing massive enterprise deployments you probably want the nova-lxd OpenStack plugin, so actually, I can't really see where you'd need to interact directly with LXC. Instead you interact with your LXC container via the LXD API. It uses YAML config files and a command line `lxc`. - -That's the basic stack, let's install it. - -### Install LXD - -On Arch I used the version of [LXD in the AUR](), but Ubuntu users should go with the Snap package. Either way you should get DNSMasq and a few other tools you'll need to handle networking between your machine and the LXC container we'll spin up in a bit. The other thing you'll want is your distros' Btrfs or ZFS tools. - -Part of LXC's magic relies on either Btrfs and ZFS to read a virtual disk not as a file the way Virtualbox and others do, but as a block device. Both filesystems also offer copy-on-write cloning and snapshot features, which makes it simple and fast to spin up new containers. It takes about 6 seconds to install and boot a complete and fully functional LXC container on my laptop, and most of that time is downloading the image file from the remote server. It takes about 3 seconds to clone that fully provisioned base container into a new container. - -In the end I set up my Arch machine using Btrfs and Ubuntu using ZFS to see if I could see any difference (so far, that would be no, the only difference I've run across in my research is that Btrfs can run LXC containers inside LXC containers. Turtles all the way down). - -Assuming you have Snap packages set up already, Debian and Ubuntu users can get everything they need to install and run LXC with these commands: - -~~~~console -apt install zfsutils-linux -~~~~ - -And then install the snap version of lxd with: - -~~~~console -snap install lxd -~~~~ - -Once that's done we need to initialize lxd. I went with the defaults for everything. I've printed out entire init command output so you can see what will happen: - -~~~~console -sudo lxd init -Create a new BTRFS pool? (yes/no) [default=yes]: -would you like to use LXD clustering? (yes/no) [default=no]: -Do you want to configure a new storage pool? (yes/no) [default=yes]: -Name of the new storage pool [default=default]: -Name of the storage backend to use (btrfs, dir, lvm) [default=btrfs]: -Create a new BTRFS pool? (yes/no) [default=yes]: -Would you like to use an existing block device? (yes/no) [default=no]: -Size in GB of the new loop device (1GB minimum) [default=15GB]: -Would you like to connect to a MAAS server? (yes/no) [default=no]: -Would you like to create a new local network bridge? (yes/no) [default=yes]: -What should the new bridge be called? [default=lxdbr0]: -What IPv4 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]: -What IPv6 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]: -Would you like LXD to be available over the network? (yes/no) [default=no]: -Would you like stale cached images to be updated automatically? (yes/no) [default=yes] -Would you like a YAML "lxd init" preseed to be printed? (yes/no) [default=no]: yes -~~~~ - -LXD will then spit out the contents of the profile you just created. It's a YAML file and you can edit it as you see fit after the fact. You can also create more than one profile if you like. To see all installed profiles use: - -~~~~console -lxc profile list -~~~~ - -To view the contents of a profile use: - -~~~~console -lxc profile show <profilename> -~~~~ - -To edit a profile use: - -~~~~console -lxc profile edit <profilename> -~~~~ - -So far I haven't needed to edit the profile by hand. - -I've also been happy with all the defaults although, when I do this again, I will probably enlarge the storage pool, and maybe partition off some dedicated disk space. But for now I'm just trying to figure things out so defaults it is. One more step in our setup, by default LXD runs as the lxd group, to interact with containers we'll need to part of that group. - -~~~~console -sudo usermod -a -G lxd yourusername -~~~~ - -#####Special note for Arch users. - -To run unprivileged containers as your own user, you'll need to jump through a couple extra hoops. As usual, the [Arch User Wiki](https://wiki.archlinux.org/index.php/Linux_Containers#Enable_support_to_run_unprivileged_containers_(optional)) has you covered. Read through and follow those instructions and then reboot and everything below should work as you'd expect. - -### Create Your First LXC Container - -Okay, now let's create our first container. - -This website runs on a Debian VM currently hosted on Vultr.com so I'm going to spin up a Debian container to mirror this environment for local development and testing. - -To create a new LXC container we use the `launch` command of the `lxc` tool. - -Out of the box there are four ways you can get LXC containers, local (meaning a container base you've created), images (which come from [https://images.linuxcontainers.org/](https://images.linuxcontainers.org/), ubuntu (release versions of Ubuntu), and ubuntu-daily (daily images). The images on linuxcontainers are unofficial, but the Debian image I used worked perfectly. There's also Alpine, Arch CentOS, Fedora, openSuse, Oracle, Palmo, Sabayon and lots of Ubuntu images. Pretty much every architecture you could image is in there. - -I created a Debian 9 Stretch container with the amd64 image. To create an LXC container from one of the remote images the basic syntax is `lxc launch images:distroname/version/architecture containername`. For example: - -~~~~console -lxc launch images:debian/stretch/amd64 debian-base -Creating debian-base -Starting debian-base -~~~~ - -That will grab the amd64 image of Debian 9 Stretch and create a container out of it and then launch it. Now if we look at the list of installed containers we should see this: - -~~~~console -lxc list -+-------------+---------+-----------------------+-----------------------------------------------+------------+-----------+ -| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS | -+-------------+---------+-----------------------+-----------------------------------------------+------------+-----------+ -| debian-base | RUNNING | 10.171.188.236 (eth0) | fd42:e406:d1eb:e790:216:3eff:fe9f:ad9b (eth0) | PERSISTENT | | -+-------------+---------+-----------------------+-----------------------------------------------+------------+-----------+ -~~~~ - -Very cool, now what? This is what I love about LXC, we can interact with our container pretty much the same way we'd interact with a VM. Let's connect to the root shell: - -~~~~console -lxc exec debian-base -- /bin/bash -~~~~ - -Look at your prompt and you'll notice it says `root@nameofcontainer`. Now you can install everything you need on your container. For me, setting up a django dev environment, that means postgres, python, virtualenv, and, for this site, all the geodjango requirements (postgis, GDAL, etc), along with a few other odds and ends. - -You don't have to do it from inside the container though. Part of LXD's charm is to be able to run commands without logging into anything. Instead you can do this: - -~~~~console -lxc exec debian-base -- apt update -lxc exec debian-base -- apt install postgresql postgis virtualenv -~~~~ - -LXD will output the results of your command as if you were SSHed into a VM. Not being one for typing, I created a bash alias that looks like this: `alias luxdev='lxc exec debian-base --'` so that all I need to type is `luxdev <command>`. - -What I haven't figured out how to do is chain commands, this does not seem to work: - -~~~~console -lxc exec debian-base -- su - lxf && cd site && source venv/bin/activate && ./manage.py runserver 0.0.0.0:8000 -~~~~ - -According to a bug report, it should work in quotes, but it doesn't for me. Something must have changed since then, or I'm doing something wrong. - -One other thing what was not simple to figure out is how to get a directory on your host machine mounted in your LXC instance. To do that you'll need to edit `/etc/subuid` and `/etc/subgid` to add your user. Use the `id` command to get your user id (it's probably 1000 but if not, adjust the commands below). Once you have your user id, add it to the files with this one liner I got from the [Ubuntu blog](https://blog.ubuntu.com/2016/12/08/mounting-your-home-directory-in-lxd): - -~~~~console -echo 'root:1000:1' | sudo tee -a /etc/subuid /etc/subgid -~~~~ - -Then you need to configure your LXC instance to use the same uid: - -~~~~console -lxc config set debian-base raw.idmap 'both 1000 1000' -~~~~ - -The last step is to add a device to your config file so LXC will mount it. You'll need to stop and start it for the changes to take effect. - -~~~~console -lxc config device add debian-base sitedir disk source=/path/to/your/directory path=/path/to/where/you/want/folder/in/lxc -lxc stop debian-base -lxc start debian-base -~~~~ - -#####Shoulders stood upon - -* [Stéphane Graber's 12 part series on lxd 2.0](https://stgraber.org/2016/03/11/lxd-2-0-blog-post-series-012/) - He wrote LXC and LXD, this is the best resource I found and highly recommend reading it all. -* [Mounting your home directory in LXD](https://blog.ubuntu.com/2016/12/08/mounting-your-home-directory-in-lxd) -* [Official how to](https://linuxcontainers.org/lxd/getting-started-cli/) -* [Linux Containers Discourse site](https://discuss.linuxcontainers.org/t/deploying-django-applications/996) -* [LXD networking: lxdbr0 explained](https://blog.ubuntu.com/2016/04/07/lxd-networking-lxdbr0-explained) - - -[^1]: Because you work with developers who use Windows would be one answer I suppose, but LXC/LXD developer Stéphane Graber has some instructions on how you can [interact with LXD from Mac and Windows](https://stgraber.org/2017/02/09/lxd-client-on-windows-and-macos/). |