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 runninglxc 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 existingweb01/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.