1. 说明
本文档针对docker swarm操作。
针对的系统是以一个本地的测试系统为例。其中机器信息如下,172.16.1.13作为docker swarm的管理机。
本地测试的机器列表信息:
主机名 | 模拟的外网 | 内网IP | 要部署模块 |
mini01 | 10.0.0.11 | 172.16.1.11 | tomcat 【swarm管理】 |
mini02 | 10.0.0.12 | 172.16.1.12 | tomcat 【swarm管理】 |
mini03 | 10.0.0.13 | 172.16.1.13 | visualizer # docker swarm 状态查看 hadoop-namnode 【swarm管理】 |
2. docker swarm初始化
根据规划在172.16.1.13这台机器上操作:
1 [root@mini03 ~]# 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 [root@mini03 ~]# 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 [root@mini03 ~]# 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 [root@mini03 ~]# docker network create -d overlay --attachable zhang 2 vu07em5fvpuojih6wgckdkdzj 3 [root@mini03 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 [root@mini03 ~]# 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 [root@mini03 docker-swarm]# docker network rm zhang 2 zhang 3 [root@mini03 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 [root@mini03 ~]# 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 [root@mini03 ~]# docker node rm --force 2pfwllgxpajx5aitlvcih9vsq # 如果mini01上的docker没有停止服务,那么就需要使用 --force 选项 4 2pfwllgxpajx5aitlvcih9vsq 5 [root@mini03 ~]# 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 [root@mini01 ~]# docker swarm leave 12 Node left the swarm.
4.3. swarm管理机退出swarm
首先需要删除所有节点,然后强制退出swarm即可
1 [root@mini03 ~]# docker node ls 2 ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION 3 yo5f7qb28gf6g38ve4xhcis17 * mini03 Ready Active Leader 17.09.0-ce 4 [root@mini03 ~]# docker swarm leave --force # swarm管理机退出swarm,需要 --force 参数 5 Node left the swarm. 6 [root@mini03 ~]# 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 [root@mini03 ~]# 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 [root@mini03 ~]# 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