Docker Macvlan网络创建及通信配置

环境说明

shell 复制代码
4: bond0: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 7c:83:34:bc:e0:c2 brd ff:ff:ff:ff:ff:ff
    inet 10.5.1.33/24 brd 10.5.1.255 scope global dynamic bond0

宿主机配置

变量配置

shell 复制代码
eth=bond0 # 宿主机网卡名称
subnet=10.5.1.0/24 # 宿主机IP网段
host_ip=10.5.1.33/32 # 宿主机IP地址
gateway=10.5.1.1 # 宿主机网关
container_ip1=10.5.1.201 # 容器1 IP
container_ip2=10.5.1.202 # 容器2 IP
docker_macvlan_name=macvlan0 # Docker新建MacVlan网络名称
host_macvlan_name=bond0s

下面命令均通过变量实现,方便理解语法

推荐提前通过变量设置好,后面直接复制粘贴,不需要修改任何内容

开启混杂模式

首先执行下面的命令查看网卡是否开启混杂模式

shell 复制代码
ip address show ${eth} | grep PROMISC

如果有输出就是开启了,例如

shell 复制代码
root@EQ12-Debian:~# ip address show  ${eth} | grep PROMISC
4: bond0: <BROADCAST,MULTICAST,PROMISC,MASTER,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000

如果没有开启,可以使用下面的命令打开

shell 复制代码
ip link set ${eth} promisc on

开启路由转发

shell 复制代码
echo -e "net.ipv4.ip_forward=1\nvm.max_map_count=655360" > /etc/sysctl.conf;sysctl -p

Docker配置

网络创建

shell 复制代码
docker network create --driver macvlan --subnet=${subnet} --gateway=${gateway} -o parent=${eth} ${docker_macvlan_name}

创建容器

shell 复制代码
docker run -tid --name ubuntu --net=${docker_macvlan_name} --ip=${container_ip1} liuyi778/ubuntu-22.04_pip3 /bin/bash
shell 复制代码
docker run -tid --name ubuntu2 --net=${docker_macvlan_name} --ip=${container_ip2} liuyi778/ubuntu-22.04_pip3 /bin/bash

路由配置

shell 复制代码
ip link add ${host_macvlan_name} link ${eth} type macvlan mode bridge
ip addr add ${ip} dev ${name}
ip link set ${host_macvlan_name} up
ip route add ${container_ip1} dev ${host_macvlan_name}
ip route add ${container_ip2} dev ${host_macvlan_name}

实践操作

shell 复制代码
root@EQ12-Debian:~# eth=bond0 # 宿主机网卡名称
subnet=10.5.1.0/24 # 宿主机IP网段
host_ip=10.5.1.33/32 # 宿主机IP地址
gateway=10.5.1.1 # 宿主机网关
container_ip1=10.5.1.201 # 容器1 IP
container_ip2=10.5.1.202 # 容器2 IP
docker_macvlan_name=macvlan0 # Docker新建MacVlan网络名称
host_macvlan_name=bond0s
root@EQ12-Debian:~# 
root@EQ12-Debian:~# ip address show ${eth} | grep PROMISC
4: bond0: <BROADCAST,MULTICAST,PROMISC,MASTER,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
root@EQ12-Debian:~# echo -e "net.ipv4.ip_forward=1\nvm.max_map_count=655360" > /etc/sysctl.conf;sysctl -p
net.ipv4.ip_forward = 1
vm.max_map_count = 655360
root@EQ12-Debian:~# docker network ls
NETWORK ID     NAME                DRIVER    SCOPE
61f710e81024   bridge              bridge    local
91c66270dbaf   data_halo_network   bridge    local
1f6d72d10049   host                host      local
d5efc9813726   none                null      local
root@EQ12-Debian:~# docker network create --driver macvlan --subnet=${subnet} --gateway=${gateway} -o parent=${eth} ${docker_macvlan_name}
6ca2023e3224149530b4a6653055135fa6a40af94b7de4ad6e8ecd4ab452e432
root@EQ12-Debian:~# docker run -tid --name ubuntu --net=${docker_macvlan_name} --ip=${container_ip1} liuyi778/ubuntu-22.04_pip3 /bin/bash
f0b8195e949892ae91d974da55fcc33d88e0bb9eb0f571c688bbd28cc6493ccf
root@EQ12-Debian:~# docker run -tid --name ubuntu2 --net=${docker_macvlan_name} --ip=${container_ip2} liuyi778/ubuntu-22.04_pip3 /bin/bash
995e585e4d83272e4a36dc7c39e1e8178d0bfe5c222671eb003a09b05e336ee6
root@EQ12-Debian:~# ip link add ${host_macvlan_name} link ${eth} type macvlan mode bridge
ip addr add ${ip} dev ${name}
ip link set ${host_macvlan_name} up
ip route add ${container_ip1} dev ${host_macvlan_name}
ip route add ${container_ip2} dev ${host_macvlan_name}
Command line is not complete. Try option "help"
root@EQ12-Debian:~# docker exec -ti ubuntu
ubuntu   ubuntu2  
root@EQ12-Debian:~# docker exec -ti ubuntu /bin/bash
root@f0b8195e9498:~# apt install net-tools
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
  net-tools
0 upgraded, 1 newly installed, 0 to remove and 3 not upgraded.
Need to get 204 kB of archives.
After this operation, 819 kB of additional disk space will be used.
Get:1 http://mirrors.tencent.com/ubuntu jammy/main amd64 net-tools amd64 1.60+git20181103.0eebece-1ubuntu5 [204 kB]
Fetched 204 kB in 0s (928 kB/s)
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package net-tools.
(Reading database ... 18038 files and directories currently installed.)
Preparing to unpack .../net-tools_1.60+git20181103.0eebece-1ubuntu5_amd64.deb ...
Unpacking net-tools (1.60+git20181103.0eebece-1ubuntu5) ...
Setting up net-tools (1.60+git20181103.0eebece-1ubuntu5) ...
root@f0b8195e9498:~# ifconfig 
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.5.1.201  netmask 255.255.255.0  broadcast 10.5.1.255
        ether 02:42:0a:05:01:c9  txqueuelen 0  (Ethernet)
        RX packets 74  bytes 209395 (209.3 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 55  bytes 3258 (3.2 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 4  bytes 597 (597.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4  bytes 597 (597.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

root@f0b8195e9498:~# 
exit
root@EQ12-Debian:~# ping  10.5.1.201
PING 10.5.1.201 (10.5.1.201) 56(84) bytes of data.
64 bytes from 10.5.1.201: icmp_seq=1 ttl=64 time=0.150 ms
64 bytes from 10.5.1.201: icmp_seq=2 ttl=64 time=0.046 ms
64 bytes from 10.5.1.201: icmp_seq=3 ttl=64 time=0.050 ms
64 bytes from 10.5.1.201: icmp_seq=4 ttl=64 time=0.055 ms
^C
--- 10.5.1.201 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3002ms
rtt min/avg/max/mdev = 0.046/0.075/0.150/0.043 ms
root@EQ12-Debian:~# 
相关推荐
仓鼠OO2 小时前
思科交换机telnet配置案例
网络·思科·远程
Andya_net2 小时前
网络安全 | F5-Attack Signatures-Set详解
网络·数据库·web安全
Zfox_3 小时前
HTTP cookie 与 session
linux·服务器·网络·c++·网络协议·http
余额很不足3 小时前
K8S知识点
linux·容器·kubernetes
栗子~~4 小时前
docker-compose的方式搭建 kafka KRaft 模式集群
docker·kafka·linq
周杰伦_Jay5 小时前
详细介绍:持续集成与持续部署(CI/CD)技术细节(关键实践、CI/CD管道、优势与挑战)
程序人生·ci/cd·docker·微服务·云原生·容器·人机交互
骑台风走9 小时前
ubunut22.04安装docker(基于阿里云 Docker 镜像源安装 Docker)
阿里云·docker·容器
仇辉攻防10 小时前
【云安全】云原生-Docker(五)容器逃逸之漏洞利用
安全·web安全·网络安全·docker·云原生·容器·安全性测试
qq_3927944811 小时前
为什么 TCP 挥手需要有 TIME_WAIT 状态?
网络·网络协议·tcp/ip
闲猫12 小时前
正向代理(动态 IP 代理)和反向代理
网络·网络协议·tcp/ip