1. 程式人生 > >docker swarm的常用操作

docker swarm的常用操作

 

1. 說明

       本文件針對docker swarm操作。

       針對的系統是以一個本地的測試系統為例。其中機器資訊如下,172.16.1.13作為docker swarm的管理機。

 

本地測試的機器列表資訊:

主機名

模擬的外網

內網IP

要部署模組

mini01

10.0.0.11

172.16.1.11

tomcat            【swarm管理】
hadoop-datanode    【swarm管理】
hbase-regionserver   【swarm管理】

mini02

10.0.0.12

172.16.1.12

tomcat             【swarm管理】
hadoop-datanode    【swarm管理】
hbase-regionserver   【swarm管理】

mini03

10.0.0.13

172.16.1.13

visualizer 

# docker swarm 狀態檢視
spark              【swarm管理】
zookeeper         【swarm管理】

hadoop-namnode    【swarm管理】
hbase-master      【swarm管理】

 

 

2. docker swarm初始化

       根據規劃在172.16.1.13這臺機器上操作:

 1 [[email protected] ~]# docker swarm init  # 針對機器只有一個IP的情況 
 2 Error response from daemon: could not choose an IP address to advertise since this system has multiple addresses on different interfaces (172.16.1.13 on eth0 and 10.0.0.13 on eth1) - specify one with --advertise-addr
 3 [[email protected] ~]# docker swarm init --advertise-addr 172.16.1.13  # 針對機器有多個IP的情況,需要指定一個IP,一般都是指定內網IP
 4 Swarm initialized: current node (yo5f7qb28gf6g38ve4xhcis17) is now a manager.
 5 
 6 To add a worker to this swarm, run the following command:
 7     # 在其他機器上執行,這樣可以加入該swarm管理
 8     docker swarm join --token SWMTKN-1-4929ovxh6agko49u0yokrzustjf6yzt30iv1zvwqn8d3pndm92-0kuha3sa80u2u27yca6kzdbnb 172.16.1.13:2377
 9 
10 To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

 

得到加入到該swarm的命令

1 [[email protected] ~]# docker swarm join-token worker  
2 To add a worker to this swarm, run the following command:
3     # 在其他機器上執行,這樣可以加入該swarm管理
4     docker swarm join --token SWMTKN-1-4929ovxh6agko49u0yokrzustjf6yzt30iv1zvwqn8d3pndm92-0kuha3sa80u2u27yca6kzdbnb 172.16.1.13:2377

 

 

3. 初始化網路

   初始化一個swarm網路,讓系統元件使用這個指定的網路。

 1 [[email protected] ~]# docker network create -d overlay --attachable zhang 
 2 vu07em5fvpuojih6wgckdkdzj
 3 [[email protected] docker-swarm]# docker network ls  # 檢視網路
 4 NETWORK ID          NAME                DRIVER              SCOPE
 5 fa8a244c6bd5        bridge              bridge              local
 6 51c95dea1e5c        docker_gwbridge     bridge              local
 7 7a7e31f4bce8        host                host                local
 8 5hgg372xwxbl        ingress             overlay             swarm
 9 lmt3pjswf7l0        zhang               overlay             swarm
10 5ea08e9a282f        none                null                local
11 [[email protected] ~]# docker network inspect zhang  # 檢視網路資訊 
12 [
13     {
14         "Name": "zhang",
15         "Id": "xiykborz8hn2td40ykhi20dck",
16         "Created": "0001-01-01T00:00:00Z",
17         "Scope": "swarm",
18         "Driver": "overlay",
19         "EnableIPv6": false,
20         "IPAM": {
21             "Driver": "default",
22             "Options": null,
23             "Config": []
24         },
25         "Internal": false,
26         "Attachable": true,
27         "Ingress": false,
28         "ConfigFrom": {
29             "Network": ""
30         },
31         "ConfigOnly": false,
32         "Containers": null,
33         "Options": {
34             "com.docker.network.driver.overlay.vxlanid_list": "4097"
35         },
36         "Labels": null
37     }
38 ]

 

刪除網路【慎用】

       刪除docker中的zhang網路

1 [[email protected] docker-swarm]# docker network rm zhang  
2 zhang
3 [[email protected] docker-swarm]# docker network ls
4 NETWORK ID          NAME                DRIVER              SCOPE
5 fa8a244c6bd5        bridge              bridge              local
6 51c95dea1e5c        docker_gwbridge     bridge              local
7 7a7e31f4bce8        host                host                local
8 5hgg372xwxbl        ingress             overlay             swarm
9 5ea08e9a282f        none                null                local

 

 

4. 加入或退出swarm管理

  在mini01、mini02上執行 docker swarm join --token SWMTKN-1-4929ovxh6agko49u0yokrzustjf6yzt30iv1zvwqn8d3pndm92-0kuha3sa80u2u27yca6kzdbnb 172.16.1.13:2377 命令。

 

4.1. 當前swarm有哪些節點

1 [[email protected] ~]# docker node ls
2 ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
3 2pfwllgxpajx5aitlvcih9vsq     mini01              Ready               Active                                  17.09.0-ce
4 zho14u85itt5l2i6cpg8fcd6t     mini02              Ready               Active                                  17.09.0-ce
5 yo5f7qb28gf6g38ve4xhcis17 *   mini03              Ready               Active              Leader              17.09.0-ce

 

4.2. 退出當前的swarm節點

 1 # 在swarm管理機mini03上的操作
 2 # 其中 2pfwllgxpajx5aitlvcih9vsq 是mini01在swarm機器上的ID,根據docker node ls 獲取
 3 [[email protected] ~]# docker node rm --force 2pfwllgxpajx5aitlvcih9vsq  # 如果mini01上的docker沒有停止服務,那麼就需要使用 --force 選項
 4 2pfwllgxpajx5aitlvcih9vsq
 5 [[email protected] ~]# docker node ls
 6 ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
 7 zho14u85itt5l2i6cpg8fcd6t     mini02              Ready               Active                                  17.09.0-ce
 8 yo5f7qb28gf6g38ve4xhcis17 *   mini03              Ready               Active              Leader              17.09.0-ce
 9 ##########################################
10 # 需要在mini01上執行的命令,這樣mini01才能徹底退出swarm管理
11 [[email protected] ~]# docker swarm leave
12 Node left the swarm.

 

4.3. swarm管理機退出swarm

       首先需要刪除所有節點,然後強制退出swarm即可

1 [[email protected] ~]# docker node ls
2 ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
3 yo5f7qb28gf6g38ve4xhcis17 *   mini03              Ready               Active              Leader              17.09.0-ce
4 [[email protected] ~]# docker swarm leave --force  # swarm管理機退出swarm,需要 --force 引數
5 Node left the swarm. 
6 [[email protected] ~]# docker node ls
7 Error response from daemon: This node is not a swarm manager. Use "docker swarm init" or "docker swarm join" to connect this node to swarm and try again.

 

4.4. 當前swarm有哪些服務

 1 [[email protected] ~]# docker service ls  # 只是示例,不是實際資料
 2 ID            NAME                  MODE        REPLICAS  IMAGE                                            PORTS
 3 lq7zkkal6ujt  hadoop_datanode       global      2/2       bde2020/hadoop-datanode:2.0.0-hadoop2.7.4-java8   
 4 ph2fu37k886b  hadoop_namenode       replicated  1/1       bde2020/hadoop-namenode:2.0.0-hadoop2.7.4-java8  *:50070->50070/tcp
 5 ca47u5i2ubes  hbase-master          replicated  1/1       bde2020/hbase-master:1.0.0-hbase1.2.6            *:16010->16010/tcp
 6 mkks4oa2ppcn  hbase-regionserver-1  replicated  1/1       bde2020/hbase-regionserver:1.0.0-hbase1.2.6      
 7 j4mhizg4j67p  hbase-regionserver-2  replicated  1/1       bde2020/hbase-regionserver:1.0.0-hbase1.2.6      
 8 yndrkc2bcpra  hbase_zoo1            replicated  1/1       zookeeper:3.4.10                                 *:2181->2181/tcp
 9 r5ycrvo0zout  spark_spark           replicated  1/1       zhang/spark:latest                               *:4040->4040/tcp,*:7777->7777/tcp,*:8081->8081/tcp,*:18080->8080/tcp
10 f2v091nz24rg  tomcat_tomcat         global      2/2       zhang/tomcat:latest                              *:6543->6543/tcp,*:9999->9999/tcp,*:18081->8081/tcp
11 clfpryaerq2l  visualizer            replicated  1/1       dockersamples/visualizer:latest                  *:8080->8080/tcp

 

 

5. swarm標籤管理

5.1. 標籤新增

       根據最開始的主機和元件部署規劃,標籤規劃如下:在swarm管理機mini03上執行。

 1 # 給mini01機器的標籤
 2 docker node update --label-add tomcat=true mini01
 3 docker node update --label-add datanode=true mini01
 4 docker node update --label-add hbase-regionserver-1=true mini01
 5 
 6 # 給mini02機器的標籤
 7 docker node update --label-add tomcat=true mini02
 8 docker node update --label-add datanode=true mini02
 9 docker node update --label-add hbase-regionserver-2=true mini02
10 
11 # 給mini03機器的標籤
12 docker node update --label-add spark=true mini03
13 docker node update --label-add zookeeper=true mini03
14 docker node update --label-add namenode=true mini03
15 docker node update --label-add hbase-master=true mini03

 

5.2. 刪除標籤

       在swarm管理機mini03上執行,示例如下:

1 docker node update --label-rm zookeeper mini03

 

5.3. 檢視swarm當前的標籤

       在swarm管理機mini03上執行:

1 [[email protected] ~]# docker node ls -q | xargs docker node inspect -f '{{.ID}}[{{.Description.Hostname}}]:{{.Spec.Labels}}'
2 6f7dwt47y6qvgs3yc6l00nmjd[mini01]:map[tomcat:true datanode:true hbase-regionserver-1:true]
3 5q2nmm2xaexhkn20z8f8ezglr[mini02]:map[tomcat:true datanode:true hbase-regionserver-2:true]
4 ncppwjknhcwbegmliafut0718[mini03]:map[hbase-master:true namenode:true spark:true zookeeper:true]

 

 

6. 檢視日誌

       啟動容器時,檢視相關日誌,例如如下:

1 docker stack ps hadoop
2 docker stack ps hadoop --format "{{.Name}}: {{.Error}}"
3 docker stack ps hadoop --format "{{.Name}}: {{.Error}}" --no-trunc
4 docker stack ps hadoop --no-trunc