106、Label 控制 Service的位置 (Swarm13)

 
上一节我们讨论了 Service部署的两种模式,global mode 和 replicated mode 。无论global mode 还是replicated mode ,副本运行在哪些节点都是由Swarm决定的。
 
那么作为用户我们没有可能精细控制 Service运行的位置呢? 答案是: 能 , 使用label
 
过程分两步:
    1、为每个node 定义label
    2、设置Service运行在指定label的node上
 
label 可以灵活的描述node的属性,其形式是 key = value ,用户可以任意指定,例如将 swarm-worker1 作为测试环境,为其添加label env=test
 
host01标记为测试环境,host02标记为线上环境
 
root@host03:~# docker node update --label-add env=test host01    #    将host01标记为测试环境
host01
 
root@host03:~# docker node inspect host01 --pretty
ID:            hvt2ez9e7zvqm2hz8nix1eke7
Labels:
- env=test
Hostname:                  host01
Joined at:                 2019-05-14 03:19:30.200301075 +0000 utc
Status:
State:            Ready
Availability:             Active
Address:        10.12.31.211
 
root@host03:~# docker node update --label-add env=prod host02    #    将host02标记为线上环境
host02
root@host03:~# docker node inspect host02 --pretty
ID:            asn5ufnogzkyqigk4mizatoer
Labels:
- env=prod
Hostname:                  host02
Joined at:                 2019-05-14 03:19:34.507855133 +0000 utc
Status:
State:            Ready
Availability:             Active
Address:        10.12.31.212
 
在测试环境创建Service my_web ,副本数 3
 
root@host03:~# docker service create --name my_web --replicas 3 --constraint node.labels.env==test --publish 80:80 httpd
addp6i8pm0msmamq6a4oc94py
overall progress: 3 out of 3 tasks
1/3: running   [==================================================>]
2/3: running   [==================================================>]
3/3: running   [==================================================>]
verify: Service converged
root@host03:~# docker service ps my_web    #    确认所有的副本都在测试环境的node上
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE            ERROR               PORTS
63mta4uzj5kh        my_web.1            httpd:latest        host01              Running             Running 11 seconds ago                       
svynuplsq5l9        my_web.2            httpd:latest        host01              Running             Running 11 seconds ago                       
y8ion2zxr2fb        my_web.3            httpd:latest        host01              Running             Running 11 seconds ago                       
root@host03:~# docker service inspect my_web --pretty
ID:        addp6i8pm0msmamq6a4oc94py
Name:        my_web
Service Mode:    Replicated
Replicas:    3
Placement:
Constraints:     [node.labels.env==test]
 
 
将 my_web 的Service 从测试环境迁移到线上环境
 
root@host03:~# docker service update --constraint-rm node.labels.env==test my_web    #    删除Service 测试环境的标签
my_web
overall progress: 3 out of 3 tasks
1/3: running   [==================================================>]
2/3: running   [==================================================>]
3/3: running   [==================================================>]
verify: Service converged
root@host03:~# docker service ps my_web
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE           ERROR               PORTS
63mta4uzj5kh        my_web.1            httpd:latest        host01              Running             Running 4 minutes ago                       
svynuplsq5l9        my_web.2            httpd:latest        host01              Running             Running 4 minutes ago                       
y8ion2zxr2fb        my_web.3            httpd:latest        host01              Running             Running 4 minutes ago                       
root@host03:~# docker service update --constraint-add node.labels.env==prod my_web    #    添加Service 线上环境的标签
my_web
overall progress: 3 out of 3 tasks
1/3: running   [==================================================>]
2/3: running   [==================================================>]
3/3: running   [==================================================>]
verify: Service converged
root@host03:~# docker service ps my_web
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE             ERROR               PORTS
mj39f4gddt7g        my_web.1            httpd:latest        host02              Running             Running 17 seconds ago                        
63mta4uzj5kh         \_ my_web.1        httpd:latest        host01              Shutdown            Shutdown 18 seconds ago                       
r82s4x7t8cft        my_web.2            httpd:latest        host02              Running             Running 12 seconds ago                        
svynuplsq5l9         \_ my_web.2        httpd:latest        host01              Shutdown            Shutdown 13 seconds ago                       
nxu3gvglcn5y        my_web.3            httpd:latest        host02              Running             Running 8 seconds ago                         
y8ion2zxr2fb         \_ my_web.3        httpd:latest        host01              Shutdown            Shutdown 9 seconds ago                
 
label 与 global mode 组合,只收集线上环境的日志
 
root@host03:~# docker service create --mode global --constraint node.labels.env==prod --name monitor busybox sleep 99999
vbo0n6hgrr8qehw1kql3znk4y
overall progress: 1 out of 1 tasks
asn5ufnogzky: running   [==================================================>]
verify: Service converged
root@host03:~# docker service ps monitor
ID                  NAME                                IMAGE               NODE                DESIRED STATE       CURRENT STATE            ERROR               PORTS
bek7a8mt387i        monitor.asn5ufnogzkyqigk4mizatoer   busybox:latest       host02              Running             Running 24 seconds ago                       
 

猜你喜欢

转载自www.cnblogs.com/www1707/p/10872799.html
106