Docker Swarm集群中的节点主机开放以下三个端口
2377端口, 用于集群管理通信
7946端口, 用于集群节点之间的通信
4789端口, 用于overlay网络流量
关闭防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service
在A上执行
创建集群并加入集群
$ docker swarm init --advertise-addr 192.168.1.5:2377 --listen-addr 192.168.1.5:2377
initialized: current node (sf4zgbesw21ko536rrgpxspv4) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-3mlkj1pouzqm8mjdqdnj5gw30i4ahjw5aayfu177vzernbzmod-1svbkyjwzhv0swzojp6xh3shm 192.168.1.5:2377
查看集群中的节点
$ docker node ls
查看 manager 角色的 token
$ docker swarm join-token manager
To add a manager to this swarm, run the following command:
docker swarm join --token SWMTKN-1-3mlkj1pouzqm8mjdqdnj5gw30i4ahjw5aayfu177vzernbzmod-0ib338l48ybx8csrlpr3yn7ug 192.168.1.5:2377
查看 worker 角色的 token
$ docker swarm join-token worker
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-3mlkj1pouzqm8mjdqdnj5gw30i4ahjw5aayfu177vzernbzmod-1svbkyjwzhv0swzojp6xh3shm 192.168.1.5:2377
在B上执行
把 B 以 manager 角色加入集群 (上面的 token 那段命令,再接上当前的监听的IP )
$ docker swarm join --token SWMTKN-1-3mlkj1pouzqm8mjdqdnj5gw30i4ahjw5aayfu177vzernbzmod-0ib338l48ybx8csrlpr3yn7ug 192.168.1.5:2377 --advertise-addr 192.168.1.7:2377 --listen-addr 192.168.1.7:2377
查看一下节点
$ docker node ls
在C上执行
把 C 以 worker 角色加入集群(上面的 token 那段命令,再接上当前的监听的IP )
$ docker swarm join --token SWMTKN-1-3mlkj1pouzqm8mjdqdnj5gw30i4ahjw5aayfu177vzernbzmod-1svbkyjwzhv0swzojp6xh3shm 192.168.1.5:2377 --advertise-addr 192.168.1.10:2377 --listen-addr 192.168.1.10:2377
在 worker 节点查看集群中的节点会报没有权限的错误
$ docker node ls
Error response from daemon: This node is not a swarm manager. Worker nodes can't be used to view or modify cluster state.
集群建好了,就开始创建服务,可以在任意 manager 角色的主机上创建
这个创建容器几乎一样,--replicas 5 在集群中运行五个实例副本 -p 80:8080 服务绑定 80 端口
$ docker service create --name web-fa -p 80:8080 --replicas 5 nigelpoulton/pluralsight-docker-ci
k4qpts36rterzzsy0ys0f7rjw
overall progress: 5 out of 5 tasks
1/5: running [==================================================>]
2/5: running [==================================================>]
3/5: running [==================================================>]
4/5: running [==================================================>]
5/5: running [==================================================>]
打开浏览器,输入 A B C 任意主机的 IP 访问一下,看看能否打开一个web页面
查看服务列表
$ docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
k4qpts36rter web-fa replicated 5/5 nigelpoulton/pluralsight-docker-ci:latest *:80->8080/tcp
查看具体服务中运行的任务
$ docker service ps web-fa
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
94thjgudevf0 web-fa.1 nigelpoulton/pluralsight-docker-ci:latest host Running Running 7 minutes ago