DCA - 09 - Scaling Docker Services with Replication

Jarret B

Well-Known Member
Staff member
May 22, 2017
Reaction score
When you create a new service, it runs on one node by default. You can replicate a service to multiple nodes if needed.

Having one instance will not provide instant fault tolerance. If a service fails, Docker will restart the service. Restarting a service takes a little time. So, if the service is running on a second or third node, it is still available. Let's look at managing the replicas. I know we've covered this a little, but let's look a little more at replication.

Adding a Replica

Some people may refer to these as 'instances', such as a service has two instances. Here, it has two replicas, most likely running on two nodes.

NOTE: If there is only one node, then you can have multiple replicas on the same node. If one replica fails, it is still available until we restart the service to have two instances again.

So, let's look at this and see what we can do. In my first example, I am running Docker in VirtualBox, so I will only start one server.

I removed all the images and running services. Use the command 'docker services rm <service-name>' and docker image rm -f <image-name>', if you want to remove everything and start over. The '-f' parameter, for removing images, may be required to force a removal.

NOTE: There may be a lot of closed containers that are still on your docker Node. Use the command 'docker rm docker ps -a -q' to remove them all and start from 'scratch'. You may need to run all the above commands on each Node to clean it all up. It will not remove running containers, such as 'httpd'.

For an example, let's use the 'httpd' service. To get the image, use the command 'docker pull httpd'. Running 'docker images', you can see that there is an image for 'httpd'.

To create a service for the node, we will run the command 'docker service create --name web -p 80:80 httpd'. The service should start and show '1/1' services started. Since we did not specify the number of replicas, the default is 1.

Of course, we are only using one Node, but we can increase this to more. Let's increase it to 2 with the command:

docker service update --replicas 2 --detach=false web

We can verify what is going on by the command 'docker service ps web'. It will show 2 instances, both on Node1.

You can open a browser on the server and go to the address 'localhost' to see the response of 'It works!' to know that the 'httpd' service is running.

We can now add a replica, but you can also scale down the number of replicas as well.

Let's add another service. In this example, we will get a database image called 'redis' and start it as a service:

docker image pull redis
docker service create --name redis redis

We should now have three service instances running on the node, 2 httpd and 1 redis. You can see this in Figure 1, with the command:

docker service ls

Figure 1.JPG


Now that we have two different services running on our Swarm, we can scale the services back. But what if we want to scale more than one service at a time? We cannot do this with the 'docker service update' command.

Let's say we wanted to change the number of replicas for the 'web' service to 3 and 'redis' to 2. We can use the command 'docker service scale --detach=false web=3 redis=2'. The 'web' service will scale up first, when done, the 'redis' service will scale up. We can put both services back with the command ' docker service scale --detach=false web=2 redis=1'. Of course, we can scale one up and one down. You can list as many services, or a minimum of one service, as you want on the list.

We now have two services running in 'replicated' mode. If there is a 'Mode' listed, then there must be other 'Modes' available.

Global Mode

To show Global Mode, you need to start at least one more node in the Swarm. You can start more than one.

After you have another service started, run the command 'docker service ps web'. You should see that the httpd service has 2 instances, but both are running on the same node.

Let's look at this real quick before we cover Global Mode.

We can try to scale back the 'web' service to one replica with the command 'docker service update --replicas 1 --detach=false web'. Once the replica is stable, you can reissue the statement with 2 or more replicas so they will start on the other nodes. Run the 'ps' command, as above, to verify that the service is on more than one node.

You can see how we can control the number of nodes that a service will run on. We can scale it up or down as needed. A service in Global Mode is completely different. Global Mode sets the service to run on all Nodes. If we added a node to a swarm, the Global Service will start on the new Node. You cannot scale the service at all. The number of replicas is always equal to the number of nodes in the swarm.

Let's stop the 'redis' service and recreate it as Global. There is no way to convert a service from Replicated Mode to Global Mode.

So, 'docker service rm redis', will remove the service. Now you can use the following command to create a new service in Global Mode:

docker create service --name redis --mode global --detach=false redis

Looking at Figure 2, you can see the results of the commands 'docker service ls' and 'docker service ps redis'.

Figure 2.JPG


If you try to scale back the 'redis' service, you will get an error. The error will be 'replicas can only be used with replicated mode'.

Scale the service back at some point, so you need to remove the one in Global Mode and recreate one in Replicated Mode.

Global Mode is on the Swarm globally on each node, no matter what command you issue.


Practice adding services and scaling the number of nodes up and down. Use both commands 'docker service update' and 'docker service scale'.

Make a service that is in Global Mode and play with it. Have a node turned off and then turn it on after the Global service is running and see it automatically added to the newly started node.

Practicing Docker is your best bet to learning it. Try different things and play with it in VirtualBox so you do not need the extra hardware.