虚拟化网络技术

虚拟化网络工具:

创建物理桥:
1 virsh命令
virsh iface-bridge eth0 br0
2 /etc/sysconfig/network-scripts/
编辑配置文件方式:
[root@www08: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

[root@www08: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和网络相关的命令:
[root@www14: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                    
如何创建一个虚拟网络:
[root@www19: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>

[root@www19:52:19networks]#virsh net-create ./mynet0.xml 
Network mynet0 created from ./mynet0.xml

拆除之前mybr0的接口:
brctl delif mybr0 eth1  
查看拆除结果:
[root@www19:56:07networks]#brctl show
bridge name bridge id       STP enabled interfaces
br0     8000.000c2970f727   yes     eth0
mybr0       8000.000000000000   yes     

将接口添加至mybr1:
[root@www19:56:17networks]#brctl addif mybr1 eth1
[root@www19: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链接:
[root@www19: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包

[root@www14: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
查看结果:
[root@www19: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 
查看结果:
[root@www19: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空间链接测试:
[root@www19: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

猜你喜欢

转载自blog.51cto.com/13878078/2313722