Quick startup with Docker Swarm

Photo by Ian Taylor on Unsplash

Quick startup with Docker Swarm

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.drawio.png

Swarm initialization:

  1. Install the Docker on all three machines. Use the following article
  2. Log into Docker manager and initialize the Docker Swarm manager:
    docker swarm init \
    --advertise-addr [private_ip_of_the_manager_node]
    
    After initialization you will see the following message:
  3. Copy the join token command and execute it on worker 1 and 2:
    sudo docker swarm join --token [token] [private_ip_of_the_manager_node]:2377
    
    On successful joining of the node you will see the following message:
  4. Go back to the manager node and check if nodes are up and running:
    docker node ls
    
    You should see the following message:

Basic Swarm management commands:

  1. To list all the existing nodes in the swarm use:
    docker node ls
    
  2. To inspect the node:
    docker node inspect [node_name]
    
  3. To promote a worker node to a manager use:
    docker node promote [node_name]
    
  4. To downgrade a manager node to a worker use:
    docker node demote [node_name]
    
  5. To remove a node from the swarm:
    docker node rm -f [node_name]
    
    Then log into the worker node and leave the swarm:
    docker swarm leave
    
  6. To get worker or manager token execute:
    docker swarm join-token [worker | manager]
    
  7. To join the swarm:
    docker swarm join --token [token] [private_ip_of_the_node_to_join]:2377
    
  8. To delete manager node, first you need to demote it to worker node and then delete as usual worker node

Docker Swarm services

  1. An application that is deployed on Docker host running in swarm mode is deployed as a service
  2. Service definition represents the desired state. Service schedules tasks which is responsible for ensuring the desired state
  3. To create a service use:
    docker create -d --name [service_name] \
    -p [host_port]:[container_port] \
    --replicas [replicas_number] \
    [image]:[tag] or [CMD]
    
    Example:
    docker service create -d --name nginx_service -p 8080:80 --replicas 4 nginx:latest
    
  4. To list existing services use:
    docker service ls
    
  5. To inspect existing service use:
    docker service inspect [service_name]
    
  6. To get logs from service use:
    docker service logs [service_name]
    
  7. To list all running tasks in the service use:
    docker service ps [service_name]
    
  8. To scale service up and down use:
    docker service scale \
    [service_name]=[number_of_replicas]
    
  9. To update a service use:
    docker service update [options] [service_name]
    
  10. To get info about all available update options use:
    docker service update --help
    

Docker Swarm networks

  1. The default network driver for Docker Swarm is overlay network.
  2. 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
  3. By default an overlay ingress network is used along with default bridge network docker_gwbridge
  4. To create a new overlay network use:
    docker network create -d overlay [network_name]
    
  5. To encrypt all data in overlay network use:
    docker network create -d overlay --opt encrypted [network_name]
    
  6. To check if network was encrypted use:
    docker network inspect [network_name]
    
  7. 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]
    
  8. To add service to a network use:
    docker service update --network-add [network_name] [service_name]
    
  9. To remove a service from a network use:
    docker service update --network-rm [network_name] [service_name]
    
  10. To delete an overlay network use:
    docker network rm [overlay_network_name]
    

Volumes in Docker Swarm

  1. You need separately install the volume plugin because the built-in plugin in Docker is local
  2. One of such plugins is "REX-Ray"
  3. To install the plugin use:
    docker plugin install [plugin_name] [options]
    
  4. To get the list of installed plugins use:
    docker plugin ls
    
  5. To remove plugin first you need to disable it first:
    docker plugin disable [plugin_ID]
    
  6. To remove plugin use:
    docker plugin rm [plugin_name]
    
  7. To create a volume using a driver use:
    docker volume create -d [driver_name] [volume_name]
    
  8. 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

Reference:

  1. How swarm mode works
  2. Docker plugins