KVM 虚拟化技术(二)
1.5 kvm虚拟机网络管理
1.5.1 桥接网络配置
1、设置桥接网络
[[email protected] ~]# virsh iface-bridge eth0 br0 使用附加设备 br0 生成桥接 eth0 失败 已启动桥接接口 br0
查看网卡配置文件
# 查看 eth0 配置文件 [[email protected] ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 ONBOOT=yes BRIDGE="br0" # 查看 br0 配置文件 [[email protected] ~]# cat /etc/sysconfig/network-scripts/ifcfg-br0 DEVICE="br0" ONBOOT="yes" TYPE="Bridge" BOOTPROTO="none" IPADDR="10.0.0.240" NETMASK="255.255.255.0" GATEWAY="10.0.0.254" IPV6INIT="yes" IPV6_AUTOCONF="yes" DHCPV6C="no" STP="on" DELAY="0"
2、修改虚拟机网络配置
[[email protected] ~]# virsh edit clsn7 修改前: <interface type='network'> <mac address='52:54:00:42:bf:bc'/> <source network='default'/> <model type='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> </interface> 修改后: <interface type='bridge'> <mac address='52:54:00:42:bf:bc'/> <source bridge='br0'/> <model type='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> </interface>
查看宿主机网桥
[[email protected] ~]# brctl show bridge name bridge id STP enabled interfaces br0 8000.000c294d551b yes eth0 virbr0 8000.5254006aaa40 yes virbr0-nic vnet0 vnet1
查看防火墙规则:
[[email protected] ~]# iptables -t nat -nvL Chain PREROUTING (policy ACCEPT 195 packets, 24665 bytes) pkts bytes target prot opt in out source destination Chain INPUT (policy ACCEPT 131 packets, 16209 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 272 packets, 24045 bytes) pkts bytes target prot opt in out source destination Chain POSTROUTING (policy ACCEPT 272 packets, 24045 bytes) pkts bytes target prot opt in out source destination 0 0 RETURN all -- * * 192.168.122.0/24 224.0.0.0/24 1 328 RETURN all -- * * 192.168.122.0/24 255.255.255.255 29 1740 MASQUERADE tcp -- * * 192.168.122.0/24 !192.168.122.0/24 masq ports: 1024-65535 0 0 MASQUERADE udp -- * * 192.168.122.0/24 !192.168.122.0/24 masq ports: 1024-65535 3 252 MASQUERADE all -- * * 192.168.122.0/24 !192.168.122.0/24
3、修改kvm虚拟机网卡配置文件
[[email protected] ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 TYPE=Ethernet BOOTPROTO=static NAME=eth0 DEVICE=eth0 ONBOOT=yes IPADDR=10.0.0.110 NETMASK=255.255.255.0 GATEWAY=10.0.0.254 DNS1=223.5.5.5
测试网络连通性
[[email protected] ~]# ping 223.5.5.5 -c1 PING 223.5.5.5 (223.5.5.5) 56(84) bytes of data. 64 bytes from 223.5.5.5: icmp_seq=1 ttl=128 time=94.4 ms
1.6 KVM虚拟机冷/热迁移
在进行迁移之前需要准备一台与KVM配置相同的机器(KVM02),部署好kvm环境。
1.6.1 虚拟机冷迁移
在kvm02中安装kvm组件
[[email protected] ~]# yum install libvirt* virt-* qemu-kvm* -y
配置桥接网络
[[email protected] ~]# virsh iface-bridge eth0 br0 [[email protected] ~]# mkdir -p /data
将虚拟机关机,导出配置文件
[[email protected] data]# virsh dumpxml clsn7 >clsn7.xml
将虚拟机文件传输到kvm02上
[[email protected] data]# scp -rp clsn7.xml clsn.qcow2 10.0.0.201:/data
导入配置文件
[[email protected] ~]# virsh define clsn7.xml
启动虚拟机
[[email protected] ~]# virsh start clsn7
查看虚拟机状态
[[email protected] ~]# virsh list --all Id 名称 状态 ---------------------------------------------------- 5 clsn7 running
至此,一次KVM冷迁移就完成了
1.6.2 virt-manager和kvm虚拟机热迁移(准备)
实现kvm虚拟机热迁移核心:共享存储。在这里使用的时NFS共享存储,关于nfs的详情参考:http://www.cnblogs.com/clsn/p/7694456.html
1、安装virt-manager所需桌面及vnc-server
[[email protected] ~]# yum groupinstall "GNOME Desktop" -y # vnc-server端 [[email protected] ~]# yum install tigervnc-server -y # virt-manager需要软件 [[email protected] ~]# yum install openssh-askpass -y
2、配置vnc服务
复制vnc配置文件
[[email protected] ~]# vi /usr/lib/systemd/system/[email protected] [[email protected] ~]# \cp /usr/lib/systemd/system/[email protected] /usr/lib/systemd/system/[email protected]\:1.service
修改配置文件,主要修改<USER>参数。
[[email protected] ~]# egrep -v "^#|^$" /usr/lib/systemd/system/[email protected]\:1.service [Unit] Description=Remote desktop service (VNC) After=syslog.target network.target [Service] Type=forking User=root ExecStartPre=-/usr/bin/vncserver -kill %i ExecStart=/usr/bin/vncserver %i PIDFile=/root/.vnc/%H%i.pid ExecStop=-/usr/bin/vncserver -kill %i [Install] WantedBy=multi-user.target # 用户为root,家目录为root
官方提供修改方法
# Quick HowTo: # 1. Copy this file to /etc/systemd/system/[email protected] # 2. Replace <USER> with the actual user name and edit vncserver # parameters appropriately # ("User=<USER>" and "/home/<USER>/.vnc/%H%i.pid") # 3. Run `systemctl daemon-reload` # 4. Run `systemctl enable v[email protected]:<display>.service`
设置vnc连接时的密码,
[[email protected] ~]# vncpasswd Password: Verify: Would you like to enter a view-only password (y/n)? n # y为创建只读用户,n为非只读用户。
启动vnc服务,设置开机自启动
[[email protected] ~]# systemctl start [email protected]\:1.services [[email protected] ~]# systemctl enable [email protected]\:1.services
查看密码文件及其他配置文件位置
[[email protected] ~]# ll ~/.vnc/
3、配置NFS存储
安装软件
[[email protected] ~]# yum install nfs-utils rpcbind -y
修改配置文件
[[email protected] ~]# cat /etc/exports /data 172.16.1.0/24(rw,sync,all_squash,anonuid=0,anongid=0)
启动nfs程序
[[email protected] ~]# systemctl restart rpcbind [[email protected] ~]# systemctl restart nfs # 设置开机自启动 [[email protected] ~]# systemctl enable rpcbind [[email protected] ~]# systemctl enable nfs
在kvm02上安装nfs
[[email protected] ~]# yum install nfs-utils rpcbind -y
查看共享信息
[[email protected] ~]# showmount -e 172.16.1.240 Export list for 172.16.1.240: /data 172.16.1.0/24
挂载目录
[[email protected] ~]# mount.nfs 172.16.1.240:/data /data # 加入开机自启动 [[email protected] ~]# echo 'mount.nfs 172.16.1.240:/data /data' >>/etc/rc.local [[email protected] ~]# chmod +x /etc/rc.d/rc.local
1.6.3 KVM虚拟机热迁移(实现)
vnc连接KVM宿主机:
图 - 连接地址
图 - 输入vnc密码
图 - 使用vmm 虚拟系统管理器
添加KVM02宿主机
图 - 添加新连接
注:连接上KVM02机器即可
图 - 添加上kvm02主机
图 - 主机添加完成
主机热迁移
图 - 迁移1
图 - 迁移2,选择要迁移到目的主机
图 - 迁移过程
图 - 迁移完成
在kvm02上查看虚拟机状态
[[email protected] ~]# virsh list --all Id 名称 状态 ---------------------------------------------------- 7 clsn7 running
虚拟机配置查看方法:
图 - clsn7 虚拟机配置信息
说明:在热迁移的过程中可能会参数丢包的情况,一般不会超过1个包。
[C:\~]$ ping 10.0.0.110 -t 来自 10.0.0.110 的回复: 字节=32 时间=1ms TTL=64 来自 10.0.0.110 的回复: 字节=32 时间=13ms TTL=64 来自 10.0.0.110 的回复: 字节=32 时间=11ms TTL=64 请求超时。 来自 10.0.0.110 的回复: 字节=32 时间=4ms TTL=64 来自 10.0.0.110 的回复: 字节=32 时间<1ms TTL=64 来自 10.0.0.110 的回复: 字节=32 时间<1ms TTL=64
至此,一次热迁移就完成了
1.7 KVM链接克隆
链接克隆脚本
#!/bin/bash # kvm link clone scripts # user clsn # blog: https://www.nmtui.com # 2018-02-06 ### # init if [ $# -ne 2 ] then echo "Usage: $0 OLD_VMNAME NEW_VMNAME" exit 2 fi LOG=/var/log/messages old_vm=$1 new_vm=$2 new_xml="/tmp/${new_vm}.xml" . /etc/init.d/functions # dump old xmlfile virsh dumpxml $old_vm >$new_xml old_disk=`awk -F "'" '/source file/{print $2}' $new_xml` tmp_dir=`dirname $old_disk` new_disk=${tmp_dir}/${new_vm}.qcow2 # make link disk qemu-img create -f qcow2 -b $old_disk $new_disk &>> $LOG # make over xml info sed -i '/uuid/d' $new_xml sed -i '/mac address/d' $new_xml sed -i '2s#'$old_vm'#'$new_vm'#' $new_xml sed -i "s#$old_disk#$new_disk#g" $new_xml sed -i '/source mode/d' $new_xml # import new xml file virsh define $new_xml &>> $LOG # start new vm virsh start $new_vm &>> $LOG if [ $? -eq 0 ] then action "vmhost $new_vm start" /bin/true else action "vmhost $new_vm start" /bin/false echo "log info : $LOG" fi # END \rm $new_xml
说明:
1.7.1 手动克隆
第一步:复制虚拟磁盘文件
第二步:修改xml配置文件
1)name
2)uuid
3)虚拟磁盘存储路径
4)mac地址
1.7.2脚本实现思路
1) 备份old_vm的配置文件,并重定向生成一个新的虚拟机配置文件 2)取出old_vm的磁盘路径 3)创建新的链接磁盘文件 4) 修改xml配置文件 5) 导入新虚拟机 6)启动测试
1.8 参考文献
[1] https://zh.wikipedia.org/wiki/
[2] http://virtual.51cto.com/art/201303/386133.htm
[4] https://zh.wikipedia.org/wiki/基于内核的虚拟机
[6] [ kvm ] 四种简单的网络模型 https://www.cnblogs.com/hukey/p/6436211.html
[7] https://www.cnblogs.com/xieshengsen/p/6215168.html
[8] http://wiki.ubuntu.org.cn/Kvm教程