使用docker service create创建容器时默认会使用调度机制选择集群内的一个节点运行容器

如果希望创建的容器运行在指定的节点上可通过--constraint参数通过label来在指定的节点


第一步,在节点启动的docker daemon加上--label


命令参数说明:

[root@centos-minion2 ~]# dockerd --help| grep label
  --label=[]     Set key=value labels to the daemon


例如我的配置 /etc/systemd/system/docker.service.d/docker.conf

[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H unix:///var/run/docker.sock --insecure-registry 192.168.209.0/24 --label hostname=minion2

然后重启服务

[root@centos-minion2 ~]# systemctl daemon-reload
[root@centos-minion2 ~]# systemctl restart docker

在MANAGER 查看label

[root@centos-master ~]# docker node inspect -f '``.`Description`.`Engine`.`Labels`' centos-minion2
map[hostname:minion2]

第二步,创建service 指定在centos-minion2 这个节点上运行容器

[root@centos-master ~]# docker service create --name web --replicas 4 --constraint engine.labels.hostname==minion2 nginx

wKiom1fSGOKg5zbzAAOB9yeIgw0654.jpg

constraint后面的参数除了可以用==还可以用!=

之前的版本有支持表达式匹配,现在这个好像用不了,难道是打开方式不对?