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