Docker容器

[docker 网络] 跨主机docker容器通信 vxlan

2019-06-08  本文已影响1人  nicktming

1. 前言

[docker 网络]docker跨主机网络 ovs gre 测试1 中已经使用gre tunnel 来进行docker的容器跨主机网络通信. 本文将使用vxlan技术来进行跨主机通信.

2.配置

2.1 vm1配置

[root@vm1 ovs-learning]# cat ovs-accoss-host-same-internet-vxlan.sh 
echo 0 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -F
iptables -F

docker run -d --name con1 --net=none --privileged=true busybox top
docker run -d --name con2 --net=none --privileged=true busybox top
# 添加ovs网桥br0
ovs-vsctl add-br br0
# 为两个容器配置网络
ovs-docker add-port br0 eth0 con1 --ipaddress=192.168.1.1/16 
ovs-docker add-port br0 eth0 con2 --ipaddress=192.168.1.2/16 
# 建立vxlan tunnel
ovs-vsctl add-port br0 vxlan0 -- set interface vxlan0 type=vxlan options:remote_ip=172.19.0.8
[root@vm1 ovs-learning]# ./ovs-accoss-host-same-internet-vxlan.sh 
d44cd97411fabbd5291439ec48f772ff8879baf894b002703677c9fc70ac0db9
858d94eed983e2d363f3326c93bac45aa3e1148ac106b14a0cc4f5b965be42f2
[root@vm1 ovs-learning]# ovs-vsctl show
9e614733-eac0-4c1c-a0b3-06149c56b7c5
    Bridge "br0"
        Port "500192b61c554_l"
            Interface "500192b61c554_l"
        Port "vxlan0"
            Interface "vxlan0"
                type: vxlan
                options: {remote_ip="172.19.0.8"}
        Port "br0"
            Interface "br0"
                type: internal
        Port "58d8cff208f84_l"
            Interface "58d8cff208f84_l"
    ovs_version: "2.5.1"

2.2 vm2配置

vm2

[root@vm2 ovs-learning]# cat ovs-accoss-host-same-internet-vxlan.sh 
echo 0 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -F
iptables -F

docker run -d --name con1 --net=none --privileged=true busybox top
docker run -d --name con2 --net=none --privileged=true busybox top
# 添加ovs网桥br0
ovs-vsctl add-br br0
# 为两个容器配置网络
ovs-docker add-port br0 eth0 con1 --ipaddress=192.168.2.1/16 
ovs-docker add-port br0 eth0 con2 --ipaddress=192.168.2.2/16 
# 建立vxlan tunnel
ovs-vsctl add-port br0 vxlan0 -- set interface vxlan0 type=vxlan options:remote_ip=172.19.0.12
[root@vm2 ovs-learning]# ./ovs-accoss-host-same-internet-vxlan.sh 
554f49de494495d6f5b106611db8dd5810e84c1aac2026eb4f0d973fda2efb60
b63baa55c61a618faeb7dc4cba004cae5b7c853b16cdbe42a0dcac415fb152b0
[root@vm2 ovs-learning]# ovs-vsctl show
877b5563-91f3-444d-99ba-a0d3f75f70ad
    Bridge "br0"
        Port "br0"
            Interface "br0"
                type: internal
        Port "dfb8856879b14_l"
            Interface "dfb8856879b14_l"
        Port "487ee928e7224_l"
            Interface "487ee928e7224_l"
        Port "vxlan0"
            Interface "vxlan0"
                type: vxlan
                options: {remote_ip="172.19.0.12"}
    ovs_version: "2.5.1"

2.3 测试

vxlan.png

测试, vm1的con1访问其他三个容器没有问题

[root@vm1 ovs-learning]# docker exec -it con1 ping -c 1 192.168.1.2
PING 192.168.1.2 (192.168.1.2): 56 data bytes
64 bytes from 192.168.1.2: seq=0 ttl=64 time=5.238 ms

--- 192.168.1.2 ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 5.238/5.238/5.238 ms
[root@vm1 ovs-learning]# docker exec -it con1 ping -c 1 192.168.2.2
PING 192.168.2.2 (192.168.2.2): 56 data bytes
64 bytes from 192.168.2.2: seq=0 ttl=64 time=7.941 ms

--- 192.168.2.2 ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 7.941/7.941/7.941 ms
[root@vm1 ovs-learning]# docker exec -it con1 ping -c 1 192.168.2.1
PING 192.168.2.1 (192.168.2.1): 56 data bytes
64 bytes from 192.168.2.1: seq=0 ttl=64 time=4.676 ms

--- 192.168.2.1 ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 4.676/4.676/4.676 ms

vm2的con1访问其他三个容器没有问题

[root@vm2 ovs-learning]# docker exec -it con1 ping -c 1 192.168.2.2
PING 192.168.2.2 (192.168.2.2): 56 data bytes
64 bytes from 192.168.2.2: seq=0 ttl=64 time=4.087 ms

--- 192.168.2.2 ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 4.087/4.087/4.087 ms
[root@vm2 ovs-learning]# docker exec -it con1 ping -c 1 192.168.1.1
PING 192.168.1.1 (192.168.1.1): 56 data bytes
64 bytes from 192.168.1.1: seq=0 ttl=64 time=8.200 ms

--- 192.168.1.1 ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 8.200/8.200/8.200 ms
[root@vm2 ovs-learning]# docker exec -it con1 ping -c 1 192.168.1.2
PING 192.168.1.2 (192.168.1.2): 56 data bytes
64 bytes from 192.168.1.2: seq=0 ttl=64 time=11.404 ms

--- 192.168.1.2 ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 11.404/11.404/11.404 ms

3. 参考

1. https://www.cnblogs.com/gzxbkk/p/8552531.html
2. https://blog.51cto.com/liufei888/2071793?from=timeline&isappinstalled=0
3. https://blog.csdn.net/song7999/article/details/80403778

上一篇下一篇

猜你喜欢

热点阅读