Welcome to today’s guide on how to install Docker Swarm Cluster on Debian 11 / Debian 10. Docker Swarm is a tool used to orchestrate docker hosts. We can create a high availability and a high performance Docker cluster with applications distributed among the hosts.
Docker swam uses a manager host and worker nodes architecture. You can have one or several manager nodes in your docker swarm cluster. The manager nodes uses the native Docker API and share the database of the Docker Swarm cluster with the worker nodes. The manager hosts maintain the cluster state, schedule tasks and handle the HTTP API endpoints.
Worker nodes are instances of Docker engine which execute the containers.
Unlike Kubernetes, which comes with a complete set of features like monitoring, ingress and volumes, Docker swarm comes with a much simpler state. Docker Swarm also lacks the auto-scaling feature whereby the containers can’t auto-scale basing on monitoring and resource utilization. You have to scale the containers manually.
This guide will discuss how to setup a Docker Swarm cluster onDebian 11 / Debian 10 hosts.
Step 1 – Node preparation
Configure the hostnames for each node in the /etc/hosts
file for local resolution.
sudo vim /etc/hosts
Add the details of the nodes on each server, both the manager and the worker nodes.
10.31.45.182 manager
10.31.45.226 worker-01
10.31.45.219 worker-02
Ensure the hosts can reach each other via ping.
Step 2 – Install Docker CE onDebian 11 / Debian 10
We need to install Docker engine on all the hosts, manager and worker nodes.
Install dependency packages on the hosts:
sudo apt-get update
sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
Add Docker GPG key:
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -
Add Docker repository:
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable"
Install Docker Engine
sudo apt update
sudo apt install docker-ce docker-ce-cli
Start and enable Docker
sudo systemctl start docker
sudo systemctl enable docker
Add your user to docker group
sudo usermod -aG docker ${USER}
Step 3 – Initialize Docker Swarm Cluster
In this step, we need to initialize Docker swarm mode on the manager node then we can later add the worker nodes.
The command below is used to initialize Docker Swarm Manager. You should replace the <manager-IP> Address with the IP of your manager node.
newgrp docker
sudo docker swarm init --advertise-addr <manager-IP>
Sample output:
$ docker swarm init --advertise-addr 10.31.45.182
Swarm initialized: current node (xj5sqdpsbs5x2pgt670dft31m) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-5yat7zlum78qmnlhv1vs0b0k4c42jafh5kt8xtb36eara4c5ip-490dr4yhpbe5qnic476wh90zg 10.31.45.182:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
With the manager node initialized,we can now add the worker nodes using the join-token that is given in the sample above.
$ docker swarm join --token SWMTKN-1-5yat7zlum78qmnlhv1vs0b0k4c42jafh5kt8xtb36eara4c5ip-490dr4yhpbe5qnic476wh90zg 10.31.45.182:2377
This node joined a swarm as a worker.
We can check if the worker nodes have joined to cluster successfully using the command below:
sudo docker node ls
Sample output:
$ docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
h673oapwzqcsoszm36dcsyx4u worker-01 Ready Active 20.10.3
tg30e58pf2ebktyu5geg5hjzd worker-02 Ready Active 20.10.3
vhqes84cz4ljrrj8ux9bx9jzv worker-03 Ready Active 20.10.3
xj5sqdpsbs5x2pgt670dft31m * manager Ready Active Leader 20.10.3
Deploy Applications on Docker Swarm
Here, we will deploy a web application on our cluster.
$ docker service create --name webserver -p 8080:80 nginx
The above command deploys Nginx application on the cluster and is exposed on port 8080.
To confirm if the service is running:
$ docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
z383boupqk5p webserver replicated 1/1 nginx:latest *:80->80/tcp
Scale Applications on Docker Swarm
You can scale applications on your Docker Swarm Cluster for high availability and high performance.
The command below is used:
$ docker service scale webserver=4
Where webserver=4 is the name of the application and the number of total containers to be up after the replication.
You can verify the replicas with the command below:
$ docker service ls
Sample output:
We have successfully deployed our Docker Swarm cluster onDebian 11 / Debian 10 hosts.
Best Docker Learning Courses:
- Docker Mastery: with Kubernetes +Swarm from a Docker Captain
- Docker for the Absolute Beginner – Hands On – DevOps
- Docker and Kubernetes: The Complete Guide
- Docker – SWARM – Hands-on – DevOps
- Docker Swarm Mastery: DevOps Style Cluster Orchestration
Check out these other articles from this website;