Docker Macvlan网络创建及通信配置

2023-11-05 04:01

本文主要是介绍Docker Macvlan网络创建及通信配置,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

环境说明

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

宿主机配置

变量配置

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

下面命令均通过变量实现,方便理解语法
推荐提前通过变量设置好,后面直接复制粘贴,不需要修改任何内容

开启混杂模式

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

ip address show ${eth} | grep PROMISC

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

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

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

ip link set ${eth} promisc on

开启路由转发

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

Docker配置

网络创建

docker network create --driver macvlan --subnet=${subnet} --gateway=${gateway} -o parent=${eth} ${docker_macvlan_name}

创建容器

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

路由配置

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}

实践操作

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 1500inet 10.5.1.201  netmask 255.255.255.0  broadcast 10.5.1.255ether 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 0TX packets 55  bytes 3258 (3.2 KB)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536inet 127.0.0.1  netmask 255.0.0.0loop  txqueuelen 1000  (Local Loopback)RX packets 4  bytes 597 (597.0 B)RX errors 0  dropped 0  overruns 0  frame 0TX packets 4  bytes 597 (597.0 B)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0root@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:~# 

这篇关于Docker Macvlan网络创建及通信配置的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/347295

相关文章

Jenkins分布式集群配置方式

《Jenkins分布式集群配置方式》:本文主要介绍Jenkins分布式集群配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装jenkins2.配置集群总结Jenkins是一个开源项目,它提供了一个容易使用的持续集成系统,并且提供了大量的plugin满

SpringBoot线程池配置使用示例详解

《SpringBoot线程池配置使用示例详解》SpringBoot集成@Async注解,支持线程池参数配置(核心数、队列容量、拒绝策略等)及生命周期管理,结合监控与任务装饰器,提升异步处理效率与系统... 目录一、核心特性二、添加依赖三、参数详解四、配置线程池五、应用实践代码说明拒绝策略(Rejected

使用Docker构建Python Flask程序的详细教程

《使用Docker构建PythonFlask程序的详细教程》在当今的软件开发领域,容器化技术正变得越来越流行,而Docker无疑是其中的佼佼者,本文我们就来聊聊如何使用Docker构建一个简单的Py... 目录引言一、准备工作二、创建 Flask 应用程序三、创建 dockerfile四、构建 Docker

SpringBoot+Docker+Graylog 如何让错误自动报警

《SpringBoot+Docker+Graylog如何让错误自动报警》SpringBoot默认使用SLF4J与Logback,支持多日志级别和配置方式,可输出到控制台、文件及远程服务器,集成ELK... 目录01 Spring Boot 默认日志框架解析02 Spring Boot 日志级别详解03 Sp

SQL Server配置管理器无法打开的四种解决方法

《SQLServer配置管理器无法打开的四种解决方法》本文总结了SQLServer配置管理器无法打开的四种解决方法,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录方法一:桌面图标进入方法二:运行窗口进入检查版本号对照表php方法三:查找文件路径方法四:检查 S

Linux中压缩、网络传输与系统监控工具的使用完整指南

《Linux中压缩、网络传输与系统监控工具的使用完整指南》在Linux系统管理中,压缩与传输工具是数据备份和远程协作的桥梁,而系统监控工具则是保障服务器稳定运行的眼睛,下面小编就来和大家详细介绍一下它... 目录引言一、压缩与解压:数据存储与传输的优化核心1. zip/unzip:通用压缩格式的便捷操作2.

java实现docker镜像上传到harbor仓库的方式

《java实现docker镜像上传到harbor仓库的方式》:本文主要介绍java实现docker镜像上传到harbor仓库的方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 前 言2. 编写工具类2.1 引入依赖包2.2 使用当前服务器的docker环境推送镜像2.2

Linux中SSH服务配置的全面指南

《Linux中SSH服务配置的全面指南》作为网络安全工程师,SSH(SecureShell)服务的安全配置是我们日常工作中不可忽视的重要环节,本文将从基础配置到高级安全加固,全面解析SSH服务的各项参... 目录概述基础配置详解端口与监听设置主机密钥配置认证机制强化禁用密码认证禁止root直接登录实现双因素

嵌入式数据库SQLite 3配置使用讲解

《嵌入式数据库SQLite3配置使用讲解》本文强调嵌入式项目中SQLite3数据库的重要性,因其零配置、轻量级、跨平台及事务处理特性,可保障数据溯源与责任明确,详细讲解安装配置、基础语法及SQLit... 目录0、惨痛教训1、SQLite3环境配置(1)、下载安装SQLite库(2)、解压下载的文件(3)、

python如何创建等差数列

《python如何创建等差数列》:本文主要介绍python如何创建等差数列的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录python创建等差数列例题运行代码回车输出结果总结python创建等差数列import numpy as np x=int(in