Creating a Web Cluster with LXD

30 minutes
  • 4 Learning Objectives

About this Hands-on Lab

Containers allow us to spin up multiple instances quickly and efficiently, making it ideal for spinning up any sort of clustered application or service. In this hands-on lab, we are going to do just that by taking an existing web container and spinning up multiple duplicates, then turning those duplicates into a working web cluster that we can access via the public IP of our server.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Create a Snapshot of the Provided Container

Take a snapshot of the container:

lxc snapshot web01 1.0
Create Four Duplicate Containers

We now want to create four web## containers based on this snapshot. We can do this manually by running lxc copy web/1.0 web## for each container, or by using a Bash script:

vim /tmp/container-script.sh
containers="web02 web03 web04 web05"

for c in $containers
do
    lxc copy web01/1.0 $c
    lxc start $c
done
sh /tmp/container-script.sh

Confirm:

lxc list
Create and Configure a Load Balancer Container

Create a lb01 container, also based on the existing web01/1.0 snapshot:

lxc copy web01/1.0 lb01
lxc start lb01

Open and edit the /etc/nginx/conf.d/default.conf file, and set up the load balancer. Remember that host records are set up between containers, so we can use the container names here:

lxc file edit lb01/etc/nginx/conf.d/default.conf
upstream lb {
        server web01;
        server web02;
        server web03;
        server web04;
        server web05;
}

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        location / {
                proxy_pass http://lb;
        }

}

Restart nginx:

lxc exec lb01 -- rc-service nginx restart

Test that we can access the website using curl and the IP of the load balancer:

lxc list
curl <lb_ip>
Map Ports

Ensure that when the host’s IP is accessed, it forwards to the load balancer container:

sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j DNAT --to <lb_ip>:80

Navigate to the public IP of your server in your browser to ensure the load balancer works.

Additional Resources

You have been tasked with setting up a web cluster using Linux containers. An initial web server has already been created using nginx on an Alpine Linux 3.11 container. Use this as a basis for creating a cluster of five servers and a load balancer container. Ensure the website can be accessed through the public IP of the provided Ubuntu 18.04 host.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?