Open In App

Replicated VS Global Services In Docker Swarm

Last Updated : 01 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Docker Swarm is a container orchestration tool used to manage multiple Docker containers across a cluster. In this guide, I will first discuss what Docker Swarm is. Then I will discuss the difference between the key services, which are replicated services and global services. After this, I will walk you through the different steps to perform both replication service and global service in a Docker Swarm.

What is Docker Swarm?

Docker Swarm is a container orchestration platform that is used to manage and schedule multiple Docker containers across a cluster of machines. Docker Swarm simplifies the deployment, scaling, and management of containerized applications. Here in Docker Swarm, there are mainly two types of nodes called manager and worker nodes. Managers manage the swarm and schedule containers across the cluster. Worker nodes help in executing the swarm services that are assigned to them. Services are defined with the desired state, which includes several replicas, exposed ports, networks, and storage resources. Docker Swarm allows configuration updates on services without any manual restarts. Swarm uses ingress to expose its services for external access. In summary, Docker Swarm simplifies the process of deploying and managing containerized applications. Its key features, like scalability, high availability, service discovery, load balancing, security, and easy deployment, make Docker Swarm a popular choice to orchestrate containerized applications efficiently.

Replicated vs Global Services In Docker Swarm

Feature

Replicated Service

Global Service

Scaling

Scaling up or down by giving a desired number of replicas

Can not scale up or down by giving a desired number.

Node-centric deployment

It does not guarantee to run one replica on each node.

It guarantees to run one replica on each node.

On Node Failure or Delete

After deleting a worker node, it will still distribute the same number of replicas mentioned while running to the available nodes.

After deleting a node, the total replica number will decrease by one.

Use cases

Applications are horizontally scaled up or down.

Ideal for those that need one instance per node, like a monitoring agent.

Examples

A web server has 5 replicas.

A log collector is running on each node to collect logs.

Pre-requisites

Before moving to next section make sure that you have a basic understanding of what is docker and how to launch an EC2 instance on AWS cloud platform , if not, follow the geeksforgeeks articles provided below for a clear understanding of these concepts.

Steps To Perform Replication and Global Services In Docker Swarm

Step 1: You should first create two or more virtual servers may be locally or using any cloud platform. Here i have created 2 EC2 instances (Ubuntu) on AWS cloud platform. Open the port 8080 and 2377 here on both EC2 instances and use the script below in the user data to install docker on the EC2 instance.

#!/bin/bash
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y

Step 2: Now connect one of the EC2 instance and create a manager node using this command . This command will make this EC2 instance as a manager node.

sudo docker swarm init --advertise-addr  <Virtual-Server-IP>

Manager Node

Step 3: After executing the above step , you will get a command with a token . Copy that entire command and paste it on the other EC2 instance . This will add a worker node to the manager node.

docker swarm join --token SWMTKN-1-5reoyqn22w1o3tv8wyq0iw56bpuzm2pavwwarrt6g4uxx0xspv-cc6jemz5n1rabpbavih0t31hb 54.210.162.225:2377

Add-worker

Step 4 : Now create a replication service using nginx image. Run the below command on the manager node.

sudo docker service create --name nginx --replicas 4 -p 8080:80 nginx

Start-nginx

You can check the replicas using this command.

sudo docker service ps nginx ( if you are running a container with different name then use that instead of nginx ) 

Replicas

Step 5 : Now create a global service using a nginx image. (Before doing this step make sure that you have opened the port 80 in both EC2 instances if not then edit the security group of each EC2 instance to add port 80. )

sudo docker service create --name globalnginx -p 80:80 --mode global nginx (run on manager node)

Global-service

You can check global service using this command.

sudo docker service ps globalnginx ( if you are running a container with different name then use that instead of globalnginx ) 

See-global-service

Step 6 : See all the services used in the cluster using the below command.

sudo docker service ls ( run on manager node)

All-services

Conclusion

Here we have first learned about what is Docker Swarm and its working . Then we have learned the difference between two key services provided by Docker Swarm that is Replication and Global Service. Finally we have implemented all the steps to perform replication and global services in Docker Swarm.

Replicated vs. Global Services In Docker Swarm-FAQ’s

What will happen to global service when a new node is added to cluster ?

On adding a new node to the cluster , it will lead to automatic deploy of an instance on the new node.

What happens on node failure in replication service ?

Docker Swarm will automatically reschedule deploy instances on the available nodes to maintain the desired number of replicas.

Do replication service guarantee to run one replica on each node in a cluster ?

No , replication service does not guarantee of running one replica on each node in a cluster.

How does Docker Swarm handles service discovery in replication and global services ?

Docker Swarm uses built in DNS based service discovery for both replication and global service.

Is there any limitation on number of replicas in replication service ?

No there is no limitation on number of replicas in replication service.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads