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