虚拟机的网络模式
基于NAT(Network Address Translation)的虚拟网络
基于网桥(Bridge)的虚拟网络
用户自定义的隔离的虚拟网络
直接分配网络设备(包括VT-d和SR-IOV)
网络配置文件
下面是默认网络配置文件,地址是122网段,默认的网络为NAT模式。
1. [root@kvmserver ~]# cat /etc/libvirt/qemu/networks/default.xml 2. <!-- 3. WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE 4. OVERWRITTEN AND LOST. Changes to this xml configuration should be made using: 5. virsh net-edit default 6. or other application using the libvirt API. 7. --> 8. 9. <network> 10. <name>default</name> 11. <uuid>d01fdb52-9f49-4e03-a2d4-fd6382f11396</uuid> 12. <forward mode='nat'/> 13. <bridge name='virbr0' stp='on' delay='0'/> 14. <mac address='52:54:00:fb:02:fe'/> 15. <ip address='192.168.122.1' netmask='255.255.255.0'> 16. <dhcp> 17. <range start='192.168.122.2' end='192.168.122.254'/> 18. </dhcp> 19. </ip> 20. </network>
virsh查看命令
1. [root@kvmserver ~]# virsh net-list 2. Name State Autostart Persistent 3. ---------------------------------------------------------- 4. default active yes yes 5. 6. [root@kvmserver ~]# virsh net-info default 7. Name: default 8. UUID: d01fdb52-9f49-4e03-a2d4-fd6382f11396 9. Active: yes 10. Persistent: yes 11. Autostart: yes 12. Bridge: virbr0 13. 14. [root@kvmserver ~]# virsh net-dumpxml default 15. <network connections='2'> 16. <name>default</name> 17. <uuid>d01fdb52-9f49-4e03-a2d4-fd6382f11396</uuid> 18. <forward mode='nat'> 19. <nat> 20. <port start='1024' end='65535'/> 21. </nat> 22. </forward> 23. <bridge name='virbr0' stp='on' delay='0'/> 24. <mac address='52:54:00:fb:02:fe'/> 25. <ip address='192.168.122.1' netmask='255.255.255.0'> 26. <dhcp> 27. <range start='192.168.122.2' end='192.168.122.254'/> 28. </dhcp> 29. </ip> 30. </network>
基于NAT的虚拟网络
默认宿主机会有一个虚拟网卡virbr0,其实是一个虚拟交换机,默认模式就不在详细介绍了。
1. [root@kvmserver ~]# ifconfig virbr0 2. virbr0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 3. inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255 4. ether 52:54:00:fb:02:fe txqueuelen 1000 (Ethernet) 5. RX packets 0 bytes 0 (0.0 B) 6. RX errors 0 dropped 0 overruns 0 frame 0 7. TX packets 0 bytes 0 (0.0 B) 8. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
基于网桥的虚拟网络
首先需要创建虚拟交换机,可以使用virt-manager来创建,也可以修改网络配置文件。
1. [root@kvmserver ~]# cd /etc/sysconfig/network-scripts/ 2. [root@kvmserver network-scripts]# cp ifcfg-ens33 ifcfg-br10 3. [root@kvmserver network-scripts]# vim ifcfg-br10 4. DEVICE=br10 5. TYPE=Bridge 6. BOOTPROTO=static 7. NAME=br10 8. ONBOOT=yes 9. IPADDR=192.168.8.100 10. GATEWAY=192.168.8.2 11. DNS1=8.8.8.8 12. [root@kvmserver network-scripts]# vim ifcfg-ens33 13. TYPE=Ethernet 14. NAME=ens33 15. DEVICE=ens33 16. ONBOOT=yes 17. BRIDGE=br10 18. [root@kvmserver network-scripts]# systemctl restart network
查看br10是否配置成功。
1. [root@kvmserver ~]# ip a show br10 2. 11: br10: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000 3. link/ether 00:0c:29:bb:33:e4 brd ff:ff:ff:ff:ff:ff 4. inet 192.168.8.100/24 brd 192.168.8.255 scope global noprefixroute br10 5. valid_lft forever preferred_lft forever 6. inet6 fe80::20c:29ff:febb:33e4/64 scope link 7. valid_lft forever preferred_lft forever
创建虚拟机测试,首先创建dp1池。
1. [root@kvmserver ~]# virsh pool-define-as dp1 dir --target /dir 2. Pool dp1 defined 3. 4. [root@kvmserver ~]# virsh pool-start dp1 5. Pool dp1 started 6. 7. [root@kvmserver ~]# virsh pool-autostart dp1 8. Pool dp1 marked as autostarted 9. 10. [root@kvmserver ~]# virsh pool-list --all 11. Name State Autostart 12. ------------------------------------------- 13. dirpool active yes 14. dp1 active yes 15. img active yes 16. iso active yes 17. nfspool active yes 18. test active yes 19. vgpool active yes
创建在dp1池创建磁盘文件,名称为vol1.qcow2。
1. [root@kvmserver ~]# virsh vol-create-as dp1 vol1.qcow2 8G --format qcow2 2. Vol vol1.qcow2 created 3. 4. [root@kvmserver ~]# virsh vol-list dp1 5. Name Path 6. ------------------------------------------------------------------------------ 7. vol1.qcow2 /dir/vol1.qcow2
安装虚拟机测试网络,桥接模式的网络应是可以访问外网,而外网也可以访问虚拟机。这里要记得不是一定要重新安装,可以使用attach-interface命令给已有虚拟机添加网络。
[root@kvmserver ~]# virt-install -n vm001 -r 1024 --vcpus 1 -l /kvm/iso/centos.iso --disk /dir/vol1.qcow2 -w bridge=br10 --nographics -x "console=ttyS0"
经过安装等待,我们来到了用户登录界面,输入root用户加密码,进入终端。
1. CentOS Linux 7 (Core) 2. Kernel 3.10.0-1160.el7.x86_64 on an x86_64 3. 4. localhost login: root 5. Password:
直接使用dhcp获取地址,来测试网络
1. [root@localhost ~]# dhclient 2. [root@localhost ~]# ip a show ens3 3. 2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 4. link/ether 52:54:00:14:8f:0b brd ff:ff:ff:ff:ff:ff 5. inet 192.168.8.131/24 brd 192.168.8.255 scope global dynamic ens3 6. valid_lft 1670sec preferred_lft 1670sec 7. [root@localhost ~]# ping www.baidu.com -c 4 8. PING www.a.shifen.com (182.61.200.7) 56(84) bytes of data. 9. 64 bytes from 182.61.200.7 (182.61.200.7): icmp_seq=1 ttl=128 time=34.7 ms 10. 64 bytes from 182.61.200.7 (182.61.200.7): icmp_seq=2 ttl=128 time=45.0 ms 11. 64 bytes from 182.61.200.7 (182.61.200.7): icmp_seq=3 ttl=128 time=75.7 ms 12. 64 bytes from 182.61.200.7 (182.61.200.7): icmp_seq=4 ttl=128 time=81.2 ms 13. 14. --- www.a.shifen.com ping statistics --- 15. 4 packets transmitted, 4 received, 0% packet loss, time 3027ms 16. rtt min/avg/max/mdev = 34.754/59.204/81.264/19.735 ms
打开windows终端,ping虚拟机成功的话,双向通信完成。未成功则检查网络信息。
基于隔离的虚拟网络
查看虚拟机网络配置文件都有哪些
1. [root@kvmserver ~]# cd /etc/libvirt/qemu/networks/ 2. [root@kvmserver networks]# ls 3. autostart default.xml
创建一个用户定义的隔离网络配置文件
1. [root@kvmserver networks]# vim dir.xml 2. 3. <network> 4. <name>dir</name> 5. <uuid>7b0b9f14-ff50-4f8f-ac0f-9adcc4714441</uuid> 6. <bridge name='virbr1' stp='on' delay='0'/> 7. <mac address='52:54:00:65:0e:f9'/> 8. <domain name='dir'/> 9. <ip address='192.168.100.1' netmask='255.255.255.0'> 10. <dhcp> 11. <range start='192.168.100.128' end='192.168.100.254'/> 12. </dhcp> 13. </ip> 14. </network>
把网络加入域并查看
1. [root@kvmserver networks]# virsh net-define dir.xml 2. Network dir defined from dir.xml 3. 4. [root@kvmserver networks]# virsh net-start dir 5. Network dir started 6. 7. [root@kvmserver networks]# virsh net-autostart dir 8. Network dir marked as autostarted 9. 10. [root@kvmserver networks]# virsh net-list --all 11. Name State Autostart Persistent 12. ---------------------------------------------------------- 13. default active yes yes 14. dir active yes yes
在dp1池中创建磁盘文件
1. [root@kvmserver networks]# virsh vol-create-as dp1 vol2.qcow2 8G --format qcow2 2. Vol vol2.qcow2 created 3. 4. [root@kvmserver networks]# virsh vol-list dp1 5. Name Path 6. ------------------------------------------------------------------------------ 7. vol1.qcow2 /dir/vol1.qcow2 8. vol2.qcow2 /dir/vol2.qcow2
创建虚拟机,在创建好的磁盘文件中创建vm002虚拟机,网络状态指定dir(dir是上面配置的配置文件dir.xml)等待配置测试即可。
[root@kvmserver networks]# virt-install -n vm002 -r 1024 --vcpus 1 -l /kvm/iso/centos.iso --disk /dir/vol2.qcow2 -w network=dir --nographics -x "console=ttyS0"
测试结果应为完全独立的网络,上不了外网,而外网也无法访问内网。