Docker swarm deployment control

I still remember that I wrote an article called "Docker Rapid Deployment Project, Speedy Building Distributed" , where I described how to use docker swarm and how to build my own private mirror warehouse. With the recent increase in business volume, more machines have been added. The difficulty of docker swarm management is on the rise. The main problems are as follows

  • The physical machine configuration is different (such as CPU, memory, etc.)
  • Different types of services are deployed (such as Web services, Job services, etc.)
  • The nodes in the Swarm cluster span computer rooms. In order to communicate faster between internal services, how should they be deployed in groups?
  • 。。。

In order to solve the above problems, in order to achieve a more reasonable and scientific management and deployment, I have this article today.

There are three mechanisms for deployment and scheduling of docker nodes: random deployment, balanced deployment, and first full deployment

Random deployment: random selection in active

Balanced deployment: try to fill up all nodes evenly

Full deployment first: In contrast to balanced deployment, first deploy to the upper limit, and then deploy the corresponding

So how to manage it? Below I introduce several ways, as follows

  • NodeId
  • HOSTNAME
  • Node role
  • node labels
  • engine.labels

First, let's check the node list information and use docker node lsit directly .

First of all, let’s interpret the restrictions of docker

# docker service create --help
--constraint list                    Placement constraints
--container-label list               Container labels

The parameters that follow them are all lists, which are used in the following example

NodeId

Specify the deployment node according to the NodeId. The following is an example of building a private mirror warehouse.

docker service

docker service create \
--name registry \
--publish published=5000,target=5000 \
--constraint node.id==ytsyvuhfs60spr361y6irpynm \
registry:2

Command interpretation

# 在docker swarm中创建服务
docker service create 	\
# --name 服务别名
# 指定node的id,ytsyvuhfs60spr361y6irpynm这个是我这里的node.id,你使用的时候需按照需求替换即可
--constraint node.id==ytsyvuhfs60spr361y6irpynm \
# 暴露公开的接口,可以让节点中的其他node可以访问
--publish published=5000,target=5000	\
# 镜像名:版本号
registry:2

In this way, we have realized the deployment of designated nodes, is it very simple?

docker stack

# docker-compose.yaml 
version: '3'
services:
    registry:
         registry:2
         ports:
           - target: 8080
           - published: 8080
           - protocol: tcp
           - mode: ingress
         deploy:
           mode: global
           placement:
              constraints:                      # 添加条件约束
                - node.id==ytsyvuhfs60spr361y6irpynm
           restart_policy:
             condition: on-failure
             max_attempts: 3

HOSTNAME

​ In addition, we can also specify the hostname to deploy the application to the specified hostname, the operation is similar to the above. Let's implement it. First, we need to view the information of the corresponding node, and use it on the manager node to docker node lsview it, as follows

docker service

The creation command is as follows , taking nginx as an example

docker service create \
--name nginx \
--constraint node.hostname==ecs-dc8a-0003 \
-p 80:80 nginx

In this way, we will deploy the nginx service to the corresponding node, and the expansion will only be deployed on this node. The example is as follows

docker service scale nginx=3

docker stack

# docker-compose.yaml 
version: '3'
services:
    nginx:
         image: nginx
         ports:
           - target: 80
           - published: 80
           - protocol: tcp
           - mode: ingress
         deploy:
           mode: global
           placement:
              constraints:                      # 添加条件约束
                - node.hostname==ecs-dc8a-0003
           restart_policy:
             condition: on-failure
             max_attempts: 3

Node role

Earlier we learned that, according node.id, node.hostnameto deploy the specified node, but specified it is unique. So how to achieve the same type of random deployment? At this point, we continue to understand more deeply and constrain more node relationships. The relationships among deployable nodes are leader and work.

For example, I want to implement not deployment on the primary node, although we can use the conditions in the usage restrictions to avoid it. For example like this

# 示例
docker service create \
--name registry \
--publish published=5000,target=5000 \
--constraint node.id!=ytsyvuhfs60spr361y6irpynm \ # 修改处
registry:2

Just change ==, to !=, and then.

docker service

But after all, I still don’t like it very much. In fact, we can do the same, and deploy constraints based on the node relationship. An example is as follows

docker service create \
--name nginx \
--publish published=80,target=80 \
--constraint node.role!=manager nginx

docker stack

# docker-compose.yaml 
version: '3'
services:
    nginx:
         image: nginx
         ports:
           - target: 80
           - published: 80
           - protocol: tcp
           - mode: ingress
         deploy:
           mode: global
           placement:
              constraints:                      # 添加条件约束
                - node.role!=manager
           restart_policy:
             condition: on-failure
             max_attempts: 3

Node lables

Having said so much, the care of novices is over. Next, we will delay the deployment of more commonly used, more labels, to deploy. As long as it is the same label, it can be deployed to, and you can perform more handy management according to the label in the subsequent steps, such as adding a label, expelling nodes, and then adding labels to expand the capacity.

Add tags and check tags

# 添加标签
docker node update --label-add role=web hostname
# 检查标签
docker node inspect hostname 
# 删除标签
docker node update --label-rm role hostname

The output is as follows

"ID": "ttdku9ch37pknxu7b9sxknimb",                                                                                   
        "Version": {                                                                                                         
            "Index": 852                                                                                                     
        },                                                                                                                   
        "CreatedAt": "2020-12-08T11:10:53.322771866Z",                                                                       
        "UpdatedAt": "2020-12-13T13:24:57.009816659Z",                                                                       
        "Spec": {                                                                                                            
            "Labels": {                                                                                                      
                "role": "web"              # 这样我们就实现了添加标签                                                                                  
            },                                                                                                               
            "Role": "manager",                                                                                               
            "Availability": "active"                                                                                         
        },                 
        # 略

docker Service

docker service create \
  --name nginx_2 \
  --constraint 'node.labels.role == web' \
  nginx

docker Stack

version: '3'
services:
    mycat:
         image: nginx
         ports:
           - target: 8080
             published: 8080
             protocol: tcp
             mode: ingress
         deploy:
           mode: global
           placement:
              constraints:                      # 添加条件约束
                - node.labels.role==web
           restart_policy:
             condition: on-failure
             max_attempts: 3

constraintsFor an array, when filling in multiple constraints, the relationship between them is AND. In other words, we can combine and use

Please refer to the table below for more details

node attribute matches example
node.id Node ID node.id==2ivku8v2gvtg4
node.hostname Node hostname node.hostname!=node-2
node.role Node role (manager/worker) node.role==manager
node.platform.os Node operating system node.platform.os==windows
node.platform.arch Node architecture node.platform.arch==x86_64
node.labels User-defined node labels node.labels.security==high
engine.labels Docker Engine’s labels engine.labels.operatingsystem==ubuntu-14.04

Recommended reading

Related official documents

Guess you like

Origin blog.csdn.net/wzp7081/article/details/111148010