• [Docker]八.Docker 容器跨主机通讯


    一.跨主机通讯原理

    在主机192.168.31.140上的docker0(172.17.0.0/16)中有一个容器mycentos( 172.17.0.2/16),

    在主机192.168.31.81上的docker0(172.17.0.0/16)中有一个容器mycentos( 172.17.0.2/16),然后在主机192.168.31.140上ping主机192.168.31.81,发现ping不通要实现两个主机间容器的通信,怎么实现呢?

    各项配置如下(举例说明):
    • 主机1IP地址为:192.168.31.140
    • 主机2IP地址为:192.168.31.81
    • 为主机1上的Docker容器分配的子网:192.168.1.0/24
    • 为主机2上的Docker容器分配的子网:192.168.2.0/24
    • 这样配置之后,两个主机上的Docker容器就肯定不会使用相同的IP地址从而避免了IP冲突
    接下来定义两条路由规则即可:
            从container1 发往 container2 的数据包,首先发往 container1 “网关 ”docker0 ,然后通过查找主机 1的路由得知需要将数据包发给主机2 ,数据包到达主机 2 后再转发给主机 2 docker0 ,最后由其将数据包转到container2 中,反向原理相同

    二.两台主机机通讯实验

    1.分别在主机1和主机2上面创建两个网络

    主机1创建网络

    1. docker network create --driver bridge --subnet 192.168.1.0/24 --gateway
    2. 192.168.1.1 docker1

    docker network ls查看网络

    1. [root@MiWiFi-R3L-srv docker]# docker network ls
    2. NETWORK ID NAME DRIVER SCOPE
    3. 2d19a8dfb493 bridge bridge local
    4. c0fbaf2266c8 docker1 bridge local
    5. fc03b9653496 host host local

    docker inspect NETWORK_ID查看配置:发现其网络在192.168.1.0/24上,网关为: 192.168.1.1

    1. [root@MiWiFi-R3L-srv docker]# docker inspect c0fbaf2266c8
    2. [
    3. {
    4. "Name": "docker1",
    5. "Id": "c0fbaf2266c86f504d02a9324c45fb49c1909f908631d6a2cd5b1bb04dc981a8",
    6. "Created": "2023-11-20T18:19:18.375826061-08:00",
    7. "Scope": "local",
    8. "Driver": "bridge",
    9. "EnableIPv6": false,
    10. "IPAM": {
    11. "Driver": "default",
    12. "Options": {},
    13. "Config": [
    14. {
    15. "Subnet": "192.168.1.0/24",
    16. "Gateway": "192.168.1.1"
    17. }
    18. ]
    19. },
    20. "Internal": false,
    21. "Attachable": false,
    22. "Ingress": false,
    23. "ConfigFrom": {
    24. "Network": ""
    25. },
    26. "ConfigOnly": false,
    27. ...
    28. }

    主机2创建网络

    docker network create --driver bridge --subnet 192.168.2.0/24 --gateway 192.168.2.1 docker1

    docker network ls查看网络

    1. [root@MiWiFi-R3L-srv centos7]# docker network ls
    2. NETWORK ID NAME DRIVER SCOPE
    3. 5fd741696fa2 bridge bridge local
    4. 04f03105e411 docker1 bridge local
    5. 2110fa85f0da host host local

    docker inspect NETWORK_ID查看配置:发现其网络在192.168.2.0/24上,网关为: 192.168.2.1

    1. [root@MiWiFi-R3L-srv docker]# docker inspect 04f03105e411
    2. [
    3. {
    4. "Name": "docker1",
    5. "Id": "c0fbaf2266c86f504d02a9324c45fb49c1909f908631d6a2cd5b1bb04dc981a8",
    6. "Created": "2023-11-20T18:19:18.375826061-08:00",
    7. "Scope": "local",
    8. "Driver": "bridge",
    9. "EnableIPv6": false,
    10. "IPAM": {
    11. "Driver": "default",
    12. "Options": {},
    13. "Config": [
    14. {
    15. "Subnet": "192.168.2.0/24",
    16. "Gateway": "192.168.2.1"
    17. }
    18. ]
    19. },
    20. "Internal": false,
    21. "Attachable": false,
    22. "Ingress": false,
    23. "ConfigFrom": {
    24. "Network": ""
    25. },
    26. "ConfigOnly": false,
    27. ...
    28. }

    这样就在两台主机上创建了两个网络,一个主机的网段为1.0,一个主机的网段为2.0

    下面在启动容器的时候需要把容器加入到不同的网络中

    2.在两个主机上启动对应的容器

    在主机1上通过镜像启动一个容器mycentos:

    1. #通过cnetos镜像启动一个mycentos1的容器,并指定网络为docker1
    2. [root@MiWiFi-R3L-srv docker]# docker run -it -d --name mycentos1 --net docker1 d757f6342cfa /bin/bash
    3. 644cdc0acda390cf236f247bbec6c20080ea9b7a064670fdbbaaeb33cef8995f
    4. [root@MiWiFi-R3L-srv docker]# docker ps
    5. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    6. 644cdc0acda3 d757f6342cfa "/bin/bash" 20 seconds ago Up 13 seconds mycentos1
    7. #查看mycentos1容器网络:发现ip:192.168.1.2 在docker1网络上
    8. [root@MiWiFi-R3L-srv docker]# docker exec -it mycentos1 ifconfig
    9. eth0: flags=4163 mtu 1500
    10. inet 192.168.1.2 netmask 255.255.255.0 broadcast 192.168.1.255
    11. ether 02:42:c0:a8:01:02 txqueuelen 0 (Ethernet)
    12. RX packets 19 bytes 2376 (2.3 KiB)
    13. RX errors 0 dropped 0 overruns 0 frame 0
    14. TX packets 0 bytes 0 (0.0 B)
    15. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
    16. lo: flags=73 mtu 65536
    17. inet 127.0.0.1 netmask 255.0.0.0
    18. loop txqueuelen 1000 (Local Loopback)
    19. RX packets 0 bytes 0 (0.0 B)
    20. RX errors 0 dropped 0 overruns 0 frame 0
    21. TX packets 0 bytes 0 (0.0 B)
    22. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

    在主机2上通过镜像启动一个容器mycentos:

    1. #通过cnetos镜像启动一个mycentos1的容器,并指定网络为docker1
    2. [root@MiWiFi-R3L-srv docker]# docker run -it -d --name mycentos1 --net docker1 d757f6342cfa /bin/bash
    3. 644cdc0acda390cf236f247bbec6c20080ea9b7a064670fdbbaaeb33cef8995f
    4. [root@MiWiFi-R3L-srv docker]# docker ps
    5. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    6. 4ba38cf3943b d757f6342cfa "/bin/bash" 20 seconds ago Up 13 seconds mycentos1
    7. #查看mycentos1容器网络:发现ip:192.168.2.2 在docker1网络上
    8. [root@MiWiFi-R3L-srv docker]# docker exec -it mycentos1 ifconfig
    9. eth0: flags=4163 mtu 1500
    10. inet 192.168.2.2 netmask 255.255.255.0 broadcast 192.168.2.255
    11. ether 02:42:c0:a8:01:02 txqueuelen 0 (Ethernet)
    12. RX packets 19 bytes 2376 (2.3 KiB)
    13. RX errors 0 dropped 0 overruns 0 frame 0
    14. TX packets 0 bytes 0 (0.0 B)
    15. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
    16. lo: flags=73 mtu 65536
    17. inet 127.0.0.1 netmask 255.0.0.0
    18. loop txqueuelen 1000 (Local Loopback)
    19. RX packets 0 bytes 0 (0.0 B)
    20. RX errors 0 dropped 0 overruns 0 frame 0
    21. TX packets 0 bytes 0 (0.0 B)
    22. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
    • 在主机1上ping主机2,能够ping通,他们处于同一网段
    • 在主机1的容器中ping主机1,他们也能ping通
    • 在主机1的容器中ping主机2可以ping通
    • 在主机1的容器中ping主机2的容器,ping不通,因为他们不处于同一网段
    1. #主机1上ping主机2,ping通
    2. [root@MiWiFi-R3L-srv docker]# ping 192.168.31.81
    3. PING 192.168.31.81 (192.168.31.81) 56(84) bytes of data.
    4. 64 bytes from 192.168.31.81: icmp_seq=1 ttl=64 time=1.42 ms
    5. ^C
    6. --- 192.168.31.81 ping statistics ---
    7. 1 packets transmitted, 1 received, 0% packet loss, time 0ms
    8. rtt min/avg/max/mdev = 1.418/1.418/1.418/0.000 ms
    9. [root@MiWiFi-R3L-srv docker]# docker ps
    10. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    11. 644cdc0acda3 d757f6342cfa "/bin/bash" 12 minutes ago Up 12 minutes mycentos1
    12. #进入主机1容器
    13. [root@MiWiFi-R3L-srv docker]# docker exec -it 644cdc0acda3 /bin/bash
    14. [root@644cdc0acda3 wwwroot]#
    15. #容器中ping主机2,ping通
    16. [root@644cdc0acda3 wwwroot]# ping 192.168.31.140
    17. PING 192.168.31.140 (192.168.31.140) 56(84) bytes of data.
    18. 64 bytes from 192.168.31.140: icmp_seq=1 ttl=64 time=0.359 ms
    19. ^C
    20. --- 192.168.31.140 ping statistics ---
    21. 1 packets transmitted, 1 received, 0% packet loss, time 0ms
    22. rtt min/avg/max/mdev = 0.359/0.359/0.359/0.000 ms
    23. #容器中ping主机1,ping通
    24. [root@644cdc0acda3 wwwroot]# ping 192.168.31.81
    25. PING 192.168.31.81 (192.168.31.81) 56(84) bytes of data.
    26. 64 bytes from 192.168.31.81: icmp_seq=1 ttl=63 time=0.419 ms
    27. 64 bytes from 192.168.31.81: icmp_seq=2 ttl=63 time=0.387 ms
    28. ^C
    29. --- 192.168.31.81 ping statistics ---
    30. 2 packets transmitted, 2 received, 0% packet loss, time 1049ms
    31. rtt min/avg/max/mdev = 0.387/0.403/0.419/0.016 ms
    32. #容器中ping主机2中的容器,ping不通
    33. [root@644cdc0acda3 wwwroot]# ping 192.168.2.2
    34. PING 192.168.2.2 (192.168.2.2) 56(84) bytes of data.

     那么要时主机1中的容器和主机2中的容器通信,怎么办呢,这就需要配置路由规则

    3.添加路由规则  

    主机1上添加路由规则

    1. #查看主机1上的路由规则
    2. [root@MiWiFi-R3L-srv docker]# route
    3. Kernel IP routing table
    4. Destination Gateway Genmask Flags Metric Ref Use Iface
    5. default XiaoQiang 0.0.0.0 UG 100 0 0 ens33
    6. 172.17.0.0 0.0.0.0 255.255.0.0 U 427 0 0 docker0
    7. 172.18.0.0 0.0.0.0 255.255.0.0 U 0 0 0 br-9a2fe27fdd30
    8. 172.19.0.0 0.0.0.0 255.255.0.0 U 0 0 0 br-fe75119d5a77
    9. 192.168.1.0 0.0.0.0 255.255.255.0 U 426 0 0 br-c0fbaf2266c8
    10. 192.168.2.0 0.0.0.0 255.255.255.0 U 425 0 0 br-2d9c2d29e6d3
    11. 192.168.31.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
    12. 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
    把访问 192.168.2.0 的请求转发到主机2服务器 192.168.31.81
    1. #在主机1上配置192.168.2.0这个路由
    2. [root@MiWiFi-R3L-srv docker]# route add -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.31.81

    当访问192.168.2.0这个结点的ip时,就会转发到192.168.31.81这台主机服务器上

    1. [root@MiWiFi-R3L-srv docker]# route
    2. Kernel IP routing table
    3. Destination Gateway Genmask Flags Metric Ref Use Iface
    4. default XiaoQiang 0.0.0.0 UG 100 0 0 ens33
    5. 172.17.0.0 0.0.0.0 255.255.0.0 U 427 0 0 docker0
    6. 172.18.0.0 0.0.0.0 255.255.0.0 U 0 0 0 br-9a2fe27fdd30
    7. 172.19.0.0 0.0.0.0 255.255.0.0 U 0 0 0 br-fe75119d5a77
    8. 192.168.1.0 0.0.0.0 255.255.255.0 U 426 0 0 br-c0fbaf2266c8
    9. 192.168.2.0 192.168.31.81 255.255.255.0 UG 0 0 0 ens33
    10. 192.168.2.0 0.0.0.0 255.255.255.0 U 425 0 0 br-2d9c2d29e6d3
    11. 192.168.31.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
    12. 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0

     主机2上添加路由规则

    1. #查看主机1上的路由规则
    2. [root@MiWiFi-R3L-srv docker]# route
    3. Kernel IP routing table
    4. Destination Gateway Genmask Flags Metric Ref Use Iface
    5. default XiaoQiang 0.0.0.0 UG 100 0 0 ens33
    6. 172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0
    7. 192.168.2.0 0.0.0.0 255.255.255.0 U 0 0 0 br-04f03105e411
    8. 192.168.31.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
    9. 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
    把访问 192.168.1.0 的请求转发到主机2服务器 192.168.31.140
    1. #在主机2上配置192.168.1.0这个路由
    2. [root@MiWiFi-R3L-srv docker]# route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.31.140

    当访问192.168.1.0这个结点的ip时,就会转发到192.168.31.140这台主机服务器上

    1. [root@MiWiFi-R3L-srv docker]# route
    2. Kernel IP routing table
    3. Destination Gateway Genmask Flags Metric Ref Use Iface
    4. default XiaoQiang 0.0.0.0 UG 100 0 0 ens33
    5. 172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0
    6. 192.168.1.0 192.168.31.140 255.255.255.0 UG 0 0 0 ens33
    7. 192.168.2.0 0.0.0.0 255.255.255.0 U 0 0 0 br-04f03105e411
    8. 192.168.31.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
    9. 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0

    4.配置iptables规则

    IPTABLES 是与最新的 3.5 版本 Linux 内核集成的 IP 信息包过滤系统 (相当于ip的防火墙),命令如下:

    1. iptables -t 表名 <-A/I/D/R> 规则链名 [规则号] <-i/o 网卡名> -p 协议名 <-s 源IP/源子网>
    2. --sport 源端口 <-d 目标IP/目标子网> --dport 目标端口 -j 动作
    3. iptables -t nat -I PREROUTING -s 192.168.1.0/24 -d 192.168.2.0/24 -j DNAT --to
    4. 192.168.1.1
    5. -t nat : 实现共享网络
    6. -I PREROUTING:用于目标地址转换(DNAT)。
    7. -I POSTOUTING:用于源地址转换(SNAT)
    8. -s 源ip/子网
    9. -d 目标ip/子网
    10. -j DNAT DNAT:目标地址转换
    主机 1 上添加如下规则:
    iptables -t nat -I PREROUTING -s 192.168.1.0/24 -d 192.168.2.0/24 -j DNAT --to 192.168.1.1
    主机 2 上添加如下规则:
    iptables -t nat -I PREROUTING -s 192.168.2.0/24 -d 192.168.1.0/24 -j DNAT --to 192.168.2.1

    5.容器间通信

    在主机1/主机1的容器中ping主机2容器,发现可以ping通

    1. #主机1上ping主机2的容器:可以ping通
    2. [root@MiWiFi-R3L-srv docker]# ping 192.168.2.2
    3. PING 192.168.2.2 (192.168.2.2) 56(84) bytes of data.
    4. 64 bytes from 192.168.2.2: icmp_seq=1 ttl=63 time=1.74 ms
    5. 64 bytes from 192.168.2.2: icmp_seq=2 ttl=63 time=0.346 ms
    6. #进入主机1容器
    7. [root@MiWiFi-R3L-srv docker]# docker exec -it 644cdc0acda3 /bin/bash
    8. #ping主机2的容器,可以ping通
    9. [root@644cdc0acda3 wwwroot]# ping 192.168.2.2
    10. PING 192.168.2.2 (192.168.2.2) 56(84) bytes of data.
    11. 64 bytes from 192.168.2.2: icmp_seq=1 ttl=64 time=0.192 ms

    在主机2/主机2的容器中ping主机1容器,发现可以ping通

    1. #主机2上ping主机1的容器:可以ping通
    2. [root@MiWiFi-R3L-srv docker]# ping 192.168.1.2
    3. PING 192.168.1.2 (192.168.1.2) 56(84) bytes of data.
    4. 64 bytes from 192.168.2.2: icmp_seq=1 ttl=63 time=1.74 ms
    5. 64 bytes from 192.168.2.2: icmp_seq=2 ttl=63 time=0.346 ms
    6. #进入主机2容器
    7. [root@MiWiFi-R3L-srv docker]# docker exec -it 644cdc0acd1a3 /bin/bash
    8. #ping主机1的容器,可以ping通
    9. [root@644cdc0acda3 wwwroot]# ping 192.168.1.2
    10. PING 192.168.1.2 (192.168.1.2) 56(84) bytes of data.
    11. 64 bytes from 192.168.2.2: icmp_seq=1 ttl=64 time=0.192 ms

    这样不同主机间的容器就可以相互通信了,即可以实现负载均衡/转发等功能,一台主机部署web,一台主机部署数据库,通过docker配置就可以实现相互通信

    三.三台主机通讯实验

    这个和上面方法一致

    1.分别在主机1、主机2、主机3上面创建两个网络

    • 主机1IP地址为:192.168.31.140
    • 主机2IP地址为:192.168.31.81
    • 主机2IP地址为:192.168.31.117

    主机1创建网络

    1. docker network create --driver bridge --subnet 192.168.1.0/24 --gateway
    2. 192.168.1.1 docker1

    docker network ls查看网络

    1. [root@MiWiFi-R3L-srv docker]# docker network ls
    2. NETWORK ID NAME DRIVER SCOPE
    3. 2d19a8dfb493 bridge bridge local
    4. c0fbaf2266c8 docker1 bridge local
    5. fc03b9653496 host host local

    docker inspect NETWORK_ID查看配置:发现其网络在192.168.1.0/24上,网关为: 192.168.1.1

    1. [root@MiWiFi-R3L-srv docker]# docker inspect c0fbaf2266c8
    2. [
    3. {
    4. "Name": "docker1",
    5. "Id": "c0fbaf2266c86f504d02a9324c45fb49c1909f908631d6a2cd5b1bb04dc981a8",
    6. "Created": "2023-11-20T18:19:18.375826061-08:00",
    7. "Scope": "local",
    8. "Driver": "bridge",
    9. "EnableIPv6": false,
    10. "IPAM": {
    11. "Driver": "default",
    12. "Options": {},
    13. "Config": [
    14. {
    15. "Subnet": "192.168.1.0/24",
    16. "Gateway": "192.168.1.1"
    17. }
    18. ]
    19. },
    20. "Internal": false,
    21. "Attachable": false,
    22. "Ingress": false,
    23. "ConfigFrom": {
    24. "Network": ""
    25. },
    26. "ConfigOnly": false,
    27. ...
    28. }

    主机2创建网络

    docker network create --driver bridge --subnet 192.168.2.0/24 --gateway 192.168.2.1 docker1

    docker network ls查看网络

    1. [root@MiWiFi-R3L-srv centos7]# docker network ls
    2. NETWORK ID NAME DRIVER SCOPE
    3. 5fd741696fa2 bridge bridge local
    4. 04f03105e411 docker1 bridge local
    5. 2110fa85f0da host host local

    docker inspect NETWORK_ID查看配置:发现其网络在192.168.2.0/24上,网关为: 192.168.2.1

    1. [root@MiWiFi-R3L-srv docker]# docker inspect 04f03105e411
    2. [
    3. {
    4. "Name": "docker1",
    5. "Id": "c0fbaf2266c86f504d02a9324c45fb49c1909f908631d6a2cd5b1bb04dc981a8",
    6. "Created": "2023-11-20T18:19:18.375826061-08:00",
    7. "Scope": "local",
    8. "Driver": "bridge",
    9. "EnableIPv6": false,
    10. "IPAM": {
    11. "Driver": "default",
    12. "Options": {},
    13. "Config": [
    14. {
    15. "Subnet": "192.168.2.0/24",
    16. "Gateway": "192.168.2.1"
    17. }
    18. ]
    19. },
    20. "Internal": false,
    21. "Attachable": false,
    22. "Ingress": false,
    23. "ConfigFrom": {
    24. "Network": ""
    25. },
    26. "ConfigOnly": false,
    27. ...
    28. }

     主机3创建网络

    docker network create --driver bridge --subnet 192.168.3.0/24 --gateway 192.168.3.1 docker1

    docker network ls查看网络

    1. [root@MiWiFi-R3L-srv centos7]# docker network ls
    2. NETWORK ID NAME DRIVER SCOPE
    3. 5fd741696fa2 bridge bridge local
    4. 04f03105e413 docker1 bridge local
    5. 2110fa85f0da host host local

    docker inspect NETWORK_ID查看配置:发现其网络在192.168.3.0/24上,网关为: 192.168.3.1

    1. [root@MiWiFi-R3L-srv docker]# docker inspect 04f03105e413
    2. [
    3. {
    4. "Name": "docker1",
    5. "Id": "c0fbaf2266c86f504d02a9324c45fb49c1909f908631d6a2cd5b1bb04dc981a8",
    6. "Created": "2023-11-20T18:19:18.375826061-08:00",
    7. "Scope": "local",
    8. "Driver": "bridge",
    9. "EnableIPv6": false,
    10. "IPAM": {
    11. "Driver": "default",
    12. "Options": {},
    13. "Config": [
    14. {
    15. "Subnet": "192.168.3.0/24",
    16. "Gateway": "192.168.3.1"
    17. }
    18. ]
    19. },
    20. "Internal": false,
    21. "Attachable": false,
    22. "Ingress": false,
    23. "ConfigFrom": {
    24. "Network": ""
    25. },
    26. "ConfigOnly": false,
    27. ...
    28. }

    这样就在三台主机上创建了三个网络,一个主机的网段为1.0,一个主机的网段为2.0,,一个主机的网段为3.0,下面在启动容器的时候需要把容器加入到不同的网络中

    2.在三台主机上启动对应的容器

    在主机1上通过镜像启动一个容器mycentos:

    1. #通过cnetos镜像启动一个mycentos1的容器,并指定网络为docker1
    2. [root@MiWiFi-R3L-srv docker]# docker run -it -d --name mycentos1 --net docker1 d757f6342cfa /bin/bash
    3. 644cdc0acda390cf236f247bbec6c20080ea9b7a064670fdbbaaeb33cef8995f
    4. [root@MiWiFi-R3L-srv docker]# docker ps
    5. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    6. 644cdc0acda3 d757f6342cfa "/bin/bash" 20 seconds ago Up 13 seconds mycentos1
    7. #查看mycentos1容器网络:发现ip:192.168.1.2 在docker1网络上
    8. [root@MiWiFi-R3L-srv docker]# docker exec -it mycentos1 ifconfig
    9. eth0: flags=4163 mtu 1500
    10. inet 192.168.1.2 netmask 255.255.255.0 broadcast 192.168.1.255
    11. ether 02:42:c0:a8:01:02 txqueuelen 0 (Ethernet)
    12. RX packets 19 bytes 2376 (2.3 KiB)
    13. RX errors 0 dropped 0 overruns 0 frame 0
    14. TX packets 0 bytes 0 (0.0 B)
    15. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
    16. lo: flags=73 mtu 65536
    17. inet 127.0.0.1 netmask 255.0.0.0
    18. loop txqueuelen 1000 (Local Loopback)
    19. RX packets 0 bytes 0 (0.0 B)
    20. RX errors 0 dropped 0 overruns 0 frame 0
    21. TX packets 0 bytes 0 (0.0 B)
    22. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

    在主机2上通过镜像启动一个容器mycentos:

    1. #通过cnetos镜像启动一个mycentos1的容器,并指定网络为docker1
    2. [root@MiWiFi-R3L-srv docker]# docker run -it -d --name mycentos1 --net docker1 d757f6342cfa /bin/bash
    3. 644cdc0acda390cf236f247bbec6c20080ea9b7a064670fdbbaaeb33cef8995f
    4. [root@MiWiFi-R3L-srv docker]# docker ps
    5. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    6. 4ba38cf3943b d757f6342cfa "/bin/bash" 20 seconds ago Up 13 seconds mycentos1
    7. #查看mycentos1容器网络:发现ip:192.168.2.2 在docker1网络上
    8. [root@MiWiFi-R3L-srv docker]# docker exec -it mycentos1 ifconfig
    9. eth0: flags=4163 mtu 1500
    10. inet 192.168.2.2 netmask 255.255.255.0 broadcast 192.168.2.255
    11. ether 02:42:c0:a8:01:02 txqueuelen 0 (Ethernet)
    12. RX packets 19 bytes 2376 (2.3 KiB)
    13. RX errors 0 dropped 0 overruns 0 frame 0
    14. TX packets 0 bytes 0 (0.0 B)
    15. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
    16. lo: flags=73 mtu 65536
    17. inet 127.0.0.1 netmask 255.0.0.0
    18. loop txqueuelen 1000 (Local Loopback)
    19. RX packets 0 bytes 0 (0.0 B)
    20. RX errors 0 dropped 0 overruns 0 frame 0
    21. TX packets 0 bytes 0 (0.0 B)
    22. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

    在主机3上通过镜像启动一个容器mycentos:

    1. #通过cnetos镜像启动一个mycentos1的容器,并指定网络为docker1
    2. [root@MiWiFi-R3L-srv docker]# docker run -it -d --name mycentos1 --net docker1 d757f6342cfa /bin/bash
    3. 644cdc0acda390cf236f247bbec6c20080ea9b7a064670fdbbaaeb33cef8995f
    4. [root@MiWiFi-R3L-srv docker]# docker ps
    5. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    6. 4ba38cf3943b3 d757f6342cfa "/bin/bash" 20 seconds ago Up 13 seconds mycentos1
    7. #查看mycentos1容器网络:发现ip:192.168.3.2 在docker1网络上
    8. [root@MiWiFi-R3L-srv docker]# docker exec -it mycentos1 ifconfig
    9. eth0: flags=4163 mtu 1500
    10. inet 192.168.3.2 netmask 255.255.255.0 broadcast 192.168.3.255
    11. ether 02:42:c0:a8:01:02 txqueuelen 0 (Ethernet)
    12. RX packets 19 bytes 2376 (2.3 KiB)
    13. RX errors 0 dropped 0 overruns 0 frame 0
    14. TX packets 0 bytes 0 (0.0 B)
    15. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
    16. lo: flags=73 mtu 65536
    17. inet 127.0.0.1 netmask 255.0.0.0
    18. loop txqueuelen 1000 (Local Loopback)
    19. RX packets 0 bytes 0 (0.0 B)
    20. RX errors 0 dropped 0 overruns 0 frame 0
    21. TX packets 0 bytes 0 (0.0 B)
    22. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
    • 在主机1上ping主机2,能够ping通,他们处于同一网段
    • 在主机1的容器中ping主机1,他们也能ping通
    • 在主机1的容器中ping主机2可以ping通
    • 在主机1的容器中ping主机2的容器,在主机1的容器中ping主机3的容器,ping不通,因为他们不处于同一网段
    1. #主机1上ping主机2,ping通
    2. [root@MiWiFi-R3L-srv docker]# ping 192.168.31.81
    3. PING 192.168.31.81 (192.168.31.81) 56(84) bytes of data.
    4. 64 bytes from 192.168.31.81: icmp_seq=1 ttl=64 time=1.42 ms
    5. ^C
    6. --- 192.168.31.81 ping statistics ---
    7. 1 packets transmitted, 1 received, 0% packet loss, time 0ms
    8. rtt min/avg/max/mdev = 1.418/1.418/1.418/0.000 ms
    9. [root@MiWiFi-R3L-srv docker]# docker ps
    10. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    11. 644cdc0acda3 d757f6342cfa "/bin/bash" 12 minutes ago Up 12 minutes mycentos1
    12. #进入主机1容器
    13. [root@MiWiFi-R3L-srv docker]# docker exec -it 644cdc0acda3 /bin/bash
    14. [root@644cdc0acda3 wwwroot]#
    15. #容器中ping主机2,ping通
    16. [root@644cdc0acda3 wwwroot]# ping 192.168.31.140
    17. PING 192.168.31.140 (192.168.31.140) 56(84) bytes of data.
    18. 64 bytes from 192.168.31.140: icmp_seq=1 ttl=64 time=0.359 ms
    19. ^C
    20. --- 192.168.31.140 ping statistics ---
    21. 1 packets transmitted, 1 received, 0% packet loss, time 0ms
    22. rtt min/avg/max/mdev = 0.359/0.359/0.359/0.000 ms
    23. #容器中ping主机1,ping通
    24. [root@644cdc0acda3 wwwroot]# ping 192.168.31.81
    25. PING 192.168.31.81 (192.168.31.81) 56(84) bytes of data.
    26. 64 bytes from 192.168.31.81: icmp_seq=1 ttl=63 time=0.419 ms
    27. 64 bytes from 192.168.31.81: icmp_seq=2 ttl=63 time=0.387 ms
    28. ^C
    29. --- 192.168.31.81 ping statistics ---
    30. 2 packets transmitted, 2 received, 0% packet loss, time 1049ms
    31. rtt min/avg/max/mdev = 0.387/0.403/0.419/0.016 ms
    32. #容器中ping主机2中的容器,ping不通
    33. [root@644cdc0acda3 wwwroot]# ping 192.168.2.2
    34. PING 192.168.2.2 (192.168.2.2) 56(84) bytes of data.
    35. #容器中ping主机3中的容器,ping不通
    36. [root@644cdc0acda3 wwwroot]# ping 192.168.3.2
    37. PING 192.168.2.2 (192.168.2.2) 56(84) bytes of data.

     那么要时主机1中的容器和主机2中的容器/主机2中的容器        通信,怎么办呢,这就需要配置路由规则

    3.添加路由规则  

    主机1上添加路由规则

    1. #查看主机1上的路由规则
    2. [root@MiWiFi-R3L-srv docker]# route
    3. Kernel IP routing table
    4. Destination Gateway Genmask Flags Metric Ref Use Iface
    5. default XiaoQiang 0.0.0.0 UG 100 0 0 ens33
    6. 172.17.0.0 0.0.0.0 255.255.0.0 U 427 0 0 docker0
    7. 172.18.0.0 0.0.0.0 255.255.0.0 U 0 0 0 br-9a2fe27fdd30
    8. 172.19.0.0 0.0.0.0 255.255.0.0 U 0 0 0 br-fe75119d5a77
    9. 192.168.1.0 0.0.0.0 255.255.255.0 U 426 0 0 br-c0fbaf2266c8
    10. 192.168.2.0 0.0.0.0 255.255.255.0 U 425 0 0 br-2d9c2d29e6d3
    11. 192.168.31.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
    12. 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
    把访问 192.168.2.0 的请求转发到主机2服务器 192.168.31.81
    把访问 192.168.2.0 的请求转发到主机3服务器 192.168.31.117
    1. #在主机1上配置192.168.2.0这个路由
    2. [root@MiWiFi-R3L-srv docker]# route add -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.31.81
    3. #在主机1上配置192.168.3.0这个路由
    4. [root@MiWiFi-R3L-srv docker]# route add -net 192.168.3.0 netmask 255.255.255.0 gw 192.168.31.117

    当访问192.168.2.0这个结点的ip时,就会转发到192.168.31.81这台主机服务器上

    当访问192.168.3.0这个结点的ip时,就会转发到192.168.31.117这台主机服务器上

    1. [root@MiWiFi-R3L-srv docker]# route
    2. Kernel IP routing table
    3. Destination Gateway Genmask Flags Metric Ref Use Iface
    4. default XiaoQiang 0.0.0.0 UG 100 0 0 ens33
    5. 172.17.0.0 0.0.0.0 255.255.0.0 U 427 0 0 docker0
    6. 172.18.0.0 0.0.0.0 255.255.0.0 U 0 0 0 br-9a2fe27fdd30
    7. 172.19.0.0 0.0.0.0 255.255.0.0 U 0 0 0 br-fe75119d5a77
    8. 192.168.1.0 0.0.0.0 255.255.255.0 U 426 0 0 br-c0fbaf2266c8
    9. 192.168.2.0 192.168.31.81 255.255.255.0 UG 0 0 0 ens33
    10. 192.168.3.0 192.168.31.117 255.255.255.0 UG 0 0 0 ens33
    11. 192.168.2.0 0.0.0.0 255.255.255.0 U 425 0 0 br-2d9c2d29e6d3
    12. 192.168.31.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
    13. 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0

     主机2上添加路由规则

    1. #查看主机1上的路由规则
    2. [root@MiWiFi-R3L-srv docker]# route
    3. Kernel IP routing table
    4. Destination Gateway Genmask Flags Metric Ref Use Iface
    5. default XiaoQiang 0.0.0.0 UG 100 0 0 ens33
    6. 172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0
    7. 192.168.2.0 0.0.0.0 255.255.255.0 U 0 0 0 br-04f03105e411
    8. 192.168.31.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
    9. 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
    把访问 192.168.1.0 的请求转发到主机2服务器 192.168.31.140
    把访问 192.168.3.0 的请求转发到主机2服务器 192.168.31.117
    1. #在主机2上配置192.168.1.0这个路由
    2. [root@MiWiFi-R3L-srv docker]# route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.31.140
    3. #在主机2上配置192.168.3.0这个路由
    4. [root@MiWiFi-R3L-srv docker]# route add -net 192.168.3.0 netmask 255.255.255.0 gw 192.168.31.117

    当访问192.168.1.0这个结点的ip时,就会转发到192.168.31.140这台主机服务器上

    当访问192.168.3.0这个结点的ip时,就会转发到192.168.31.117这台主机服务器上

    1. [root@MiWiFi-R3L-srv docker]# route
    2. Kernel IP routing table
    3. Destination Gateway Genmask Flags Metric Ref Use Iface
    4. default XiaoQiang 0.0.0.0 UG 100 0 0 ens33
    5. 172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0
    6. 192.168.1.0 192.168.31.140 255.255.255.0 UG 0 0 0 ens33
    7. 192.168.3.0 192.168.31.117 255.255.255.0 UG 0 0 0 ens33
    8. 192.168.2.0 0.0.0.0 255.255.255.0 U 0 0 0 br-04f03105e411
    9. 192.168.31.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
    10. 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0

    4.配置iptables规则

    IPTABLES 是与最新的 3.5 版本 Linux 内核集成的 IP 信息包过滤系统 (相当于ip的防火墙),命令如下:

    1. iptables -t 表名 <-A/I/D/R> 规则链名 [规则号] <-i/o 网卡名> -p 协议名 <-s 源IP/源子网>
    2. --sport 源端口 <-d 目标IP/目标子网> --dport 目标端口 -j 动作
    3. iptables -t nat -I PREROUTING -s 192.168.1.0/24 -d 192.168.2.0/24 -j DNAT --to
    4. 192.168.1.1
    5. -t nat : 实现共享网络
    6. -I PREROUTING:用于目标地址转换(DNAT)。
    7. -I POSTOUTING:用于源地址转换(SNAT)
    8. -s 源ip/子网
    9. -d 目标ip/子网
    10. -j DNAT DNAT:目标地址转换
    主机 1 上添加如下规则:
    1. iptables -t nat -I PREROUTING -s 192.168.1.0/24 -d 192.168.2.0/24 -j DNAT --to 192.168.1.1
    2. iptables -t nat -I PREROUTING -s 192.168.1.0/24 -d 192.168.3.0/24 -j DNAT --to 192.168.1.1
    主机 2 上添加如下规则:
    1. iptables -t nat -I PREROUTING -s 192.168.2.0/24 -d 192.168.1.0/24 -j DNAT --to 192.168.2.1
    2. iptables -t nat -I PREROUTING -s 192.168.2.0/24 -d 192.168.3.0/24 -j DNAT --to 192.168.2.1
     主机3 上添加如下规则:
    1. iptables -t nat -I PREROUTING -s 192.168.3.0/24 -d 192.168.1.0/24 -j DNAT --to 192.168.2.1
    2. iptables -t nat -I PREROUTING -s 192.168.3.0/24 -d 192.168.2.0/24 -j DNAT --to 192.168.2.1

    5.容器间通信

    在主机1/主机1的容器中ping主机2容器/主机3容器,发现可以ping通

    1. #主机1上ping主机2的容器:可以ping通
    2. [root@MiWiFi-R3L-srv docker]# ping 192.168.2.2
    3. PING 192.168.2.2 (192.168.2.2) 56(84) bytes of data.
    4. 64 bytes from 192.168.2.2: icmp_seq=1 ttl=63 time=1.74 ms
    5. 64 bytes from 192.168.2.2: icmp_seq=2 ttl=63 time=0.346 ms
    6. #主机1上ping主机3的容器:可以ping通
    7. [root@MiWiFi-R3L-srv docker]# ping 192.168.3.2
    8. PING 192.168.3.2 (192.168.3.2) 56(84) bytes of data.
    9. 64 bytes from 192.168.3.2: icmp_seq=1 ttl=63 time=1.74 ms
    10. 64 bytes from 192.168.3.2: icmp_seq=2 ttl=63 time=0.346 ms
    11. #进入主机1容器
    12. [root@MiWiFi-R3L-srv docker]# docker exec -it 644cdc0acda3 /bin/bash
    13. #ping主机2的容器,可以ping通
    14. [root@644cdc0acda3 wwwroot]# ping 192.168.2.2
    15. PING 192.168.2.2 (192.168.2.2) 56(84) bytes of data.
    16. 64 bytes from 192.168.2.2: icmp_seq=1 ttl=64 time=0.192 ms
    17. #ping主机3的容器,可以ping通
    18. [root@644cdc0acda3 wwwroot]# ping 192.168.3.2
    19. PING 192.168.3.2 (192.168.3.2) 56(84) bytes of data.
    20. 64 bytes from 192.168.3.2: icmp_seq=1 ttl=64 time=0.192 ms

    在主机2/主机2的容器中ping主机1容器/主机3容器,发现可以ping通

    1. #主机2上ping主机1的容器:可以ping通
    2. [root@MiWiFi-R3L-srv docker]# ping 192.168.1.2
    3. PING 192.168.1.2 (192.168.1.2) 56(84) bytes of data.
    4. 64 bytes from 192.168.2.2: icmp_seq=1 ttl=63 time=1.74 ms
    5. 64 bytes from 192.168.2.2: icmp_seq=2 ttl=63 time=0.346 ms
    6. #进入主机2容器
    7. [root@MiWiFi-R3L-srv docker]# docker exec -it 644cdc0acd1a3 /bin/bash
    8. #ping主机1的容器,可以ping通
    9. [root@644cdc0acda3 wwwroot]# ping 192.168.1.2
    10. PING 192.168.1.2 (192.168.1.2) 56(84) bytes of data.
    11. 64 bytes from 192.168.2.2: icmp_seq=1 ttl=64 time=0.192 ms

    [上一节][Docker]七.配置 Docker 网络 

  • 相关阅读:
    Jmeter接口测试 —— jmeter对图片验证码的处理
    统信UOS升级Python3及安装spyder5
    使用 OpenCV 的文档扫描仪
    C++命名规范
    【数据结构与算法】线性表 - 顺序表
    linux 测试
    闲谈JVM(一):浅析JVM Heap参数配置
    Exchangis1.0演讲稿
    高考有哪些东西没有考,但是却对人生发展至关重要的东西
    永州植物细胞实验室建设布局方案
  • 原文地址:https://blog.csdn.net/zhoupenghui168/article/details/134528185