Docker Swarm is a container cluster management service developed by Docker Company. As of version 1.12.0, Docker has been installed as part of the package, also known as Swarm Mode, without additional installation.

Compared to Kubernetes, Docker Swarm is a simple piece of software that seems overwhelmed. But its compatibility with Docker-compose makes up for everything. Docker Swarm is a good choice for those who have no experience in using clusters.

concept

Docker Swarm, mainly contains the following concepts:

  • Swarm
  • Node
  • Stack
  • Service
  • Task
  • Load balancing

Swarm Swarm Swarm Swarm Swarm Swarm Swarm Here refers to the state of computer cluster after Docker connection. Docker swarm creates, joins, and leaves a cluster.

A Node is a computer Node, also known as a Docker Node. Nodes fall into two categories: Manager and Worker. A Swarm must have at least one Manager. Some management commands can only be used on the Manager. Both types of Nodes can run services, but only the Manager can run commands. For example, only the Docker Node command can be used in the Manager to view, configure, and delete nodes.

A Stack is a group of services, similar to Docker-compose. By default, one Stack shares a Network and is accessible to each other, isolated from other Stack networks. The concept is just for ease of arrangement. The Docker stack command makes it easy to manipulate one stack instead of one Service at a time.

A Service is a type of container. For users, Service is the core of their interaction with Swarm. The first is replicated, specifying the number of containers in which a Service is run. The second is global, which runs a container on all eligible nodes. Docker docker docker docker docker docker

Swarm Swarm is the smallest unit of commands to execute a Task. To run a Service successfully, one or more tasks (depending on the number of containers for a Service) need to be executed to ensure that each container starts smoothly. Typically, users operate services, not tasks.

Load balancing is also a reverse proxy. Swarm uses load balancing in the form of Ingress, that is, visiting a Published port on each node automatically proxies to a real service. The general principle is shown in the figure below.



Replicated Mode

services:
  some-serivce:
    ...
    deploy:
      mode: replicated
      replicas: 3
Copy the code

By default, mode is replicated, so this line can be omitted. The default replicas number is 1, which means that the Service starts only one container. Swarm can start multiple services on demand, and sometimes a Node will start multiple containers. Global Mode

services:
  some-serivce:
    ...
    deploy:
      mode: global
      placement:
        ...
Copy the code

Deploy one of all deployable nodes. Placement allows you to restrict the nodes that meet the criteria and avoid deploying on nodes that are not appropriate.

operation

Some common operations are listed here.

Create the first Node

docker swarm init --advertise-addr $IP
Copy the code

$IP is the externally accessible IP address of the current Node for other nodes to address.

Swarm is now initialized with only one Manager node.

Swarm added a new Node to Swarm

On the Manager Node, run the following command to view how to add a Node:

$ docker swarm join-token manager To add a manager to this swarm, run the following command: docker swarm join --token SWMTKN-1-2zspelk468gb6wgw5adea4wlbw4kfy3q1uhr86zpafl9m5a3ho-ezs4fylj526e801b3cl0pojr5 10.174.28.52:2377 $docker swarm join-token worker To add a worker To this swarm, run the following command: docker swarm join --token SWMTKN-1-2zspelk468gb6wgw5adea4wlbw4kfy3q1uhr86zpafl9m5a3ho-164iqklrfv8o3t55g088hylyk 10.174.28.52:2377Copy the code

Docker Swarm join –token docker Swarm join –token docker Swarm join –token To become the Manage or Worker node of this Swarm.

Setting node Label

On the Manager node, you can label any node:

docker node update $node_name --label-add main=true
Copy the code

$node_name specifies the node ID or HOSTNAME. Label is a key-value pair. Main =true is the key and true is the value.

With the Label set, constraints from placement can be used in the Compose file to limit the available nodes.

services:
  some-serivce:
    ...
    deploy:
      placement:
        constraints:
          - node.labels.main == true
      ...
Copy the code

Some-service can only be used on nodes whose Label is set to main=true.

Start or stop the service

docker stack deploy $stack_name -c docker-compose.yaml -c other.yaml ...
Copy the code

$stack_name is the Stack name. You can use -c to specify multiple docker-compose files or deploy multiple files in batches under the same Stack. The YAML file is composed in the same way as the docker-compose command, adding the following unique configurations and ignoring some configurations not supported in Swarm scenarios.

It is recommended to use the Docker-compose file to compose the Stack rather than create it manually using the Docker service create.

To stop all services in the Stack, run the following command.

docker stack rm $stack_name
Copy the code

Update an image of a running service

docker service update --image $image:$tag $service_name
Copy the code