1. 程式人生 > >Docker Swarm 叢集部署

Docker Swarm 叢集部署

介紹

     Swarm 作為Docker 三劍客之一,於2014年提出,自 v1.12.3 以來, docker swarm 成為 docker 引擎內建的服務。它和谷歌的 K8s 產生了直接競爭。很難說哪個好,對於熟悉 docker 容器的人來說,docker swarm 應該友好一些。

安裝

   準備材料:

         1、硬體:centos 7 2臺(可以是虛擬機器)

         2、docker 版本: 172.16.0.45 是 18.06.0-ce

                                    172.16.0.43 是 1.13.1

          3、yum 源 :阿里源

  部署叢集:

     1、在master上執行docker pull swarm 拉取swarm映象

     

    2、初始化swarm : docker swarm init --advertise-addr 172.16.0.45

[[email protected] /]# docker swarm init --advertise-addr 172.16.0.45
Swarm initialized: current node (npb3iydf14kkts0b5ie66k2vn) is now a manager.

To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-4j7abdlcx8seeia13e609hy536gu0vazt3gz1tv7nao0072wwl-3igsdtz5niaoxvt6ysmskt031 172.16.0.45:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

    注: docker swarm join --token SWMTKN-1-4j7abdlcx8seeia13e609hy536gu0vazt3gz1tv7nao0072wwl-3igsdtz5niaoxvt6ysmskt031 172.16.0.45:2377 為加入該叢集的服務需要執行的tokenId

    3、在node 上執行docker pull swarm 拉取swarm映象

[[email protected]_hdp_03 home]# docker pull swarm
Using default tag: latest
Trying to pull repository docker.io/library/swarm ... 
sha256:406022f04a3d0c5ce4dbdb60422f24052c20ab7e6d41ebe5723aa649c3833975: Pulling from docker.io/library/swarm
d85c18077b82: Pull complete 
1e6bb16f8cb1: Pull complete 
85bac13497d7: Pull complete 
Digest: sha256:406022f04a3d0c5ce4dbdb60422f24052c20ab7e6d41ebe5723aa649c3833975
Status: Downloaded newer image for docker.io/swarm:latest

 4、在node 上執行 docker swarm join --token SWMTKN-1-4j7abdlcx8seeia13e609hy536gu0vazt3gz1tv7nao0072wwl-3igsdtz5niaoxvt6ysmskt031 172.16.0.45:2377 即可加入45上建立的叢集

[[email protected]_hdp_03 home]# docker swarm join --token SWMTKN-1-4j7abdlcx8seeia13e609hy536gu0vazt3gz1tv7nao0072wwl-3igsdtz5niaoxvt6ysmskt031 172.16.0.45:2377
This node joined a swarm as a worker.

5、如果返回This node joined a swarm as a worker. 標識成功加入叢集

6、如果返回Error response from daemon: rpc error: code = 14 desc = grpc: the connection is unavailable 考慮防火牆問題

7、採用firewall-cmd --state 查詢防火牆(如果返回not running 標識沒有執行)

     如果要採用防火牆那麼需要通過開發下拉埠

firewall-cmd --add-port=2376/tcp --permanent
firewall-cmd --add-port=2377/tcp --permanent
firewall-cmd --add-port=7946/tcp --permanent
firewall-cmd --add-port=7946/udp --permanent
firewall-cmd --add-port=4789/udp --permanent

  切記需要重啟docker 才能生效。

8、在master 上執行docker node ls 來檢視叢集中的節點

[[email protected] /]# docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
npb3iydf14kkts0b5ie66k2vn *   k8smaster           Ready               Active              Leader              18.06.0-ce
qn9ewaj1kxn9g6wclylsow1vi     pb_hdp_03           Ready               Active                                  1.13.1

輸出資訊第二行 id 後面的*表示當前連線到了該節點。HOSTNAME 欄輸出節點的 hostname。MANAGER 用於指示 swarm中的管理節點,該欄值為 Leader 表示為管理節點,空值表示為工作節點

9、在master 上執行docker node ps 檢視當前節點容器資訊(類似於docker ps)

[[email protected] /]# docker node ps
ID                  NAME                IMAGE                 NODE                DESIRED STATE       CURRENT STATE               ERROR               PORTS
tf6rgcdtg9lz        helloworld.1        busybox:1.25.1-musl   k8smaster           Running             Running about an hour ago                       
sx52pbyed2jg        helloworld.4        busybox:1.25.1-musl   k8smaster           Running             Running about an hour ago 

測試叢集:

   1、在master上通過“docker service create --replicas 1 --name helloworld busybox:1.25.1-musl /bin/sh -c "while true; do echo hello world; sleep 3; done"”來建立一個服務,每隔三秒輸出一個 “hello world”

[[email protected] /]# docker service create --replicas 1 --name helloworld busybox:1.25.1-musl /bin/sh -c "while true; do echo hello world; sleep 3; done"
edkbe2lic2ie9vl7cxbihfn0n
overall progress: 1 out of 1 tasks 
1/1: running   [==================================================>] 
verify: Service converged 

解說:

docker service create 命令建立服務。 --name 標誌將服務命名為helloworld。 --replicas 標誌指定了期望狀態為 1 個執行示例。 引數 busybox:1.25.1-musl /bin/sh -c "while true; do echo hello world; sleep 3; done 將服務定義為使用映象busybox:1.25.1-musl 建立容器,並在裡面執行 /bin/sh -c "while true; do echo hello world; sleep 3; done"。

   2、在master上通過docker service ls 查詢服務列表

[[email protected] /]# docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE                 PORTS
edkbe2lic2ie        helloworld          replicated          1/1                 busybox:1.25.1-musl   

3、通過“docker service inspect --pretty helloworld” 查詢容器的詳細資訊

[[email protected] /]# docker service inspect --pretty helloworld

ID:		edkbe2lic2ie9vl7cxbihfn0n
Name:		helloworld
Service Mode:	Replicated
 Replicas:	5
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:		busybox:[email protected]:46634e32e559271b8e18b7f1b21d981da4cd63ed8d36fbdc35c0b56464238a0c
 Args:		/bin/sh -c while true; do echo hello world; sleep 3; done 
 Init:		false
Resources:
Endpoint Mode:	vip

4、檢視那個節點執行在該服務上 docker service ps helloworld

[[email protected] /]# docker service ps helloworld
ID                  NAME                IMAGE                 NODE                DESIRED STATE       CURRENT STATE               ERROR               PORTS
tf6rgcdtg9lz        helloworld.1        busybox:1.25.1-musl   k8smaster           Running             Running 2 hours ago                             
pnv59taeonqy        helloworld.2        busybox:1.25.1-musl   pb_hdp_03           Running             Running about an hour ago                       
qccg5si9wtxm        helloworld.3        busybox:1.25.1-musl   pb_hdp_03           Running             Running about an hour ago                       
sx52pbyed2jg        helloworld.4        busybox:1.25.1-musl   k8smaster           Running             Running about an hour ago                       
r0riw4d2hvrt        helloworld.5        busybox:1.25.1-musl   pb_hdp_03           Running             Running about an hour ago    

5、刪除執行的服務 docker service rm helloworld

[[email protected] /]# docker service rm helloworld
helloworld

6、docker service ls 檢視執行在叢集上的服務

下線節點:

1、通過docker node ls 檢視當前活動的節點

[[email protected] /]# docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
npb3iydf14kkts0b5ie66k2vn *   k8smaster           Ready               Active              Leader              18.06.0-ce
qn9ewaj1kxn9g6wclylsow1vi     pb_hdp_03           Ready               Active                                  1.13.1

2、通過docker node update --availability drain pb_hdp_03 下線名稱為pb_hdp_03 的節點

[[email protected] /]# docker node update --availability drain pb_hdp_03
pb_hdp_03
[[email protected] /]# docker service ps helloworld
ID                  NAME                IMAGE                 NODE                DESIRED STATE       CURRENT STATE           ERROR               PORTS
wmjzu4nrra59        helloworld.1        busybox:1.25.1-musl   k8smaster           Ready               Ready 5 seconds ago                         
90zczn851j81         \_ helloworld.1    busybox:1.25.1-musl   pb_hdp_03           Shutdown            Running 6 seconds ago                       
rxzzchtmlnb5        helloworld.2        busybox:1.25.1-musl   k8smaster           Ready               Ready 5 seconds ago                         
v5qlp8cma8tj         \_ helloworld.2    busybox:1.25.1-musl   pb_hdp_03           Shutdown            Running 6 seconds ago                       
1nrd1op72awm        helloworld.3        busybox:1.25.1-musl   k8smaster           Running             Running 2 minutes ago                       
vntrn9vwumxq        helloworld.4        busybox:1.25.1-musl   k8smaster           Ready               Ready 4 seconds ago                         
pbxar3qcyvgf         \_ helloworld.4    busybox:1.25.1-musl   pb_hdp_03           Shutdown            Running 6 seconds ago                       
j0thl91hkm53        helloworld.5        busybox:1.25.1-musl   k8smaster           Running             Running 2 minutes ago                 

可以看到該節點的狀態為Ready,但可用性為Drain

3、通過docker node update --availability active pb_hdp_03 再次將pb_hdp_03啟用

[[email protected] /]# docker node update --availability active pb_hdp_03
pb_hdp_03
[[email protected] /]# docker service ps helloworld
ID                  NAME                IMAGE                 NODE                DESIRED STATE       CURRENT STATE            ERROR               PORTS
wmjzu4nrra59        helloworld.1        busybox:1.25.1-musl   k8smaster           Running             Running 2 minutes ago                        
90zczn851j81         \_ helloworld.1    busybox:1.25.1-musl   pb_hdp_03           Shutdown            Shutdown 2 minutes ago                       
rxzzchtmlnb5        helloworld.2        busybox:1.25.1-musl   k8smaster           Running             Running 2 minutes ago                        
v5qlp8cma8tj         \_ helloworld.2    busybox:1.25.1-musl   pb_hdp_03           Shutdown            Shutdown 2 minutes ago                       
1nrd1op72awm        helloworld.3        busybox:1.25.1-musl   k8smaster           Running             Running 5 minutes ago                        
vntrn9vwumxq        helloworld.4        busybox:1.25.1-musl   k8smaster           Running             Running 2 minutes ago                        
pbxar3qcyvgf         \_ helloworld.4    busybox:1.25.1-musl   pb_hdp_03           Shutdown            Shutdown 2 minutes ago                       
j0thl91hkm53        helloworld.5        busybox:1.25.1-musl   k8smaster           Running             Running 5 minutes ago         

說明現在該節點又可以重新接收任務了。

注:

一個可用性為Active的節點在以下情況下可以接收到新任務:

  • 當一個服務在伸縮規模時
  • 滾動更新時
  • 當你把其他某個節點的可用性設為 Drain 時
  • 當某個任務在另外某個 Active 節點上啟動失敗時