主要参考每天5分钟玩转docker容器技术博客。
记录下自己的实验过程。
如果想学习,请直接参考该博客http://www.cnblogs.com/CloudMan6/

实验环境准备

我们使用host2与host3上单独的网卡enp0s8创建macvlan。
为保证多个 MAC 地址的网络包都可以从 enp0s9 通过,我们需要打开网卡的混杂模式。
混杂模式简介
clipboard.png

首先在/etc/network/interfaces文件中添加如下内容

auto enp0s8
iface enp0s8 inet dhcp

确认网卡PROMISC已经生效,且网卡up

root@host2:~# ip link set enp0s8 promisc on
root@host2:~# /etc/init.d/networking restart
[ ok ] Restarting networking (via systemctl): networking.service.
root@host2:~# ip a |grep -A3 enp0s8
3: enp0s8: <BROADCAST,MULTICAST,PROMISC,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:43:fd:30 brd ff:ff:ff:ff:ff:ff
    inet 192.168.56.101/24 brd 192.168.56.255 scope global enp0s8
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe43:fd30/64 scope link 
       valid_lft forever preferred_lft forever

创建macvlan网络

在host2host3上执行下面的语句

root@host2:~# docker network create -d macvlan \
>                    --subnet=172.16.86.0/24   \
>                    --gateway=172.16.86.1     \
>                    -o parent=enp0s8 mac_net1
9e831209515c38d091f7fc10265fed7f995f49ec6acd9cb22c7b2c83c5819d97

在host2运行容器bbox1 并连接至mac_net1

root@host2:~# docker run -itd --name bbox1 --ip=172.16.86.10 --network mac_net1 busybox
06d1244098d1d0a8eac41cdc8505d6f145b86b55161603014c2247bd84cae939

在host3中运行bbox2, 并连接至mac_net2

root@host3:~# docker run -itd --name bbox2 --ip=172.16.86.11 --network mac_net1 busybox
b033f3ad107eeb18e93f130bd1add171874b9ad54325f5d24ff58ac79cac183d

为了避免自动分配造成ip冲突,创建时最好指定ip

验证连通性

root@host2:~# docker exec -it bbox1 ping -c 2 172.16.86.10
PING 172.16.86.10 (172.16.86.10): 56 data bytes
64 bytes from 172.16.86.10: seq=0 ttl=64 time=0.108 ms
^C
--- 172.16.86.10 ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 0.108/0.108/0.108 ms
root@host2:~# docker exec -it bbox1 ping -c 2 172.16.86.11
PING 172.16.86.11 (172.16.86.11): 56 data bytes
64 bytes from 172.16.86.11: seq=0 ttl=64 time=1.345 ms
64 bytes from 172.16.86.11: seq=1 ttl=64 time=1.116 ms

--- 172.16.86.11 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 1.116/1.230/1.345 ms

但是无法解析主机名


felix0913
27 声望1 粉丝