Linux 不同network namespace 之间通信方式

9 篇文章 0 订阅
7 篇文章 1 订阅

在云平台中,网络实现网元与外界通信,而对于虚拟机或容器来说,其没有物理网卡,需要通过虚拟网卡与外界通过。在Linux环境中,network namespace实现了网络资源的隔离,它可以为网元提供网络设备(网卡)、ip配置等。下面我们来看看Linux环境下network namespace如何工作的。

network namespaces

network namespaces主要提供了关于网络资源的隔离,包括:网络设备、IPV4/IPV6协议栈、IP路由表、防火墙、/proc/net目录、/sys/class/net目录、套接字(socket)等

一个物理的网络设备最多存在于一个network namespace中,可通过veth-pair连接两个network namespace

默认情况下,物理网络设备都分配在最初的root namespace中

添加网络设备实践

下面与容器场景为例,我们创建两个container,两个container之间通过veth-pair(以太对)通信。对于虚拟机的场景也使用,只需要将container换成vm即可。

 

1. 如何为容器添加虚拟网卡(veth)

添加veth-pair到container

# 将容器的 network namespace 映射到主机中,从而我们可以直接在host上操作容器的 network namespace
# 其中 13816 为容器的 pid
mkdir /var/run/netns
ln -s /proc/13816/ns/net /var/run/netns/13816

# 将 veth0 放入容器中
ip link set veth0 netns 13816 up
ip netns exec 13816 ip a

2. 不同network namespace间通信:

2.1 利用简单的 veth-pair 实现通信

# 创建两个network namespace: ns1 和 ns2
ip netns add ns1
ip netns add ns2

# 创建一对以太对 tap1 <--> tap2
ip link add tap1 type veth peer name tap2

# 将 veth 放入 ns 中, 其中 tap1 放入 ns1 中, tap2 放入 ns2 中
ip link set tap1 netns ns1
ip link set tap2 netns ns2

# 使能 tap 口
ip netns exec ns1 ip link set dev tap1 up
ip netns exec ns2 ip link set dev tap2 up

# 为 tap 口配置 ip
ip netns exec ns1  ip addr add 10.0.0.1/24 dev tap1
ip netns exec ns2  ip addr add 10.0.0.2/24 dev tap2

 

2.2  利用Linux bridge实现多个veth pair互连


 

# 创建network namespace: ns1 、ns2
ip netns add ns1
ip netns add ns2

# 创建 linuxBridge
BRIDGE=br-test
brctl addbr $BRIDGE
brctl stp   $BRIDGE off
ip link set dev $BRIDGE up

##### PORT 1
# 创建第一对以太对,然后一端放入容器中,另一端挂在Linuxbridge上
ip link add tap1 type veth peer name br-tap1

# 一端挂在Linuxbridge上
brctl addif br-test br-tap1

# 一端放入容器中
ip link set tap1 netns ns1

# 使能 tap 口
ip netns exec ns1 ip link set dev tap1 up
ip link set dev br-tap1 up

##### PORT 2 与 PORT 1 操作一样
ip link add tap2 type veth peer name br-tap2
brctl addif br-test br-tap2
ip link set tap2 netns ns2
ip netns exec ns2 ip link set dev tap2 up
ip link set dev br-tap2 up

# 配置 ip
ip netns  exec  ns1  ip addr add 10.0.0.1/24 dev tap1
ip netns  exec  ns2  ip addr add 10.0.0.2/24 dev tap2

 

2.3. 采用ovs代替Linuxbridge,同样利用veth pair

# 创建network namespace: ns1 、ns2
ip netns add ns1
ip netns add ns2

# 创建 ovs 网桥
BRIDGE=ovs-test
ovs-vsctl add-br $BRIDGE

##### PORT 1
# 创建第一对以太对,然后一端放入容器中,另一端挂在 ovs 网桥上
ip link add tap1 type veth peer name ovs-tap1

# 一端挂在 ovs 网桥上
ovs-vsctl add-port $BRIDGE ovs-tap1

# 一端放入容器中
ip link set tap1 netns ns1

# 使能 tap 口
ip netns exec ns1 ip link set dev tap1 up
ip link set dev ovs-tap1 up

##### PORT 2 (与 PORT 1 一样)与
ip link add tap2 type veth peer name ovs-tap2
ovs-vsctl add-port $BRIDGE ovs-tap2
ip link set tap2 netns ns2
ip netns exec ns2 ip link set dev tap2 up
ip link set dev ovs-tap2 up

# 配置 ip
ip netns  exec  ns1  ip addr add 10.0.0.1/24 dev tap1
ip netns  exec  ns2  ip addr add 10.0.0.2/24 dev tap2

 

2.4. 利用ovs,但这次用ovs的 internal port,此时不需要veth pair

# 创建network namespace: ns1、 ns2
ip netns add ns1
ip netns add ns2

# 创建 ovs 网桥
BRIDGE=ovs-test
ovs-vsctl add-br $BRIDGE

##### PORT 1
# 创建 ovs internal 端口
ovs-vsctl add-port $BRIDGE tap1 -- set Interface tap1 type=internal

# 将端口放入容器 ns 中
ip link set tap1 netns ns1

# 使能端口
ip netns exec ns1 ip link set dev tap1 up

##### PORT 2 (与 PORT 1 一样)
ovs-vsctl add-port $BRIDGE tap2 -- set Interface tap2 type=internal
ip link set tap2 netns ns2
ip netns exec ns2 ip link set dev tap2 up

# 配置 ip
ip netns  exec  ns1  ip addr add 10.0.0.1/24 dev tap1
ip netns  exec  ns2  ip addr add 10.0.0.2/24 dev tap2

 

 

 

 


 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Network NamespaceLinux内核提供的功能,用于实现网络虚拟化。它可以创建多个隔离的网络空间,每个空间都有独立的网络栈信息,使得虚拟机或容器在运行时仿佛处于独立的网络环境中。\[1\]通过创建命名的network namespace文件,并将其与进程的/proc/self/ns/net文件绑定,可以实现对容器的网络命名空间进行查看和管理。\[2\]Network Namespace的作用是隔离Linux系统的网络资源,包括设备、IP地址、端口、路由表、防火墙规则等。每个network namespace都有自己独立的网络设备和网络配置信息。在跨network namespace之间进行通信时,常常使用veth pair(虚拟以太网卡对)来连接不同namespace。\[3\] #### 引用[.reference_title] - *1* [ip netns的使用及network namespace 简介](https://blog.csdn.net/hbuxiaofei/article/details/107116064)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [理解linux network namespace](https://blog.csdn.net/u014634338/article/details/119343985)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Network Namespace与ipip](https://blog.csdn.net/weixin_43394724/article/details/121577473)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值