(八)创建 macvlan 网络

上一节我们准备好了 macvlan 的实验环境,今天在 host1 和 host2 中创建 macvlan 网络 mac_net1:

root@host1:~# docker network create -d macvlan --subnet=172.16.86.0/24 --gateway=172.16.86.1 -o parent=ens38 mac_net1
3ed6d6fe61b21271575c9edfe1f1e8a7991d8428939d301d1c4d71559d5bece8

注意:在 host2 中也要执行相同的命令

-d macvlan 指定 driver 为 macvlan。

② macvlan 网络是 local 网络,为了保证跨主机能够通信,用户需要自己管理 IP subnet。

③ 与其他网络不同,docker 不会为 macvlan 创建网关,这里的网关应该是真实存在的,否则容器无法路由。

-o parent 指定使用的网络 interface。

在 host1 中运行容器 bbox10 并连接到 mac_net1。

root@host1:~# docker run -itd --name bbox10 --ip=172.16.86.100 --network mac_net1 busybox
1e5dbb897e7e8b0dc3d9e8f92ceb4face61c96e0495691e3a2c8faee47836883
root@host1:~# 

由于 host1 中的 mac_net1 与 host2 中的 mac_net1 本质上是独立的,为了避免自动分配造成 IP 冲突,我们最好通过 --ip 指定 bbox10 地址为 172.16.86.100。

在 host2 中运行容器 bbox11,指定 IP 172.16.86.11。

root@host2:~# docker run -itd --name bbox11 --ip=172.16.86.11 --network mac_net1 busybox
812e38fd15a355de13d7705b6e9ea001cc7e34b290e73c4ed320e010b3c49bd7
root@host2:~# 

验证 bbox11 和 bbox10 的连通性。

root@host2:~# docker exec bbox11 ping -c 2 172.16.86.100
PING 172.16.86.10 (172.16.86.10): 56 data bytes
64 bytes from 172.16.86.100: seq=0 ttl=64 time=0.584 ms
64 bytes from 172.16.86.100: seq=1 ttl=64 time=0.396 ms

--- 172.16.86.10 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.396/0.490/0.584 ms
root@host2:~# 

bbox2 能够 ping 到 bbox10 的 IP 172.16.86.100,但无法解析 “bbox10” 主机名。

root@host2:~# docker exec bbox11 ping -c 2 bbox10
ping: bad address 'bbox100'
root@host2:~# 

可见 docker 没有为 macvlan 提供 DNS 服务,这点与 overlay 网络是不同的。

内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/cuiyongchao007/p/14102459.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!