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虚拟化平台的网络模型就介绍完成了,这些模型对以后的云计算平台网络的虚拟化也是很重要的。
上一篇: 我男友说我没有腰