Hello In this guide we will be seeing how to configure our docker container with ubuntu and nginx with port 80 exposed so that we can access our container from outside and as an additional one an ubuntu container with Haproxy and thus be able to have a load balancer configured with two web servers in different servers
The purpose of this guide is to be able to have 3 different servers each with its own configured docker container, 2 web servers and a load balancer.
Step 1 — Installing Docker on our servers:
The first step is to download Install docker on our server correctly, in case you already have it installed you can skip this step.
taking into account that we will use 3 servers we want to avoid writing the same command lines continuously on each server, for this we will create a script that does the work for us
#!/usr/bin/env bash
# This scripts installs docker apt by https
sudo apt-get update
sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg \
lsb-release
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo \
"deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/nullsudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io
You can copy this script or follow the official Guide
Once installed we must verify if the installation was correct for this we will use a test image:
sudo docker run hello-world
When executing the above, the following message should appear that verifies that the installation was correct:
Step 2 Configure our containers:
Once docker is installed on each server, we continue to download and configure our ubuntu container, in this step I recommend reading carefully to avoid lengthening the work
Each one will decide which version of ubuntu to install, to verify the available ones we will use:
sudo docker search ubuntu
In my case I will use the official version, I will have to install some packages to configure correctly but that leaves us a cleaner Ubuntu without many additions, in short, we will avoid having programs or tools that we will not need
Importan!:At the time of downloading the image we will configure the name of the container, the host name and also the port that we will use to access the container, in this step it is advisable to verify that port 80 is not being used.
That is the one used by the hhtp protocol, we will use the following command line to see the ports that are being used by our server:
sudo lsof -i -P -n
In my case you can see that my haproxy is making use of port 80 that I will have to use, in your case it can be many different situations, but the solution is simple, the only thing we have to do is stop process
As a recommendation, verify that the program they are using does not start when doing a reboot since it may cause conflict when using port 80
Once we are sure that our port 80 is free, we can continue to download and configure our container, as I had mentioned, I will be using the official version
The following flags will be of importance since we configure the container correctly so that we do not have to do additional steps:
— hostname: to set the hostname of our ubuntu, this will help us so that when we do curl it complies with the requirements of the checker in the case of holberton students, in the same way it is recommended.
— name: is to give a name to our container so that we have an appropriate order since at some point we may have multiple containers
-p: is to set the port of our container in which it will be 80:80, 80 for the host port, then when we call port 80 of our server it redirects to our container and 80 for our container so we can follow the idea of the example I recommend that you read the documentation so that you have a more concise idea of these topics.
taking into account what is mentioned we continue to download and configure our ubuntu container .
sudo docker run -d -ti --hostname 2814-lb-01 --name 2814-lb-01 -p 80:80 ubuntu
Here I recommend modifying the previous script if necessary, I refer to the hostname, name and port that you need to use.
Once downloaded and installed we use “sudo docker ps” to see the status of our container.
As we can see, our container has the name that we provide and is in the port that we indicate, now when we access the ip of our server we are going to redirect directly to our ubuntu container
Step 3 — Install nginx in our container and verify its correct operation
Now let’s see what the ip of our server returns
As we can see, we have no answer, let’s go on to enter our container and do the basic configuration with nginx
As you can see, our hostname is the one we provide, make sure you have the hostname you need, otherwise you will have to do other steps to change it.
In this step, in order to install our libraries we must do the update, install sudo and your preferred editor, the necessary commands are the following
apt update
apt install sudo
sudo apt install nano
Once this is done, I will create a script that installs nginx and does some basic configuration
#!/usr/bin/env bash
# Configures a new ubuntu serversudo apt-get -y update
sudo apt-get -y install nginx
ufw allow 'Nginx HTTP'
echo "Holberton School" > /var/www/html/index.htmlsudo sed -i "11i\\\tadd_header X-Served-By $HOSTNAME;" /etc/nginx/sites-available/default
redirect="\n\tlocation /redirect_me {\n\t\treturn 301 https://www.youtube.com/watch?v=QH2-TGUlwu4;\n\t}\n"
sudo sed -i "37i\ $redirect" /etc/nginx/sites-available/default
echo "Ceci n'est pas une page" | sudo tee /usr/share/nginx/html/404.html
err_page="\\\terror_page 404 /404.html;\n\tlocation = /404.html {\n\t\troot /usr/share/nginx/html;\n\t\tinternal;\n\t}"
sudo sed -i "27i $err_page" /etc/nginx/sites-available/default
service nginx start
sudo service nginx restart
sudo service nginx reload
When executing our script there will be some requirements such as location, time zone and they will have to specify and continue with the installation
The end of your script should look like the following image:
Once finished, we fly to check what it returns when making a request to the ip of our server
As we can verify our server responds with what is contained in our container, with this we can make sure that our docker container and nginx are correctly configured.
Load Balancer in docker container:
Once I understand how to install our configured with your hostname and port 80, follow the same steps to the container for your load balancer executing.
IMPORTANT !: In this part we will use another script to install haproxy, read the script carefully and modify the lines of the servers, since you will be using your own servers, replace where it says NAMESERVER by the IP of your servers that you want to add in this case I am using two web servers .
I recommend reading the following guide to understand how to correctly configure it : Link
#!/usr/bin/env bash
# Configures load balancerapt-get update
apt-get install -y software-properties-common
add-apt-repository -y ppa:vbernat/haproxy-2.0
apt-get update
apt-get install -y haproxy=2.0.\*
sed -i "$ a frontend load_balancer\n bind *:80\n mode http\n default_backend web_servers\nbackend web_servers\n balance roundrobin\n server web-01 NAMESERVER:80 check\n server web-02 NAME-SERVER:80 check" /etc/haproxy/haproxy.cfg
service haproxy start
Once the script or your script is executed we will verify that the load balancer is working correctly
As you can see, my load balancer is correctly redirecting to both servers, we can see web1 and web2.
At this point we already have our servers with their own container configured correctly. For Holberton students, once these changes are made, all checks will go to 100%, so if there is a mistake, check that you have not made any mistakes or skipped any step, make sure that your script has a correct operation.
Additionally, if you want to modify the hostname of any container, I recommend you check the following page
If you have any suggestions about the guide presented or find any errors, do not hesitate to collaborate.
Having said that, I hope this guide has been useful to you.