# 在docker-0服务器上执行
$ docker swarm init --advertise-addr 10.10.10.30 # 在有多块网卡时指定绑定ip
Swarm initialized: current node (uj64m1ay4ftshvlpatzcklhsa) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-0tdwzqsm8scntd9pj32x7py3yglp7j921gaswjg0td2qxs2b90-5ng50j4e3fz6rfyigyamtw6q0 10.10.10.30:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
加入docker swarm集群
# 在docker-1和docker-2服务器上执行
$ docker swarm join --token SWMTKN-1-0tdwzqsm8scntd9pj32x7py3yglp7j921gaswjg0td2qxs2b90-5ng50j4e3fz6rfyigyamtw6q0 10.10.10.30:2377
This node joined a swarm as a worker.
docker swarm高可用方案
# 将docker-1及docker-2提升为备用manager节点
# 在docker-0上执行命令
$ docker node promote docker-1
Node docker-1 promoted to a manager in the swarm.
# 检查node状态
$ docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
uj64m1ay4ftshvlpatzcklhsa * docker-0 Ready Active Leader 18.09.4
z0kqnpnsylcxw0i0na05kb0b3 docker-1 Ready Active Reachable 18.09.4
nlmnwivdsap75qx8798foom0u docker-2 Ready Active Reachable 18.09.4
创建一个测试服务
$ docker service create --name test1 alpine ping www.baidu.com
开通一个nginx服务
$ docker service create --name nginx nginx
# 开通nginx暴露的端口
$ docker service update --publish-add 80:80 nginx
# 开启nginx高可用(增加nginx运行节点)
$ docker service scale nginx=3
创建docker network网络
$ docker network create -d overlay nginx-overlay
# 检查网络情况
$ docker network ls
NETWORK ID NAME DRIVER SCOPE
740da093ed9b bridge bridge local
76ec1a041199 docker_gwbridge bridge local
65ba5fffa724 host host local
l9luemje94ft ingress overlay swarm
w90la9zdlxyg nginx-overlay overlay swarm
8bbec75fc40d none null local
将docker加入网络
# 停止nginx,test容器运行
$ docker service rm nginx test
# 启动nginx容器
$ docker service create --network nginx-overlay --name nginx -p 80:80 -p 443:443 nginx
# 启动客户端容器
$ docker service create --network nginx-overlay --name client alpine ping www.baidu.com
# 验证容器间访问
# 进入客户端容器访问nginx
$ docker exec -it 322a276ac1c3 sh (可以通过docker ps 命令获取client的CONTAINER ID)
/ # ping nginx
PING nginx (10.0.0.2): 56 data bytes
64 bytes from 10.0.0.2: seq=0 ttl=64 time=0.261 ms
64 bytes from 10.0.0.2: seq=1 ttl=64 time=0.162 ms