docker部署Macvlan实现跨主机网络通信的实现

2020-06-17 07:00:37易采站长站整理

(3)启动子网卡:


[root@sqm-docker01 network-scripts]# ifup ifcfg-ens33.10
[root@sqm-docker01 network-scripts]# ifup ifcfg-ens33.20


//查看网络信息
[root@sqm-docker01 network-scripts]# ifconfig

(4)基于ens33.10和ens33.20创建macvlan网络:

注意:网段不同,网络名称不同


[root@sqm-docker01 ~]# docker network create -d macvlan --subnet 172.16.200.0/24 --gateway 172.16.200.1 -o parent=ens33.10 mac_net10

[root@sqm-docker01 ~]# docker network create -d macvlan --subnet 172.16.210.0/24 --gateway 172.16.210.1 -o parent=ens33.20 mac_net20

(5)基于以上网络分别运行2个容器:


[root@sqm-docker01 ~]# docker run -itd --name test1 --ip 172.16.200.10 --network mac_net10 busybox

[root@sqm-docker01 ~]# docker run -itd --name test2 --ip 172.16.210.10 --network mac_net20 busybox

部署docker02:

基本与docker01操作相同,注意网段的相同,但主机ip得不同。

#以下操作将不做解释:


开启路由转发:
[root@sqm-docker01 ~]# echo "net.ipv4.ip_forward = 1" > /etc/sysctl.conf
[root@sqm-docker01 ~]# sysctl -p
net.ipv4.ip_forward = 1


[root@sqm-docker02 network-scripts]# pwd
/etc/sysconfig/network-scripts
[root@sqm-docker02 network-scripts]# vim ifcfg-ens33


[root@sqm-docker02 network-scripts]# cp -p ifcfg-ens33 ifcfg-ens33.10
[root@sqm-docker02 network-scripts]# cp -p ifcfg-ens33 ifcfg-ens33.20


[root@sqm-docker02 network-scripts]# vim ifcfg-ens33.10


[root@sqm-docker02 network-scripts]# vim ifcfg-ens33.20

[root@sqm-docker02 network-scripts]# ifup ifcfg-ens33.10 [root@sqm-docker02 network-scripts]# ifup ifcfg-ens33.20

//创建macvlan网络:[root@sqm-docker02 ~]# docker network create -d macvlan --subnet 172.16.200.0/24 --gateway 172.16.200.1 -o parent=ens33.10 mac_net10[root@sqm-docker02 ~]# docker network create -d macvlan --subnet 172.16.210.0/24 --gateway 172.16.210.1 -o parent=ens33.20 mac_net20

//运行容器(ip地址不同):[root@sqm-docker02 ~]# docker run -itd --name test3 --network mac_net10 --ip 172.16.200.11 busybox[root@sqm-docker02 ~]# docker run -itd --name test4 --network mac_net20 --ip 172.16.210.11 busybox