很简单,先给节点添加标签,然后服务发布时添加限制条件即可!
示例集群信息:
docker@node1:~$ docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
axr4zun8u1es8ytizjpt3zlnw * node1 Ready Active Leader 18.03.0-ce
vdip2js7tfflxv0smj6wdw0bv node2 Ready Active 18.03.0-ce
vi17ametnwd58297z6nlcl2o0 node3 Ready Active 18.03.0-ce
※ 添加标签
docker node update --label-add role=web node1
※ 查看标签
docker node inspect node1
[
{
"ID": "axr4zun8u1es8ytizjpt3zlnw",
"Version": {
"Index": 476
},
"CreatedAt": "2018-07-19T03:50:02.734603631Z",
"UpdatedAt": "2018-07-30T06:37:04.465194614Z",
"Spec": {
"Labels": {
"role": "web" # 人工添加的标签
},
"Role": "manager",
"Availability": "active"
}
# 省略
}
]
※ 删除标签
docker node update --label-rm role node1
※ Service 方式
docker service create \
--name nginx_2 \
--constraint 'node.labels.role == web' \
nginx
※ Stack 方式
version: '3.6'
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
constraints
为数组,填写多个约束时,它们之间的关系是 AND
。
constraints
可以匹配 node
标签和 engine
标签,engine.labels
适用于 Docker Engine 标签,如操作系统,驱动程序等,node.labels
适用于上述人为添加到节点的。
node | attribute matches | example |
---|---|---|
node.id | Node ID | node.id==2ivku8v2gvtg4 |
node.hostname | Node hostname | node.hostname!=node-2 |
node.role | Node role | node.role==manager |
node.labels | user defined node labels | node.labels.security==high |
engine.labels | Docker Engine's labels | engine.labels.operatingsystem==ubuntu 14.04 |