使用docker service create建立容器時默認會使用調度機制選擇集羣內的一個節點運行容器node
若是但願建立的容器運行在指定的節點上可經過--constraint參數經過label來在指定的節點nginx
第一步,在節點啓動的docker daemon加上--labelweb
命令參數說明:docker
[root@centos-minion2 ~]# dockerd --help| grep label --label=[] Set key=value labels to the daemon
例如個人配置 /etc/systemd/system/docker.service.d/docker.confcentos
[Service] ExecStart= ExecStart=/usr/bin/dockerd -H unix:///var/run/docker.sock --insecure-registry 192.168.209.0/24 --label hostname=minion2
而後重啓服務bash
[root@centos-minion2 ~]# systemctl daemon-reload [root@centos-minion2 ~]# systemctl restart docker
在MANAGER 查看labelide
[root@centos-master ~]# docker node inspect -f '``.`Description`.`Engine`.`Labels`' centos-minion2 map[hostname:minion2]
第二步,建立service 指定在centos-minion2 這個節點上運行容器unix
[root@centos-master ~]# docker service create --name web --replicas 4 --constraint engine.labels.hostname==minion2 nginx
constraint後面的參數除了能夠用==還能夠用!=rest
以前的版本有支持表達式匹配,如今這個好像用不了,難道是打開方式不對?blog