docker实现跨宿主机的容器之间网络互联
作者:这样的宋哥哥
本文主要介绍了docker实现跨宿主机的容器之间网络互联,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
背景:最近闲来无事,想到之前开发提出的问题不用k8s那套网络插件如何实现跨宿主机的容器之间网络互联,顺便复习下iptables知识点。
docker实现跨宿主机的容器之间网络互联 一. 环境介绍二. docker跨主机互联实现说明三. 修改docker0网桥网段3.1 A宿主机10.1.10.1133.2 B宿主机10.1.10.114
一. 环境介绍
两台centos8.2的虚拟机以及harbor仓库的镜像
宿主机A:ip 10.1.10.113
宿主机B:ip 10.1.10.114
二. docker跨主机互联实现说明
跨主机互联是说A宿主机的容器可以访问B主机上的容器,但是前提是保证各宿主机之间的网络是可以相互通信的,然后各容器才可以通过宿主机访问到对方的容器。
实现原理: 是在宿主机做一个网络路由就可以实现A宿主机的容器访问B主机的容器的目的
三. 修改docker0网桥网段
3.1 A宿主机10.1.10.113
设置docker网桥网段为192.168.100.1
systemctl restart docker docker login 10.1.10.15 docker pull 10.1.10.15/syimage/centos8.2:v2 docker run -itd --restart=always --name=vm01 --privileged 10.1.10.15/syimage/centos8.2:v2
添加静态路由和iptables规则
在各宿主机添加静态路由,网关指向对方宿主机的IP
route add -net 192.168.200.0/24 gw 10.1.10.114 iptables -A FORWARD -s 10.0.0.0/24 -j ACCEPT #或者修改FORWARD默认规则 iptables -P FORWARD ACCEPT route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 10.1.10.1 0.0.0.0 UG 100 0 0 ens32 10.1.10.0 0.0.0.0 255.255.255.0 U 100 0 0 ens32 192.168.100.0 0.0.0.0 255.255.255.0 U 0 0 0 docker0 192.168.200.0 10.1.10.114 255.255.255.0 UG 0 0 0 ens32
测试跨宿主机之间容器互联
docker exec -it vm01 ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.100.2 netmask 255.255.255.0 broadcast 192.168.100.255 ether 02:42:c0:a8:64:02 txqueuelen 0 (Ethernet) RX packets 36 bytes 25292 (24.6 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 45 bytes 5310 (5.1 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 loop txqueuelen 1000 (Local Loopback) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@shudun ~]# docker exec -it vm01 ping 192.168.200.2 PING 192.168.200.2 (192.168.200.2) 56(84) bytes of data. 64 bytes from 192.168.200.2: icmp_seq=1 ttl=62 time=0.907 ms 64 bytes from 192.168.200.2: icmp_seq=2 ttl=62 time=0.678 ms ^C --- 192.168.200.2 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 2ms rtt min/avg/max/mdev = 0.678/0.792/0.907/0.117 ms
3.2 B宿主机10.1.10.114
设置docker网桥网段为192.168.200.1
systemctl restart docker route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 10.1.10.1 0.0.0.0 UG 100 0 0 ens32 10.1.10.0 0.0.0.0 255.255.255.0 U 100 0 0 ens32 192.168.100.0 10.1.10.113 255.255.255.0 UG 0 0 0 ens32 192.168.200.0 0.0.0.0 255.255.255.0 U 0 0 0 docker0 docker login 10.1.10.15 docker pull 10.1.10.15/syimage/centos8.2:v2 docker run -itd --restart=always --name=vm01 --privileged 10.1.10.15/syimage/centos8.2:v2
添加静态路由和iptables规则
在各宿主机添加静态路由,网关指向对方宿主机的IP
route add -net 192.168.100.0/24 gw 10.1.10.113 iptables -A FORWARD -s 10.0.0.0/24 -j ACCEPT #或者修改FORWARD默认规则 iptables -P FORWARD ACCEPT route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 10.1.10.1 0.0.0.0 UG 100 0 0 ens32 10.1.10.0 0.0.0.0 255.255.255.0 U 100 0 0 ens32 192.168.100.0 10.1.10.113 255.255.255.0 UG 0 0 0 ens32 192.168.200.0 0.0.0.0 255.255.255.0 U 0 0 0 docker0
测试跨宿主机之间容器互联
docker exec -it vm01 ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.200.2 netmask 255.255.255.0 broadcast 192.168.200.255 ether 02:42:c0:a8:c8:02 txqueuelen 0 (Ethernet) RX packets 36 bytes 25280 (24.6 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 45 bytes 5322 (5.1 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 loop txqueuelen 1000 (Local Loopback) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@shudun ~]# docker exec -it vm01 ping 192.168.100.2 PING 192.168.100.2 (192.168.100.2) 56(84) bytes of data. 64 bytes from 192.168.100.2: icmp_seq=1 ttl=62 time=0.704 ms 64 bytes from 192.168.100.2: icmp_seq=2 ttl=62 time=0.650 ms ^C --- 192.168.100.2 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 51ms rtt min/avg/max/mdev = 0.650/0.677/0.704/0.027 ms
最后宿主机A的容器vm01访问宿主机B容器vm02,同时在宿主机B上tcpdump抓包观察
到此这篇关于docker实现跨宿主机的容器之间网络互联的文章就介绍到这了,更多相关docker 跨宿主机互联内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!