LXD/LXC Containers
- Creator/Maintainter: Yogan Naidoo (talk) - nTier Software Services
- Current Version: 1.0.0
- License: GPLv2
Credits
- Canonical Ltd
Background
I have just started dabbling with LXC containers and I am excited with my experience thus far. I find them easy to use with command line instructions. I like how lightweight these containers are and that one can use various Linux distros as the os of the container. So stopping, starting, creating, moving, copying and deleting are all easily achievable and happens quickly. I will document my use case below. My intention, however, is to introduce this technology to others so that more people can experiment with these containers. Ultimately more innovative use cases may be shared thus raising the productivity of our iDempiere community.
Resources
The following were helpful to me:
- Getting started with LXC containers - https://www.youtube.com/watch?v=CWmkSj_B-wo&list=PL34sAs7_26wPfLNZ5NKoH0RKbTAWMlh2I&index=2
- Beginners guide on using LXC profiles - Linux Containers - https://www.youtube.com/watch?v=ZEL1BSoUhSI&list=PL34sAs7_26wPfLNZ5NKoH0RKbTAWMlh2I&index=3
- LXD Containers backup, import, export, migrate between lxd instances - https://www.youtube.com/watch?v=RebZppmY8nw&list=PL34sAs7_26wPfLNZ5NKoH0RKbTAWMlh2I&index=4
- https://blog.simos.info/tag/lxd/
- LXD 4.0 quick recipe: LXC and KVM coexisting - https://discourse.ubuntu.com/t/lxd-4-0-quick-recipe-lxc-and-kvm-coexisting/15222
...used a major portion of this article's profile as my own
- https://linuxcontainers.org/lxd/advanced-guide/
- cloud-init Documentation - https://cloudinit.readthedocs.io/en/latest/
Aliases
I use the following aliases:
- lxc alias add login 'exec @ARGS@ --mode interactive -- /bin/sh -xac $@yogan - exec /bin/login -p -f '
...This allows me to log into a container with user name 'yogan' like: lxc login <container>
- lxc alias add delete 'delete -f'
- lxc alias add shell 'exec @ARGS@ -- bash
...This allows me to log into a container with 'root' like: lxc shell <container>
My Use Case
My personal laptop is the host. The os is Ubuntu 20.04.2 LTS. The MEGA/sources is where the various customer/idempiere repositories are. I share this with all new containers that are created.
I created a container called ID82z using os image: Ubuntu 20.04.2 LTS. I downloaded Eclipse, OpenJDK 11, maven, etc in here. Used the source from the shared directory.
I created another container called database using os image: Ubuntu 20.04.2 LTS. I downloaded PostgreSql 13 here. Changed the access port from 5432 and ensured that the db is available from outside.
So my lxc list looks like:
The Steps
- Install Lxd: snap install lxd
- LXD initial configuration: sudo /snap/bin/lxd init
...I chose the defaults and set the storage to 'dir'
- sudo usermod -a -G lxd <user>
...register your user into the lxd group and not to use sudo the entire time
- I set the Lxd network as follows: lxc network set lxdbr0 ipv4.address 172.16.0.1/24
Set the other properties similarly. This how my bridge network has been set up
- lxc launch images:ubuntu/focal/cloud
...I chose the image that has cloud-init support. This is important if you want cloud-init to automate some of your profile tasks
The Profile
The profile is the key to automate regular tasks when a container is built.
You can find my profile at: https://pastebin.com/AhAF2vVr
I found it a good idea to create your own file like default.yaml for your profile. I use this as my master file.
Then I update the default as follows
lxc profile edit default < default.yaml
This section in the profile is automatically creating a user and share my SSH public key. To copy the key, I had to 'cat' the key on the command line and copy the long key like that.
name: yogan ssh_authorized_keys:
Created a physical network called eth0 using dhcp
network: version: 1 config: - type: physical name: eth0 subnets: - type: dhcp
Copied some of my profiles so that my experience in these containers are the same as in the host
- cp /home/yogan/Downloads/.bashrc /root/.bashrc - cp /home/yogan/Downloads/.bashrc /home/yogan/.bashrc - cp /home/yogan/Downloads/.profile /root/.bash_profile - cp /home/yogan/Downloads/.profile /home/yogan/.bash_profile - cp /home/yogan/Downloads/.profile /root/.profile - cp /home/yogan/Downloads/.profile /home/yogan/.profile
Sharing some directories from the host with a new container
Downloadsdir: path: /home/yogan/Downloads source: /home/yogan/Downloads type: disk Sourcedir: path: /home/yogan/sources source: /home/yogan/MEGA/sources type: disk
I followed https://blog.simos.info/ to add X11 graphics to my containers in the profile.
After the container is created you can see the log of cloud-init stuff in /var/log/cloud-init-output.log
What Has Been Achieved With this Use Case?
- When I run Eclipse in one of the containers e.g ID82z, I am guaranteed to run this version of ID with the correct versions of software e.g Eclipse, Java and OS. So upgrading the host (a problem I typically had previously) will not affect the container.
- My software versions are in the host under sources separated by different directories. Git works well in those directories as well.
- The PostgreSQl database is in it's own container. I guess each db version you require could be in it's own container. The ID setup in each Eclipse container will point to it's relevant database container. Notice you can refer to other containers from a container like <container>.lxd. See below:
- Once the ID Server is running in it's Eclipse container I can browse to it from the host.
- The containers are lightweight compared to VirtualBox with similar applications running
What I Still Want to Achieve
- I am currently using ip addresses to browse to ID server. I am keen to setup DNS on the host so that reboots and dhcp will not affect the way I go into ID. It has been done here: https://blog.simos.info/how-to-use-lxd-container-hostnames-on-the-host-in-ubuntu-18-04/
- Map ports from the host to containers. I think this will be important if the container is publicly accessible. Resource: https://blog.simos.info/how-to-use-the-lxd-proxy-device-to-map-ports-between-the-host-and-the-containers/
- Play around with clustering
- Look into projects
- I was not successful in doing stateful closing of containers like:
lxc stop ID82z --stateful
Other Stuff
If you have many containers and are resource constrained. You may want to constrain your containers like:
lxc config set <container> limits.memory 8GiB lxc config set <container> limits.cpu 4
If you want to provide additional comments, please use the iDempiere forum post: iDempiere forum post
