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>

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

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

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 ) 

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)

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 ) 

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

sudo docker service ls ( run on manager node)

Replicated VS Global Services In Docker Swarm

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.

Similar Reads

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

...

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

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

Contact Us