|
netns 可以创建一个完全隔离的新网络环境,这个环境包括一个独立的网卡空间,路由表,ARP表,ip地址表,iptables等。总之,与网络有关的组件都是独立的。
* z) G7 Z% H# T0 O7 k
8 t3 f, e1 }" j1 s! ?创建网络空间: # ip netns add ns1/ O9 H# W( D( [: d" ]7 b e
查看网络空间: # ip netns list( V& ~7 |+ {" z1 v: P* }
删除网络空间: # ip netns del ns1
; d* [ |9 X/ @# L! g( C8 \进入网络空间执行命令: # ip netns exec ns1 `command`% A5 z/ Z! \) P
. u6 ?" M0 v% O& N实例一:: g6 a' x5 |4 h I5 a1 `/ |
用netns连接两个隔离环境中的虚拟机,如图: : {+ B& R( Y1 |- b7 D3 h/ P r
在虚拟化中有两个虚拟机网络隔离环境需要通信。 系统: 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 up
- E+ J, z4 O* H! W取消默认nat网络模式# mv /etc/libvirt/qemu/networks/default.xml /etc/libvirt/qemu/networks/default.xml_bak# systemctl start libvirtd
2 h; ~5 s! M# m6 B+ n0 u创建虚拟机并连接至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 vnet13 k) F' G+ x V; g' a* |
到此,虚拟机已经连接上各自的桥设备了。完成如图:
8 Y, F3 Y( C2 s* w1 W3 M9 H# `& R创建虚拟网络空间:# ip netns add ns1# ip netns listns1: V, H. z' ]9 A- z. }' D
接下来创建一张虚拟网卡,虚拟网卡分为前半段和后半段,我们将前半段添加到br0中,并将后半段添加到虚拟网络空间中,这样br0桥设备中主机就能够连接到虚拟网络空间中。 # ip link add net-in type veth peer name net-out# ifconfig net-in up# ifconfig net-out up$ Q3 i& o* }- L. E# L& o% O
将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 vnet01 q% Z& U8 t: u- r# Y8 P
将net-out添加到ns1中,并重命名为eth0# ip link set dev net-out name eth0 netns ns1
1 j6 u6 G+ A, F/ N查看是否添加成功# 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
( Q4 y: T. \& o6 b现在vm1 --> br0 --> ns1 网络做通了,完成如下图:
2 B- c4 e9 y6 Q9 K' k; P+ |同理,和上面操作一样。 # 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 ns12 q! \* B' P$ V" } v+ @5 K5 }* E
/ [8 f4 v; I: C& ^
# 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 06 M% u! v% O7 N0 |8 f
ip地址配置如下:5 H! X; K; {3 m* v/ M+ `
, o% a1 T/ T$ }. O9 X
vm1 - eth0 : 192.168.1.2
- P0 i: P0 v; I$ `, F7 Lns1 - eth0 : 192.168.1.1 l# I) q1 \0 T- U$ h$ c, X
, a) A$ O8 B, f* }3 j4 J% e1 e5 _vm2 - eth0 : 172.168.10.25 f3 t2 f* @# n5 i8 n0 F/ j
ns1 - eth0 : 172.168.10.1 记住:当宿主机开启了网络转发功能,虚拟网络空间才会开启,在以上场景中,必须开启网络转发功能。 # sysctl -w net.ipv4.ip_forward=1net.ipv4.ip_forward = 12 h0 r, ]6 W' [% v* J. l* t
x; R A/ U! D$ w, }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)
- @4 V3 x0 K- o2 q7 Zns1 - 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
: V% O5 I% S# }
. r3 `$ a$ W/ y( {2 evm2 - 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)
) t. N- S' V. F0 }) p+ x# H% c; g T
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 0
4 [" ~. U# P5 O' g
* i7 c; z- g* c, x; j. U为虚拟机指定路由: 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- @) t; x0 l4 J3 T5 n
注意:如果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.19 {% \* x7 N; c5 V
, a' v/ ~( t: P1 i9 ?
接下来,使用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! i" Z! f. Z( P7 @9 K/ {2 q
6 x% G+ l; ]; @: K6 K
& H! G/ M4 c7 s5 {这样,就完成了在宿主机中,两个虚拟主机隔离模式的通信。 1 {# }. v) G. j: V6 S
4 }6 }) Y+ \1 W& T- H实例二:
$ ?0 `% m; R/ h1 A! Y6 h
& ~1 `9 Q3 O2 a' v6 u 说明:宿主机中两组隔离模型,其中只有一组可以访问公网
接下来,在模式一的基础上进行修改:
4 i7 H+ y$ D* ^9 `
' L% b/ }: K- T0 q # ip netns del ns1
7 X( \! a7 m0 U- I
4 ~) r$ { D- Z$ I! |! ^) b' _3 b删除虚拟网络空间模式,所有和虚拟网络空间有关的虚拟网卡都会被删除。
现在的模式如下: ! H+ C. a4 S6 e1 F" T- q
vm1: 192.168.1.2/24' n) P1 t8 x% Z1 h( U
vm2: 192.168.1.2/248 M' J( @! S! v& j; z: |/ d! k+ P
ns1: 192.168.1.1/246 J V8 k8 e/ H) P
& o( l! `9 ~( L. q9 d. Q$ v
这里故意把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 up0 Y: O0 G0 F- R
添加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' N% [! q9 O- S' u' _
为vm1配置网关为192.168.1.1; k) [; w7 t L
. h' I4 P; s( a& c" ]5 `4 h; e
创建桥设备,并将物理网卡添加到桥设备中,这里建议直接修改物理网卡配置文件 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
# J/ Z6 t c$ u" A. j3 B6 S/ Z P0 P- i) {& P$ o
现在创建一对网卡,连接ns1和br-out 0 z9 ]9 @, I) m& h2 r8 x
# 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-out7 Z! {1 i$ \% L1 D d( S" g9 r
1 X: u8 E4 d U5 }1 D) l
我真实局域网的ip为10.0.0.0/24! B- w6 I H" H; O- \2 V9 r
& y& b; Q1 O3 J: {
因此添加到ns1中的eth1要配置到同网段 # ip netns exec ns1 ifconfig eth1 10.0.0.12 netmask 255.255.255.0 up( G) E2 X& m* R$ u& j8 d
 ! H. h1 R0 P! W% R
能够到达网关了。
% W0 G v4 D+ ^7 ^0 E7 l' ?1 R" |; M. B; r& W! }3 k+ T
已实现如下: * Q. U0 W7 Q2 h2 N
在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$ L& j. m* W( L4 P
再次通过vm1 ping 公网ip
' F) u; E: ?& L( h. d: `: J# k* G( ?* Y% k( G5 e; ?4 R- V
这样就实现了宿主机内部分网络中的主机可以访问公网,部分主机没有访问公网权限。 + m( X' b8 ^) R2 Y2 V6 N
总之,网络逻辑很重要。 2 ~( b! E! S* o' C+ _1 ^9 W
|