deploy service on swarm

轉自:https://www.cnblogs.com/jsonhc/p/7852530.html

swarm集羣配置完成後,查看一些基本的信息:html

[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 

查看節點信息:node

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

若是須要更新詳細的查看節點信息:linux

複製代碼
[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命令來建立服務:nginx

[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是鏡像web

查看建立好的service:docker

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

查看service服務在節點的運行狀況:json

[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服務就行less

若是須要詳細查看service信息:tcp

複製代碼
[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.1四、192.168.101.15),不指定爲何建立的service一直沒有實現負載均衡,一直只是manager1節點可以提供訪問,這個問題等待解決

 

上面能夠看出service由manager一、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
相關文章
相關標籤/搜索