易陆发现互联网技术论坛

 找回密码
 开始注册
查看: 3789|回复: 0
收起左侧

Linux 网络虚拟化ip netns

[复制链接]
发表于 2019-6-21 15:02:52 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?开始注册

x

netns 可以创建一个完全隔离的新网络环境,这个环境包括一个独立的网卡空间,路由表,ARP表,ip地址表,iptables等。总之,与网络有关的组件都是独立的。: D, \! K7 \  |- c5 x
3 Z9 S  i* X. U! X) H1 c; ^
创建网络空间:

# ip netns add ns1
" L" \, m9 i3 N- l$ Q! T6 _

查看网络空间:

# ip netns list
/ G  H- O# r7 s: ?

删除网络空间:

# ip netns del ns10 n* ]- l" ?6 K+ ]% |! v

进入网络空间执行命令:

# ip netns exec ns1 `command`0 i5 l: Q; d3 e) [

: i7 `/ Q& v$ w! B: S

实例一:
( X+ K9 n3 h( @7 Y/ S: R    用netns连接两个隔离环境中的虚拟机,如图:


0 W6 x# c/ C' ^$ O

    在虚拟化中有两个虚拟机网络隔离环境需要通信。

系统: centos7.2 x64

安装程序包# yum install bridge-utils libvirt libvirt-client virt-install virt-viewer net-tools -y# brctl addbr br0# brctl addbr br1# ifconfig br0 up# ifconfig br1 up0 f. I% ]8 f0 R$ A$ k0 v8 k
取消默认nat网络模式# mv /etc/libvirt/qemu/networks/default.xml /etc/libvirt/qemu/networks/default.xml_bak# systemctl start libvirtd
! E# O2 H" M- X0 {创建虚拟机并连接至br0# virt-install --name vm1 --ram 512 --vcpus=1 --disk /images/linux/cirros-0.3.5-i386-disk-1.img --network bridge=br0,model=virtio --force --import --nographics --serial=pty --console=pty打开第二个终端创建第二个虚拟机并连接至br1# virt-install --name vm2 --ram 512 --vcpus=1 --disk /images/linux/cirros-0.3.5-i386-disk-2.img --network bridge=br1,model=virtio --force --import --nographics --serial=pty --console=pty# brctl showbridge name    bridge id        STP enabled    interfacesbr0        8000.fe54007e1861    no        vnet0br1        8000.fe5400be1885    no        vnet1' r* A. t( s4 G4 E% P! y

到此,虚拟机已经连接上各自的桥设备了。完成如图:

* O# e: N- k$ I
创建虚拟网络空间:# ip netns add ns1# ip netns listns1
; o4 R! ?7 t" ?

接下来创建一张虚拟网卡,虚拟网卡分为前半段和后半段,我们将前半段添加到br0中,并将后半段添加到虚拟网络空间中,这样br0桥设备中主机就能够连接到虚拟网络空间中。

# ip link add net-in type veth peer name net-out# ifconfig net-in up# ifconfig net-out up4 {% _, z1 S' @7 j. g

将net-in虚拟网卡添加到br0中,将net-out虚拟网卡添加到ns1中

# brctl addif br0 net-in查看是否添加成功# brctl show br0bridge name    bridge id        STP enabled    interfacesbr0        8000.46c7e9d2c0fa    no            net-in                                        vnet0
* ?; A2 x, B- w; N( J( \将net-out添加到ns1中,并重命名为eth0# ip link set dev net-out name eth0 netns ns1! W& L  n2 {2 l5 M
查看是否添加成功# ip netns exec ns1 ifconfig -aeth0: flags=4098<BROADCAST,MULTICAST>  mtu 1500        ether a2:07:dc:ba:35:a2  txqueuelen 1000  (Ethernet)        RX packets 0  bytes 0 (0.0 B)        RX errors 0  dropped 0  overruns 0  frame 0        TX packets 0  bytes 0 (0.0 B)        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0lo: flags=8<LOOPBACK>  mtu 65536        loop  txqueuelen 0  (Local Loopback)        RX packets 0  bytes 0 (0.0 B)        RX errors 0  dropped 0  overruns 0  frame 0        TX packets 0  bytes 0 (0.0 B)        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0# ip netns exec ns1 ifconfig lo up
4 N/ y2 f" _. @$ h* M

现在vm1 --> br0 --> ns1 网络做通了,完成如下图:


! U7 N. {6 z. @( u% X5 {( U4 R

同理,和上面操作一样。

# ip link add net1-in type veth peer name net1-out# ifconfig net1-in up# ifconfig net1-out up# brctl addif br1 net1-in# brctl show br1bridge name    bridge id        STP enabled    interfacesbr1        8000.1291a963b290    no        net1-in                            vnet1# ip link set dev net1-out name eth1 netns ns18 {; o& s6 W4 i& F

& c3 S+ }/ F/ J$ @3 L# ip netns exec ns1 ifconfig
-aeth0: flags=4098<BROADCAST,MULTICAST>  mtu 1500        ether a2:07:dc:ba:35:a2  txqueuelen 1000  (Ethernet)        RX packets 0  bytes 0 (0.0 B)        RX errors 0  dropped 0  overruns 0  frame 0        TX packets 0  bytes 0 (0.0 B)        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0eth1: flags=4098<BROADCAST,MULTICAST>  mtu 1500        ether 02:d4:3c:7d:3b:2e  txqueuelen 1000  (Ethernet)        RX packets 0  bytes 0 (0.0 B)        RX errors 0  dropped 0  overruns 0  frame 0        TX packets 0  bytes 0 (0.0 B)        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536        inet 127.0.0.1  netmask 255.0.0.0        inet6 ::1  prefixlen 128  scopeid 0x10<host>        loop  txqueuelen 0  (Local Loopback)        RX packets 0  bytes 0 (0.0 B)        RX errors 0  dropped 0  overruns 0  frame 0        TX packets 0  bytes 0 (0.0 B)        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

" k+ k9 `& T  ^9 [% b

ip地址配置如下:
) l( c# }. C& _' w* Z) M2 v* C3 G/ q. E) Q' \
vm1 - eth0 : 192.168.1.2
1 e! }. T2 Y' N* p1 Q- S: Ans1 - eth0 : 192.168.1.19 a% o" {# \+ R& x  S7 M: [6 L
) o: E9 d2 l% D. T3 _
vm2 - eth0 : 172.168.10.2/ r2 M9 R% o& h9 R# x! }* W5 r
ns1 - eth0 : 172.168.10.1

记住:当宿主机开启了网络转发功能,虚拟网络空间才会开启,在以上场景中,必须开启网络转发功能。

# sysctl -w net.ipv4.ip_forward=1net.ipv4.ip_forward = 1
4 o( m8 T; U. H) ^, y, W1 H- l) b4 n' ?; @7 l2 O0 |0 p  u

vm1 - eth0 网络配置如下:

# ifconfig lo up # ifconfig eth0 192.168.1.2/24 up# ifconfig eth0      Link encap:Ethernet  HWaddr 52:54:00:7E:18:61            inet addr:192.168.1.2  Bcast:192.168.1.255  Mask:255.255.255.0          inet6 addr: fe80::5054:ff:fe7e:1861/64 Scope:Link          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1          RX packets:8 errors:0 dropped:0 overruns:0 frame:0          TX packets:2 errors:0 dropped:0 overruns:0 carrier:0          collisions:0 txqueuelen:1000           RX bytes:648 (648.0 B)  TX bytes:168 (168.0 B)lo        Link encap:Local Loopback            inet addr:127.0.0.1  Mask:255.0.0.0          inet6 addr: ::1/128 Scope:Host          UP LOOPBACK RUNNING  MTU:16436  Metric:1          RX packets:0 errors:0 dropped:0 overruns:0 frame:0          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0          collisions:0 txqueuelen:0           RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B). I# i& y2 j/ p" o2 s5 @' K5 o

ns1 - eth0 网络配置如下:

# ip netns exec ns1 ifconfig lo up# ip netns exec ns1 ifconfig eth0 192.168.1.1/24 up# ip netns exec ns1 ifconfig eth0eth0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500        inet 192.168.1.1  netmask 255.255.255.0  broadcast 192.168.1.255        ether a2:07:dc:ba:35:a2  txqueuelen 1000  (Ethernet)        RX packets 0  bytes 0 (0.0 B)        RX errors 0  dropped 0  overruns 0  frame 0        TX packets 0  bytes 0 (0.0 B)        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
6 K9 {  ?" _" h- [
4 _( i( g9 y. |' C% r

vm2 - eth0 网络配置如下:

# ifconfig lo up# ifconfig eth0 172.168.10.2/24 up# ifconfig eth0eth0      Link encap:Ethernet  HWaddr 52:54:00:BE:18:85            inet addr:172.168.10.2  Bcast:172.168.255.255  Mask:255.255.0.0          inet6 addr: fe80::5054:ff:febe:1885/64 Scope:Link          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1          RX packets:8 errors:0 dropped:0 overruns:0 frame:0          TX packets:2 errors:0 dropped:0 overruns:0 carrier:0          collisions:0 txqueuelen:1000           RX bytes:648 (648.0 B)  TX bytes:168 (168.0 B)
. Z$ g/ j- K. s9 F1 t, N
! m# L0 o1 D, n5 e% A+ s

ns1 - eth1 网络配置如下:

# ip netns exec ns1 ifconfig eth1 172.168.10.1/24 up# ip netns exec ns1 ifconfig eth1eth1: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500        inet 172.168.10.1  netmask 255.255.255.0  broadcast 172.168.10.255        ether 02:d4:3c:7d:3b:2e  txqueuelen 1000  (Ethernet)        RX packets 0  bytes 0 (0.0 B)        RX errors 0  dropped 0  overruns 0  frame 0        TX packets 0  bytes 0 (0.0 B)        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 00 S: `; ~) B0 ?: E

+ q( j8 g( S' g# _

为虚拟机指定路由:

vm1 :# ping 192.168.1.1 -c1PING 192.168.1.1 (192.168.1.1): 56 data bytes64 bytes from 192.168.1.1: seq=0 ttl=64 time=0.811 ms--- 192.168.1.1 ping statistics ---1 packets transmitted, 1 packets received, 0% packet lossround-trip min/avg/max = 0.811/0.811/0.811 ms# ip route add default via 192.168.1.1# u6 q  S! B3 y. P* ]1 f5 v

注意:如果ping不通,请检查链路上的网卡状态是否是up状态。

vm2 :# ping  172.168.10.1 -c1PING 172.168.10.1 (172.168.10.1): 56 data bytes64 bytes from 172.168.10.1: seq=0 ttl=64 time=2.385 ms--- 172.168.10.1 ping statistics ---1 packets transmitted, 1 packets received, 0% packet lossround-trip min/avg/max = 2.385/2.385/2.385 ms添加默认路由# ip route add default via 172.168.10.1
1 L7 n, r6 {! \0 i7 o) ^6 u. n, @- |0 A$ O

接下来,使用ping测试。

vm1 - eth0 : 192.168.1.2  --> ns1 - eth1 : 172.168.10.1# ping 172.168.10.1 -c1PING 172.168.10.1 (172.168.10.1): 56 data bytes64 bytes from 172.168.10.1: seq=0 ttl=64 time=0.426 ms--- 172.168.10.1 ping statistics ---1 packets transmitted, 1 packets received, 0% packet lossround-trip min/avg/max = 0.426/0.426/0.426 ms能够达到ns1 eth1网卡,说明ns1从eth0 - 192.168.10.1 转发到了 172.168.10.1vm1 - eth0 : 192.168.1.2  --> vm2 - eth0 : 172.168.10.2
( s# U7 v7 M$ z' G9 F5 A
6 o4 r6 }* Y$ Y+ t. \& @4 M- ]# L7 S2 z6 K

这样,就完成了在宿主机中,两个虚拟主机隔离模式的通信。

" M5 a9 C- Z( L8 |9 Q" C

# ~8 S; B* n6 D

实例二:


: r4 D' D3 A9 ~9 F; k


! z8 j4 @) |3 I; _: G8 y- H    说明:宿主机中两组隔离模型,其中只有一组可以访问公网

接下来,在模式一的基础上进行修改:
$ u. L, ~  W9 G& Z
. C3 P  b; r, ]/ K5 n

# ip netns del ns1. E2 K! I. ?: R3 o! G* v

! r! O; ~, X% u2 r5 J& ^# O
删除虚拟网络空间模式,所有和虚拟网络空间有关的虚拟网卡都会被删除。

现在的模式如下:

7 ]' i6 k+ w# F7 E

vm1: 192.168.1.2/24
9 v- |: a/ s; j) }vm2: 192.168.1.2/24
9 |- u/ V+ J+ Y1 n4 t& Gns1: 192.168.1.1/24. n4 u2 }* d# r* Z
& M. `# {& s% K* r, l' h: ^
这里故意把vm1和vm2的ip设置为一样,方便我们进行测试。

添加虚拟网络空间# ip netns add ns1# ip link add net-in type veth peer name net-out# ifconfig net-in up# ifconfig net-out up+ K: D4 {3 s7 Y# E/ J
添加net-in到br0,添加net-out到虚拟网络空间ns1# brctl addif br0 net-in# ip link set dev net-out name eth0 netns ns1为ns1启动网卡并配置ip地址# ip netns exec ns1 ifconfig lo up# ip netns exec ns1 ifconfig eth0 192.168.1.1 netmask 255.255.255.0 up
% Q' H# b3 d7 H- c  @1 m( H

为vm1配置网关为192.168.1.1+ e% B0 W1 |7 R5 Z$ h1 c4 @2 K. l

8 {: \3 ^; c* W创建桥设备,并将物理网卡添加到桥设备中,这里建议直接修改物理网卡配置文件

cp -a ifcfg-eno16777736 ifcfg-br-out# vim ifcfg-eno16777736 TYPE=EthernetBOOTPROTO=noneDEFROUTE=yesPEERDNS=yesPEERROUTES=yesIPV4_FAILURE_FATAL=noIPV6INIT=noNAME=eno16777736UUID=100e462e-c0d0-4271-9b5a-1c8e47ff0d03DEVICE=eno16777736ONBOOT=yesBRIDGE=br-out# vim ifcfg-br-out TYPE=BridgeBOOTPROTO=noneDEFROUTE=yesPEERDNS=yesPEERROUTES=yesIPV4_FAILURE_FATAL=noIPV6INIT=noNAME=br-outDEVICE=br-outONBOOT=yesIPADDR=10.0.0.11NETMASK=255.255.255.0GATEWAY=10.0.0.1DNS1=10.0.0.1DNS2=114.114.114.114重启下网络# systemctl restart network物理网卡添加成功# brctl show br-outbridge name    bridge id        STP enabled    interfacesbr-out        8000.000c2923e15d    no        eno16777736
/ E( F- Z+ G* S4 J, B+ I' @5 b8 X2 i3 p8 C

现在创建一对网卡,连接ns1和br-out

$ m+ r; d% |4 q
# ip link add net1-in type veth peer name net1-out# ifconfig net1-in up# ifconfig net1-out up# ip link set dev net1-in name eth1 netns ns1# brctl addif br-out net1-out# brctl show br-outbridge name    bridge id        STP enabled    interfacesbr-out        8000.000c2923e15d    no        eno16777736                                        net1-out; _, r3 M* g: h$ o
+ N2 `1 q' V3 t" v

我真实局域网的ip为10.0.0.0/24
- ^4 ~- t" ^0 w# V7 [3 B$ j' s/ i# g/ i6 {
因此添加到ns1中的eth1要配置到同网段

# ip netns exec ns1 ifconfig eth1 10.0.0.12 netmask 255.255.255.0 up( P4 O( [7 q8 w- j' ^3 E& R+ m: e. `5 W


' |$ h0 k% j1 ]# I2 l                               
登录/注册后可看大图

+ T$ p4 p- n  t* a% B/ h. e# B! R

能够到达网关了。6 l! k! V4 a( f, `' d

$ e9 V) a/ e( B+ m/ z# Z已实现如下:

" q8 m; F) }5 G9 [& Y6 g7 `6 J" L. e

在ns1中添加源地址转换

# ip netns exec ns1 iptables -t nat -A POSTROUTING -s 192.168.1.0/24 ! -d 192.168.1.0/24 -j SNAT --to-source 10.0.0.12# ip netns exec ns1 ip route default via 10.0.0.1# E( o3 y8 F; {2 N. C

再次通过vm1 ping 公网ip

8 a9 Z! l9 @* \5 X4 V4 A

4 P, _4 g% f. ^  }3 j

这样就实现了宿主机内部分网络中的主机可以访问公网,部分主机没有访问公网权限。


4 Y+ f; w' z7 S& m0 H4 O; L- j

总之,网络逻辑很重要。

$ E1 s' x: _) q3 H; k% r
您需要登录后才可以回帖 登录 | 开始注册

本版积分规则

关闭

站长推荐上一条 /4 下一条

北京云银创陇科技有限公司以云计算运维,代码开发

QQ|返回首页|Archiver|小黑屋|易陆发现技术论坛 ( 蜀ICP备2026014127号-1 )点击这里给我发消息

GMT+8, 2026-4-9 00:03 , Processed in 0.061891 second(s), 22 queries .

Powered by Discuz! X3.4 Licensed

© 2012-2025 Discuz! Team.

快速回复 返回顶部 返回列表