美文网首页云原生
多个docker节点使用直接路由的方式通信

多个docker节点使用直接路由的方式通信

作者: 印随2018 | 来源:发表于2019-07-17 13:54 被阅读6次

    关键技术:路由软件的使用

    一、虚拟机

    首先准备三台虚拟机,假设节点IP地址分别为

    • docker-1: 192.168.0.30
    • docker-2: 192.168.0.31
    • docker-3: 192.168.0.32

    在每个节点上安装docker软件,并修改Docker0网桥默认网段为

    • docker-1: 10.0.1.1/16
    • docker-2: 10.0.2.1/16
    • docker-3: 10.0.3.1/16

    比如,修改docker-1的Docker0网桥的默认网段,编辑/etc/docker/daemon.json文件,添加以下内容

    {
     "bip":"10.0.1.1/24"
    }
    

    然后重启docker,查看网桥的IP地址

    [root@localhost ~]# systemctl restart docker
    
    [root@localhost ~]# ifconfig docker0
    docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
            inet 10.0.1.1  netmask 255.255.255.0  broadcast 10.0.1.255
            ether 02:42:bd:d7:e3:8b  txqueuelen 0  (Ethernet)
            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
    

    其他两个节点,也做类似的修改。

    路由器

    这节内容是把虚拟机节点变成一个路由器节点,下面的内容需要分别在三个节点上执行

    1. 安装路由软件
    yum install -y quagga
    
    1. 启动软路由
    [root@localhost ~]# cat << EOF > /etc/quagga/zebra.conf
    hostname router-1
    password zebra
    enable password zebra
    EOF
    [root@localhost ~]# cat << EOF >  /etc/quagga/ripd.conf
    hostname ripd
    password zebra
    
    router rip
    network eth0
    network docker0
    
    neighbor 192.168.0.30
    neighbor 192.168.0.31
    neighbor 192.168.0.32
    
    EOF
    [root@localhost ~]# systemctl start zebra
    [root@localhost ~]# systemctl start ripd
    

    大概等待十几秒后,路由完成更新,可以查看任何一个节点的路由表

    [root@localhost quagga]# ip route
    default via 192.168.0.1 dev eth0
    10.0.1.0/24 dev docker0 proto kernel scope link src 10.0.1.1
    10.0.2.0/24 via 192.168.0.31 dev eth0 proto zebra metric 2
    10.0.3.0/24 via 192.168.0.32 dev eth0 proto zebra metric 2
    192.168.0.0/24 dev eth0 proto kernel scope link src 192.168.0.30
    

    可以看到,有两个路由有zibra的字样,刚好是其他两个节点的docker0的网络,我们来测试一下网络

    [root@localhost ~]# ping 10.0.2.1 -c 3
    PING 10.0.2.1 (10.0.2.1) 56(84) bytes of data.
    64 bytes from 10.0.2.1: icmp_seq=1 ttl=64 time=0.545 ms
    64 bytes from 10.0.2.1: icmp_seq=2 ttl=64 time=0.516 ms
    64 bytes from 10.0.2.1: icmp_seq=3 ttl=64 time=0.839 ms
    
    --- 10.0.2.1 ping statistics ---
    3 packets transmitted, 3 received, 0% packet loss, time 2002ms
    rtt min/avg/max/mdev = 0.516/0.633/0.839/0.147 ms
    [root@localhost ~]#
    [root@localhost ~]#
    [root@localhost ~]# ping 10.0.3.1 -c 3
    PING 10.0.3.1 (10.0.3.1) 56(84) bytes of data.
    64 bytes from 10.0.3.1: icmp_seq=1 ttl=64 time=0.495 ms
    64 bytes from 10.0.3.1: icmp_seq=2 ttl=64 time=0.480 ms
    64 bytes from 10.0.3.1: icmp_seq=3 ttl=64 time=0.455 ms
    
    --- 10.0.3.1 ping statistics ---
    3 packets transmitted, 3 received, 0% packet loss, time 2002ms
    rtt min/avg/max/mdev = 0.455/0.476/0.495/0.030 ms
    

    我们已经打通多个节点上的容器之间的通信,下面我们可以在各个节点运行docker容器来测试网络

    容器

    分别在三个节点,启动一个容器,然后测试网络

    [root@localhost ~]# docker run -it alpine sh
    / # ping 10.0.1.1 -c 3
    PING 10.0.1.1 (10.0.1.1): 56 data bytes
    64 bytes from 10.0.1.1: seq=0 ttl=64 time=0.600 ms
    64 bytes from 10.0.1.1: seq=1 ttl=64 time=0.272 ms
    64 bytes from 10.0.1.1: seq=2 ttl=64 time=0.268 ms
    
    --- 10.0.1.1 ping statistics ---
    3 packets transmitted, 3 packets received, 0% packet loss
    round-trip min/avg/max = 0.268/0.380/0.600 ms
    / # ping 10.0.2.1 -c 3
    PING 10.0.2.1 (10.0.2.1): 56 data bytes
    64 bytes from 10.0.2.1: seq=0 ttl=63 time=2.079 ms
    64 bytes from 10.0.2.1: seq=1 ttl=63 time=1.292 ms
    64 bytes from 10.0.2.1: seq=2 ttl=63 time=0.755 ms
    
    --- 10.0.2.1 ping statistics ---
    3 packets transmitted, 3 packets received, 0% packet loss
    round-trip min/avg/max = 0.755/1.375/2.079 ms
    / # ping 10.0.3.1 -c 3
    PING 10.0.3.1 (10.0.3.1): 56 data bytes
    64 bytes from 10.0.3.1: seq=0 ttl=63 time=0.886 ms
    64 bytes from 10.0.3.1: seq=1 ttl=63 time=0.777 ms
    64 bytes from 10.0.3.1: seq=2 ttl=63 time=0.807 ms
    
    --- 10.0.3.1 ping statistics ---
    3 packets transmitted, 3 packets received, 0% packet loss
    round-trip min/avg/max = 0.777/0.823/0.886 ms
    

    相关文章

      网友评论

        本文标题:多个docker节点使用直接路由的方式通信

        本文链接:https://www.haomeiwen.com/subject/crrqlctx.html