KVM虚拟化技术之virt-manager使用及KVM虚拟化平台网
一.使用virt-manager创建和管理虚拟机
1.使用VNC Viewer连接进入虚拟化平台主机
2.打开终端输入virt-manager命令启动virt-manager虚拟机管理界面
#virt-manager
3.通过virt-manager安装CentOS 6.6的虚拟机
点击如图所示图标新建虚拟机:
选择PXE引导,我的网络内存在一个系统自动化部署服务器:
选择操作系统类型和版本:
设置内存和CPU个数:
设置硬盘大小,这里采用动态扩展磁盘空间方式:
忽略这个错误,由于是虚拟磁盘,不用担心空间,只需要保证系统空间不会超过物理磁盘实际空间:
勾选选项是可以查看配置在安装前:
我们可以在这个界面进行设置,我这里就不设置了,直接点击Begin Installation:
我们选择安装个基本的系统:
进入安装了:
可以观察安装时Virt-manager界面的情况:
可以知晓虚拟机正在运行,可以查看CPU的使用情况:
安装完成后如图;
关闭虚拟机,在虚拟机输入关机指令即可将虚拟机关闭;
基于virt-manager创建管理虚拟机就完成了,很简单的。
下面的实验我们还是使用cirros轻量级的linux系统。
二.KVM虚拟化平台的网络模型
1.网络模型介绍
一般虚拟机虚拟网络的设置主要包括三种方式。主要如下:
NAT模式
也有人称此种模式为host模式。在这种模式下虚拟机可以理解成没有自己的独立网卡。所有访问虚拟机的请求其实是直接发送给宿主机,然后通过访问宿主机转发到虚拟机上的。相应的虚拟机访问其他网络,也是先转发到宿主机然后在转发出去。对于宿主机之外的网络,是不知道该虚拟机存在的。
Bridge模式
桥接模式是使用比较多的模式,它是虚拟机拥有自己的独立网卡和IP,然后通过借用宿主机的网卡对外连接网络。它把宿主机的网卡当作了一种桥,通过这个桥连接外网的世界。在这种模式下,可以简单的理解成虚拟机和宿主机是两个不同的机器,有独立IP可以相互访问。对于虚拟机的IP获取,一般可以直接指定也可以通过DHCP获取得到。
Internal模式(host-only)
这个是把虚拟机之间的网络和主机的网络隔离开来。虚拟机是一片网络,主机也是一片网络,彼此之间不能相互访问。
桥接模型我们前面使用的很多例子,我这里就不做介绍了,我重点介绍一下host-only模型和NAT模型。
2.host-only模式实例
1).我们创建一个host-only的桥设备,将虚拟机之间的网络和KVM虚拟化平台宿主机隔离开来;
# brctl addbr isolationbr
查看桥设备:
# brctl show bridge name bridge id STP enabled interfaces br0 8000.000c293e6326 yes eth0 isolationbr 8000.000000000000 no virbr0 8000.525400305441 yes virbr0-nic
但是这个桥设备是未激活的,我们需要使用ip命令激活桥设备:
# ip link set isolationbr up
激活后查看我们的桥设备:
# ip link show 1: lo: <LOOPBACK,UP,LOWER_UP> mtu65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: eth0:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen1000 link/ether 00:0c:29:3e:63:26 brd ff:ff:ff:ff:ff:ff 3: br0:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN link/ether 00:0c:29:3e:63:26 brd ff:ff:ff:ff:ff:ff 4: virbr0:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN link/ether 52:54:00:30:54:41 brd ff:ff:ff:ff:ff:ff 5: virbr0-nic: <BROADCAST,MULTICAST>mtu 1500 qdisc noop state DOWN qlen 500 link/ether 52:54:00:30:54:41 brd ff:ff:ff:ff:ff:ff 16: isolationbr:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN link/ether 6e:5e:8d:39:56:b5 brd ff:ff:ff:ff:ff:ff 17: vnet1:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWNqlen 500 link/ether 3a:ce:49:1d:f4:a3 brd ff:ff:ff:ff:ff:ff 18: vnet2:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWNqlen 500 link/ether 62:fc:96:5c:1f:7d brd ff:ff:ff:ff:ff:ff
2).启动两个虚拟机:
第一台cirros虚拟机:
代码如下:
# qemu-kvm -m 128 -name cirros1 -drive file=/kvm/images/cirros-0.3.0-x86_64-disk.img,media=disk,format=qcow2,if=ide -net nic -net tap,ifname=vnet1,script=/etc/qemu-ifup,downscript=/etc/qemu-ifdown -boot c -daemonize
启动后从vncviewer登录如图:
第二台cirros虚拟机,启动时需要指定mac地址;
代码如下:
# qemu-kvm -m 128 -name cirros2 -drive file=/kvm/images/cirros-0.3.0-x86_64-disk2.img,media=disk,format=qcow2,if=ide -net nic,macaddr=52:54:00:65:43:21 -net tap,ifname=vnet2,script=/etc/qemu-ifup,downscript=/etc/qemu-ifdown -boot c -daemonize
启动后从vncviewer登录如图:
查看两台虚拟机的ip地址:
使用ping测试两台虚拟机的连通性:
现在是连通的哦!
我们启动两台虚拟机后我们的vnet1和vnet2网卡是桥接在br0上的;
# brctl show bridge name bridge id STP enabled interfaces br0 8000.000c293e6326 yes eth0 vnet1 vnet2 isolationbr 8000.000000000000 no virbr0 8000.525400305441 yes virbr0-nic
3).我们现在将vnet1和vnet2桥接到isolationbr上:
先将vnet1和vnet2从桥接设备br0上移除:
# brctl delif br0 vnet1 # brctl delif br0 vnet2
现在查看桥接设备的网卡,两个虚拟机的网卡未桥接在桥接设备br0上了:
# brctl show bridge name bridge id STP enabled interfaces br0 8000.000c293e6326 yes eth0 isolationbr 8000.000000000000 no virbr0 8000.525400305441 yes virbr0-nic
我们再去两台虚拟机进行ping连通性测试:
现在虚拟机的连通性是不通的。
下面我们将vnet1和vnet2的网卡桥接到我们刚创建的桥接设备isolationbr上:
# brctl addif isolationbr vnet1 # brctl addif isolationbr vnet2
去查看桥接设备的网卡关联:
# brctl show bridge name bridge id STP enabled interfaces br0 8000.000c293e6326 yes eth0 isolationbr 8000.3ace491df4a3 no vnet1 vnet2 virbr0 8000.525400305441 yes virbr0-nic
我们虚拟机的两个网卡已经关联到了isolationbr桥设备上;
我们再去虚拟机上测试连通性:
现在两台虚拟机之间是在同一个网络的,可以实现通信,但是跟宿主机之间是隔离的,我们虚拟机与宿主机之间的联通性是不能连通的。如果我们需要实现虚拟机与宿主机之间的通信,那么我们就需要开启NAT模型,下面就介绍NAT模型。
3.NAT模型实例
其实就是配置host-only网络内的主机同外部主机通信实验,开启桥设备的NAT功能。
1).我们虚拟机的地址是经过网络内的DHCP服务器分配的,我们为了实验来手动设置两台虚拟机的地址和桥接设备isolationbr的地址
两台虚拟机的ip设置如图:
桥设备isolationbr的ip设置如图:
[root@createOS ~]# ifconfig isolationbr 10.0.0.254/8 up [root@createOS ~]# ifconfig isolationbr isolationbr Link encap:Ethernet HWaddr 3A:CE:49:1D:F4:A3 inet addr:10.0.0.254 Bcast:10.255.255.255 Mask:255.0.0.0 inet6 addr: fe80::6c5e:8dff:fe39:56b5/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:1 errors:0 dropped:0overruns:0 frame:0 TX packets:6 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:28 (28.0 b) TX bytes:468(468.0 b)
设置完成后测试虚拟机之间网络连通性:
我们将虚拟机的网关指向isolationbr桥设备地址即可与宿主机进行连通:
2).我们还是不能与物理网络中的真实网关172.16.0.1通信,我们需要将宿主机的路由转发功能打开:
# sysctl -w net.ipv4.ip_forward=1 net.ipv4.ip_forward = 1
我们先来通过虚拟机ping一下网关,如图:
在进行ping连通性测试的时候我们再在宿主机上打开抓包功能查看数据包:
# tcpdump -i eth0 icmp -nn tcpdump: WARNING: eth0: no IPv4 addressassigned tcpdump: verbose output suppressed, use -vor -vv for full protocol decode listening on eth0, link-type EN10MB(Ethernet), capture size 65535 bytes 10:24:52.377558 IP 10.0.0.2 >172.16.0.1: ICMP echo request, id 1793, seq 0, length 64 10:24:53.384063 IP 10.0.0.2 >172.16.0.1: ICMP echo request, id 1793, seq 1, length 64
数据报文能够到达网关设备,但是数据包不能回来了。
我们需要开启宿主机的路由功能,设置防火墙中的nat模式:
# iptables -t nat -A POSTROUTING -s 10.0.0.0/8 ! -d 10.0.0.0/8 -j MASQUERADE # iptables -t nat -L POSTROUTING Chain POSTROUTING (policy ACCEPT) target prot opt source destination MASQUERADE tcp -- 192.168.122.0/24 !192.168.122.0/24 masq ports: 1024-65535 MASQUERADE udp -- 192.168.122.0/24 !192.168.122.0/24 masq ports: 1024-65535 MASQUERADE all -- 192.168.122.0/24 !192.168.122.0/24 MASQUERADE all -- 10.0.0.0/8 !10.0.0.0/8
开始从虚拟机ping测试到达真实网关的连通性:
同时开始抓包,我们在宿主机eth0和桥接设备isolationbr上都要抓包:
桥接设备isolationbr的抓包:
# tcpdump -i isolationbr -nn tcpdump: verbose output suppressed, use -vor -vv for full protocol decode listening on isolationbr, link-type EN10MB(Ethernet), capture size 65535 bytes 10:35:35.391069 IP 10.0.0.2 >172.16.0.1: ICMP echo request, id 2305, seq 0, length 64 10:35:35.393619 ARP, Request who-has10.0.0.2 tell 10.0.0.254, length 28 10:35:35.395095 ARP, Reply 10.0.0.2 is-at52:54:00:65:43:21, length 28 10:35:35.395137 IP 172.16.0.1 >10.0.0.2: ICMP echo reply, id 2305, seq 0, length 64 10:35:36.394760 IP 10.0.0.2 >172.16.0.1: ICMP echo request, id 2305, seq 1, length 64 10:35:36.395943 IP 172.16.0.1 >10.0.0.2: ICMP echo reply, id 2305, seq 1, length 64 10:35:41.426182 ARP, Request who-has10.0.0.254 tell 10.0.0.2, length 28 10:35:41.427695 ARP, Reply 10.0.0.254 is-at3a:ce:49:1d:f4:a3, length 28
可以发现虚拟机的请求到达了网关,网关也回复了;这里的地址转换未显示,但是可以猜测是通过nat地址转换eth0将虚拟机的请求发送给网关。
宿主机的eth0抓包:
# tcpdump -i eth0 icmp -nn tcpdump: WARNING: eth0: no IPv4 addressassigned tcpdump: verbose output suppressed, use -vor -vv for full protocol decode listening on eth0, link-type EN10MB(Ethernet), capture size 65535 bytes 10:35:35.392027 IP 172.16.31.7 >172.16.0.1: ICMP echo request, id 2305, seq 0, length 64 10:35:35.393361 IP 172.16.0.1 >172.16.31.7: ICMP echo reply, id 2305, seq 0, length 64 10:35:36.395052 IP 172.16.31.7 >172.16.0.1: ICMP echo request, id 2305, seq 1, length 64 10:35:36.395860 IP 172.16.0.1 >172.16.31.7: ICMP echo reply, id 2305, seq 1, length 64
宿主机的eth0通过nat功能将虚拟机的请求转换成本机地址向网关请求回复了;
3).上述的步骤可以通过脚本自动化实现哦!
安装dnsmasq软件给虚拟机提供DHCP服务自动分配IP地址:
# yum install -y dnsmasq
注意:由于我们的KVM平台存在一个vibrd0的网卡,它自动启动了dnsmasq服务,我们在使用NAT模型时如果不是使用的这个网卡,我们就需要将其dnsmasq服务关闭。
代码如下:
# ps -ef | grep “dnsmasq” |grep-v “grep”
nobody 6378 1 0 11:49 ? 00:00:00 /usr/sbin/dnsmasq–strict-order –pid-file=/var/run/libvirt/network/default.pid –conf-file=–except-interface lo –bind-interfaces –listen-address 192.168.122.1–dhcp-range 192.168.122.2,192.168.122.254–dhcp-leasefile=/var/lib/libvirt/dnsmasq/default.leases –dhcp-lease-max=253–dhcp-no-override –dhcp-hostsfile=/var/lib/libvirt/dnsmasq/default.hostsfile–addn-hosts=/var/lib/libvirt/dnsmasq/default.addnhosts
关闭dnsmasq服务:
#kill 6378
nat模型脚本示例:
开启nat功能的脚本;
#vim /etc/qemu-natup #!/bin/bash BRIDGE=isolationbr NETWORK=10.0.0.0 GATEWAY=10.0.0.254 NETMASK=255.0.0.0 DHCPRANGE=10.0.0.1,10.0.0.100 TFTPROOT= BOOTP= function check_bridge() { if brctl show | grep "^BRIDGE"&> /dev/null;then return 1 else return 0 fi } function create_bridge() { brctl addbr "BRIDGE" brctl stp "BRIDGE" on brctl setfd "BRIDGE" 0 ifconfig "$BRIDGE""GATEWAY" netmask "$NETMASK" up } function enable_ip_forward() { echo 1 > /proc/sys/net/ipv4/ip_forward } function add_filter_rules() { iptables -t nat -A POSTROUTING -s"$NETWORK"/"$NETMASK" ! -d"$NETWORK"/"$NETMASK" -j MASQUERADE } function start_dnsmasq() { ps -ef | grep "dnsmasq" |grep -v"grep" &> /dev/null if [ $? -eq 0 ];then echo "warning:dnsmasq is already running" return 1 fi dnsmasq --strict-order--except-interface=lo --interface=$BRIDGE --listen-address=$GATEWAY--bind-interfaces --dhcp-range=$DHCPRANGE --conf-file=""--pid-file=/var/run/qemu-dhcp-$BRIDGE.pid --dhcp-leasefile=/var/run/qemu-dhcp-$BRIDGE.leases--dhcp-no-override ${TFTPROOT:+"--enable-tftp"}${TFTPROOT:+"--tftp-root=$TFTPROOT"}${BOOTP:+"--dhcp-boot=$BOOTP"} } function setup_bridge_nat() { check_bridge "$BRIDGE" if [ $? -eq 0 ];then create_bridge fi enable_ip_forward add_filter_rules "$BRIDGE" start_dnsmasq "$BRIDGE" } if [ -n "$1" ];then setup_bridge_nat ifconfig "$1" 0.0.0.0 up brctl addif "$BRIDGE""$1" exit 0 else echo "Error:no interfacespecified" exit 1 fi
关闭nat功能及从桥设备移除虚拟网卡脚本:
#vim /etc/qemu-natdown #!/bin/bash BRIDGE="isolotionbr" if [ -n "$1" ];then ip link set $1 down brctl delif "$BRIDGE" $1 ip link set "$BRIDGE" down brctl delbr "$BRIDGE" iptables -t nat -F exit 0 else echo "Error: no interface specified" exit 1 fi
设置脚本执行权限:
# chmod +x /etc/qemu-natup # chmod +x /etc/qemu-natdown
启动第一台虚拟机:
代码如下:
# qemu-kvm -m 128 -name cirros1 -drive file=/kvm/images/cirros-0.3.0-x86_64-disk.img,media=disk,format=qcow2,if=ide -net nic -net tap,ifname=vnet1,script=/etc/qemu-natup,downscript=/etc/qemu-natdown -boot c -daemonize
我们去查看dnsmasq服务启动与否:
# ps -ef | grep "dnsmasq" |grep-v "grep" nobody 38355 1 0 11:49 ? 00:00:00 dnsmasq --strict-order--except-interface=lo --interface=isolationbr --listen-address=10.0.0.254--bind-interfaces --dhcp-range=10.0.0.1,10.0.0.100 --conf-file=--pid-file=/var/run/qemu-dhcp-isolationbr.pid--dhcp-leasefile=/var/run/qemu-dhcp-isolationbr.leases --dhcp-no-override
查看宿主机上的网卡设备:
# ifconfig |grep -Ei"(vnet1|vnet2)" vnet1 Link encap:Ethernet HWaddr16:85:A7:5C:84:9D vnet2 Link encap:Ethernet HWaddrE6:81:C9:31:4F:78
启动虚拟机后在vncserver上连接到虚拟机界面操作,我们进行查看IP地址,可以发现我们的dnsmasq已经自动分配IP地址给虚拟机了。
查看一下宿主机的防火墙中的NAT规则:
#iptables -t nat -L POSTROUTING Chain POSTROUTING (policy ACCEPT) target prot opt source destination MASQUERADE all -- 10.0.0.0/8 !10.0.0.0/8
嘿嘿,我把防火墙NAT规则都给清空了,所以这里就只有一条规则了。o(∩_∩)o
在虚拟机上进行网络连通性测试:
在测试的同时开启抓包哦!
桥接设备网卡的数据报文如下:
# tcpdump -i isolationbr -nn tcpdump: verbose output suppressed, use -vor -vv for full protocol decode listening on isolationbr, link-type EN10MB(Ethernet), capture size 65535 bytes 12:05:14.655667 IP 10.0.0.83 >172.16.0.1: ICMP echo request, id 257, seq 0, length 64 12:05:14.658466 IP 172.16.0.1 >10.0.0.83: ICMP echo reply, id 257, seq 0, length 64 12:05:15.657273 IP 10.0.0.83 >172.16.0.1: ICMP echo request, id 257, seq 1, length 64 12:05:15.658252 IP 172.16.0.1 >10.0.0.83: ICMP echo reply, id 257, seq 1, length 64 12:05:19.659800 ARP, Request who-has10.0.0.83 tell 10.0.0.254, length 28 12:05:19.661522 ARP, Request who-has10.0.0.254 tell 10.0.0.83, length 28 12:05:19.661569 ARP, Reply 10.0.0.254 is-at16:85:a7:5c:84:9d, length 28 12:05:19.662053 ARP, Reply 10.0.0.83 is-at52:54:00:88:88:88, length 28 12:05:47.759101 ARP, Request who-has10.0.0.47 tell 10.0.0.83, length 28 12:05:47.760926 ARP, Reply 10.0.0.47 is-at52:54:00:12:34:56, length 28 12:05:47.761579 IP 10.0.0.83 >10.0.0.47: ICMP echo request, id 513, seq 0, length 64 12:05:47.765075 IP 10.0.0.47 >10.0.0.83: ICMP echo reply, id 513, seq 0, length 64 12:05:48.759703 IP 10.0.0.83 >10.0.0.47: ICMP echo request, id 513, seq 1, length 64 12:05:48.760848 IP 10.0.0.47 >10.0.0.83: ICMP echo reply, id 513, seq 1, length 64 12:05:52.775287 ARP, Request who-has10.0.0.83 tell 10.0.0.47, length 28 12:05:52.776601 ARP, Reply 10.0.0.83 is-at52:54:00:88:88:88, length 28 12:05:59.376454 IP 10.0.0.83 >172.16.31.7: ICMP echo request, id 769, seq 0, length 64 12:05:59.376548 IP 172.16.31.7 >10.0.0.83: ICMP echo reply, id 769, seq 0, length 64 12:06:00.482899 IP 10.0.0.83 >172.16.31.7: ICMP echo request, id 769, seq 1, length 64 12:06:00.483035 IP 172.16.31.7 >10.0.0.83: ICMP echo reply, id 769, seq 1, length 64 12:06:04.376987 ARP, Request who-has10.0.0.83 tell 10.0.0.254, length 28 12:06:04.378153 ARP, Reply 10.0.0.83 is-at52:54:00:88:88:88, length 28
物理网卡的数据报文如下:
# tcpdump -i eth0 icmp -nn tcpdump: WARNING: eth0: no IPv4 addressassigned tcpdump: verbose output suppressed, use -vor -vv for full protocol decode listening on eth0, link-type EN10MB (Ethernet),capture size 65535 bytes 12:05:14.657680 IP 172.16.31.7 >172.16.0.1: ICMP echo request, id 257, seq 0, length 64 12:05:14.658427 IP 172.16.0.1 >172.16.31.7: ICMP echo reply, id 257, seq 0, length 64 12:05:15.657329 IP 172.16.31.7 >172.16.0.1: ICMP echo request, id 257, seq 1, length 64 12:05:15.658215 IP 172.16.0.1 >172.16.31.7: ICMP echo reply, id 257, seq 1, length 64
至此,我们的KVM虚拟化平台的网络模型就介绍完成了,这些模型对以后的云计算平台网络的虚拟化也是很重要的。
原文链接:http://sohudrgon.blog.51cto.com/3088108/1613872