Docker容器——容器之间的互联

  1. 创建一个bridge模式的网络
[root@foundation19 ~]# docker network create --driver bridge my_net1
89b97e70be47c758684ca2bd34e86242a0643be9a01fd297f5487b5cdef4f82c
[root@foundation19 ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
5c0d59e54cbc        bridge              bridge              local
082bf249cd4b        host                host                local
89b97e70be47        my_net1             bridge              local
bf84585ce5e5        none                null                local

[root@foundation19 ~]# ip addr
14: br-89b97e70be47: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN 
    link/ether 02:42:fd:98:f5:ac brd ff:ff:ff:ff:ff:ff
    inet 172.18.0.1/16 brd 172.18.255.255 scope global br-89b97e70be47
       valid_lft forever preferred_lft forever

[root@foundation19 ~]# docker network inspect my_net1 
                {
                    "Subnet": "172.18.0.0/16",    ##默认单调递增,1819...
                    "Gateway": "172.18.0.1"
                }
  1. 再创建一个bridge的网络,自定义ip和网关
[root@foundation19 ~]# docker network create --driver bridge --subnet 172.20.0.0/24 --gateway 172.20.0.1 my_net2   ##自定义

[root@foundation19 ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
5c0d59e54cbc        bridge              bridge              local
082bf249cd4b        host                host                local
89b97e70be47        my_net1             bridge              local
8f8e4347cd7d        my_net2             bridge              local
bf84585ce5e5        none                null                local

8f8e4347cd7d66ba1beee88cbea7557945b32b4f2fff1c24196a4f1f1f7c248e
[root@foundation19 ~]# docker network inspect my_net2
                {
                    "Subnet": "172.20.0.0/24",
                    "Gateway": "172.20.0.1"
                }
```javascript                
[root@foundation19 ~]# ip addr
14: br-89b97e70be47: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN 
    link/ether 02:42:fd:98:f5:ac brd ff:ff:ff:ff:ff:ff
    inet 172.18.0.1/16 brd 172.18.255.255 scope global br-89b97e70be47
       valid_lft forever preferred_lft forever
15: br-8f8e4347cd7d: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN 
    link/ether 02:42:e7:f9:c5:d8 brd ff:ff:ff:ff:ff:ff
    inet 172.20.0.1/24 brd 172.20.0.255 scope global br-8f8e4347cd7d
       valid_lft forever preferred_lft forever
  1. 创建容器
[root@foundation19 images]# docker load -i ubuntu.tar 

[root@foundation19 ~]# docker run -it --name vm1 --net my_net1 ubuntu
root@bfd519244ae0:/# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
16: eth0@if17: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:12:00:02 brd ff:ff:ff:ff:ff:ff
    inet 172.18.0.2/16 brd 172.18.255.255 scope global eth0    ##容器IP172.18.0.2/16
       valid_lft forever preferred_lft forever
  1. 另开一个shell,再创建一个容器
[root@foundation19 ~]# docker run -it --name vm2 --net my_net2 ip 172.20.0.10 ubuntu
root@b010954d0ef6:/# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
18: eth0@if19: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:14:00:0a brd ff:ff:ff:ff:ff:ff
    inet 172.20.0.10/24 brd 172.20.0.255 scope global eth0
       valid_lft forever preferred_lft forever

root@b010954d0ef6:/# ping 172.20.0.1   ## ping网关可以通
PING 172.20.0.1 (172.20.0.1) 56(84) bytes of data.
64 bytes from 172.20.0.1: icmp_seq=1 ttl=64 time=0.103 ms
64 bytes from 172.20.0.1: icmp_seq=2 ttl=64 time=0.116 ms
^C
--- 172.20.0.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.103/0.109/0.116/0.012 ms
root@b010954d0ef6:/# ping 172.18.0.2    ##但是容器间不能ping通
PING 172.18.0.2 (172.18.0.2) 56(84) bytes of data.
^C
--- 172.18.0.2 ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 999ms
  1. 建立两个容器之间的连接
[root@foundation19 ~]# docker network connect my_net1 vm2
18: eth0@if19: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:14:00:0a brd ff:ff:ff:ff:ff:ff
    inet 172.20.0.10/24 brd 172.20.0.255 scope global eth0
       valid_lft forever preferred_lft forever
20: eth1@if21: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:12:00:03 brd ff:ff:ff:ff:ff:ff
    inet 172.18.0.3/16 brd 172.18.255.255 scope global eth1
       valid_lft forever preferred_lft forever

root@b010954d0ef6:/# ping 172.18.0.2  ##此时可以ping通
PING 172.18.0.2 (172.18.0.2) 56(84) bytes of data.
64 bytes from 172.18.0.2: icmp_seq=1 ttl=64 time=0.128 ms
64 bytes from 172.18.0.2: icmp_seq=2 ttl=64 time=0.089 ms
^C
--- 172.18.0.2 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms

docker的跨主机网络方案——macvlan

  • macvlan网络方案实现,必须要打开网卡混杂模式

  • 实验环境
    创建两台虚拟机server1,server2,都是两块网卡
    两台虚拟机都安装docker,都导入ubuntu.tar镜像

server1:

 [root@server1 ~]# docker network create -d  macvlan --subnet 172.25.1.0/24 --gateway 172.25.1.1 -o parent=eth0 mac_net1
 

[root@server1 ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
590d47201aeb        bridge              bridge              local
7acae6b7116a        docker_gwbridge     bridge              local
aabe3d541f65        host                host                local
8xbgnhdmo84i        ingress             overlay             swarm
a7a0fc0f184b        mac_net1            macvlan             local
85ed6bc9ad0b        none                null                local

[root@server1 ~]# docker run -it --name vm1 --net mac_net1 --ip 172.25.1.10 ubuntu
root@4f333ebdc7bb:/# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
16: eth0@if2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN group default 
    link/ether 02:42:ac:19:01:0a brd ff:ff:ff:ff:ff:ff
    inet 172.25.1.10/24 brd 172.25.1.255 scope global eth0
       valid_lft forever preferred_lft forever

server2:

[root@server2 ~]# docker network create -d  macvlan --subnet 172.25.1.0/24 --gateway 172.25.1.1 -o parent=eth0 mac_net1
65605f0175a07dc9feb7966ae63ed3b50b647b8d2745885669cbbb71096fb576
[root@server2 ~]# docker run -it --name vm1 --net mac_net1 --ip 172.25.1.11 ubuntu
root@e1ed97672c79:/# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
12: eth0@if2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN group default 
    link/ether 02:42:ac:19:01:0b brd ff:ff:ff:ff:ff:ff
    inet 172.25.1.11/24 brd 172.25.1.255 scope global eth0
       valid_lft forever preferred_lft forever
root@e1ed97672c79:/# ping 172.25.1.10     ##可以ping通,则互联成功
PING 172.25.1.10 (172.25.1.10) 56(84) bytes of data.
64 bytes from 172.25.1.10: icmp_seq=1 ttl=64 time=0.683 ms

在这里插入图片描述

再添加一块网卡
server1:

[root@server1 ~]# ip link set up eth1
[root@server1 ~]# ip link set eth1 promisc on
[root@sevrer2 mnt]# ip link set up eth1
[root@sevrer2 mnt]# ip link set eth1 promisc on

[root@server1 ~]# docker network create -d  macvlan --subnet 172.25.2.0/24 --gateway 172.25.2.1 -o parent=eth1 mac_net2

[root@server1 ~]# docker run -it --name vm2 --net mac_net2 --ip 172.25.2.10 ubuntu
root@282e8ddf9c14:/# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
17: eth0@if15: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN group default 
    link/ether 02:42:ac:19:02:0a brd ff:ff:ff:ff:ff:ff
    inet 172.25.2.10/24 brd 172.25.2.255 scope global eth0
       valid_lft forever preferred_lft forever

server2:

[root@server2 ~]# docker network create -d  macvlan --subnet 172.25.2.0/24 --gateway 172.25.2.1 -o parent=eth1 mac_net2
[root@server2 ~]# docker run -it --name vm2 --net mac_net2 --ip 172.25.2.11 ubuntu

在这里插入图片描述
如何解决?
server1:

[root@server1 ~]# docker network create -d  macvlan --subnet 172.25.3.0/24 --gateway 172.25.3.1 -o parent=eth1.1 mac_net3
8c1ffd97639e45ff9f4b73c61e83355bf91cba9ff73052811657706db1cad167
[root@server1 ~]# docker run -it --name vm3 --net mac_net3 --ip 172.25.3.10 ubuntu

server2:

[root@server2 ~]# docker network create -d  macvlan --subnet 172.25.3.0/24 --gateway 172.25.3.1 -o parent=eth1.1 mac_net3
[root@server2 ~]# docker run -it --name vm3 --net mac_net3 --ip 172.25.3.11 ubuntu
root@e1ed97672c79:/# ping 172.25.3.10      
PING 172.25.1.10 (172.25.1.10) 56(84) bytes of data.
64 bytes from 172.25.1.10: icmp_seq=1 ttl=64 time=0.683 ms
root@e1ed97672c79:/# ping 172.25.2.10    
PING 172.25.1.10 (172.25.1.10) 56(84) bytes of data.
64 bytes from 172.25.1.10: icmp_seq=1 ttl=64 time=0.683 ms

再次测试容器内网络都可以互通了

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页