Overview:
Docker swarm is a container orchestration tool, that allows the user to manage multiple containers deployed across multiple host machines. It provides the ability to manage and scale your containers.
Testbed scheme:
Swarm initialization:
- Install the Docker on all three machines. Use the following article
- Log into Docker manager and initialize the Docker Swarm manager:
After initialization you will see the following message:docker swarm init \ --advertise-addr [private_ip_of_the_manager_node]
- Copy the join token command and execute it on worker 1 and 2:
On successful joining of the node you will see the following message:sudo docker swarm join --token [token] [private_ip_of_the_manager_node]:2377
- Go back to the manager node and check if nodes are up and running:
You should see the following message:docker node ls
Basic Swarm management commands:
- To list all the existing nodes in the swarm use:
docker node ls
- To inspect the node:
docker node inspect [node_name]
- To promote a worker node to a manager use:
docker node promote [node_name]
- To downgrade a manager node to a worker use:
docker node demote [node_name]
- To remove a node from the swarm:
Then log into the worker node and leave the swarm:docker node rm -f [node_name]
docker swarm leave
- To get worker or manager token execute:
docker swarm join-token [worker | manager]
- To join the swarm:
docker swarm join --token [token] [private_ip_of_the_node_to_join]:2377
- To delete manager node, first you need to demote it to worker node and then delete as usual worker node
Docker Swarm services
- An application that is deployed on Docker host running in swarm mode is deployed as a service
- Service definition represents the desired state. Service schedules tasks which is responsible for ensuring the desired state
- To create a service use:
Example:docker create -d --name [service_name] \ -p [host_port]:[container_port] \ --replicas [replicas_number] \ [image]:[tag] or [CMD]
docker service create -d --name nginx_service -p 8080:80 --replicas 4 nginx:latest
- To list existing services use:
docker service ls
- To inspect existing service use:
docker service inspect [service_name]
- To get logs from service use:
docker service logs [service_name]
- To list all running tasks in the service use:
docker service ps [service_name]
- To scale service up and down use:
docker service scale \ [service_name]=[number_of_replicas]
- To update a service use:
docker service update [options] [service_name]
- To get info about all available update options use:
docker service update --help
Docker Swarm networks
- The default network driver for Docker Swarm is
overlay
network. - The
overlay
network driver creates a distributed network among multiple Docker daemon hosts. This network sits on top of (overlays) the host-specific networks, allowing containers connected to it (including swarm service containers) to communicate securely when encryption is enabled. Docker transparently handles routing of each packet to and from the correct Docker daemon host and the correct destination container. Link - By default an overlay
ingress
network is used along with default bridge networkdocker_gwbridge
- To create a new overlay network use:
docker network create -d overlay [network_name]
- To encrypt all data in overlay network use:
docker network create -d overlay --opt encrypted [network_name]
- To check if network was encrypted use:
docker network inspect [network_name]
- To create a service with an overlay network use:
docker service create -d --name [service_name] \ --network [network_name] \ -p [host_port]:[container_port] \ --replicas [replicas_number] \ [image]
- To add service to a network use:
docker service update --network-add [network_name] [service_name]
- To remove a service from a network use:
docker service update --network-rm [network_name] [service_name]
- To delete an overlay network use:
docker network rm [overlay_network_name]
Volumes in Docker Swarm
- You need separately install the volume plugin because the built-in plugin in Docker is
local
- One of such plugins is "REX-Ray"
- To install the plugin use:
docker plugin install [plugin_name] [options]
- To get the list of installed plugins use:
docker plugin ls
- To remove plugin first you need to disable it first:
docker plugin disable [plugin_ID]
- To remove plugin use:
docker plugin rm [plugin_name]
- To create a volume using a driver use:
docker volume create -d [driver_name] [volume_name]
- To create a service with volume use:
docker service create -d --name [service_name] \ --mount type=[volume_type], src=[source_volume],dst=[destination_volume] \ -p [host_port]:[container_port] \ --replicas [number_of_replicas] \ [image_name]
Notes:
--advertise-addr
flag is used to inform other members of the Swarm that Manager node has the following IP address