How to use Docker Swarm
In this tutorial we will see how to use Docker Swarm step by step, follow the steps below to learn using Swarm:
Step 1. So this is the Syntax of the command we need to create a new swarm on our docker desktop:
docker swarm init --advertise-addr <MANAGER-IP>
Hence we will enter the following command in our terminal:
docker swarm init --advertise-addr 192.168.99.100
If this doesn’t work in your computer, you can use:
docker swarm init --advertise-addr 127.0.0.1
Our swarm is now created. you will get a similar output:
Step 2. Now we can enter the following to check the state of the swarm:
docker info
This will give you the information about the swarm:
Step 3. if we enter
docker node ls
on the terminal. It will give us the list of nodes running and we can see our leader node there:
Step 4. In order to add a worker nodes to swarm you can enter the following command in your terminal
docker swarm join --token SWMTKN-1-49nj1cmql0jkz5s954yi3oex3nedyz0fb0xx14ie39trti4wxv-8vxv8rssmk743ojnwacrr2e7c 192.168.99.100:2377
Step 5. Now let’s deploy a service in Docker Swarm, in order to deploy any service in swarm, in syntax of the command is:
docker service create –replicas[NUMBER OF REPLICAS] –name [NAME OF SERVICE] [IMAGE]
Therefore, we can enter the following command, to create a alpine service named gfg:
docker service create --replicas 1 --name gfg alpine ping docker.com
and your gfg service will be created:
Step 6. We can check our list of services using the following command:
docker service ls
you will find the gfg service in the list:
Step 7. Now let’s scale this service to 2 replicas, to do that the syntax for the command is:
docker service scale [NAME_OF_SERVICE] = [NUMBER_OF_REPLICAS]
Enter the following command in your terminal to scale our gfg service to 2 replicas:
docker service scale gfg=2
Step 8. Now when we check our list of services, we see that our gfg service has 2 replicas:
docker service ls
Step 9. Finally, we would want to delete our service, for that the syntax of the program is
docker service rm [NAME_OF_SERVICE]
To delete our gfg service we can simply enter this command:
docker service rm gfg
Upon checking the list of services, we will not find your gfg service anymore:
docker service ls
And with we will learn how to use Docker swarm, we create our first Docker swarm, then we created nodes and services and learned hot to scale them and remove them.
Docker Swarm: Building a Highly Scalable Cluster
Docker Swarm is one of the most popular container orchestration engines. It is not only used by professional engineers but also by people is their learning phase of how operations work. What makes Docker Swarm so popular is the fact that is very lightweight and very simple to understand – you don’t even have to learn a new CLI for Docker Swarm, we can use it with Docker CLI.
In this article, we will learn about Docker Swarm, its features, and use cases and will also go through a tutorial to create highly scalable clusters in Docker Swarm.