Skip to main content
 首页 » 操作系统

Docker之deploy service on swarm

2022年07月18日36shanyou

swarm集群配置完成后,查看一些基本的信息:

[root@manager1 ~]# docker-machine ls 
NAME       ACTIVE   DRIVER    STATE     URL                         SWARM   DOCKER        ERRORS 
manager1   -        generic   Running   tcp://192.168.101.14:2376           v17.09.0-ce    
work1      -        generic   Running   tcp://192.168.101.15:2376           v17.10.0-ce 

查看节点信息:

[root@manager1 ~]# docker node ls 
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS 
4ecjeeu8273dzsmfta7u6op9t *   manager1            Ready               Active              Leader 
sn7c5u5wtupgt3qpz22o117hl     work1               Ready               Active  

如果需要更新详细的查看节点信息:

复制代码
[root@manager1 ~]# docker node inspect --pretty manager1 
ID:                     4ecjeeu8273dzsmfta7u6op9t 
Hostname:               manager1 
Joined at:              2017-11-15 18:28:21.103210247 +0000 utc 
Status: 
 State:                 Ready 
 Availability:          Active 
 Address:               192.168.101.14 
Manager Status: 
 Address:               192.168.101.14:2377 
 Raft Status:           Reachable 
 Leader:                Yes 
Platform: 
 Operating System:      linux 
 Architecture:          x86_64 
Resources: 
 CPUs:                  2 
 Memory:                3.686GiB 
Plugins: 
 Log:           awslogs, fluentd, gcplogs, gelf, journald, json-file, logentries, splunk, syslog 
 Network:               bridge, host, macvlan, null, overlay 
 Volume:                local 
Engine Version:         17.09.0-ce 
Engine Labels: 
 - provider=generic 
TLS Info: 
 TrustRoot: 
-----BEGIN CERTIFICATE----- 
MIIBazCCARCgAwIBAgIUPDGTCzw2cZelyvysFW+jcF3PcQMwCgYIKoZIzj0EAwIw 
EzERMA8GA1UEAxMIc3dhcm0tY2EwHhcNMTcxMTE1MTgyMzAwWhcNMzcxMTEwMTgy 
MzAwWjATMREwDwYDVQQDEwhzd2FybS1jYTBZMBMGByqGSM49AgEGCCqGSM49AwEH 
A0IABNyMg4cqG1XaXB6g5PbNGxjishle1cPF9t0kn5LbRKwh+IGwDZD2fdbli2bk 
9fzVQVWCj3aZzCyyVSFxZ30+cPWjQjBAMA4GA1UdDwEB/wQEAwIBBjAPBgNVHRMB 
Af8EBTADAQH/MB0GA1UdDgQWBBTElfvEWZqpfIfCYCr+Vn6peYGOPDAKBggqhkjO 
PQQDAgNJADBGAiEAh44+/Ns5T+MwiD49jXmIpr7mz/8qyMEvFDtk55j5eQ0CIQCP 
HEbLBRkJWxqNs6p5O+emfvIVpFHuy/X8K6tDF7qmiw== 
-----END CERTIFICATE----- 
 
 Issuer Subject:        MBMxETAPBgNVBAMTCHN3YXJtLWNh 
 Issuer Public Key:     MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE3IyDhyobVdpcHqDk9s0bGOKyGV7Vw8X23SSfkttErCH4gbANkPZ91uWLZuT1/NVBVYKPdpnMLLJVIXFnfT5w9Q==
复制代码

现在利用docker service命令来创建服务:

[root@manager1 ~]# docker service create --name web --replicas 2 nginx 
oq2jsjc4zhwtpcwqqd2whj701 
Since --detach=false was not specified, tasks will be created in the background. 
In a future release, --detach=false will become the default.

其中的参数:--name表示创建服务的名称,--replicas表示创建的副本数,nginx是镜像

查看创建好的service:

[root@manager1 ~]# docker service ls 
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS 
oq2jsjc4zhwt        web                 replicated          2/2                 nginx:latest   

查看service服务在节点的运行情况:

[root@manager1 ~]# docker service ps web 
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE           ERROR               PORTS 
rk7pyq7pr49b        web.1               nginx:latest        work1               Running             Running 7 seconds ago                        
w3orhk7uzvzy        web.2               nginx:latest        manager1            Running             Running 7 seconds ago 

如果在创建的过程中出现如下报错:

复制代码
[root@manager1 ~]# docker service ps web 
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE             ERROR                              PORTS 
klcan0r46jih        web.1               nginx:latest        work1               Ready               Preparing 3 seconds ago                                       
ypx26zy1gp63         \_ web.1           nginx:latest        work1               Shutdown            Rejected 3 seconds ago    "No such image: nginx:latest@s…"    
dy51asan25pf        web.2               nginx:latest        manager1            Running             Running 18 seconds ago
复制代码

work1节点上没有相应的镜像,导致启动失败,可能的原因之一:work1节点使用的默认的官方镜像源,所以下载缓慢,导致失败,解决办法:将work1节点配置镜像加速器重启docker服务就行

如果需要详细查看service信息:

复制代码
[root@manager1 ~]# docker service inspect --pretty web 
 
ID:             oq2jsjc4zhwtpcwqqd2whj701 
Name:           web 
Service Mode:   Replicated 
 Replicas:      2 
Placement: 
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:         nginx:latest@sha256:9fca103a62af6db7f188ac3376c60927db41f88b8d2354bf02d2290a672dc425 
Resources: 
Endpoint Mode:  vip
复制代码

如果需要访问容器提供的服务,需要为容器进行映射端口:

[root@manager1 ~]# docker service update --publish-add 8080:80 web 
web

使用命令update进行对服务的修改,更新等等操作

复制代码
[root@manager1 ~]# docker service ps web 
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE                    ERROR               PORTS 
jzq72hlu434g        web.1               nginx:latest        work1               Running             Running less than a second ago                        
rk7pyq7pr49b         \_ web.1           nginx:latest        work1               Shutdown            Shutdown 1 second ago                                 
ys5voif2df7f        web.2               nginx:latest        manager1            Running             Running 2 seconds ago                                 
w3orhk7uzvzy         \_ web.2           nginx:latest        manager1            Shutdown            Shutdown 3 seconds ago 
复制代码
[root@manager1 ~]# docker service ls 
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS 
oq2jsjc4zhwt        web                 replicated          2/2                 nginx:latest        *:8080->80/tcp

然后进行访问web服务:

这里创建的服务在节点manager1和worl1上(192.168.101.14、192.168.101.15),不指定为什么创建的service一直没有实现负载均衡,一直只是manager1节点能够提供访问,这个问题等待解决

上面可以看出service由manager1、work1两个节点提供,现在将其中的某一个节点进行下线:

[root@manager1 ~]# docker node update --availability drain work1 
work1 
[root@manager1 ~]# docker node ls 
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS 
4ecjeeu8273dzsmfta7u6op9t *   manager1            Ready               Active              Leader 
sn7c5u5wtupgt3qpz22o117hl     work1               Ready               Drain   

节点的availability的值可以显示节点的状态(active表示活跃在线,drain表示下线不提供服务或者处于维护状态)

将节点wrok1下线后,查看服务的replicas数量:

[root@manager1 ~]# docker service ls 
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS 
oq2jsjc4zhwt        web                 replicated          2/2                 nginx:latest        *:8080->80/tcp

可以看见service web的replicas并没有减少:

复制代码
[root@manager1 ~]# docker service ps web 
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE                 ERROR               PORTS 
o0p9zt3su44g        web.1               nginx:latest        manager1            Running             Running about a minute ago                         
jzq72hlu434g         \_ web.1           nginx:latest        work1               Shutdown            Shutdown about a minute ago                        
rk7pyq7pr49b         \_ web.1           nginx:latest        work1               Shutdown            Shutdown 6 minutes ago                             
ys5voif2df7f        web.2               nginx:latest        manager1            Running             Running 6 minutes ago                              
w3orhk7uzvzy         \_ web.2           nginx:latest        manager1            Shutdown            Shutdown 6 minutes ago  
复制代码

而提供服务的节点由之前的manager1和worlk1共同提供变成了只由节点manager1提供:

现在将work1节点进行更改为active:

[root@manager1 ~]# docker node update --availability active work1 
work1 
[root@manager1 ~]# docker node ls                                 
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS 
4ecjeeu8273dzsmfta7u6op9t *   manager1            Ready               Active              Leader 
sn7c5u5wtupgt3qpz22o117hl     work1               Ready               Active  

当work1节点上线后,但是work1并没有抢回提供服务:

复制代码
[root@manager1 ~]# docker service ps web                          
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE            ERROR               PORTS 
o0p9zt3su44g        web.1               nginx:latest        manager1            Running             Running 4 minutes ago                         
jzq72hlu434g         \_ web.1           nginx:latest        work1               Shutdown            Shutdown 4 minutes ago                        
rk7pyq7pr49b         \_ web.1           nginx:latest        work1               Shutdown            Shutdown 9 minutes ago                        
ys5voif2df7f        web.2               nginx:latest        manager1            Running             Running 9 minutes ago                         
w3orhk7uzvzy         \_ web.2           nginx:latest        manager1            Shutdown            Shutdown 9 minutes ago 
复制代码

现在将service的replicas进行伸缩或者扩展,这里将replicas增加到3(现在数量为2)

[root@manager1 ~]# docker service scale web=3 
web scaled to 3
复制代码
[root@manager1 ~]# docker service ps web      
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE             ERROR               PORTS 
o0p9zt3su44g        web.1               nginx:latest        manager1            Running             Running 6 minutes ago                          
jzq72hlu434g         \_ web.1           nginx:latest        work1               Shutdown            Shutdown 6 minutes ago                         
rk7pyq7pr49b         \_ web.1           nginx:latest        work1               Shutdown            Shutdown 11 minutes ago                        
ys5voif2df7f        web.2               nginx:latest        manager1            Running             Running 11 minutes ago                         
w3orhk7uzvzy         \_ web.2           nginx:latest        manager1            Shutdown            Shutdown 11 minutes ago                        
g0liotj1z9j6        web.3               nginx:latest        work1               Running             Running 1 second ago 
复制代码

可以看见work1节点又开始为web提供服务了

删除掉上面创建的服务,重新创建新的服务,使用--publish:

[root@manager1 ~]# docker service rm web 
web

本文参考链接:https://www.cnblogs.com/maohuidong/p/9914823.html