目錄
- 一. 環(huán)境介紹
- 二. docker跨主機(jī)互聯(lián)實(shí)現(xiàn)說明
- 三. 修改docker0網(wǎng)橋網(wǎng)段
- 3.1 A宿主機(jī)10.1.10.113
- 3.2 B宿主機(jī)10.1.10.114
背景:最近閑來無事,想到之前開發(fā)提出的問題不用k8s那套網(wǎng)絡(luò)插件如何實(shí)現(xiàn)跨宿主機(jī)的容器之間網(wǎng)絡(luò)互聯(lián),順便復(fù)習(xí)下iptables知識(shí)點(diǎn)。
docker實(shí)現(xiàn)跨宿主機(jī)的容器之間網(wǎng)絡(luò)互聯(lián) 一. 環(huán)境介紹二. docker跨主機(jī)互聯(lián)實(shí)現(xiàn)說明三. 修改docker0網(wǎng)橋網(wǎng)段3.1 A宿主機(jī)10.1.10.1133.2 B宿主機(jī)10.1.10.114
一. 環(huán)境介紹
兩臺(tái)centos8.2的虛擬機(jī)以及harbor倉庫的鏡像
宿主機(jī)A:ip 10.1.10.113
宿主機(jī)B:ip 10.1.10.114
二. docker跨主機(jī)互聯(lián)實(shí)現(xiàn)說明
跨主機(jī)互聯(lián)是說A宿主機(jī)的容器可以訪問B主機(jī)上的容器,但是前提是保證各宿主機(jī)之間的網(wǎng)絡(luò)是可以相互通信的,然后各容器才可以通過宿主機(jī)訪問到對(duì)方的容器。
實(shí)現(xiàn)原理: 是在宿主機(jī)做一個(gè)網(wǎng)絡(luò)路由就可以實(shí)現(xiàn)A宿主機(jī)的容器訪問B主機(jī)的容器的目的
三. 修改docker0網(wǎng)橋網(wǎng)段
3.1 A宿主機(jī)10.1.10.113
設(shè)置docker網(wǎng)橋網(wǎng)段為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
添加靜態(tài)路由和iptables規(guī)則
在各宿主機(jī)添加靜態(tài)路由,網(wǎng)關(guān)指向?qū)Ψ剿拗鳈C(jī)的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默認(rèn)規(guī)則 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
測(cè)試跨宿主機(jī)之間容器互聯(lián)
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宿主機(jī)10.1.10.114
設(shè)置docker網(wǎng)橋網(wǎng)段為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
添加靜態(tài)路由和iptables規(guī)則
在各宿主機(jī)添加靜態(tài)路由,網(wǎng)關(guān)指向?qū)Ψ剿拗鳈C(jī)的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默認(rèn)規(guī)則 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
測(cè)試跨宿主機(jī)之間容器互聯(lián)
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
最后宿主機(jī)A的容器vm01訪問宿主機(jī)B容器vm02,同時(shí)在宿主機(jī)B上tcpdump抓包觀察