12. HANDS-ON TUTORIAL: Scaling Containers over Multiple Instances using Docker Swarm
Increasing scalability, availability, & reliability
Table of contents
Introduction
This introduction provides an overview of the steps required to deploy microservices in containers across three instances using Docker Swarm. The process begins with installing Docker and Docker Compose on all instances, followed by initializing a Swarm manager and joining workers to the manager. The microservices are then deployed on the Swarm using a compose file, and the public IP of the manager is used to access the services in a web browser. The final step is to check the status of the services to ensure they have started successfully.
Tools Utilised
Ubuntu
Docker
Docker-Compose
Docker Swarm
Git
GitHub
Python
Flask
MySQL
NGINX
Step-by-Step Guide
Install Docker & Docker Compose on ALL INSTANCES
Docker Swarm already comes installed with Docker.
1. Update and install the necessary packages
sudo apt update && sudo apt install curl jq -y
2. Use the official Docker script to install Docker
To install Docker on Linux, we can use an official script from get.docker.com:
curl
https://get.docker.com
| sudo bash
3. Add the current user to the docker group
The script installs Docker and creates a Docker user group with access to the commands. This means that Docker can only be used with sudo privileges or if the current user is in the Docker user group:
sudo usermod -aG docker $(whoami)
4. Give /var/run/docker.sock correct permissions
sudo chmod 666 /var/run/docker.sock
5. Test Docker works
docker run --rm hello-world
The --rm
flag removes the container when it exits or when the daemon exits.
6. Set the version of Docker-Compose to download
version=$(curl -s
https://api.github.com/repos/docker/compose/releases/latest
| jq -r '.tag_name')
7. Download Docker-Compose to /usr/local/bin/
sudo curl -L "https://github.com/docker/compose/releases/download/${version}/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
8. Make the file executable
sudo chmod +x /usr/local/bin/docker-compose
9. Verify the installation by checking the version
docker-compose --version
Docker Swarm Setup
10. Initialise the Swarm on manager-node
sudo docker swarm init
11. Copy the command provided in the terminal
Example:
12. Enter the join command on worker-node-1 & worker-node-2
Example:
sudo docker swarm join --token SWMTKN-1-3ax04jx5w6svrrvjrjyryt5hubvlxra7am1lb2gqysgtpgulwd-4xfpw2vcpoeefrztu4kagdv6f 172.31.88.55:2377
[DO NOT COPY!]
Output:
Deploy Microservices in Docker Swarm
13. On manager-node, clone the repo and go into the folder
git clone
https://github.com/mrbilalshafiq/DevOps-Project-2
&& cd DevOps-Project-2/
14. Still on the manager-node, start the containers using Docker Swarm and the docker-compose.yaml file
sudo docker stack deploy --compose-file docker-compose.yaml Project2
15. View Containers on all nodes
docker ps
16. Find your public IP on all nodes
curl
ifconfig.me
17. Access the application using all 3 IP addresses
Example:
Conclusion
In conclusion, Docker Swarm is a powerful tool that can greatly benefit companies by enabling them to easily deploy and manage microservices in a containerized environment. With its ability to scale and distribute services across multiple instances, Docker Swarm ensures high availability and fault tolerance, making it a reliable solution for companies that rely on microservices to power their applications.
Additionally, the use of Docker and Docker Compose simplifies the process of building, shipping and running containers, making it a valuable addition to any company's DevOps toolkit. By following the step-by-step guide provided in this blog post, you can successfully implement Docker Swarm and take advantage of its benefits to improve the performance and reliability of their microservices.