快速上手 Swarm mode搭建叢集
一、概述
Swarm 是使用 SwarmKit 構建的 Docker 引擎內建(原生)的叢集管理和編排工具。Swarm 叢集由 管理節點 和 工作節點 組成。
本篇使用的環境包括3個節點,一個作為Swarm的manager節點,兩個為worker節點,機器名和IP地址如下:
- wuweixiang: 139.9.44.81 (Swarm manager)
- VM_0_14_centos: 188.131.152.100 (Swarm worker)
centos7-WorkerB: 192.168.71.168 (Swarm worker)
二、初始化Swarm叢集
# 初始化一個叢集[root@wuweixiang ~]# docker swarm init --help Usage: docker swarm init [OPTIONS] Initialize a swarm Options: --advertise-addr string Advertised address (format: <ip|interface>[:port]) --autolock Enable manager autolocking (requiring an unlock key to start a stopped manager) --availability string Availability of the node ("active"|"pause"|"drain") (default "active") --cert-expiry duration Validity period for node certificates (ns|us|ms|s|m|h) (default 2160h0m0s) --data-path-addr string Address or interface to use for data path traffic (format: <ip|interface>) --default-addr-pool ipNetSlice default address pool in CIDR format (default []) --default-addr-pool-mask-length uint32 default address pool subnet mask length (default 24) --dispatcher-heartbeat duration Dispatcher heartbeat period (ns|us|ms|s|m|h) (default 5s) --external-ca external-ca Specifications of one or more certificate signing endpoints --force-new-cluster Force create a new cluster from current state --listen-addr node-addr Listen address (format: <ip|interface>[:port]) (default 0.0.0.0:2377) --max-snapshots uint Number of additional Raft snapshots to retain --snapshot-interval uint Number of log entries between Raft snapshots (default 10000) --task-history-limit int Task history retention limit (default 5) # Master - > 初始化一個叢集, 建立swarm管理節點[root@wuweixiang ~]# docker swarm init --advertise-addr 139.9.44.81 Swarm initialized: current node (xvmqc3op6e9lkao153u410m8x) is now a manager. To add a worker to this swarm, run the following command: docker swarm join --token SWMTKN-1-255nm4msqjuij5q0phuhy25ptz4m1qw7rfdbhwv4rbjl0ftg4j-0moyoy6mn3i4ewpaqh5wqrdq4 139.9.44.81:2377 To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions. # Master - > 檢視Worker節點連線所需要的Token資訊[root@wuweixiang ~]# docker swarm join-token worker To add a worker to this swarm, run the following command: docker swarm join --token SWMTKN-1-255nm4msqjuij5q0phuhy25ptz4m1qw7rfdbhwv4rbjl0ftg4j-0moyoy6mn3i4ewpaqh5wqrdq4 139.9.44.81:2377 # 使用docker info檢視叢集中的相關資訊 [root@wuweixiang ~]# docker info …… Swarm: active NodeID: xvmqc3op6e9lkao153u410m8x Is Manager: true ClusterID: oucnrveg187xttygnm6fak4di Managers: 1 Nodes: 2 Default Address Pool: 10.0.0.0/8 SubnetSize: 24 Orchestration: Task History Retention Limit: 5 …… # Master - > docker node ls 檢視叢集 [root@wuweixiang ~]# docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION nmejr3ko2c1hcuxljzete8fsf VM_0_14_centos Ready Active 18.09.0 xvmqc3op6e9lkao153u410m8x * wuweixiang Ready Active Leader 18.09.0
node ID旁邊那個*號表示現在連線到這個節點上。
三、將Worker節點加入Swarm叢集
[root@VM_0_14_centos ~]# docker swarm join --token SWMTKN-1-255nm4msqjuij5q0phuhy25ptz4m1qw7rfdbhwv4rbjl0ftg4j-0moyoy6mn3i4ewpaqh5wqrdq4 139.9.44.81:2377 This node joined a swarm as a worker.
四、管理Swarm叢集
1、刪除Swarm叢集節點
[root@VM_0_14_centos ~]# docker swarm leave Node left the swarm.
[root@wuweixiang ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
i3ma0jg3a0dzqezh1tjbwyxxk VM_0_14_centos Ready Active 18.09.0
nmejr3ko2c1hcuxljzete8fsf VM_0_14_centos Down Active 18.09.0
xvmqc3op6e9lkao153u410m8x * wuweixiang Ready Active Leader 18.09.0
[root@wuweixiang ~]# docker node rm --force nm
nm
[root@wuweixiang ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
i3ma0jg3a0dzqezh1tjbwyxxk VM_0_14_centos Ready Active 18.09.0
xvmqc3op6e9lkao153u410m8x * wuweixiang Ready Active Leader 18.09.0
2、更新Swarm叢集節點
[root@wuweixiang ~]# docker swarm update Usage: docker swarm update [OPTIONS] Update the swarm Options: --autolock Change manager autolocking setting (true|false) --cert-expiry duration Validity period for node certificates (ns|us|ms|s|m|h) (default 2160h0m0s) --dispatcher-heartbeat duration Dispatcher heartbeat period (ns|us|ms|s|m|h) (default 5s) --external-ca external-ca Specifications of one or more certificate signing endpoints --max-snapshots uint Number of additional Raft snapshots to retain --snapshot-interval uint Number of log entries between Raft snapshots (default 10000) --task-history-limit int Task history retention limit (default 5)
五、Swarm叢集的服務部署實踐
1 在Swarm中部署服務
在wuweixiang也就是manager節點上執行如下命令來部署服務:
[root@wuweixiang ~]# docker service create --replicas 1 --name helloworld alpine ping docker.com
引數說明:
--replicas
引數指定啟動的服務由幾個例項組成;--name
引數指定啟動服務的服務名;alpine ping docker.com
指定了使用alpine映象建立服務,例項啟動時執行ping docker.com命令。
這與docker run命令是一樣的。
使用docker service ls
檢視正在執行服務的列表:
[root@wuweixiang ~]# docker service ls ID NAME MODE REPLICAS IMAGE PORTS iswutf06uqkm helloworld replicated 1/1 alpine:latest
2 查詢Swarm中服務的資訊
在部署了服務之後,登入到manager節點,執行下面的命令來顯示服務的資訊。引數--pretty
使命令輸出格式化為可讀的格式,不加--pretty
可以輸出更詳細的資訊:
[root@wuweixiang ~]# docker service inspect --pretty helloworld ID: iswutf06uqkmpbbn57chv6dvi Name: helloworld Service Mode: Replicated Replicas: 1 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: alpine:latest@sha256:621c2f39f8133acb8e64023a94dbdf0d5ca81896102b9e57c0dc184cadaf5528 Args: ping docker.com Init: false Resources: Endpoint Mode: vip
使用命令docker service ps <SERVICE-ID>
可以查詢到哪個節點正在執行該服務:
[root@wuweixiang ~]# docker service ps is ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS ldbbln9dkuq2 helloworld.1 alpine:latest wuweixiang Running Running about an hour ago umsugc762yje \_ helloworld.1 alpine:latest VM_0_14_centos Shutdown Shutdown about an hour ago
3 在Swarm中動態擴充套件服務
登入到manager節點,使用命令docker service scale <SERVICE-ID>=<NUMBER-OF-TASKS>
來將服務擴充套件到指定的例項數:
[root@wuweixiang ~]# docker service scale helloworld=5 helloworld 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@wuweixiang ~]# docker service ls ID NAME MODE REPLICAS IMAGE PORTS iswutf06uqkm helloworld replicated 5/5 alpine:latest [root@wuweixiang ~]# docker service ps helloworld ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS ldbbln9dkuq2 helloworld.1 alpine:latest wuweixiang Running Running about an hour ago umsugc762yje \_ helloworld.1 alpine:latest VM_0_14_centos Shutdown Shutdown about an hour ago 3t4legcx7plv helloworld.2 alpine:latest VM_0_14_centos Running Running 43 seconds ago 6zkztr92rs62 helloworld.3 alpine:latest VM_0_14_centos Running Running 42 seconds ago w79zkyl803tk helloworld.4 alpine:latest wuweixiang Running Running 43 seconds ago k5jlwnx59agf helloworld.5 alpine:latest VM_0_14_centos Running Running 42 seconds ago
可見Swarm叢集建立了4個新的task來將整個服務的例項數擴充套件到5個。這些服務分佈在不同的Swarm節點上。
4 刪除Swarm叢集中的服務
在manager節點上執行docker service rm helloworld
便可以將服務刪除。刪除服務時,會將服務在各個節點上建立的容器一同刪除,而並不是將容器停止。
此外Swarm模式還提供了服務的滾動升級,將某個worker置為維護模式,及路由網等功能。在Docker將Swarm整合進Docker引擎後,可以使用原生的Docker CLI對容器叢集進行各種操作,使叢集的部署更加方便、快捷。
5 更新Swarm叢集中的服務版本
在前面的步驟中, 我們擴充套件了一個服務的多個例項, 如上所示, 我們擴充套件了基於Tomcat Server 8.5.8的Docker映象。 假如,現在我們需要使用Tomcat Server 8.6.0版本做為Docker容器版本來替換原有的Tomcat Server 8.5.8版本。
[root@centos7-Master ~]# docker service update --image tomcat:8.6.0 tomcat-service
tomcat-service
服務版本更新計劃將按以下步驟執行:
重新啟動一個暫停更新的服務, 可以使用docker service update <SERVICE-ID>
命令, 例如:
[root@centos7-Master ~]# docker service update tomcat-service
- 在Swarm叢集中的Manager節點上執行操作,用於完成服務版本的更新。
- 停止第一個任務
- 計劃對已停止任務的更新
- 啟動已更新任務的容器
- 如果任務更新返回“RUNNING”狀態,等待指定的延遲時間後,停止下一個任務
- 如果在任務更新時,任務返回“FAILED”狀態,將會暫停更新。
- 檢視服務版本更新結果
[root@centos7-Master ~]# docker service ps tomcat-service
6 停用Swarm叢集中的服務節點
如果我們想要停止Swarm叢集中某個服務的Worker節點, 我們可以使用docker node update --availability drain <Node-ID>
來停止Worker節點上的服務。
[root@wuweixiang ~]# docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION i3ma0jg3a0dzqezh1tjbwyxxk VM_0_14_centos Ready Active 18.09.0 xvmqc3op6e9lkao153u410m8x * wuweixiang Ready Active Leader 18.09.0 [root@wuweixiang ~]# docker node update --availability drain i3 i3 [root@wuweixiang ~]# docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION i3ma0jg3a0dzqezh1tjbwyxxk VM_0_14_centos Ready Drain 18.09.0 xvmqc3op6e9lkao153u410m8x * wuweixiang Ready Active Leader 18.09.0
在停止Worker節點上的服務後, 我們可以通過docker node inspect --pretty <Node-ID>
檢視節點狀態。
[root@wuweixiang ~]# docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION i3ma0jg3a0dzqezh1tjbwyxxk VM_0_14_centos Ready Drain 18.09.0 xvmqc3op6e9lkao153u410m8x * wuweixiang Ready Active Leader 18.09.0 [root@wuweixiang ~]# docker node inspect --pretty i3 ID: i3ma0jg3a0dzqezh1tjbwyxxk Hostname: VM_0_14_centos Joined at: 2018-12-11 09:21:15.918346569 +0000 utc Status: State: Ready Availability: Drain Address: 188.131.152.100 Platform: Operating System: linux Architecture: x86_64 Resources: CPUs: 1 Memory: 992.7MiB Plugins: Log: awslogs, fluentd, gcplogs, gelf, journald, json-file, local, logentries, splunk, syslog Network: bridge, host, macvlan, null, overlay Volume: local Engine Version: 18.09.0 TLS Info: TrustRoot: -----BEGIN CERTIFICATE----- MIIBajCCARCgAwIBAgIUaoragJW4UwMO+DCs1zkxpt1xPdswCgYIKoZIzj0EAwIw EzERMA8GA1UEAxMIc3dhcm0tY2EwHhcNMTgxMjExMDc0MTAwWhcNMzgxMjA2MDc0 MTAwWjATMREwDwYDVQQDEwhzd2FybS1jYTBZMBMGByqGSM49AgEGCCqGSM49AwEH A0IABFLvlDlCVuPyAbqMCKIl4MAdVfvgYLvoAIbkzX0EPPdlB5jiVR2oI6xSmWHg Yt5mivr+b0eRVg17RneCz/zJjgWjQjBAMA4GA1UdDwEB/wQEAwIBBjAPBgNVHRMB Af8EBTADAQH/MB0GA1UdDgQWBBSoVH4AOp4ATVDNzsnA/8aP/Qx2aDAKBggqhkjO PQQDAgNIADBFAiARza3fA5h4sFguVfiFEE4JYputzRyZ3CdvfUoR2DNK3QIhAM6j 5WCUR5syguW3xhFRpuQqgztsekBAjoUakQD7mSu/ -----END CERTIFICATE----- Issuer Subject: MBMxETAPBgNVBAMTCHN3YXJtLWNh Issuer Public Key: MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEUu+UOUJW4/IBuowIoiXgwB1V++Bgu+gAhuTNfQQ892UHmOJVHagjrFKZYeBi3maK+v5vR5FWDXtGd4LP/MmOBQ==
使用docker service ps tomcat-service
檢視當前helloworld啟動的叢集資訊。
[root@wuweixiang ~]# docker service ps helloworld ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS ldbbln9dkuq2 helloworld.1 alpine:latest wuweixiang Running Running 2 hours ago umsugc762yje \_ helloworld.1 alpine:latest VM_0_14_centos Shutdown Shutdown 2 hours ago jstnhnighx0n helloworld.2 alpine:latest wuweixiang Running Running 2 minutes ago 3t4legcx7plv \_ helloworld.2 alpine:latest VM_0_14_centos Shutdown Shutdown 2 minutes ago y9gy6qm9wqx6 helloworld.3 alpine:latest wuweixiang Running Running 2 minutes ago 6zkztr92rs62 \_ helloworld.3 alpine:latest VM_0_14_centos Shutdown Shutdown 2 minutes ago w79zkyl803tk helloworld.4 alpine:latest wuweixiang Running Running 8 minutes ago t5zz00dxclqj helloworld.5 alpine:latest wuweixiang Running Running 2 minutes ago k5jlwnx59agf \_ helloworld.5 alpine:latest VM_0_14_centos Shutdown Shutdown 2 minutes ago
如果我們需要重新啟用WorkerA的Swarm叢集服務, 我們可以通過docker node update --availability active <NODE-ID>
來實現對服務節點的啟用。
[root@wuweixiang ~]# docker node update --availability active i3 i3 [root@wuweixiang ~]# docker node inspect --pretty i3 ID: i3ma0jg3a0dzqezh1tjbwyxxk Hostname: VM_0_14_centos Joined at: 2018-12-11 09:21:15.918346569 +0000 utc Status: State: Ready Availability: Active Address: 188.131.152.100 Platform: Operating System: linux Architecture: x86_64 Resources: CPUs: 1 Memory: 992.7MiB Plugins: Log: awslogs, fluentd, gcplogs, gelf, journald, json-file, local, logentries, splunk, syslog Network: bridge, host, macvlan, null, overlay Volume: local Engine Version: 18.09.0 TLS Info: TrustRoot: -----BEGIN CERTIFICATE----- MIIBajCCARCgAwIBAgIUaoragJW4UwMO+DCs1zkxpt1xPdswCgYIKoZIzj0EAwIw EzERMA8GA1UEAxMIc3dhcm0tY2EwHhcNMTgxMjExMDc0MTAwWhcNMzgxMjA2MDc0 MTAwWjATMREwDwYDVQQDEwhzd2FybS1jYTBZMBMGByqGSM49AgEGCCqGSM49AwEH A0IABFLvlDlCVuPyAbqMCKIl4MAdVfvgYLvoAIbkzX0EPPdlB5jiVR2oI6xSmWHg Yt5mivr+b0eRVg17RneCz/zJjgWjQjBAMA4GA1UdDwEB/wQEAwIBBjAPBgNVHRMB Af8EBTADAQH/MB0GA1UdDgQWBBSoVH4AOp4ATVDNzsnA/8aP/Qx2aDAKBggqhkjO PQQDAgNIADBFAiARza3fA5h4sFguVfiFEE4JYputzRyZ3CdvfUoR2DNK3QIhAM6j 5WCUR5syguW3xhFRpuQqgztsekBAjoUakQD7mSu/ -----END CERTIFICATE----- Issuer Subject: MBMxETAPBgNVBAMTCHN3YXJtLWNh Issuer Public Key: MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEUu+UOUJW4/IBuowIoiXgwB1V++Bgu+gAhuTNfQQ892UHmOJVHagjrFKZYeBi3maK+v5vR5FWDXtGd4LP/MmOBQ==
當我們設定Swarm叢集的Worker節點為可用時,它便能接收新的任務:
- 當服務需要進行擴充套件時
- 當對服務的版本進行更新時
- 當我們對停用另外一個Swarm叢集節點時
- 當任務在另外一個活動狀態節點出現失敗時
參考garyond:https://www.jianshu.com/p/df744c4e375e