Docker 再学习 (5) Swarm 的 overlay 网络

Submitted by Lizhe on Sat, 02/23/2019 - 14:25

 

docker1

[root@docker1 ~]# docker swarm init --advertise-addr 192.168.186.133
Swarm initialized: current node (1l1piabkdtxhxbxqww6i389zo) is now a manager.

To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-5d2ygkplafv3xhxodgpshyfgemynvmj6220rf69lg32m127sdm-9us67nv7u2g2ny5enjcokm95l 192.168.186.133:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

[root@docker1 ~]# 
 

 

docker2

[root@docker2 ~]# docker swarm join --token SWMTKN-1-5d2ygkplafv3xhxodgpshyfgemynvmj6220rf69lg32m127sdm-9us67nv7u2g2ny5enjcokm95l 192.168.186.133:2377
This node joined a swarm as a worker.
[root@docker2 ~]# 
 

 

创建网络

[root@docker1 ~]# docker network create -d overlay swarm_ov_network
os4l9hc7dfb0gyo90o76ia4cr
[root@docker1 ~]# 

 

创建 service 然后尝试使用 docker1 的容器 ping docker2 的容器

[root@docker1 ~]# docker service create --replicas 5 --network swarm_ov_network --name my_busybox busybox sleep 3000
ve79yur4jmp1im0bbnflhvdkj
overall progress: 5 out of 5 tasks 
1/5: running   [==================================================>] 
2/5: running   [==================================================>] 
3/5: running   [==================================================>] 
4/5: running   [==================================================>] 
5/5: running   [==================================================>] 
verify: Service converged 
[root@docker1 ~]# 
[root@docker1 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
996d69686686        busybox:latest      "sleep 3000"        About a minute ago   Up About a minute                       my_busybox.2.p61manlzj02q08vyjq3to6dhu
042878148cbf        busybox:latest      "sleep 3000"        About a minute ago   Up About a minute                       my_busybox.5.cu3khpj0fmpbnqjurnzex7bwr
[root@docker1 ~]# docker exec -it my_busybox.2.p61manlzj02q08vyjq3to6dhu sh
/ # 
/ # ping my_busybox.1.mzn2tfn5rt3cri1nxu45c9chx
PING my_busybox.1.mzn2tfn5rt3cri1nxu45c9chx (10.0.0.18): 56 data bytes
64 bytes from 10.0.0.18: seq=0 ttl=64 time=0.698 ms
64 bytes from 10.0.0.18: seq=1 ttl=64 time=0.381 ms
64 bytes from 10.0.0.18: seq=2 ttl=64 time=0.324 ms
64 bytes from 10.0.0.18: seq=3 ttl=64 time=0.470 ms
64 bytes from 10.0.0.18: seq=4 ttl=64 time=0.918 ms
^C
--- my_busybox.1.mzn2tfn5rt3cri1nxu45c9chx ping statistics ---
5 packets transmitted, 5 packets received, 0% packet loss
round-trip min/avg/max = 0.324/0.558/0.918 ms
 

20190223_9.JPG

20190223_10.JPG

 

稍微提一句如果要把 service 暴露出来的话

20190223_11.JPG

要追加映射的端口的话

[root@docker1 ~]# docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
ve79yur4jmp1        my_busybox          replicated          5/5                 busybox:latest      
n43q3w95u0o0        my_nginx            replicated          5/5                 nginx:latest        *:8099->80/tcp
[root@docker1 ~]# docker service update --publish-add 8999:80 my_nginx
my_nginx
overall progress: 5 out of 5 tasks 
1/5: running   [==================================================>] 
2/5: running   [==================================================>] 
3/5: running   [==================================================>] 
4/5: running   [==================================================>] 
5/5: running   [==================================================>] 
verify: Service converged 
[root@docker1 ~]# docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
ve79yur4jmp1        my_busybox          replicated          5/5                 busybox:latest      
n43q3w95u0o0        my_nginx            replicated          5/5                 nginx:latest        *:8099->80/tcp, *:8999->80/tcp
[root@docker1 ~]#