Docker的MacVLAN方法实现跨主机容器之间的互联

前期准备
主机1:10.10.10.20
主机2:10.10.10.22
下载docker及其依赖
安装Docker的依赖库
yum install -y yum-utils device-mapper-persistent-data lvm2
添加Docker CE的软件源信息
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
安装Docker CE
yum makecache fast
yum -y install docker-ce
启动docker服务
systemctl start docker
主机1:创建maxvlan

[root@base-vm dockerjdk8]#  docker network create --driver macvlan --subnet 10.10.10.0/24 --gatway 10.10.10.254 -o parent=eth0 macvlantest
[root@base-vm dockerjdk8]# docker network ls
NETWORK ID     NAME          DRIVER    SCOPE
5b562dc6924a   bridge        bridge    local
006d4e7bd5e7   host          host      local
d4840d5e6aa7   macvlantest   macvlan   local
c5a276d9a8fd   none          null      local

主机2:创建maxvlan

[root@test ~]# docker network create --driver macvlan --subnet 10.10.10.0/24 --gateway 10.10.10.254 -o parent=ens33 macvlantest
f7c54e2edcd57e71d35bd0a2be0fd2a5fb92d8bc74127a164f871b8b3b53b49b
[root@test ~]# docker network ls
NETWORK ID     NAME          DRIVER    SCOPE
a8e8ae2928ee   bridge        bridge    local
e1f24b79ea56   host          host      local
f7c54e2edcd5   macvlantest   macvlan   local
6888a9f1ddf1   none          null      local

主机1:运行容器

[root@base-vm dockerjdk8]# docker run -itd --name centos1 --net macvlantest --ip 10.10.10.33 centos:v1
564ca65e78ceb6614e540a2f75bb3f6b95c73919a58adeea1ad0ef01c9dd7b46
[root@base-vm dockerjdk8]# docker run -itd --name centos2 --net macvlantest --ip 10.10.10.34 centos:v1
9e1a6e9cd9624ffd058a29f5359d046bcebafee7bd7267b1cca10ea06e28a23b
[root@base-vm dockerjdk8]# docker ps
CONTAINER ID   IMAGE       COMMAND       CREATED          STATUS          PORTS     NAMES
9e1a6e9cd962   centos:v1   "/bin/bash"   17 minutes ago   Up 17 minutes             centos2
564ca65e78ce   centos:v1   "/bin/bash"   18 minutes ago   Up 18 minutes             centos1

主机2:运行容器

[root@test ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@test ~]# docker run -itd --name centos3 --net macvlantest --ip 10.10.10.55 centos:v1
1ad49521d90fd34ccf6045193b7187cd11b7a258be847556244356088c3d0cfa
[root@test ~]# docker run -itd --name centos4 --net macvlantest --ip 10.10.10.66 centos:v1
d55ad8dcb6449e80682d4e76ef471d623122706a8d82f78bb2aef8abcbbcc264
[root@test ~]# docker ps
CONTAINER ID   IMAGE       COMMAND       CREATED          STATUS          PORTS     NAMES
d55ad8dcb644   centos:v1   "/bin/bash"   4 seconds ago    Up 3 seconds              centos4
1ad49521d90f   centos:v1   "/bin/bash"   54 seconds ago   Up 53 seconds             centos3

主机1:进入容器测试通信

[root@base-vm dockerjdk8]# docker exec -it centos1 /bin/bash
[root@564ca65e78ce /]# ifconfig
eth0: flags=4163  mtu 1500
        inet 10.10.10.33  netmask 255.255.255.0  broadcast 10.10.10.255
        ether 02:42:0a:0a:0a:21  txqueuelen 0  (Ethernet)
        RX packets 6  bytes 436 (436.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4  bytes 280 (280.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73  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@564ca65e78ce /]# ping 10.10.10.55
PING 10.10.10.55 (10.10.10.55) 56(84) bytes of data.
64 bytes from 10.10.10.55: icmp_seq=1 ttl=64 time=0.515 ms
64 bytes from 10.10.10.55: icmp_seq=2 ttl=64 time=0.320 ms
64 bytes from 10.10.10.55: icmp_seq=3 ttl=64 time=0.330 ms
^C
[root@base-vm dockerjdk8]# docker exec -it centos2 /bin/bash
[root@9e1a6e9cd962 /]# ifconfig
eth0: flags=4163  mtu 1500
        inet 10.10.10.34  netmask 255.255.255.0  broadcast 10.10.10.255
        ether 02:42:0a:0a:0a:22  txqueuelen 0  (Ethernet)
        RX packets 5  bytes 300 (300.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

lo: flags=73  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@9e1a6e9cd962 /]# ping 10.10.10.55
PING 10.10.10.55 (10.10.10.55) 56(84) bytes of data.
64 bytes from 10.10.10.55: icmp_seq=1 ttl=64 time=1.18 ms
64 bytes from 10.10.10.55: icmp_seq=2 ttl=64 time=0.187 ms
64 bytes from 10.10.10.55: icmp_seq=3 ttl=64 time=0.215 ms
^C
[root@9e1a6e9cd962 /]# ping 10.10.10.66
PING 10.10.10.66 (10.10.10.66) 56(84) bytes of data.
64 bytes from 10.10.10.66: icmp_seq=1 ttl=64 time=0.607 ms
64 bytes from 10.10.10.66: icmp_seq=2 ttl=64 time=0.169 ms
64 bytes from 10.10.10.66: icmp_seq=3 ttl=64 time=0.244 ms
64 bytes from 10.10.10.66: icmp_seq=4 ttl=64 time=0.282 ms

你可能感兴趣的:(docker,centos,运维,linux)