Introduction to Docker Swarm Mode
Docker swarm is a container orchestration tool. Swarm Mode in Docker was introduced in version 1.12 which enables the ability to deploy multiple containers on multiple Docker hosts. For this Docker use an overlay network for the service discovery and with a built-in load balancer for scaling the services. One of the key advantages of docker swarm is that the configuration of the docker services such as volume and network can be modified without the need to manually restart the service Docker will update the configuration, stop the service tasks with the out of date configuration, and create new ones matching the desired configuration.
The key concepts of swarm mode
- Node: A Node is an instance of a Docker engine that connects to the Swarm. You can run one or more nodes on a single physical computer or cloud server. Nodes can be either a manager or workers. The manager node dispatches units of work called tasks to worker nodes. Worker nodes receive and execute tasks dispatched from manager nodes.
- Services: A service is a high-level concept relating to a collection of tasks to be executed by workers. An example of a service is an HTTP Server running as a Docker Container on three nodes.
- Load Balancing: Docker includes a load balancer to process requests across all containers in the service.
How does Docker swarm work?
When you want to deploy a container in the swarm first, you have to launch services. Service consists of multiple containers of the same image. These services are deployed inside a node so to deploy a swarm at least one node has to be deployed. As you see below diagram the manager node is responsible for the allocation of the task, dispatch the tasks, and schedule the tasks. API in the manager is the medium between the manager node and the worker node to communicate with each other by using the HTTP protocol. The service of one cluster can be used by the other. All the execution of the task is performed by the worker node.
A single manager node can be created but the worker node can not be created without a manager node. The ideal number for the count of manager node is seven. By increasing the number of the manager node does not mean that the scalability will increase.
Features of swarm mode
- Cluster management:- To create Swarm you can use the Docker engine CLI where you can deploy the applications. Additional orchestration software is not required to manage a swarm.
- Multi-host networking:- Swarm can contain multiple overlay networks so while deploying the service you can specify the network on which you want to deploy your service. The swarm manager automatically assigns addresses to the containers on the overlay network when it initializes or updates the application.
- Load balancing:- While deploying any service on the particular port the swarm automatically balances the load of these port.
- Scaling:- When you scale up or down, the swarm manager automatically adapts by adding or removing tasks to maintain the desired state.
Swarm mode CLI commands
docker swarm init: This command is used to initialize the swarm.
docker swarm init [OPTIONS]
docker swarm join : By using this command you can join a node to a swarm. The node joins as a manager node or worker node based upon the token you pass with the –token flag.
docker swarm join [OPTIONS] HOST:PORT
docker service create: This is a cluster management command, and must be executed on a Swarm manager node.
docker service create [OPTIONS] IMAGE [COMMAND] [ARG...]
docker service inspect: This command is used to inspect the particular service and all the details will display in JSON format.
docker service inspect [OPTIONS] SERVICE [SERVICE...]
docker service ls: This command is used to see the complete list of all the services in that network.
docker service ls [OPTIONS]
docker service rm: This command is used to remove the specific service you want to remove.
docker service rm SERVICE [SERVICE...]