Docker Swarm 创建overlay网络

Posted liujunjun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker Swarm 创建overlay网络相关的知识,希望对你有一定的参考价值。

环境:

CentOS Linux release 7.4.1708 (Core) 
Docker version 19.03.5, build 633a0ea
管理节点:192.168.1.220
工作节点:192.168.1.221
工作节点:192.168.1.222

一、创建网络与服务

1、管理节点:创建overlay网络名字为my-network

[root@docker-01 ~]# docker network create --driver overlay my-network
pc3xhx63syp9p65ot6yaqm2ej
[root@docker-01 ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
pc3xhx63syp9        my-network          overlay             swarm
00c1eda0108e        none                null                local

2、管理节点:创建服务并使用overlay网络。

注:busybox启动失败后manager尝试重启容器,但是busybox容器内没有持续运行的任务,manager会将它关闭,所以没有启动成功。向容器添加ping www.baidu.com持续动作避免容器关闭。

[root@docker-01 ~]# docker service create --replicas 3 --network my-network --name test busybox ping www.baidu.com
1fvx4gtmbhl2sm8e7g7j8hf8o
overall progress: 3 out of 3 tasks 
1/3: running   [==================================================>] 
2/3: running   [==================================================>] 
3/3: running   [==================================================>] 
verify: Service converged 
# 创建服务
docker service create # 副本数
--replicas 3 # 添加网络
--network my-network # 服务名
--name test # 镜像
busybox 
# 容器执行指令
ping www.baidu.com
[root@docker-01 ~]# docker service ps test
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE            ERROR               PORTS
h76dz7b16etb        test.1              busybox:latest      docker-02           Running             Running 9 minutes ago                        
lgse45b0hcfz        test.2              busybox:latest      docker-03           Running             Running 9 minutes ago                        
5vp9r0z0gmhk        test.3              busybox:latest      docker-01           Running             Running 12 minutes ago   

二、测试网络连通

1、工作节点1与2:进入容器测试连通情况

[root@docker-02 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS               NAMES
244d215ed21b        busybox:latest      "ping www.baidu.com"   2 minutes ago       Up 2 minutes                            test.1.h76dz7b16etb3h7n2cm8mrbpe
[root@docker-02 ~]# docker exec -it 244d215ed21b  sh
/ # ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:0A:00:01:05  
          inet addr:10.0.1.5  Bcast:10.0.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1450  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

eth1      Link encap:Ethernet  HWaddr 02:42:AC:12:00:03  
          inet addr:172.18.0.3  Bcast:172.18.255.255  Mask:255.255.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:214 errors:0 dropped:0 overruns:0 frame:0
          TX packets:207 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:20235 (19.7 KiB)  TX bytes:19570 (19.1 KiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:4 errors:0 dropped:0 overruns:0 frame:0
          TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1 
          RX bytes:379 (379.0 B)  TX bytes:379 (379.0 B)
[root@docker-03 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS               NAMES
0495920cc2c4        busybox:latest      "ping www.baidu.com"   4 minutes ago       Up 4 minutes                            test.2.lgse45b0hcfzdbgjke0g6jw5e
[root@docker-03 ~]# docker exec -it 0495920cc2c4 sh
/ # ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:0A:00:01:03  
          inet addr:10.0.1.3  Bcast:10.0.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1450  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

eth1      Link encap:Ethernet  HWaddr 02:42:AC:12:00:03  
          inet addr:172.18.0.3  Bcast:172.18.255.255  Mask:255.255.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:293 errors:0 dropped:0 overruns:0 frame:0
          TX packets:287 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:27775 (27.1 KiB)  TX bytes:27180 (26.5 KiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:4 errors:0 dropped:0 overruns:0 frame:0
          TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1 
          RX bytes:379 (379.0 B)  TX bytes:379 (379.0 B)

/ # 
/ # ping 10.0.1.3
PING 10.0.1.3 (10.0.1.3): 56 data bytes
64 bytes from 10.0.1.3: seq=0 ttl=64 time=0.739 ms
64 bytes from 10.0.1.3: seq=1 ttl=64 time=1.145 ms
64 bytes from 10.0.1.3: seq=2 ttl=64 time=1.354 ms
^C
--- 10.0.1.3 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.739/1.079/1.354 ms
/ # ping 10.0.1.5
PING 10.0.1.5 (10.0.1.5): 56 data bytes
64 bytes from 10.0.1.5: seq=0 ttl=64 time=0.875 ms
64 bytes from 10.0.1.5: seq=1 ttl=64 time=1.055 ms
64 bytes from 10.0.1.5: seq=2 ttl=64 time=1.168 ms
64 bytes from 10.0.1.5: seq=3 ttl=64 time=1.095 ms
^C
--- 10.0.1.5 ping statistics ---
4 packets transmitted, 4 packets received, 0% packet loss
round-trip min/avg/max = 0.875/1.048/1.168 ms

三、其他动作

将现有服务连接到overlay网络

docker service update --network-add my-network hello

删除正在运行的网络连接

docker service update --network-rm my-network hello

 

以上是关于Docker Swarm 创建overlay网络的主要内容,如果未能解决你的问题,请参考以下文章

docker swarm (三):overlay与docker_gwbridge网络详解

【swarm】Docker跨主机网络:overlay

Docker swarm中的LB和服务发现详解

如何创建docker network

37. docker swarm docker service 的更新

六十二、Docker Swarm-手动部署多service应用