Docker swarm — manage services and high availability

Docker swarm allows you to deploy services distributed to the nodes of the swarm, this allows high availability and high performance of the service. To start a single service we can enter something very simple like this

Create a simple service and cause a on the running node

This command started a new service with the container nginx.

To see details about the services we can run

Note that the replicas are 1 and does not equal the number of my worker nodes which are 3 in my setup, This is because by default docker swarm creates one replica but still enjoys high availability, in case of a failure of the node that runs this container the service will move to another node. Lets first find on which node it runs

To do this we need to pass the name of the container to docker service ps which is nifty_lefmann.1 in mycase and the host of the swarm that executes the container is worker_node3.

Lets simulate a real world scenario, lets stop docker on this worker which equals to a problem in the node.

We can verify the status of the nodes with docker node ls. Using this command we can see that the status of workder_node3 is “Down”.

Now lets see the state of the service from the swarm manager

Here we can see that the container is in shutdown state on worker_node3 and in running state in the swarm_manager, this is happening because if not configured the swarm managers are workers as well

Lets bring back to life docker on node worker_node3

Verify that the node is back

Lets see what happens to the service

Nothing changes! despite that the node is up the container runs in the swarm_manager node.

Delete a service

To delete a running service enter

Its very simple! it should return the name of the service

Run a service with more than one replicas

Lets do something more complicated, lets define the number of replicas of the service along with some options

With this command we did the following

  • The name of the service is nginx
  • The number of replicas are 3
  • We exposure port 80 of each container to port 8080 of each node

Running docker service ps nginx we can see the following

Each node has a running container for service nginx

How Load balancing works

Lets do something very interesting, run this on any host of your swarm that is a worker node

Then run the following in a swarm node

No matter how many times you will run it and no matter on which host you should see that the number of requests should be divided equal to each worker node that runs this container

And this is happening because docker swarm internally load balance the requests in a round-robin fashion despite on which node we perform the request and this is great for simple solutions that do not require a very specialized load balancing scheme.

Changing the number of replicas

We can on the fly increase or decrease the number of running replicas for a service, to do this we can enter in a swarm manager

This will reduce the number of nginx replicas from 3 to 2, we can verify this by running

To increase the number of replicas

Note that the containers have been equally balanced to each running node

How to disallow a node running containers

A good practice is not to use swarm managers to run heavy load containers because this can disrupt the consistency of the swarm managers statuses which can cause problems to the quorum of the swarm, to avoid this you can configure a node not to run containers

And to verify that the swarm_manager does not execute any containers we run docker service ps nginx

We can see that swarm_manager does not execute any containers but the number of replicas are still 6 and distributed equal to the 2 workers.

How to automatically use all nodes of a swarm

In case you want to use all nodes of the swarm there is not need to define the number of replicas, you can instead use the global option

This command will create as many replicas as the number of nodes that can run containers in the swarm.

I hope you found my article interesting! :)

Written by

DevOps engineer, loves Linux, Python, cats and Amiga computers

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store