KVM技术博客-周万春
KVM虚拟化软件安装 宿主机cpu需要开启Intel VT-x虚拟化。 [root@linux-node1 ~]# yum install libvirt* virt-* qemu-kvm* -y libvirt 管理kvm虚拟机的生命周期,启动、暂停、关机、销毁。 virt工具集 virt-install虚拟机的安装 virt-clone虚拟机的克隆。 qemu-kvm 为kvm虚拟机提供虚拟磁盘。 [root@linux-node1 ~]# systemctl start libvirtd.service [root@linux-node1 ~]# systemctl status libvirtd.service 安装好TightVNC Viewer http://www.tightvnc.com [root@linux-node1 ~]# virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name centos7 > --memory 1024 --vcpus 1 --disk /opt/centos2.raw,format=raw,size=10 > --cdrom /opt/CentOS-7.6-x86_64-DVD-1810.iso > --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole 赶紧打开TightVNC Viewer
为了提高虚拟机性能,不用创建swap分区。
KVM虚拟机的virsh日常管理和配置 列表list [root@linux-node1 ~]# virsh list Id Name State ---------------------------------------------------- [root@linux-node1 ~]# virsh list --all Id Name State ---------------------------------------------------- - centos7 shut off 开机start [root@linux-node1 ~]# virsh start centos7 Domain centos7 started 显示运行的虚拟机 [root@linux-node1 ~]# virsh list Id Name State ---------------------------------------------------- 2 centos7 running 显示运行和关闭的虚拟机 [root@linux-node1 ~]# virsh list --all Id Name State ---------------------------------------------------- 2 centos7 running 关机shutdown [root@linux-node1 ~]# virsh shutdown 2 Domain 2 is being shutdown [root@linux-node1 ~]# virsh list --all Id Name State ---------------------------------------------------- - centos7 shut off 拔电源强制关机destroy [root@linux-node1 ~]# virsh start centos7 Domain centos7 started [root@linux-node1 ~]# virsh list --all Id Name State ---------------------------------------------------- 3 centos7 running [root@linux-node1 ~]# virsh destroy centos7 Domain centos7 destroyed [root@linux-node1 ~]# virsh list --all Id Name State ---------------------------------------------------- - centos7 shut off 导出配置dumpxml [root@linux-node1 ~]# virsh dumpxml centos7 > /opt/centos7.xml 删除undefine 推荐:先destroy,再undefine [root@linux-node1 ~]# ls -l /etc/libvirt/qemu/ total 4 -rw------- 1 root root 4070 Mar 25 05:34 centos7.xml drwx------ 3 root root 42 Mar 25 05:09 networks [root@linux-node1 ~]# virsh list --all Id Name State ---------------------------------------------------- - centos7 shut off [root@linux-node1 ~]# virsh undefine centos7 Domain centos7 has been undefined [root@linux-node1 ~]# virsh list --all Id Name State ---------------------------------------------------- [root@linux-node1 ~]# ls -l /etc/libvirt/qemu/ total 0 drwx------ 3 root root 42 Mar 25 05:09 networks 导入配置define [root@linux-node1 ~]# virsh define /opt/centos7.xml Domain centos7 defined from /opt/centos7.xml [root@linux-node1 ~]# virsh list --all Id Name State ---------------------------------------------------- - centos7 shut off 修改配置edit(自带语法检查) [root@linux-node1 ~]# cd /opt/ [root@linux-node1 /opt]# ls -l total 6544808 -rw------- 1 root root 10737418240 Mar 25 06:03 centos2.raw -rw-r--r-- 1 qemu qemu 4588568576 Mar 25 05:26 CentOS-7.6-x86_64-DVD-1810.iso -rw-r--r-- 1 root root 3846 Mar 25 06:08 centos7.xml drwx--x--x 4 root root 28 Mar 24 22:29 containerd [root@linux-node1 /opt]# mv centos2.raw centos7.raw [root@linux-node1 /opt]# ls -l total 6544808 -rw-r--r-- 1 qemu qemu 4588568576 Mar 25 05:26 CentOS-7.6-x86_64-DVD-1810.iso -rw------- 1 root root 10737418240 Mar 25 06:03 centos7.raw -rw-r--r-- 1 root root 3846 Mar 25 06:08 centos7.xml drwx--x--x 4 root root 28 Mar 24 22:29 containerd [root@linux-node1 ~]# virsh start centos7 error: Failed to start domain centos7 error: Cannot access storage file '/opt/centos2.raw': No such file or directory 由于把centos2.raw移走了,导致启动centos7的虚拟机失败,现在就需要使用edit编辑配置文件。 [root@linux-node1 ~]# virsh edit centos7 ... <emulator>/usr/libexec/qemu-kvm</emulator> <disk type='file' device='disk'> <driver name='qemu' type='raw'/> <source file='/opt/centos7.raw'/> <target dev='vda' bus='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/> ... Domain centos7 XML configuration edited. [root@linux-node1 ~]# virsh start centos7 Domain centos7 started [root@linux-node1 ~]# virsh list --all Id Name State ---------------------------------------------------- 5 centos7 running 重命名domrename [root@linux-node1 ~]# virsh domrename centos7 vm-db01 error: Requested operation is not valid: cannot rename active domain 处于激活状态的虚拟机无法重命名。 [root@linux-node1 ~]# virsh destroy centos7 Domain centos7 destroyed [root@linux-node1 ~]# virsh list --all Id Name State ---------------------------------------------------- - centos7 shut off [root@linux-node1 ~]# virsh domrename centos7 vm-db01 Domain successfully renamed [root@linux-node1 ~]# virsh list --all Id Name State ---------------------------------------------------- - vm-db01 shut off 挂起suspend [root@linux-node1 ~]# virsh start vm-db01 Domain vm-db01 started [root@linux-node1 ~]# virsh suspend vm-db01 Domain vm-db01 suspended [root@linux-node1 ~]# virsh list --all Id Name State ---------------------------------------------------- 2 vm-db01 paused 恢复resume [root@linux-node1 ~]# virsh resume vm-db01 Domain vm-db01 resumed [root@linux-node1 ~]# virsh list --all Id Name State ---------------------------------------------------- 2 vm-db01 running 查询虚拟机vnc端口号vncdisplay [root@linux-node1 ~]# virsh vncdisplay vm-db01 :0 使用vnc连接虚拟机。
kvm虚拟机开机启动autostart 开机启动autostart,前提是systemctl enable libvirtd 取消开机启动autostart --disable [root@linux-node1 ~]# systemctl is-enabled libvirtd.service enabled [root@linux-node1 ~]# virsh autostart vm-db01 Domain vm-db01 marked as autostarted [root@linux-node1 ~]# reboot 重启之后即可验证虚拟机vm-db01是否处于运行状态。 [root@linux-node1 ~]# virsh list --all Id Name State ---------------------------------------------------- 1 vm-db01 running [root@linux-node1 ~]# virsh autostart --disable vm-db01 Domain vm-db01 unmarked as autostarted kvm虚拟机console登录 先使用vnc连接虚拟机查看虚拟机的ip地址。
[root@linux-node1 ~]# ssh root@192.168.122.11 The authenticity of host '192.168.122.11 (192.168.122.11)' can't be established. ECDSA key fingerprint is SHA256:FptM/K9jp0UdfRL3l0Aj3qhrWeCL4qfxxSsswM4M8Po. ECDSA key fingerprint is MD5:e4:25:5c:01:f1:2a:a0:9e:0b:e0:28:36:33:6e:6e:fb. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.122.11' (ECDSA) to the list of known hosts. root@192.168.122.11's password: Last login: Wed Mar 25 21:11:53 2020 [root@vm-db01 ~]# [root@vm-db01 ~]# cp -a /boot/grub2/grub.cfg /tmp/ [root@vm-db01 ~]# grubby --update-kernel=ALL --args="console=ttyS0,115200n8" [root@vm-db01 ~]# reboot Connection to 192.168.122.11 closed by remote host. Connection to 192.168.122.11 closed. [root@linux-node1 ~]# 温馨提示:一定要记得敲一次回车哦!否则会一直夯住看不到宿主机终端。 当虚拟机网络有问题的时候,用vnc不方便连接的时候,console连接就可以解燃眉之急了。 [root@linux-node1 ~]# virsh console vm-db01 Connected to domain vm-db01 Escape character is ^] CentOS Linux 7 (Core) Kernel 3.10.0-957.el7.x86_64 on an x86_64 vm-db01 login: root Password: Last login: Wed Mar 25 21:47:45 on ttyS0 [root@vm-db01 ~]# exit logout CentOS Linux 7 (Core) Kernel 3.10.0-957.el7.x86_64 on an x86_64 vm-db01 login: Killed [root@linux-node1 ~]# [root@linux-node1 ~]# ps -ef | grep console root 2931 1776 0 21:49 pts/1 00:00:00 virsh console vm-db01 root 2950 2006 0 21:50 pts/2 00:00:00 grep --color=auto console [root@linux-node1 ~]# kill -9 2931 kvm虚拟机虚拟磁盘格式转换 raw : 裸格式,占用空间比较大,不支持快照功能,读写性能较好。 qcow2 : cow(copy on write)占用空间小,支持快照,性能比raw差一点。 kvm虚拟机的磁盘文件,都是通过qemu-img创建出来的。 常用的qemu-img命令: info 查看某一个虚拟机磁盘文件的信息 create 创建一个虚拟机磁盘文件 resize 调整某一个虚拟机磁盘文件的虚拟磁盘容量大小 convert 虚拟机磁盘文件的格式转换 把raw磁盘格式 转换为 qcow2格式 [root@linux-node1 ~]# qemu-img convert -f raw -O qcow2 /opt/centos7.raw /opt/centos7.qcow2 [root@linux-node1 ~]# ls -l /opt/ total 8603248 -rw-r--r-- 1 qemu qemu 4588568576 Mar 25 05:26 CentOS-7.6-x86_64-DVD-1810.iso -rw-r--r-- 1 root root 2107899904 Mar 25 21:54 centos7.qcow2 -rw------- 1 qemu qemu 10737418240 Mar 25 21:52 centos7.raw -rw-r--r-- 1 root root 3846 Mar 25 06:08 centos7.xml drwx--x--x 4 root root 28 Mar 24 22:29 containerd 让vm-db01虚拟机使用centos7.qcow2的磁盘文件 [root@linux-node1 ~]# virsh shutdown vm-db01 Domain vm-db01 is being shutdown [root@linux-node1 ~]# virsh list --all Id Name State ---------------------------------------------------- - vm-db01 shut off [root@linux-node1 ~]# virsh edit vm-db01 <disk type='file' device='disk'> <driver name='qemu' type='qcow2'/> <source file='/opt/centos7.qcow2'/> <target dev='vda' bus='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/> </disk> Domain vm-db01 XML configuration edited. [root@linux-node1 ~]# virsh start vm-db01 Domain vm-db01 started [root@linux-node1 ~]# virsh list --all Id Name State ---------------------------------------------------- 4 vm-db01 running kvm虚拟机快照管理 创建快照 [root@linux-node1 ~]# virsh snapshot-create vm-db01 Domain snapshot 1585145075 created 查看快照 [root@linux-node1 ~]# virsh snapshot-list vm-db01 Name Creation Time State ------------------------------------------------------------ 1585145075 2020-03-25 22:04:35 +0800 running 如果创建快照的时候没有指定快照名称,kvm默认会以unix时间戳作为快照名称。 还原快照 [root@linux-node1 ~]# virsh snapshot-revert vm-db01 --snapshotname 1585145075 删除快照 [root@linux-node1 ~]# virsh snapshot-delete vm-db01 --snapshotname 1585145075 Domain snapshot 1585145075 deleted [root@linux-node1 ~]# virsh snapshot-list vm-db01 Name Creation Time State ------------------------------------------------------------ kvm虚拟机克隆 完整克隆 [root@linux-node1 ~]# virt-clone --auto-clone -o vm-db01 ERROR Domain with devices to clone must be paused or shutoff. [root@linux-node1 ~]# virsh shutdown vm-db01 Domain vm-db01 is being shutdown [root@linux-node1 ~]# virt-clone --auto-clone -o vm-db01 sh: line 1: 3492 Illegal instruction /sbin/ldconfig -p 2> /dev/null Allocating 'centos7-clone.qcow2' | 10 GB 00:00:06 Clone 'vm-db01-clone' created successfully. [root@linux-node1 ~]# ls -l /opt/ total 12952816 -rw-r--r-- 1 qemu qemu 4588568576 Mar 25 05:26 CentOS-7.6-x86_64-DVD-1810.iso -rw------- 1 root root 2107899904 Mar 25 22:12 centos7-clone.qcow2 -rw-r--r-- 1 root root 2396061696 Mar 25 22:12 centos7.qcow2 -rw------- 1 root root 10737418240 Mar 25 21:58 centos7.raw -rw-r--r-- 1 root root 3846 Mar 25 06:08 centos7.xml drwx--x--x 4 root root 28 Mar 24 22:29 containerd [root@linux-node1 ~]# virsh list --all Id Name State ---------------------------------------------------- - vm-db01 shut off - vm-db01-clone shut off 这个过程,我们也可以手动来完成,主要思路如下: kvm手动克隆虚拟机: 1.复制虚拟磁盘文件。 2.备份vm-db01的虚拟机配置文件,另存为vm-db02。 3.修改vm-db02的虚拟机配置文件。 (1).<name>vm-db02</name> (2).删除uuid (3).删除mac address (4).修改虚拟机磁盘路径 (5).导入vm-db02 (6).检查是否能正常启动 关于连接克隆,官方没有工具实现,只能手动或通过脚本实现。 脚本实现思路: (1).备份old_vm的配置文件,并重定向生成一个新的虚拟机配置文件。 (2).取出old_vm的磁盘路径。 (3).创建新的链接磁盘文件。 (4).修改xml配置文件。 (5).导入新虚拟机。 (6).测试启动。 kvm虚拟机的桥接网络 [root@linux-node1 ~]# virsh iface-bridge bond1 br0 Created bridge br0 with attached device bond1 Bridge interface br0 started [root@linux-node1 ~]# virsh edit vm-db01 <interface type='bridge'> <mac address='52:54:00:e2:43:9f'/> <source bridge='br0'/> <model type='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> </interface> Domain vm-db01 XML configuration edited. [root@linux-node1 ~]# virsh shutdown vm-db01 Domain vm-db01 is being shutdown [root@linux-node1 ~]# virsh list --all Id Name State ---------------------------------------------------- - vm-db01 shut off - vm-db01-clone shut off [root@linux-node1 ~]# virsh start vm-db01 Domain vm-db01 started [root@linux-node1 ~]# virsh list --all Id Name State ---------------------------------------------------- 2 vm-db01 running - vm-db01-clone shut off [root@linux-node1 ~]# virsh console vm-db01 Connected to domain vm-db01 Escape character is ^] CentOS Linux 7 (Core) Kernel 3.10.0-957.el7.x86_64 on an x86_64 vm-db01 login: root Password: Last login: Wed Mar 25 23:26:59 on tty1 [root@vm-db01 ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0 TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=static DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=eth0 UUID=c7d6ccfd-3b6b-4d63-8293-e6abbc4ec2ea DEVICE=eth0 ONBOOT=yes IPADDR=10.0.0.111 PREFIX=255.255.255.0 GATEWAY=10.0.0.2 IPV6_PRIVACY=no [root@vm-db01 ~]# vi /etc/resolv.conf nameserver 10.0.0.2 [root@vm-db01 ~]# //etc/init.d/network restart Shutting down interface eth0: Device 'eth0' successfully disconnected. [ OK ] Shutting down loopback interface: [ OK ] Bringing up loopback interface: [ OK ] Bringing up interface eth0: ipcalc: bad prefix: 255.255.255.0 [ OK ] [root@vm-db01 ~]# ping -c 3 www.baidu.com PING www.a.shifen.com (36.152.44.95) 56(84) bytes of data. 64 bytes from 36.152.44.95 (36.152.44.95): icmp_seq=1 ttl=128 time=74.3 ms 64 bytes from 36.152.44.95 (36.152.44.95): icmp_seq=2 ttl=128 time=58.7 ms 64 bytes from 36.152.44.95 (36.152.44.95): icmp_seq=3 ttl=128 time=58.7 ms --- www.a.shifen.com ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2003ms rtt min/avg/max/mdev = 58.771/63.954/74.310/7.325 ms 基于桥接网络安装虚拟机 virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name centos7 --memory 1024 --vcpus 1 --disk /opt/centos7.qcow2,format=qcow2,size=10 --cdrom /opt/CentOS-7.6-x86_64-DVD-1810.iso --network bridge=br0 --graphics vnc,listen=0.0.0.0 --noautoconsole kvm虚拟机在线热添加硬盘 [root@linux-node1 ~]# qemu-img create -f qcow2 /opt/vdb.qcow2 5G Formatting '/opt/vdb.qcow2', fmt=qcow2 size=5368709120 encryption=off cluster_size=65536 lazy_refcounts=off [root@linux-node1 ~]# virsh attach-disk vm-db01 /opt/vdb.qcow2 vdb --live --cache=none --subdriver=qcow2 Disk attached successfully [root@linux-node1 ~]# virsh console vm-db01 Connected to domain vm-db01 Escape character is ^] CentOS Linux 7 (Core) Kernel 3.10.0-957.el7.x86_64 on an x86_64 vm-db01 login: root Password: Last login: Thu Mar 26 00:21:32 from 10.0.0.11 [root@vm-db01 ~]# fdisk -l Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x000cc7fc Device Boot Start End Blocks Id System /dev/vda1 * 2048 411647 204800 83 Linux /dev/vda2 411648 20971519 10279936 83 Linux Disk /dev/vdb: 5368 MB, 5368709120 bytes, 10485760 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes 虚拟机磁盘卸载 [root@linux-node1 ~]# virsh detach-disk vm-db01 vdb Disk detached successfully [root@vm-db01 ~]# fdisk -l Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x000cc7fc Device Boot Start End Blocks Id System /dev/vda1 * 2048 411647 204800 83 Linux /dev/vda2 411648 20971519 10279936 83 Linux 虚拟机磁盘扩容(要先卸载磁盘,再扩容) [root@linux-node1 ~]# qemu-img resize /opt/vdb.qcow2 +1G Image resized. [root@linux-node1 ~]# virsh attach-disk vm-db01 /opt/vdb.qcow2 vdb --live --cache=none --subdriver=qcow2 Disk attached successfully [root@vm-db01 ~]# fdisk -l Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x000cc7fc Device Boot Start End Blocks Id System /dev/vda1 * 2048 411647 204800 83 Linux /dev/vda2 411648 20971519 10279936 83 Linux Disk /dev/vdb: 6442 MB, 6442450944 bytes, 12582912 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes [root@vm-db01 ~]# xfs_growfs /dev/vdb 卸载磁盘 [root@linux-node1 ~]# virsh detach-disk vm-db01 vdb Disk detached successfully 根分区扩容 (1).在宿主机上关闭虚拟机并调整虚拟机磁盘大小。 [root@linux-node1 ~]# qemu-img resize /opt/centos7.qcow2 +10G (2).在虚拟机中fdisk重新分区。 [root@vm-db01 ~]# fdisk /dev/vda (3).虚拟机重启之后执行xfs_growfs /dev/vda2 如果虚拟机磁盘文件系统是ext4,执行resize2fs /dev/vda2 [root@vm-db01 ~]# reboot [root@vm-db01 ~]# xfs_growfs /dev/vda2 kvm虚拟机热迁移 热迁移描述: 相比KVM虚拟机冷迁移中需要拷贝虚拟机虚拟磁盘文件,kvm虚拟机热迁移无需拷贝虚拟磁盘文件,但是需要迁移到的宿主机之间需要有相同的目录结构虚拟机磁盘文件,也就是共享存储,本文这部分内容通过nfs来实现,当然也可以采用Glusterfs集群文件系统来实现。 热迁移流程: 在kvm01上挂起虚拟机vm01,发送vm的虚拟机配置文件和运行时内存中的数据到kvm02, 接受完毕,kvm02恢复vm01,热迁移完成。 架构图如下
环境要求:
注意:需要互相做好host解析。
操作步骤: (1).在kvm01和kvm02上安装kvm和nfs,配置桥接网卡 yum install libvirt* virt-* qemu-kvm* nfs-utils openssh-askpass -y systemctl start libvirtd.service virsh iface-bridge eth0 br0 (2).在nfs01上安装配置nfs yum install nfs-utils -y mkdir /data vim /etc/exports /data 10.0.0.0/24(rw,async,no_root_squash,no_all_squash) systemctl restart rpcbind systemctl restart nfs (3).kvm01和kvm02挂载共享目录/opt mount -t nfs 10.0.0.31:/data /opt (4).安装一台基于桥接模式的虚拟机 virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name web04 --memory 512,maxmemory=2048 --vcpus 1 --disk /data/web04.qcow2 --boot hd --network bridge=br0 --graphics vnc,listen=0.0.0.0 --noautoconsole 将虚拟机ip配置为10.0.0.111 热迁移的命令: virsh migrate --live --verbose oldboy qemu+ssh://10.0.0.12/system --unsafe 将宿主机10.0.0.11上的kvm虚拟机oldboy迁移到10.0.0.12 (5).在kvm01上安装图形界面、vnc服务端和virt-manager yum groups install "GNOME Desktop" -y yum install tigervnc-server.x86_64 -y yum install virt-manager -y (6).启动vnc服务端 vncserver :1 启动5901端口的vnc服务端 vncserver -kill :1 关闭5901端口的vnc服务端
(7).使用vnc连接宿主机,使用virt-manager进行迁移
这时候会提醒输入密码,就是之前第6步的时候设置的vnc连接密码。
迁移成功!
在迁移的过程中,使用ping虚拟机的ip,发现只丢了一个包。
至此热迁移完成!