097、如何实现Service伸缩?(Swarm04)

 
上一节部署了只有一个副本的Service,不过对于web服务,我们通常会运行多个实例,这样可以做负载均衡,同时也能提高可用性。
 
swarm 要实现这个目标非常简单,增加Service 的副本数就可以了。在 swarm-manager 上执行如下命令
 
root@host03:~# docker service scale web_server=5
web_server scaled to 5
overall progress: 5 out of 5 tasks
1/5: running   [==================================================>]
2/5: running   [==================================================>]
3/5: running   [==================================================>]
4/5: running   [==================================================>]
5/5: running   [==================================================>]
verify: Service converged
 
root@host03:~# docker service ps web_server    #    副本数增加过程中
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE              ERROR               PORTS
jykct1jmfrte        web_server.1        httpd:latest        host03              Running             Running 20 minutes ago                         
n4afxlx16tny        web_server.2        httpd:latest        host03              Running             Running 13 seconds ago                         
kmtovvs8i1rk        web_server.3        httpd:latest        host01              Running             Preparing 14 seconds ago                       
mlsb1ey4n65r        web_server.4        httpd:latest        host02              Running             Preparing 14 seconds ago                       
a08aabmfiprh        web_server.5        httpd:latest        host02              Running             Preparing 14 seconds ago                       
root@host03:~# docker service ps web_server    #    副本数增加完毕
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE            ERROR               PORTS
jykct1jmfrte        web_server.1        httpd:latest        host03              Running             Running 21 minutes ago                       
n4afxlx16tny        web_server.2        httpd:latest        host03              Running             Running 52 seconds ago                       
kmtovvs8i1rk        web_server.3        httpd:latest        host01              Running             Running 29 seconds ago                       
mlsb1ey4n65r        web_server.4        httpd:latest        host02              Running             Running 31 seconds ago                       
a08aabmfiprh        web_server.5        httpd:latest        host02              Running             Running 31 seconds ago       
root@host03:~# docker service ls    #    要求副本数和实际副本数均已达到 5 
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
9aa10i3l6xjw        web_server          replicated          5/5                 httpd:latest     
 
设置 swarm-manager 为仅manager模式
 
root@host03:~# docker node update --availability drain host03    #    将 swarm-manager(host03)设置为仅 manager
host03
root@host03:~# docker node ls    #    host03 AVAILABILITY 已经变成 Drain
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
hvt2ez9e7zvqm2hz8nix1eke7     host01              Ready               Active                                  18.09.6
asn5ufnogzkyqigk4mizatoer     host02              Ready               Active                                  18.09.6
h6rzavsz2vjxstwj3pytiebjb *   host03              Ready               Drain               Leader              18.09.6
root@host03:~# docker service ps web_server    #    可以看到 host03 上的容器都已 shutdown,另外在host01 上新起了两个容器
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE               ERROR               PORTS
tznw0lrn8wh8        web_server.1        httpd:latest        host01              Running             Running 14 seconds ago                          
jykct1jmfrte         \_ web_server.1    httpd:latest        host03              Shutdown            Shutdown 15 seconds ago                         
4x32c9x1hizg        web_server.2        httpd:latest        host01              Running             Running 14 seconds ago                          
n4afxlx16tny         \_ web_server.2    httpd:latest        host03              Shutdown            Shutdown 15 seconds ago                         
kmtovvs8i1rk        web_server.3        httpd:latest        host01              Running             Running about an hour ago                       
mlsb1ey4n65r        web_server.4        httpd:latest        host02              Running             Running about an hour ago                       
a08aabmfiprh        web_server.5        httpd:latest        host02              Running             Running about an hour ago                       
 
弹性收缩,将副本数由 5 降到 3
 
root@host03:~# docker service scale web_server=3    #    副本数设置为 3
web_server scaled to 3
overall progress: 3 out of 3 tasks
1/3: running   [==================================================>]
2/3: running   [==================================================>]
3/3: running   [==================================================>]
verify: Service converged
root@host03:~# docker service ps web_server    #    可以查看编号为 3 和 5 的容器被删除了
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE               ERROR               PORTS
tznw0lrn8wh8        web_server.1        httpd:latest        host01              Running             Running 2 minutes ago                           
jykct1jmfrte         \_ web_server.1    httpd:latest        host03              Shutdown            Shutdown 2 minutes ago                          
4x32c9x1hizg        web_server.2        httpd:latest        host01              Running             Running 2 minutes ago                           
n4afxlx16tny         \_ web_server.2    httpd:latest        host03              Shutdown            Shutdown 2 minutes ago                          
mlsb1ey4n65r        web_server.4        httpd:latest        host02              Running             Running about an hour ago     
 
posted @ 2019-05-15 22:47  三角形  阅读(239)  评论(0编辑  收藏  举报