部署service到swarm集群,指定task运行的节点?(节点自定义标签)
如标题所示,如何将service中task,指定运行在具有某个特殊的节点上,比如,存储特别大,有GPU的?
方法,非常的简单。
1、节点增加标签
首先,给特殊的主机增加label
[root@nccztsjb-node-01 ~]# docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION gxfkhuc95br6ltkhorpw1q4tq * nccztsjb-node-01 Ready Active Reachable 20.10.17 8zjicf39fk28jn106symk1g5e nccztsjb-node-02 Ready Active 20.10.17 7d59usghrgq05k0yh4lbykw5v nccztsjb-node-04 Ready Active Reachable 20.10.17 wnd24l698iruhhp1xw0y3iyig nccztsjb-node-05 Ready Active Leader 20.10.17 [root@nccztsjb-node-01 ~]# docker node update --label-add store=large nccztsjb-node-04 nccztsjb-node-04 [root@nccztsjb-node-01 ~]#
2、部署service
此时,具有特殊资源的节点,已经增加好了标签,接下来,就是关键点了,部署service
通过--constraint参数指定具体约束条件
docker service create \ --name redis \ --constraint node.labels.store==large \ 172.20.58.152/middleware/redis:3.0.6
部署过程
[root@nccztsjb-node-01 ~]# docker service create \
> --name redis \
> --constraint node.labels.store==large \
> 172.20.58.152/middleware/redis:3.0.6
u2a9nm7byfnxv3t7lofgsc321
overall progress: 1 out of 1 tasks
1/1: running [==================================================>]
verify: Service converged
查看task的运行状态
[root@nccztsjb-node-01 ~]# docker service ps redis ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS rzwlggf8i37r redis.1 172.20.58.152/middleware/redis:3.0.6 nccztsjb-node-04 Running Running 42 seconds ago [root@nccztsjb-node-01 ~]# [root@nccztsjb-node-01 ~]# docker service inspect redis --pretty ID: u2a9nm7byfnxv3t7lofgsc321 Name: redis Service Mode: Replicated Replicas: 1 Placement: Constraints: [node.labels.store==large] UpdateConfig: Parallelism: 1 On failure: pause Monitoring Period: 5s Max failure ratio: 0 Update order: stop-first RollbackConfig: Parallelism: 1 On failure: pause Monitoring Period: 5s Max failure ratio: 0 Rollback order: stop-first ContainerSpec: Image: 172.20.58.152/middleware/redis:3.0.6@sha256:d161fc7de1183ed220e28cf5e4cc50f1b535d580f2c3503c0b4ae4f2d63c1b1f Init: false Resources: Endpoint Mode: vip [root@nccztsjb-node-01 ~]#
Placement:
Constraints: [node.labels.store==large] # 执行部署的限制条件。
OK,这样task就运行到这个节点了。
那别停,如果是多个副本呢?
[root@nccztsjb-node-01 ~]# docker service scale redis=2 redis scaled to 2 overall progress: 2 out of 2 tasks 1/2: running [==================================================>] 2/2: running [==================================================>] verify: Service converged [root@nccztsjb-node-01 ~]#
毫无疑问,多个副本,都是运行在这一个节点上的,因为,只有一个节点,有这样的标签
[root@nccztsjb-node-01 ~]# docker service ps redis ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS rzwlggf8i37r redis.1 172.20.58.152/middleware/redis:3.0.6 nccztsjb-node-04 Running Running 3 minutes ago shonptv3fcae redis.2 172.20.58.152/middleware/redis:3.0.6 nccztsjb-node-04 Running Running 18 seconds ago [root@nccztsjb-node-01 ~]#
到这里应该结束了吧,没有,后面还会介绍到,其他的约束该怎么做?
比如:
- 根据主机名
- 根据主机的操作系统
- 根据主机平台
- 根据主机的ID
- 根据的CPU架构
这些,后面的文章中,我们会讲到······