Docker Swarm clustering practices

Create the cluster

Docker swarm init (docker swarm init) docker swarm init (docker swarm init)

docker swarm init --advertise-addr xx.xx.xx.xx
Copy the code

Typically, the first management node to join the cluster becomes the Leader, and the next management node to join the cluster is Reachable. If the current Leader fails, all Reachable users will reelect a new Leader.

To join the cluster

The clustering mode built into Docker comes with its own public key infrastructure (PKI) system, making it easy to deploy containers securely. Nodes in the cluster use transport Layer Security protocol (TLS) to authenticate, authorize, and encrypt the communications of other nodes in the cluster.

By default, when a swarm cluster is created using the docker swarm init command, the Manager node generates a new root certificate authority (CA) and key pair to secure communication with other nodes joining the cluster.

The Manager node generates two tokens for other nodes to use when joining the cluster: one Worker token and one Manager token. Each token includes a summary of the root CA certificate and a randomly generated key. When a node is added to the cluster, the added node uses the digest to validate the root CA certificate from the remote administrative node. The remote administrative node uses a key to ensure that the node joining is an approved node.

Manager

To add a Manager node to the cluster, run the docker swarm join-token Manager command to view the token information of the management node.

docker swarm join-token manager
Copy the code

Then run the above command on the other Docker nodes to join the Swarm cluster as Manager.

Worker

As you can see from the results returned when creating the cluster, to add a Worker node to the cluster, run the commands shown in the following figure. Alternatively, the management node can run the docker swarm join-token worker command to view the token information of the working node.

Then run on other nodes docker swarm the join, and they carry a token parameters to swarm cluster, the node role for the Worker.

Viewing Cluster Information

Run Docker Info on any Manager node to view the current cluster information.

docker info
Copy the code

  

Viewing cluster Nodes

Run docker node ls on any Manager node to view the node information in the current cluster.

docker node ls
Copy the code

* represents the current node. The current environment consists of three management nodes, one master, two slave, and two working nodes.

Node MANAGER STATUS Indicates whether the node belongs to MANAGER or Worker. If there is no value, the node belongs to Worker node.

  • Leader: This node is the primary node of the management node and is responsible for cluster management and choreography decisions of the cluster.
  • Reachable: This node is the slave node of the management node. If the Leader node is unavailable, this node can be elected as the new Leader node.
  • Unavailable: The management node cannot communicate with other management nodes. If the management node is not available, either add the new management node to the cluster or upgrade the working node to the management node.

Node AVAILABILITY: Indicates whether the scheduler can assign tasks to this node.

  • Active: The scheduler can assign tasks to this node;
  • Pause: The scheduler does not assign new tasks to this node, but existing tasks can still run;
  • Drain: The scheduler does not assign new tasks to the node, and closes all existing tasks on the node and schedules them on available nodes.

Remove nodes

Manager

Before deleting a node, change AVAILABILITY to Drain. The purpose is to migrate the services of this node to other available nodes to ensure normal services. It is a good idea to check the container migration to ensure that this step has been handled before proceeding.

Docker node update - the availability drain node name tapping | node IDCopy the code

Then, demote the Manager node to the Worker node.

Docker node name | demote node IDCopy the code

Then, run the following command on the node that has been downgraded to Worker to leave the cluster.

docker swarm leave
Copy the code

Finally, delete the node you just left in the management node.

Docker node name | rm node IDCopy the code

  

Worker

Before deleting a node, change AVAILABILITY to Drain. The purpose is to migrate the services of this node to other available nodes to ensure normal services. It is a good idea to check the container migration to ensure that this step has been handled before proceeding.

Docker node update - the availability drain node name tapping | node IDCopy the code

Then, run the following command on the Worker node that you want to delete to leave the cluster.

docker swarm leave

## Disband the clusterdocker swarm leave --forceCopy the code

Finally, delete the node you just left in the management node.

Docker node name | rm node IDCopy the code

 

Service deployment

Note: Any operations related to cluster management are performed on the Manager node.

Create a service

Detailed commands can be used to query docker official documents. Here I take our actual situation as an example:

docker service create \
--replicas 2 \
--name report-middle-service \
--network=host \
--mount type=bind,source=/home/application/logs,destination=/logs \
--env spring.cloud.nacos.config.namespace=xxxxxxx \
image_name -v /home/application/logs:/logs  
Copy the code

  • docker service create: Create a service.
  • --replicas: Specifies how many instances of a service are running.
  • --name: Service name;
  • --network: Network mode;
  • --mount: Set the data volume, I here is bound to the host directory;
  • --envSet the environment variable. I set the namespace of nacOS here

  

Check the service

You can view running services using docker service ls.

docker service ls
Copy the code

Can through the docker service inspect service name | service ID to check the details.

Docker service inspect service name | IDCopy the code

Can through the docker service ps service name | ID to check the service running on which nodes.

docker service ps
Copy the code

Run Docker PS on the corresponding task node to view the relevant information of the container corresponding to the service.

Flexible service

After deploying a Service to a cluster, you can run commands to flexibly expand the number of containers in the service. A container that runs within a service is called a task.

You can use the following two methods to expand the number of tasks that a Service can run to N.

Docker service scale service name | ID = n docker service update - replicas nCopy the code

It is worth noting that here my network is set to host, so I need to control one machine to start at most one service container, otherwise port conflict occurs

Swarm cluster mode in the real sense to achieve the so-called elastic service, dynamic expansion and contraction of a line of command to handle, simple, convenient, powerful.

Remove the service

Through the docker service rm service name | service ID can be deleted.

Docker service rm service name | IDCopy the code

  

Common commands

docker swarm

The command instructions
docker swarm init Initializing a Cluster
docker swarm join-token worker Check the token of the working node
docker swarm join-token manager Check the token of the management node
docker swarm join To join the cluster

docker node

The command instructions
docker node ls View all nodes in the cluster
docker node ps View all tasks on the current node
Docker node name | rm node ID Deleting a node (-fForced deletion)
Docker node name | inspect node ID Viewing Node Details
Docker node name | demote node ID A node is degraded from a management node to a working node
Docker node name | promote node ID A node is upgraded from a working node to a management node
Docker node name | update node ID Update the node

docker service

The command instructions
docker service create Create a service
docker service ls Viewing All Services
Docker service inspect service name | ID Viewing Service Details
Docker service logs service name | ID Viewing Service Logs
Docker service rm service name | ID Deleting a service (-fForced deletion)
Docker service scale service name | ID = n Setting the Number of Services
Docker service update service name | ID Update service

At the end

The general practice of Docker Swarm is here. On the whole, it is relatively easy to use. The simple Docker container cluster management tool will try to be applied in the production environment later, and the problems encountered will be recorded.

The resources

  • Docs.docker.com/engine/swar…
  • Docs.docker.com/engine/swar…
  • Docs.docker.com/engine/refe…