Docker 1.12版本后,Swarm变化比较大,更加简单、好用,而且采用了gossip协议后,支持多个Manager,也更加稳定可靠了。不需要单独安装,Swarm的集群功能已经成为Docker Engine的一部分。因此,可以说,集群已经成为Docker的一个内置功能。
docker swarm 命令详情
有关集群的docker命令如下:
- docker swarm:集群管理,子命令有init, join, leave, update
- docker node:节点管理,子命令有accept, promote, demote, inspect, update, tasks, ls, rm
- docker service:服务管理,子命令有create, inspect, update, remove, tasks
- docker stack/deploy:试验特性,用于多应用部署
创建swarm 集群
- 查看docker swarm 命令说明
[root@centos-node4 ~]# docker swarm -h
Flag shorthand -h has been deprecated, please use --help
Usage: docker swarm COMMAND
Manage Docker Swarm
Options:
--help Print usage
Commands:
init Initialize a Swarm
join Join a Swarm as a node and/or manager
update Update the Swarm
leave Leave a Swarm
inspect Inspect the Swarm
Run 'docker swarm COMMAND --help' for more information on a command.
- 在swarm manager(centos-node4:192.168.99.101)初始化swarm集群
用--listen-addr
指定监听的ip与端口
#命令格式: docker swarm init --listen-addr <MANAGER-IP>:<PORT>
[root@centos-node4 ~]# docker swarm init --listen-addr 192.168.99.101:2377
Swarm initialized: current node (a60d5c3ttymvtozr46uvk17q4) is now a manager.
查看结果
[root@centos-node4 ~]# docker node ls
ID HOSTNAME MEMBERSHIP STATUS AVAILABILITY MANAGER STATUS
a60d5c3ttymvtozr46uvk17q4 * centos-node4 Accepted Ready Active Leader
- 把swarm-agent1(centos-node5: 192.168.99.102)添加到swarm集群
在swarm-agent1上执行:
#命令格式: docker swarm join <MANAGER-IP>:<PORT>
[root@centos-node5 ~]# docker swarm join 192.168.99.101:2377
This node joined a Swarm as a worker.
- 在swarm manager查看结果
[root@centos-node4 ~]# docker node ls
ID HOSTNAME MEMBERSHIP STATUS AVAILABILITY MANAGER STATUS
0ypcw58hjlcvr0xqbtizmau62 centos-node5 Accepted Ready Active
a60d5c3ttymvtozr46uvk17q4 * centos-node4 Accepted Ready Active Leader
创建一个overlay 跨主机网络
- 查看原有网络
[root@centos-node4 ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
abec77415f48 bridge bridge local
e2fff9d572a6 docker_gwbridge bridge local
166bd71f7d0e host host local
9gr6bfff1rv9 ingress overlay swarm
1d2bfc590294 none null local
可以看到在swarm上默认已有一个名为ingress的overlay 网络, 可以直接使用, 但本文会创建一个新的
- 创建一个新的overlay网络
[root@centos-node4 ~]# docker network create --driver overlay docker-net
aoqs3p835s5glx69hi46ou2dw
[root@centos-node4 ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
abec77415f48 bridge bridge local
aoqs3p835s5g docker-net overlay swarm
e2fff9d572a6 docker_gwbridge bridge local
166bd71f7d0e host host local
9gr6bfff1rv9 ingress overlay swarm
1d2bfc590294 none null local
新的网络(docker-net)已创建
在新的跨主机overlay 网络(docker-net)上创建应用
- 部署
用alpine
镜像在docker-net
网络上启动两个实例, 并编排为一组服务
[root@centos-node4 ~]# docker service create --replicas 2 --name helloworld --network=ingress alpine ping docker.com
5lgdq3ihiez0o7h2uegu4fgd3
- 查看部署结果
[root@centos-node4 ~]# docker service ls
ID NAME REPLICAS IMAGE COMMAND
5lgdq3ihiez0 helloworld 0/2 alpine ping docker.com
[root@centos-node4 ~]# docker service tasks helloworld
ID NAME SERVICE IMAGE LAST STATE DESIRED STATE NODE
eul3bus45qz3b555wekotdmo5 helloworld.1 helloworld alpine Running 14 seconds Running centos-node5
55uhq6xxcv53xlkqv2f0be9b9 helloworld.2 helloworld alpine Running 14 seconds Running centos-node4
可以看到两个实例分别运行在两个节点上
测试两个主机的网络是否能互通
- 分别查看两个实例的名称
# 在swarm-manager 上执行
[root@centos-node4 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f4a197abdb0b alpine:latest "ping docker.com" 42 minutes ago Up 42 minutes helloworld.2.55uhq6xxcv53xlkqv2f0be9b9
# 在swarm-agnet1 上执行
[root@centos-node5 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
39cc35cd54b5 alpine:latest "ping docker.com" 50 seconds ago Up 49 seconds helloworld.1.eul3bus45qz3b555wekotdmo5
- 在swarm-manager 上测试
[root@centos-node4 ~]# docker exec -ti helloworld.2.55uhq6xxcv53xlkqv2f0be9b9 sh
/ # ping helloworld.1.eul3bus45qz3b555wekotdmo5
PING helloworld.1.eul3bus45qz3b555wekotdmo5 (10.0.9.3): 56 data bytes
64 bytes from 10.0.9.3: seq=0 ttl=64 time=0.514 ms
64 bytes from 10.0.9.3: seq=1 ttl=64 time=0.508 ms
64 bytes from 10.0.9.3: seq=2 ttl=64 time=0.381 ms
64 bytes from 10.0.9.3: seq=3 ttl=64 time=0.408 ms
^C
--- helloworld.1.eul3bus45qz3b555wekotdmo5 ping statistics ---
4 packets transmitted, 4 packets received, 0% packet loss
round-trip min/avg/max = 0.381/0.452/0.514 ms
- 在swarm-agent1 上测试
[root@centos-node5 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
39cc35cd54b5 alpine:latest "ping docker.com" 50 seconds ago Up 49 seconds helloworld.1.eul3bus45qz3b555wekotdmo5
[root@centos-node5 ~]# docker exec -ti helloworld.1.eul3bus45qz3b555wekotdmo5 sh
/ # ping helloworld.2.55uhq6xxcv53xlkqv2f0be9b9
PING helloworld.2.55uhq6xxcv53xlkqv2f0be9b9 (10.0.9.4): 56 data bytes
64 bytes from 10.0.9.4: seq=0 ttl=64 time=0.892 ms
64 bytes from 10.0.9.4: seq=1 ttl=64 time=0.463 ms
64 bytes from 10.0.9.4: seq=2 ttl=64 time=0.462 ms
64 bytes from 10.0.9.4: seq=3 ttl=64 time=0.478 ms
64 bytes from 10.0.9.4: seq=4 ttl=64 time=0.468 ms
64 bytes from 10.0.9.4: seq=5 ttl=64 time=0.459 ms
^C
--- helloworld.2.55uhq6xxcv53xlkqv2f0be9b9 ping statistics ---
6 packets transmitted, 6 packets received, 0% packet loss
round-trip min/avg/max = 0.459/0.537/0.892 ms
现在新版的docker swarm 管理非常简单, 可以快速的搭建起一个跨主机的集群并部署应用
来源:oschina
链接:https://my.oschina.net/u/2306127/blog/791076