What is Docker Swarm?
An Orchestration tool. An Orchestrator is basically a tool that helps you manage, scale, and maintain your containerized applications. In other words, this tool helps in scaling your application, automating its maintenance, replacing failed containers automatically, and rolling out updates. Other examples of this tool are Kubernetes and Apache Mesos.
In this article, I will take you through managing and controlling multiple docker containers as a single service.
What you'll need:
Install Docker
Install Docker machine
Install Virtual box. we'll use this to create docker machines.
> sudo apt-get install Virtualbox
or on Mac
> brew cask install virtualbox
1. Create 3 docker-machines
Create a docker-machine called manager which will act as the manager.
> docker-machine create --driver virtualbox manager
The next step would be to create 2 more machines which will act as worker machines by running
docker-machine create --driver virtualbox machineName
> docker-machine create --driver virtualbox worker1
>
> docker-machine create --driver virtualbox worker2
You can check all the machines by running:
> docker-machine ls
You can now connect to any of the machines by running docker-machine ssh machineName
:
> docker-machine ssh manager
2. Create a Swarm
To create a swarm, ssh into the machine which will act as the manager (machine named manager in our case) and run:
docker swarm init --advertise-addr MANAGER_IP
To get the manager IP, run :
docker-machine ip manager
The next step would be to add the two worker machines to the Swarm we've just created. While on the manager run the command below to get the actual command to use.
docker swarm join-token manager
Copy the generated code and ssh into each of the worker machines to join them.
While on the manager machine, run this command to see the created nodes
docker node ls
As shown above, you can see we have 3 nodes and the machine named manager as the Leader.
3. Running the application
In the manager machine, we are going to run three replicas of the application by running:
docker service create --replicas 3 -p 80:80 --name serviceName dockerImage
serviceName is the name of our application and you can use any Docker image you have. In this instance, I'm going to use Nginx.
To see the service we've created, run
docker service ls
To List the tasks under a particular service run:
docker service ps serviceName
3. Test the application
Copy the Ip of one of the docker machines and you'll see the Nginx service running.
Other commands
docker service inspect serviceName
- Display detailed information on one or more services
docker service logs serviceName
- Fetch the logs of service or task
docker service rm serviceName
- Remove a service
docker service scale serviceName=2
- Scale one or multiple replicated services
And that's all folks. Thank you for reading through. If this article was helpful, feel free to share it.
Happy Coding! ๐