Containerization |Part 3: LXD ON STEROID

Adefemi Afuwpe
FAUN — Developer Community 🐾
8 min readAug 12, 2020

--

lxd on steroids🔥🔥🔥🔥🔥🔥

You read it right we will be looking at LXD on Steroid🙃, in the last post we install and setup LXC/LXD on our computer, so I will be picking up from there and will also try to make this post as clear as I can. We will be looking at a simple use case of LXC/LXD containers.

Let’s Dig in!!

Dig_in🥳 🥳🥳🥳🥳

For this article, I will be spinning up a web server (NGINX) on our Ubuntu instance and also create a load balancer across multiple instances.

Moving on from the installation and setting up LXD, I will execute it into the image created and then install Nginx. Installing Nginx is quite simple using this command

nginx installation

You should also get an output like the one below:

nginx_installation

There might be little hiccups after installing Nginx, for instance, the application might not be in a running state, the next three commands will check the status of Nginx, if Nginx is not actively running then you will need to enable and start it and then check the status again.

status, enable, start of Nginx

Now if things go well you should have an output like the one below:

nginx_status

Hold on, what is Nginx?

hold_on😕

In simple terms, Nginx is software for serving web pages(just serve content to the web browser) and doing some bunch of other tech jargon like reverse proxying, caching, load balancing, and more, you can learn more about Nginx here.

So let’s test out our installation, I will log out of the instance and list out all the images I have running on LXC (This is in the last post) so I can get the IP Address of that instance and run it in my browser. You should have an output like mine:

nginx_homepage

Of cos, this is not the page we will be serving but before the installation of that page, let me introduce you to snapshot on LXC to allow us to perform load balancing very easily.

LXC supports snapshot(you could refer to it as backup), what this does is versioning that is, it allows you to roll back to a previous state you were if something is wrong with the current states you are in.

Let me paint a scenario, let’s say you have an Android/IOS app on your favorite store but recently you just made some changes, maybe you added speech and finger print recognition to your application and then you published this app to production and your app comments start spitting fire🔥 with people complaining that the voice recognition accepts just anyone’s voice and allows anyone the access to login or do some random stuff with the app. This is where snapshot/versioning comes into play, if you created a snapshot you can easily roll back your application to the previous state before you added the finger print and speech recognition.

Creating a snapshot in LXC is quite easy, the format is:

lxc snapshot imagename versionname

I used the command below to create mine:

Now when you list out the image in your LXC, you should see the 1 under the snapshot tab:

lxc_snapshot

With the snapshot created if I have any issues with my webserver, I could roll back to the previous state (which is when I installed Nginx).

Load balancing (distributing efficiently)

loadbalancing

Now that we have Nginx set up, I said earlier we would be doing load balancing. In simple terms Load Balancing simply means distributing incoming traffic across multiple resources ensuring no computing machine is overloaded, under-loaded, or idle. In a tech environment, you should load balance your servers if you want your productivity to remain high. You can learn more about load balancing and reasons here.

To set up load balancing in LXD, what I will do is to make a copy of the only instance we have running and then give it a new name. To make a copy of the instance the format is:

lxc copy imagename/versionname new_image_name

I will be making a copy of three instances to what I have currently running and giving them a different name. Use the command below to create a copy of your own instance:

And when you list all your images you should have an output like the one below:

lb_state

By default the instance will be in a stopped state, to start them, this format is always used

lxc start imagename

Now when you list your images you should have an output like this:

lxc_start

Don’t forget we have a copy of ubuntu image and this image has Nginx installed and running, you can input the IP Address of each image in your browser to also have the same result as ubuntu image just like the one here:

ubuntuimage3_output

If your instance does not automatically have an IP Address, just hold on for few seconds and then try to list out the instance again.

Let’s now make LXD high on some webpages

nginx dance 😂

By default Nginx serves webpage from /var/www/html (The actual web content) let’s change directory into that folder in one of the images we have running and see the file in this directory and also display this file in our terminal using the cat command:

nginx_html_file

As you can see we have the same result as what is displaying in our browser except that we see the various HTML tags. The next thing we will be doing is loading a webpage we want to serve into this directory, I will be using a template from html5up, and I will be downloading three different templates so that each image has a different web page on it (except the lb image).

I will be doing this from my terminal using the wget command, by default wget is always installed and if it’s not installed, go ahead and install using

apt install wget

After that I just used wget against the download link, the output below shows that

wget_installation

I got an error at first when I did wget https://html5up.net/massively/download because the website certificate could not be verified so I passed in the — no-check-certificate command to bypass the error. Notice that what was downloaded is a zip file as you can see from the image above when I tried to change directory against that file. There are various ways to unzip a file in Linux but I will be using the simple one called unzip.

Unzip is not always installed by default, so I will install unzip and then run unzip against the zip file that was downloaded. To install unzip and run unzip against the zip file using this command:

Now when you list all the files in the /var/www/html directory you should see your files unzipped, just like the image below:

unzip_webpage

You will be doing this same process for the other two instances we have running which is ubuntuimage2 and ubuntuimage3. Once this is done go ahead and reload the IP Address of this image on your web browser (Note that you should install three different templates). I won’t be displaying the three different image but you should have three different websites up and running an example is this:

ubuntuimage3_webpage

The next thing step is to set up our load balancer, by adding the load balancing configuration to the server configuration file which includes the upstream and then the module reference for further configuration. This configuration file will be added to a file in our lb instance, log in to the instance and change directory to /etc/nginx/sites-available/ edit the default file in the directory using nano or any editing tool you are comfortable with

cd /etc/nginx/sites-available/
nano default

Now go ahead and delete the current configuration in that file and paste this one below:

upstream and module config

and then restart Nginx using:

service nginx restart

To get more insight on how to set up Nginx load balancing, I find this article by DigitalOcean very helpful.

lb_working

Now that we have the configuration set up, the next step is to put the lb instance IP Address in the browser and the first webpage that will be displayed will be the ubuntuimage1 webpage, keep on reloading this page and the other instance webpage will also be displayed.

You made it to the end

master_clap

Great job so far, you should chill off with your favorite drink or if you’re like me watch a movie. My next post will be on Docker🐳.

Key Take Away: Nginx is simply an application for serving web pages and a lot more. Load balancing allows the distribution of incoming traffic across resources.

PS: All the commands used can actually be written without you logging in to each instance but just so you know what’s going on in each instance it’s advisable to log in to each instance and try each commands out. You can read more on LXD here. Also in the future, I hope to document about using GUI for LXC/LXD with the help of either LXDMosaic and/or LXDUI

Leave a comment if you set up yours differently and how you did it ✔.

Subscribe to FAUN topics and get your weekly curated email of the must-read tech stories, news, and tutorials 🗞️

Follow us on Twitter 🐦 and Facebook 👥 and Instagram 📷 and join our Facebook and Linkedin Groups 💬

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author! ⬇

--

--