6.1 Main Concepts of Docker Swarm
Docker Swarm
is Docker's built-in container orchestration system. It lets you create, manage, and scale a cluster of Docker hosts, turning them into a single logical host where you can run containers. Swarm automatically distributes tasks among nodes, offering convenience and fault tolerance. In this lecture, we'll go over the key components and basic commands for managing Docker Swarm
.
Main concepts of Docker Swarm
:
- Cluster (Swarm): a group of Docker hosts combined into a single logical cluster.
- Managers: nodes that manage the cluster and distribute tasks among worker nodes.
- Worker Nodes (Workers): nodes that execute tasks and run containers.
- Services: a logical representation of applications, describing what tasks need to be performed and how many container instances to run.
- Tasks: individual containers, which are instances of services.
Docker Swarm Architecture:
Docker Swarm uses a manager-worker architecture. Managers maintain the cluster's state, while worker nodes perform tasks.
Managers:
- Maintain the state and configuration of the cluster.
- Distribute tasks to worker nodes.
- Ensure fault tolerance and high availability (if there are multiple managers).
Worker Nodes:
- Execute tasks assigned by managers.
- Run containers based on the tasks.
Essential Docker Swarm
Commands:
-
docker swarm init
: initialize a new Swarm cluster. -
docker swarm join
: join a node to an existing cluster. -
docker node ls
: view the list of nodes in the cluster. -
docker service create
: create a new service. -
docker service ls
: view running services. -
docker service scale
: scale a service to a specific number of instances. -
docker service rm
: remove a service from the cluster.
Using Docker Swarm
makes managing apps in distributed systems easier by providing fault tolerance, scalability, and deployment simplicity.
6.2 Creating a Docker Swarm
Cluster
Creating and managing a Docker Swarm cluster
Step 1: Initialize the Cluster
Run the following command on the first host (manager):
docker swarm init --advertise-addr <MANAGER_IPl>
This command initializes a new Swarm cluster, makes the current node a manager, and outputs the command for joining worker nodes.
Step 2: Join Worker Nodes
On other hosts, run the command output by docker swarm init
to join them to the cluster:
docker swarm join --token <SWARM_TOKEN> <MANAGER_IP>:2377
Where:
<SWARM_TOKEN>
: the token used to connect to the cluster (provided by thedocker swarm init
command).<MANAGER_IP>
: IP address of the manager node.
Step 3: View Nodes
To view all nodes in the cluster, use the command:
docker node ls
This command will show a list of all nodes, including their roles, states, and availability.
Sample output of docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
qwertyuiop12345 manager-node Ready Active Leader
asdfghjkl67890 worker-node1 Ready Active
zxcvbnm09876 worker-node2 Ready Active
Explanation of columns:
- ID: The unique identifier of the node.
- HOSTNAME: The hostname of the machine running the node.
- STATUS: The current status of the node (e.g., Ready).
- AVAILABILITY: The availability of the node (e.g., Active).
- MANAGER STATUS: Indicates if the node is a manager (e.g., Leader, Reachable) or a worker (empty).
Now the Docker Swarm cluster is set up, and you can deploy and scale applications.
6.3 Managing Docker Swarm
Managing services in Docker Swarm
1. Creating a Service
Create a service that will run with a defined number of replicas:
docker service create --name my_web --replicas 3 -p 8080:80 nginx
In this example, a service my_web
is created, which runs 3 instances of Nginx. Containers expose port 80 to port 8080 on the host.
2. Viewing Services
To see all running services in the cluster, use the command:
docker service ls
Example output of the docker service ls
command
ID NAME MODE REPLICAS IMAGE
ab12345 my_web replicated 3/3 nginx:latest
3. Scaling a Service
To change the number of service instances (replicas) use the docker service scale
command:
docker service scale my_web=5
Now the my_web
service will have 5 instances.
4. Updating a Service
To update the service image to a new version, use the docker service update
command:
docker service update --image nginx:latest my_web
Ensuring High Availability
Docker Swarm ensures high availability through:
- Task distribution across nodes in the cluster for automatic container recovery in case of failures.
- Support for multiple managers: It's recommended to have an odd number of managers (e.g., 3 or 5) to avoid quorum issues.
- Using replicas: Configure multiple replicas for each service to improve fault tolerance.
Quorum Concept
Quorum represents the majority of nodes that need to agree on a specific action (e.g., data write or leader election) for it to be considered valid. For example, in a 5-node cluster, the quorum would be 3 nodes.
Helpful tip!
Make sure managers are placed on different physical or cloud servers to minimize the risk of simultaneous failures.
6.4 Example of Docker Compose
configuration for Docker Swarm
You can use Docker Compose
with Docker Swarm
to make managing multi-container apps easier.
Example of a docker-compose.yml
file
version: '3.8'
services:
web:
image: nginx:latest
ports:
- "8080-8090:80"
deploy:
replicas: 3
restart_policy:
condition: on-failure
networks:
- my_overlay_network
db:
image: postgres:latest
environment:
POSTGRES_PASSWORD: example
deploy:
replicas: 1
restart_policy:
condition: on-failure
networks:
- my_overlay_network
networks:
my_overlay_network:
driver: overlay
Running a Docker Compose
stack in Swarm
To deploy the stack in Docker Swarm
, use the command:
docker stack deploy -c docker-compose.yml mystack
Viewing the stack
To check deployed stacks:
docker stack ls
Viewing services in the stack
docker stack services mystack
GO TO FULL VERSION