虛擬化網路技術
阿新 • • 發佈:2018-11-07
虛擬化網路工具:
建立物理橋: 1 virsh命令 virsh iface-bridge eth0 br0 2 /etc/sysconfig/network-scripts/ 編輯配置檔案方式: [[email protected]:26:15network-scripts]#cat ifcfg-eth0 DEVICE=eth0 #IPADDR=172.20.23.30 #NETMASK=255.255.0.0 #GATEWAY=172.20.0.1 #DNS1=114.114.114.114 #DNS2=8.8.8.8 BRIDGE=br0 [[email protected]:26:42network-scripts]#cat ifcfg-br0 DEVICE=br0 TYPE=Bridge IPADDR=172.20.23.30 NETMASK=255.255.0.0 GATEWAY=172.20.0.1 DNS1=114.114.114.114 DNS2=172.20.0.1 BOOTPROTO=none ONBOOT=yes 重啟網路服務~
virsh和網路相關的命令:
[[email protected]:09:43~]#virsh help network
Networking (help keyword 'network'):
net-autostart
net-create
net-define 建立網路
net-destroy
net-dhcp-leases
net-dumpxml 檢視網橋建立配置檔案
net-edit
net-event
net-info
net-list 檢視網橋列表
net-name
net-start
net-undefine 刪除網橋
net-update
net-uuid
如何建立一個虛擬網路:
[ [email protected]:48:51networks]#cat mynet0.xml
<!--
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:
virsh net-edit default
or other application using the libvirt API.
<forward mode='nat'/>
-->
<network>
<name>mynet0</name>
<uuid>72c15a3e-89ab-4d2c-819b-841342262eb1</uuid>
<bridge name='mybr1' stp='on' delay='0'/>
<mac address='52:54:00:d7:ee:54'/>
<ip address='192.168.24.3' netmask='255.255.255.0'>
<dhcp>
<range start='192.168.24.10' end='192.168.24.100'/>
</dhcp>
</ip>
</network>
[ [email protected]:52:19networks]#virsh net-create ./mynet0.xml
Network mynet0 created from ./mynet0.xml
拆除之前mybr0的介面:
brctl delif mybr0 eth1
檢視拆除結果:
[[email protected]:56:07networks]#brctl show
bridge name bridge id STP enabled interfaces
br0 8000.000c2970f727 yes eth0
mybr0 8000.000000000000 yes
將介面新增至mybr1:
[[email protected]:56:17networks]#brctl addif mybr1 eth1
[[email protected]:57:07networks]#brctl show
bridge name bridge id STP enabled interfaces
br0 8000.000c2970f727 yes eth0
mybr0 8000.000000000000 yes
mybr1 8000.525400d7ee54 yes eth1
mybr1-nic
測試和外面的mybr1連結:
[[email protected]:59:38networks]#ip netns exec r1 ping 192.168.24.3
PING 192.168.24.3 (192.168.24.3) 56(84) bytes of data.
64 bytes from 192.168.24.3: icmp_seq=1 ttl=64 time=0.069 ms
64 bytes from 192.168.24.3: icmp_seq=2 ttl=64 time=0.093 ms
建立一個虛擬機器,選擇和 mynet0 在同一網路!
brctl工具:
來自----bridge-utils包
[[email protected]:09:24~]#rpm -qf `which brctl`
bridge-utils-1.5-9.el7.x86_64
常見選項:
addbr --新增橋裝置[軟]
delbr --刪除橋裝置[硬]
addif --給網橋新增介面
delif --網橋中拆除網線
show --檢視所有橋資訊
stp --開啟生成樹
常見命令:
新增網橋:
brctl addbr mybr0
啟用禁用生成樹:
brctl stp mybr0 on/off
啟用網橋:
ip link set [網橋名] up
建立虛擬網絡卡對:
ip link add veth1.1 type veth peer name veth1.2
刪除虛擬網絡卡對:
ip link del veth1.1
檢視關聯網絡卡對:
ip link show
更改虛擬網絡卡名稱:
ip link set veth1.1 name eth1
啟用虛擬網絡卡:
ip link set eth1 up
ip link set eth2 up
同樣可以啟用網橋:
ip link set mybr0 up
將網絡卡關聯至網橋:
brctl addif mybr0 eth1
檢視結果:
[[email protected]:26:30~]#brctl show
bridge name bridge id STP enabled interfaces
br0 8000.000c2970f727 yes eth0
mybr0 8000.82383e8abd07 yes eth1
新增網路名稱空間:
ip netns add r1
檢視網路名稱空間列表:
ip netns list
把虛擬網絡卡裝置關聯至網路名稱空間:
ip link set dev eth2 netns r1
檢視結果:
[[email protected]:30:21~]#ip netns exec r1 ifconfig -a
eth2: flags=4098<BROADCAST,MULTICAST> mtu 1500
ether fe:46:56:2f:ee:61 txqueuelen 1000 (Ethernet)
RX packets 8 bytes 648 (648.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 8 bytes 648 (648.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
網路名稱空間配置IP:
ip netns exec r1 ifconfig eth0 172.20.23.2/16
具體執行:
ip netns exec r1 ifconfig eth2 192.168.23.2/24
和r1空間連結測試:
[[email protected]:37:07~]#ping 192.168.23.2
PING 192.168.23.2 (192.168.23.2) 56(84) bytes of data.
64 bytes from 192.168.23.2: icmp_seq=1 ttl=64 time=0.461 ms
64 bytes from 192.168.23.2: icmp_seq=2 ttl=64 time=0.036 ms
如何從虛擬網橋中拆除介面:
brctl delif mybr0 eth1
更改網路名稱空間中的虛擬網絡卡裝置名稱:
ip netns exec r1 ip link set dev veth1.2 name eth0