Bobcares

Install Docker Swarm Cluster on Debian 10

by | May 18, 2021

Install of Docker Swarm Cluster on Debian 10 involves a series of steps that include install of the docker engine and initializing the docker swarm cluster.

As a part of our Server Management Services, we help our customers with software installations regularly.

Today let us discuss the steps to install Docker Swarm Cluster on Debian 10.


 

Install Docker Swarm Cluster on Debian 10

Docker Swarm is a tool used to orchestrate docker hosts. Installation of Docker Swarm Cluster on Debian involves a series of steps as listed below:

  1. Node preparation
  2. Install Docker CE on Debian 10
  3. Initialize Docker Swarm Cluster
  4. Deploy Applications on Docker Swarm
  5. Scale Applications on Docker Swarm

 

Node preparation

Initially, we need to 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

Now, our support engineers ensure that the hosts can reach each other via ping.
 

Install Docker CE on Debian 10

Once the node preparation is over, we need to install Docker engine on all the hosts, manager and worker nodes.

First, 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

Then, we need to add Docker GPG key:

$ curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -

Next, we will add Docker repository:

$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable"

After adding the repository, we can install Docker Engine:

$ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli

Then, start and enable Docker:

$ sudo systemctl start docker
$ sudo systemctl enable docker

After that, add the user to docker group:

$ sudo usermod -aG docker ${USER}

 

Initialize Docker Swarm Cluster

In this step, we need to initialize Docker swarm mode on the manager node. Then we add the worker nodes later.

The command below is used to initialize Docker Swarm Manager. We should replace the <manager-IP> address with the IP of manager node.

$ newgrp docker
$ sudo docker swarm init --advertise-addr <manager-IP>

Sample output:

$ docker swarm init --advertise-addr 10.31.45.182

Install Docker Swarm Cluster on Debian 10

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

Install Docker Swarm Cluster on Debian 10
 

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 exposes on port 8080.

To confirm if the service is running use the command below:

$ docker service ls

 

Scale Applications on Docker Swarm

We can scale applications on Docker Swarm Cluster for high availability and high performance.

The command below helps with this:

$ 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.

We can verify the replicas with the command below:

$ docker service ls

We have successfully deployed our Docker Swarm cluster on Debian 10 hosts.
 

Common errors with the install of Docker Swarm Cluster on Debian 10

Let us now look at some of the common errors that our support engineers notice during the install.

Error response from daemon: rpc error: code = 9 desc = node node9 is not down and can’t be removed

This error occurs while removing a node from a swarm. In most cases, we should shut down a node before removing it from a swarm with the command:

docker node rm

Usually, we will remove a node from a swarm, if a node becomes unreachable, unresponsive or compromised. For instance, if node9 becomes compromised:

$ docker node rm node9
Error response from daemon: rpc error: code = 9 desc = node node9 is not down and can't be removed

We can resolve this by forcefully removing the node without shutting it down bypassing the –force flag.

$ docker node rm --force node9

Node node9 removed from swarm

Before we forcefully remove a manager node, we must first demote it to the worker role. Make sure that we always have an odd number of manager nodes if we demote or remove a manager.
 

Error response from daemon: rpc error: code = 4 desc = context deadline exceeded

Swarm is resilient to failures and the swarm can recover from any number of temporary node failures or other transient errors. However, a swarm cannot automatically recover if it loses a quorum. Tasks on existing worker nodes continue to run, but administrative tasks are not possible, including scaling or updating services and joining or removing nodes from the swarm. The best way to recover is to bring the missing manager nodes back online.

If we lose the quorum of managers, we cannot administer the swarm. If we have lost the quorum and we attempt to perform any management operation on the swarm, an error occurs:

Error response from daemon: rpc error: code = 4 desc = context deadline exceeded

The best way to recover from losing the quorum is to bring the failed nodes back online. If that is not possible, the only way to recover from this state is to use the –force-new-cluster action from a manager node. This removes all managers except the manager the command was run from. The quorum is achieved because there is now only one manager. Promote nodes to be managers until we have the desired number of managers.

# From the node to recover
docker swarm init --force-new-cluster --advertise-addr node01:2377

When we run the docker swarm init command with the –force-new-cluster flag, the Docker Engine where we run the command becomes the manager node of a single-node swarm that is capable of managing and running services.

The manager has all the previous information about services and tasks, worker nodes are still part of the swarm and services are still running. We need to add or re-add manager nodes to achieve previous task distribution and ensure that we have enough managers to maintain a high availability and prevent losing the quorum.

 
[Need any further assistance with Docker errors? – We’re available 24*7]

Conclusion

In short, the install of Docker Swarm Cluster on Debian 10 involves a series of steps. Today, we saw how our Support Engineers perform this task.

 

 

Are you using Docker based apps?

There are proven ways to get even more out of your Docker containers! Let us help you.

Spend your time in growing business and we will take care of Docker Infrastructure for you.

GET STARTED

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.