KVM虚拟机的搭建及使用
KVM虚拟机简介
KVM,Kernel Virtual Machine的简写,即基于内核的虚拟机。自Linux2.6.20后集成在Linux的各种发行版本中,现已成为学术界的主流虚拟机之一。
KVM的虚拟化实现是使用Linux自身的调度器进行管理,核心源码较少。在KVM中,每一个虚拟机都是由Linux调度程序管理的标准进程。但是KVM虚拟化的实现需要硬件支持(比如Intel的VT技术和AMD的V技术),是基于硬件的完全虚拟化。
KVM官网网址为:https://www.linux-kvm.org
安装kvm虚拟机
- 开启cpu虚拟化
- 虚拟机最低配置,内存 == 4G 磁盘 == 100G
- 操作系统CentOS7.6 1810
1 | [root@localhost ~]# cat /proc/cpuinfo | grep vm |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | [root@localhost ~]# yum install -y libvirt virt-install qemu-kvm libvirt # 虚拟机管理, kvm xen lxc等软件 virt-install # 虚拟机的安装和克隆,相当于命令行操作工具 qemu-kvm # 管理虚拟机的磁盘 # 可选的安装包 "GNOME Desktop" #安装GNOME桌面环境 qemu-kvm #KVM模块 qemu-kvm-tools #KVM调试工具,可不安装 qemu-img #qemu组件,创建磁盘、启动虚拟机等 bridge-utils #网络支持工具 libvirt #虚拟机管理工具 virt-manager #图形界面管理虚拟机,在虚拟机桌面上的管理工具 libguestfs-tools #用来管理虚拟机磁盘格式 # 启动服务 [root@localhost ~]# systemctl start libvirtd.service [root@localhost ~]# systemctl enable libvirtd.service [root@localhost ~]# systemctl status libvirtd.service ● libvirtd.service - Virtualization daemon Loaded: loaded (/usr/lib/systemd/system/libvirtd.service; enabled; vendor preset: enabled) Active: active (running) since 六 2022-07-09 13:43:43 CST; 11min ago Docs: man:libvirtd(8) |
安装一台centos虚拟机
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | mkdir -p /data/vm_disk virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name=centos7_web01 --memory 1024 --vcpus 1 --disk /opt/centos7_web01.raw,format=raw,size=10 --cdrom=/opt/CentOS-7-x86_64-DVD-1810.iso --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole 开始安装...... 正在分配 'centos7_web01.raw' | 10 GB 00:00:00 域安装仍在进行。您可以重新连接 到控制台以便完成安装进程。 # 安装虚拟机的参数 # 参数 参数说明 --virt-type # 要使用的管理程序名称 (kvm, qemu, xen, ...) --os-type # 系统类型 linux unix windows --os-variant # 客户机上安装的操作系统,如:'fedora18'、'rhel7'、'winxp' 等,具体的通过。 # osinfo-query os 命令查询支持的操作系统。 -n/--name # 客户机实例名称 --memory # 配置客户机虚拟内存大小 --vcpus # 配置客户机虚拟 CPU数量。 --disk # 指定存储的各种选项。 raw #不支持做快照,性能好。qcow2 #支持快照。 -cdrom # iso镜像位置 -w/--network # 配置客户机网络接口。 --graphics # 配置客户机显示设置。 --noautoconsole # 不要自动尝试连接到客户端控制台 --autostart # 主机启动时自动启动域。 --noreboot # 安装完成后不重新启动客户机。 # 生成的数据盘 [root@localhost ~]# ll /data/vm_disk/centos7_web01.raw -rw------- 1 qemu qemu 10737418240 7月 9 14:16 /data/vm_disk/centos7_web01.raw # 生成虚拟机的配置xml文件,里面记录了虚拟机的各个硬件配置信息,可以使用这个xml来定义虚拟机。 [root@localhost ~]# ll /etc/libvirt/qemu/centos7_web01.xml -rw------- 1 root root 3933 7月 9 14:16 /etc/libvirt/qemu/centos7_web01.xml # 保存了网络配置信息 [root@localhost ~]# cat /etc/libvirt/qemu/networks/default.xml # 虚拟机控制台默认监听5900 端口,如果有下一台虚拟机就是5901端口,使用vnc连接这个端口。 [root@localhost ~]# ss -lntup | grep kvm tcp LISTEN 0 1 *:5900 *:* users:(("qemu-kvm",pid=10283,fd=18)) |
(参考步骤)[https://www.cnblogs.com/gshelldon/p/16460865.html]
然后就是正常的centos安装步骤。
虚拟机配置文件
导出配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | # 编辑配置文件 virsh edit centos7_web01 # 开机状态下导出 [root@localhost ~]# virsh list --all| grep runn 9 centos7_web01 running [root@localhost ~]# virsh dumpxml centos7_web01 > centos7_web01_bak.xml # 关机状态下导出 [root@localhost ~]# virsh list --all| grep centos7_web01 - centos7_web01 关闭 [root@localhost ~]# virsh dumpxml centos7_web01 > centos7_web01_shutdown_bak.xml # 删除配置文件 [root@localhost ~]# rm -fr /etc/libvirt/qemu/centos7_web01.xml [root@localhost ~]# systemctl restart libvirtd [root@localhost ~]# virsh list --all Id 名称 状态 ---------------------------------------------------- # 恢复虚拟机 [root@localhost ~]# virsh define centos7_web01_bak.xml 定义域 centos7_web01(从 centos7_web01_bak.xml) [root@localhost ~]# virsh list --all Id 名称 状态 ---------------------------------------------------- [root@localhost ~]# virsh define centos7_web01_bak.xml 定义域 centos7_web01(从 centos7_web01_bak.xml) [root@localhost ~]# virsh list --all Id 名称 状态 ---------------------------------------------------- - centos7_web01 关闭 [root@localhost ~]# virsh start centos7_web01 域 centos7_web01 已开始 [root@localhost ~]# virsh list |grep run 1 centos7_web01 running |
挂载光驱修改启动项
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | [root@localhost ~]# virsh destroy centOS7 域 centOS7 被删除 [root@localhost ~]# virsh edit centOS7 # ......省略...... <os> <type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type> <boot dev='cdrom'/> # 第一启动项 </os> # ......省略...... <disk type='file' device='cdrom'> # 光盘 <driver name='qemu' type='raw'/> <source file='/opt/CentOS-7-x86_64-Minimal-2009.iso'/> <target dev='hda' bus='ide'/> <readonly/> <address type='drive' controller='0' bus='0' target='0' unit='0'/> </disk> # 编辑之后启动 |
虚拟机开启启动
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | # 前提libvir[root@localhost ~]# systemctl is-enabled libvirtd enabled [root@localhost ~]# virsh autostart centOS7 域 centOS7标记为自动开始 [root@localhost ~]# ll /etc/libvirt/qemu/autostart/ 总用量 0 lrwxrwxrwx 1 root root 29 7月 9 21:26 centOS7.xml -> /etc/libvirt/qemu/centOS7.xml #关闭开机自启动 [root@kvm01 ~]# virsh autostart --disable Centos7_db01 Domain Centos7_db01 unmarked as autostarted [root@kvm01 ~]# ll /etc/libvirt/qemu/autostart/ total 0 |
磁盘管理
1 创建磁盘
1 2 3 4 5 6 7 8 9 10 11 12 13 | # 创建一块2G的磁盘 [root@localhost ~]# qemu-img create -f qcow2 /data/vm_disk/test.qcow2 2G Formatting '/data/vm_disk/test.qcow2', fmt=qcow2 size=2147483648 encryption=off cluster_size=65536 lazy_refcounts=off [root@localhost ~]# qemu-img info /data/vm_disk/test.qcow2 image: /data/vm_disk/test.qcow2 file format: qcow2 virtual size: 2.0G (2147483648 bytes) disk size: 196K cluster_size: 65536 Format specific information: compat: 1.1 lazy refcounts: false |
2 格式转换
把raw的磁盘转换为qcow2格式的磁盘再启动虚拟机。
本人提供Oracle(OCP、OCM)、MySQL(OCP)、PostgreSQL(PGCA、PGCE、PGCM)等数据库的培训和考证业务,私聊QQ646634621或微信db_bao,谢谢!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | [root@localhost ~]# virsh shutdown centOS7 域 centOS7 被关闭 [root@localhost vm_disk]# qemu-img convert -f raw -O qcow2 centos7_web01.raw centOS7.qcow2 [root@localhost vm_disk]# ll 总用量 4785756 -rw-r--r-- 1 root root 1639579648 7月 9 21:39 centOS7.qcow2 -rw------- 1 root root 10737418240 7月 9 21:35 centos7_web01.raw -rw-r--r-- 1 root root 197120 7月 9 21:32 test.qcow2 # 1、修改虚拟机配置文件 <disk type='file' device='disk'> <driver name='qemu' type='qcow2'/> # 硬件类型也需要修改 <source file='/data/vm_disk/centOS7.qcow2'/> # 修改启动的镜像文件 <target dev='vda' bus='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/> </disk> # 2、启动配置文件 [root@localhost ~]# virsh start centOS7 域 centOS7 已开始 [root@localhost ~]# virsh list | grep run 3 centOS7 running |
快照管理
raw 格式不支持快照功能
1 创建快照
1 2 3 | # 快照的默认名称是时间戳 [root@localhost ~]# virsh snapshot-create centOS7 已生成域快照 1657374440 |
2 查看快照
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | [root@localhost ~]# virsh snapshot-list centOS7 名称 生成时间 状态 ------------------------------------------------------------ 1657374440 2022-07-09 21:47:20 +0800 running [root@localhost ~]# virsh snapshot-list centOS7 名称 生成时间 状态 ------------------------------------------------------------ 1657374440 2022-07-09 21:47:20 +0800 running # 开机状态下的快照 1657374776 2022-07-09 21:52:56 +0800 shutoff # 关机状态下的快照 # 查看快照的详细信息 [root@localhost ~]# virsh snapshot-info centOS7 --snapshotname 1657374440 名称: 1657374440 域: centOS7 当前: 否 状态: running 位置: 内部 上级: - 下级: 1 降序: 1 元数据: 是 # 快照生成的位置 [root@localhost ~]# ll /var/lib/libvirt/qemu/snapshot/centOS7/ 总用量 16 -rw------- 1 root root 4824 7月 9 21:52 1657374440.xml -rw------- 1 root root 4846 7月 9 21:52 1657374776.xml |
3 恢复快照
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | # 1、登陆破坏 [root@localhost ~]# virsh console centOS7 连接到域 centOS7 换码符为 ^] centos7 login: root Password: Last failed login: Sat Jul 9 21:58:25 CST 2022 on ttyS0 There was 1 failed login attempt since the last successful login. Last login: Sat Jul 9 21:21:37 on ttyS0 [root@centos7 ~]# rm -fr /* # ------------省略----------- [root@centos7 ~]# bash -bash: /usr/bin/bash: /lib64/ld-linux-x86-64.so.2: bad ELF interpreter: No such file or directory # 2、恢复快照 [root@localhost ~]# virsh snapshot-list centOS7 名称 生成时间 状态 ------------------------------------------------------------ 1657374440 2022-07-09 21:47:20 +0800 running 1657374776 2022-07-09 21:52:56 +0800 shutoff # 恢复快照 [root@localhost ~]# virsh snapshot-revert centOS7 1657374440 [root@localhost ~]# virsh snapshot-list centOS7 名称 生成时间 状态 ------------------------------------------------------------ 1657374440 2022-07-09 21:47:20 +0800 running 1657374776 2022-07-09 21:52:56 +0800 shutoff [root@localhost ~]# virsh snapshot-revert centOS7 1657374440 # 恢复正常 [root@localhost ~]# virsh console centOS7 连接到域 centOS7 换码符为 ^] CentOS Linux 7 (Core) Kernel 3.10.0-1160.el7.x86_64 on an x86_64 centos7 login: root Password: Last login: Sat Jul 9 21:21:37 on ttyS0 [root@centos7 ~]# bash |
4 删除快照
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | [root@localhost ~]# virsh snapshot-list centOS7 名称 生成时间 状态 ------------------------------------------------------------ 1657374440 2022-07-09 21:47:20 +0800 running 1657374776 2022-07-09 21:52:56 +0800 shutoff # 删除快照 [root@localhost ~]# virsh snapshot-delete centOS7 1657374440 已删除域快照 1657374440 [root@localhost ~]# virsh snapshot-list centOS7 名称 生成时间 状态 ------------------------------------------------------------ 1657374776 2022-07-09 21:52:56 +0800 shutoff [root@localhost ~]# ll /var/lib/libvirt/qemu/snapshot/centOS7/ 总用量 8 -rw------- 1 root root 4795 7月 9 22:04 1657374776.xml |
硬盘管理
1 创建硬盘
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | [root@localhost ~]# qemu-img create -f qcow2 /data/vm_disk/disk1.qcow2 4G Formatting 'disk1.qcow2', fmt=qcow2 size=4294967296 encryption=off cluster_size=65536 lazy_refcounts=off [root@localhost ~]# ll /data/vm_disk/disk1.qcow2 -rw-r--r-- 1 root root 197120 7月 9 22:20 /data/vm_disk/disk1.qcow2 [root@localhost ~]# qemu-img info /data/vm_disk/disk1.qcow2 image: /data/vm_disk/disk1.qcow2 file format: qcow2 virtual size: 4.0G (4294967296 bytes) disk size: 196K cluster_size: 65536 Format specific information: compat: 1.1 lazy refcounts: false |
2 给centos7主机添加硬盘
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | # 如果不加--config 只是临时生效,重启之后配置就丢失了。 [root@localhost ~]# virsh attach-disk centOS7 /data/vm_disk/disk1.qcow2 vdb --live --cache=none --subdriver=qcow2 成功附加磁盘 vdb #第二块虚拟磁盘 --live #在线热添加 --cache=none #开启宿主机对客户机的镜像的读取缓存 off 关闭 --subdriver #驱动类型 --config #会修改虚拟机的配置文件 # 进入到虚拟机中查看 [root@localhost ~]# virsh console centOS7 连接到域 centOS7 换码符为 ^] [root@centos7 ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sr0 11:0 1 1.1G 0 rom vda 253:0 0 10G 0 disk ├─vda1 253:1 0 1G 0 part /boot └─vda2 253:2 0 8G 0 part / vdb 253:16 0 4G 0 disk # 卸载磁盘 [root@localhost ~]# virsh detach-disk CentOS7 vdb |
3 给硬盘扩容
1 2 3 | # 硬盘扩容不支持快照过的硬盘,增加2G [root@localhost ~]# qemu-img resize /data/vm_disk/centOS7.qcow2 +2G Image resized. |
虚拟机需要重启。
4 给虚拟机添加光驱
1 2 3 4 5 6 7 8 9 | # 使用ide方式挂载,不支持热插拔 virsh attach-disk {instance_name} {iso_path} hda --sourcetype block --driver qemu --subdriver raw --type cdrom # 使用scsi方式挂载:支持热插拔 virsh attach-disk {instance_name} {iso_path} sda --sourcetype block --driver qemu --subdriver raw --type cdrom virsh change-media instance_name hdb(光驱盘符) ISO的位置(绝对路径) --config # 更新配置文件,不加此参数重启虚拟机之后会失效。 |
KVM的克隆
- 虚拟机必须在关闭的情况下进行扩容。
1 完整克隆
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | [root@centos7 ~]# virt-clone --auto-clone -o centOS7 -n centOS7_new 正在分配 'centOS7_new.qcow2' | 12 GB 00:00:21 成功克隆 'centOS7_new'。 -o ORIGINAL_GUEST, --original ORIGINAL_GUEST Name of the original guest; The status must be shut off or paused. --auto-clone Auto generate clone name and storage paths from the original guest configuration. -n NEW_NAME, --name NEW_NAME Name for the new guest [root@centos7 ~]# ll /etc/libvirt/qemu/ 总用量 16 drwxr-xr-x 2 root root 25 7月 9 21:29 autostart -rw------- 1 root root 4310 7月 10 13:39 centOS7_new.xml -rw------- 1 root root 4298 7月 9 23:18 centOS7.xml drwx------. 3 root root 42 7月 9 17:02 networks [root@centos7 ~]# virsh list --all Id 名称 状态 ---------------------------------------------------- - centOS7 关闭 - centOS7_new 关闭 |
2 使用复制配置文件的方式进行完整克隆
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | # 1、复制磁盘的镜像文件 [root@centos7 vm_disk]# cp centOS7_new.qcow2 centOS7_new_copy.qcow2 # 2、导出源客户机的配置信息。 [root@centos7 vm_disk]# virsh dumpxml centOS7_new > centOS7_new_copy.xml [root@kvm ~]# mv centOS7_new_copy.xml /etc/libvirt/qemu/ # 3、修改xml配置文件地址。 <name>centOS7_new_copy</name> 删除UUID一行 <source file='/data/vm_disk/centOS7_new_copy.qcow2'/> 删除mac地址 # 4、根据配置文件定义新的虚拟机 [root@kvm ~]# virsh define /etc/libvirt/qemu/centOS7_new_copy.xml 定义域 centOS7_new_copy(从 /etc/libvirt/qemu/centOS7_new_copy.xml) # 5、查看 [root@kvm ~]# virsh list --all Id 名称 状态 ---------------------------------------------------- - centOS7 关闭 - centOS7_new 关闭 - centOS7_new_copy 关闭 [root@kvm ~]# virsh start centOS7_new_copy 域 centOS7_new_copy 已开始 [root@kvm ~]# virsh list Id 名称 状态 ---------------------------------------------------- 11 centOS7_new_copy running [root@kvm ~]# virsh console centOS7_new_copy 连接到域 centOS7_new_copy 换码符为 ^] CentOS Linux 7 (Core) Kernel 3.10.0-1160.el7.x86_64 on an x86_64 centos7 login: |
3 链接克隆
可以根据流程变换为脚本的方式来进行克隆。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | # 1、创建一个基于链接克隆的虚拟磁盘文件 [root@kvm vm_disk]# qemu-img create -f qcow2 -b centOS7.qcow2 centOS7_link.qcow2 Formatting 'centOS7_link.qcow2', fmt=qcow2 size=12884901888 backing_file='centOS7.qcow2' encryption=off cluster_size=65536 lazy_refcounts=off # 2、导出源客户机的配置信息 [root@kvm ~]# virsh dumpxml centOS7 > /etc/libvirt/qemu/centOS7_link.xml # 3.修改配置信息 [root@kvm ~]# vi /etc/libvirt/qemu/centOS7_link.xml <name>db01</name> 删除UUID一行 <source file='/opt/db01.qcow2'/> 删除mac地址 # 4、重新定义虚拟机 [root@kvm ~]# virsh define /etc/libvirt/qemu/centOS7_link.xml 定义域 centOS7_link(从 /etc/libvirt/qemu/centOS7_link.xml) [root@kvm ~]# virsh list --all|grep link - centOS7_link 关闭 # 开机检查 [root@kvm ~]# virsh start centOS7_link 域 centOS7_link 已开始 [root@kvm ~]# virsh console centOS7_link 连接到域 centOS7_link 换码符为 ^] CentOS Linux 7 (Core) Kernel 3.10.0-1160.el7.x86_64 on an x86_64 centos7 login: |
KVM 虚拟机的桥接网络
1 nat上网
2 桥接模式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | # 添加桥接网卡 virsh iface-bridge ens33 br0 [root@kvm ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33 DEVICE=ens33 ONBOOT=yes BRIDGE="br0" [root@kvm ~]# cat /etc/sysconfig/network-scripts/ifcfg-br0 DEVICE="br0" ONBOOT="yes" TYPE="Bridge" BOOTPROTO="none" IPADDR="10.4.7.20" NETMASK="255.255.255.0" GATEWAY="10.4.7.254" IPV6INIT="yes" IPV6_AUTOCONF="yes" DHCPV6C="no" STP="on" DELAY="0" [root@kvm ~]# brctl show bridge name bridge id STP enabled interfaces br0 8000.000c29131e87 yes ens33 virbr0 8000.5254007db86f yes virbr0-nic vnet0 # 2、编辑虚拟机配置文件 <interface type='bridge'> # 修改的网络类型 <mac address='52:54:00:9e:35:b0'/> <source bridge='br0'/> # 桥接的网卡 <model type='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> </interface> # 3、启动虚拟机 [root@kvm ~]# virsh start centOS7 域 centOS7 已开始 # 4、登陆虚拟机查看 [root@kvm ~]# virsh console centOS7 连接到域 centOS7 换码符为 ^] CentOS Linux 7 (Core) Kernel 3.10.0-1160.el7.x86_64 on an x86_64 centos7 login: root Password: Last login: Sun Jul 10 00:52:22 on tty1 [root@centos7 ~]# [root@centos7 ~]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 52:54:00:9e:35:b0 brd ff:ff:ff:ff:ff:ff inet 10.4.7.130/24 brd 10.4.7.255 scope global noprefixroute dynamic eth0 valid_lft 1790sec preferred_lft 1790sec inet6 fe80::5054:ff:fe9e:35b0/64 scope link valid_lft forever preferred_lft forever # 5、物理机登陆查看 [E:\~]$ ssh root@10.4.7.130 Connecting to 10.4.7.130:22... Connection established. To escape to local shell, press 'Ctrl+Alt+]'. Last failed login: Sun Jul 10 15:09:31 CST 2022 from 10.4.7.3 on ssh:notty There was 1 failed login attempt since the last successful login. Last login: Sun Jul 10 15:06:51 2022 [root@centos7 ~]# |
虚拟机的迁移
1 冷迁移虚拟机
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | # 1、准备新的虚拟机 [root@kvm02 ~]# yum install libvirt virt-install qemu-kvm -y [root@kvm02 ~]# systemctl enable libvirtd.service [root@kvm02 ~]# systemctl start libvirtd.service [root@kvm02 ~]# virsh list --all Id 名称 状态 ---------------------------------------------------- # 2、源主机,迁移的虚拟机是关闭状态 # 导出配置 [root@kvm ~]# virsh dumpxml centOS7_new > centOS7_new.xml # 检查虚拟机的磁盘是否是qcow2格式如果是raw格式会很大。 [root@kvm ~]# ll /data/vm_disk/centOS7_new.qcow2 -rw------- 1 root root 1984626688 7月 10 13:39 /data/vm_disk/centOS7_new.qcow2 # 3、将磁盘和配置文件传到新主机 [root@kvm ~]# scp centOS7_new.xml root@10.4.7.30:/etc/libvirt/qemu/ [root@kvm ~]# scp /data/vm_disk/centOS7_new.qcow2 root@10.4.7.30:/data/vm_disk/ # 目的主机上重新定义虚拟机 [root@kvm02 ~]# virsh define /etc/libvirt/qemu/centOS7_new.xml [root@kvm02 ~]# virsh list --all Id 名称 状态 ---------------------------------------------------- - centOS7_new 关闭 # 启动虚拟机 [root@kvm02 ~]# virsh start centOS7_new 域 centOS7_new 已开始 [root@kvm02 ~]# virsh list Id 名称 状态 ---------------------------------------------------- 3 centOS7_new running |
2 热迁移
磁盘文件 : 共享存储
热迁移的流程:
在运行的过程中,发送Kvm01主机的配置文件及运行在内存中的数据到Kvm02
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | # 1、部署共享存储 [root@kvm ~]# yum install -y nfs-utils [root@kvm ~]# vim /etc/exports /data/vm_disk 10.4.7.0/24(rw,sync,no_root_squash,no_all_squash) [root@kvm ~]# systemctl restart rpcbind [root@kvm ~]# systemctl restart nfs # ------源主机------ # 使用共享存储存放虚拟机的磁盘文件 [root@kvm ~]# mkdir /share_store [root@kvm ~]# mount -t nfs 10.4.7.20:/data/vm_disk/ /share_store/ [root@kvm ~]# df -h | grep share 10.4.7.20:/data/vm_disk 80G 3.8G 77G 5% /share_store # ------目的主机------ [root@kvm02 ~]# mkdir /share_store/ [root@kvm02 ~]# mount -t nfs 10.4.7.20:/data/vm_disk/ /share_store/ [root@kvm02 ~]# df -h|grep sha 10.4.7.20:/data/vm_disk 80G 3.8G 77G 5% /share_store # 2、创建主机为共享存储的虚拟机 [root@kvm vm_disk]# mv centOS7_new.qcow2 centOS7_live_migration.qcow2 [root@kvm share_store]# virsh dumpxml centOS7 > /etc/libvirt/qemu/centOS7_live_migration.xml [root@kvm share_store]# vim /etc/libvirt/qemu/centOS7_live_migration.xml <name>centOS7_live_migration</name> # 修改主机名 删除uuid <source file='/share_store/centOS7_live_migration.qcow2'/> # 启动地址 删除mac地址 [root@kvm share_store]# virsh define /etc/libvirt/qemu/centOS7_live_migration.xml 定义域 centOS7_live_migration(从 /etc/libvirt/qemu/centOS7_live_migration.xml) [root@kvm share_store]# virsh start centOS7_live_migration 域 centOS7_live_migration 已开始 # 3、开始进行热迁移 # 需要添加hosts解析记录 10.4.7.20 kvm 10.4.7.30 kvm02 # 4、开始迁移 [root@kvm ~]# virsh migrate --live --verbose centOS7_live_migration qemu+ssh://10.4.7.30/system --unsafe root@10.4.7.30's password: 迁移: [100 %] |
virt-manager管理管理虚拟机
一个图形化的管理工具,在安装了桌面版的系统上操作。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | # 1.安装图形界面 [root@kvm01 ~]# yum groupinstall -y "GNOME Desktop" # 2.安装vnc的服务端 [root@kvm01 ~]# yum install -y tigervnc-server #3.给vnc设置个密码 [root@kvm01 ~]# vncpasswd Password: Verify: Would you like to enter a view-only password (y/n)? n A view-only password is not used # 4.开启vnc [root@kvm01 ~]# vncserver :1 xauth: file /root/.Xauthority does not exist New 'kvm01:1 (root)' desktop is kvm01:1 Creating default startup script /root/.vnc/xstartup Creating default config /root/.vnc/config Starting applications specified in /root/.vnc/xstartup Log file is /root/.vnc/kvm01:1.log [root@kvm01 ~]# netstat -lntp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:5901 0.0.0.0:* LISTEN 16460/Xvnc tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd tcp 0 0 0.0.0.0:6001 0.0.0.0:* LISTEN 16460/Xvnc tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 9430/dnsmasq tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 9043/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 9319/master tcp6 0 0 :::5901 :::* LISTEN 16460/Xvnc tcp6 0 0 :::111 :::* LISTEN 1/systemd tcp6 0 0 :::6001 :::* LISTEN 16460/Xvnc tcp6 0 0 :::22 :::* LISTEN 9043/sshd tcp6 0 0 ::1:25 :::* LISTEN 9319/master #安装管理工具 [root@kvm01 ~]# yum install -y virt-manager #安装ssh插件 [root@kvm01 ~]# yum install -y openssh-askpass |
KVM虚拟机常用的管理命令
1.查看正在运行的虚拟机
1 2 3 4 5 6 7 8 9 10 11 12 13 | [root@kzkvm2022 ~]# virsh list Id 名称 状态 ---------------------------------------------------- 1 centos7.3-zabbix running 2 centos7.3-zhibo running 3 centos7.3-ansible running 5 centos7-itcmdb running 6 centos7.8-ferry running 7 centos7.0-fms running 8 centos76u-jmss running 12 centos7.8-mysql01 running 14 centos7.8-mysql02 running [root@kzkvm2022 ~]# |
2.查看所有虚拟机状态列表
这里将会显示在宿主机上的所有虚拟机状态,包括正在运行的和已关闭的虚拟机:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | [root@kzkvm2022 ~]# virsh list --all Id 名称 状态 ---------------------------------------------------- 1 centos7.3-zabbix running 2 centos7.3-zhibo running 3 centos7.3-ansible running 5 centos7-itcmdb running 6 centos7.8-ferry running 7 centos7.0-fms running 8 centos76u-jmss running 12 centos7.8-mysql01 running 14 centos7.8-mysql02 running - centos7.8-itfreey 关闭 - centos7.8-k8s01 关闭 - centos76u-001 关闭 - centos76u-002 关闭 [root@kzkvm2022 ~]# |
3.查看虚拟机信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | [root@kzkvm2022 ~]# virsh dominfo centos76u-jmss Id: 8 名称: centos76u-jmss UUID: 42e172d6-5c79-4f1f-8d22-1ee352d00f6c OS 类型: hvm 状态: running CPU: 4 CPU 时间: 1480233.7s 最大内存:8388608 KiB 使用的内存:8388608 KiB 持久: 是 自动启动: 禁用 管理的保存: 否 安全性模式:none 安全性 DOI: 0 [root@kzkvm2022 ~]# |
4.启动一个虚拟机
启动一个虚拟机,直接用virsh start 虚拟机名称
,即启动一个虚拟机:
1 2 3 4 | [root@kzkvm2022 ~]# virsh start centos7.8-k8s01 域 centos7.8-k8s01 已开始 [root@kzkvm2022 ~]# |
5.关闭一个虚拟机
以刚才新启动的虚拟机centos7.8-k8s01
为例,将其关闭,只需将start
改成shutdown
可完成一台虚拟机的关闭工作。
1 2 3 4 | [root@kzkvm2022 ~]# virsh shutdown centos7.8-k8s01 域 centos7.8-k8s01 被关闭 [root@kzkvm2022 ~]# |
上面操作可能无法关闭虚拟机,需要安装ACPI
,高级配置和电源管理接口,并设置随机启动。
1 2 | [root@kzkvm2022 ~]# yum install -y acpid acpid-sysvinit [root@kzkvm2022 ~]# |
6.设置虚拟机随主机一起启动
1 2 3 4 | [root@kzkvm2022 ~]# virsh autostart centos76u-jmss 域 centos76u-jmss标记为自动开始 [root@kzkvm2022 ~]# |
7.取消虚拟机自动启动
1 2 3 4 | [root@kzkvm2022 ~]# virsh autostart --disable centos76u-jmss 域 centos76u-jmss取消标记为自动开始 [root@kzkvm2022 ~]# |
8.强制关闭虚拟机电源
有时候用shutdown
无法关闭虚拟机的时候,就需要强制关闭电源的方式来关闭虚拟机。
1 2 3 4 | [root@kzkvm2022 ~]# virsh destroy centos7.8-k8s01 域 centos7.8-k8s01 被删除 [root@kzkvm2022 ~]# |
也可以接着查看一下状态:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | [root@kzkvm2022 ~]# virsh list --all Id 名称 状态 ---------------------------------------------------- 1 centos7.3-zabbix running 2 centos7.3-zhibo running 3 centos7.3-ansible running 5 centos7-itcmdb running 6 centos7.8-ferry running 7 centos7.0-fms running 8 centos76u-jmss running 12 centos7.8-mysql01 running 14 centos7.8-mysql02 running - centos7.8-itfreey 关闭 - centos7.8-k8s01 关闭 - centos76u-001 关闭 - centos76u-002 关闭 [root@kzkvm2022 ~]# |
centos7.8-k8s01
虚拟机已关闭。
9.重命名虚拟机名称
在关机状态下对centos7.0
的虚拟机命名为MYSLB01
:
1 2 3 4 | [root@kzkvm2022 ~]# virsh domrename centos7.0 MYSLB01 Domain successfully renamed [root@kzkvm2022 ~]# |
10.虚拟机的其他管理方式
10.1通过交互式试管理虚拟机
直接virsh
命令回车,再执行子命令:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | [root@kzkvm2022 ~]# virsh 欢迎使用 virsh,虚拟化的交互式终端。 输入:'help' 来获得命令的帮助信息 'quit' 退出 virsh # list Id 名称 状态 ---------------------------------------------------- 1 centos7.3-zabbix running 2 centos7.3-zhibo running 3 centos7.3-ansible running 5 centos7-itcmdb running 6 centos7.8-ferry running 7 centos7.0-fms running 8 centos76u-jmss running 12 centos7.8-mysql01 running 14 centos7.8-mysql02 running virsh # |
10.2通过控制台管理虚拟机
这里前提是保证虚拟机已经运行的状态:
1 | [root@kzkvm2022 ~]# virsh console centos7.8-k8s01 |
注:这里有个问题,使用该命令可能会卡死,然后进不了系统,登录客户机系统,解决办法如下:
1)查看/etc/securetty
文件中有没有ttyS0
,没有就进行追加:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | [root@centos7.8-k8s01 ~]# cat /etc/securetty console vc/1 vc/2 vc/3 vc/4 vc/5 vc/6 vc/7 vc/8 vc/9 vc/10 vc/11 tty1 tty2 tty3 tty4 tty5 tty6 tty7 tty8 tty9 tty10 tty11 ttyS0 ttysclp0 sclp_line0 3270/tty1 hvc0 hvc1 hvc2 hvc3 hvc4 hvc5 hvc6 hvc7 hvsi0 hvsi1 hvsi2 xvc0 [root@centos7.8-k8s01 ~]# |
2)在/etc/grub.conf
文件中为内核添加参数
1 | console=ttyS0 |
这步要注意:
console=ttyS0
一定要放在kernel
这行中(大约在第16行),不能单独一行,即console=ttyS0
是kernel
的一个参数,不是单独的,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | # cat -n /etc/grub.conf # grub.conf generated by anaconda # # Note that you do not have to rerun grub after making changes to this file # NOTICE: You have a /boot partition. This means that # all kernel and initrd paths are relative to /boot/, eg. # root (hd0,0) # kernel /vmlinuz-version ro root=/dev/mapper/VolGroup-lv_root # initrd /initrd-[generic-]version.img #boot=/dev/vda default=0 timeout=5 splashimage=(hd0,0)/grub/splash.xpm.gz hiddenmenu title Red Hat Enterprise Linux (2.6.32-431.el6.x86_64) root (hd0,0) kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=/dev/mapper/VolGroup-lv_root rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD rd_LVM_LV=VolGroup/lv_swap SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=VolGroup/lv_root KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet console=ttyS0 initrd /initramfs-2.6.32-431.el6.x86_64.img |
3)在/etc/inittab
中添加agetty
:
1 | S0:12345:respawn:/sbin/agetty ttyS0 115200 |
4)重启客户机
查看虚拟机
1 2 3 4 5 6 7 8 9 10 11 | # 只能查看到正在运行的虚拟机 [root@localhost ~]# virsh list Id 名称 状态 ---------------------------------------------------- 2 centos7_web01 running # 所有状态的虚拟机 [root@localhost ~]# virsh list --all Id 名称 状态 ---------------------------------------------------- 2 centos7_web01 running |
关闭虚拟机
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | [root@localhost ~]# virsh list --all Id 名称 状态 ---------------------------------------------------- 2 centos7_web01 running # 根据域名称或者是Id来关闭虚拟机。 [root@localhost ~]# virsh shutdown 2 域 2 被关闭 [root@localhost ~]# virsh list --all Id 名称 状态 ---------------------------------------------------- - centos7_web01 关闭 # 强制关机相当于直接拔掉电源 virsh destroy centos7_web01 |
启动虚拟机
1 2 3 4 5 6 7 | [root@localhost ~]# virsh start centos7_web01 域 centos7_web01 已开始 [root@localhost ~]# virsh list Id 名称 状态 ---------------------------------------------------- 3 centos7_web01 running |
获取控制台
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | # 1、通过ssh的方式 [root@localhost ~]# ssh root@192.168.122.186 The authenticity of host '192.168.122.186 (192.168.122.186)' can't be established. ECDSA key fingerprint is SHA256:uZBFYR0qhsK21WdFBqMTr0hFTKy/KnL5VCmRFFc7Yj8. ECDSA key fingerprint is MD5:22:6c:a8:e0:9b:1c:77:79:88:80:66:e6:77:a7:8f:1f. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.122.186' (ECDSA) to the list of known hosts. root@192.168.122.186's password: Last login: Sat Jul 9 16:02:07 2022 # 通过获取控制台,linux 无图形界面的方式 [root@localhost ~]# virsh vncdisplay centos7_web01 :0 virsh console 虚拟机ID/域名称 # 1、登陆虚拟机设置 [root@localhost ~]# ssh root@192.168.122.1 [root@centos7 ~]# grubby --update-kernel=ALL --args="console=ttyS0,115200n8" [root@centos7 ~]# reboot Connection to 192.168.122.186 closed by remote host. Connection to 192.168.122.186 closed. # 2、使用console获取控制端 [root@localhost ~]# virsh console centOS7 连接到域 centOS7 换码符为 ^] CentOS Linux 7 (Core) Kernel 3.10.0-1160.el7.x86_64 on an x86_64 centos7 login: root Password: Last login: Sat Jul 9 21:19:07 from gateway [root@centos7 ~]# |
查询主机信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | [root@localhost ~]# virsh dominfo centos7_web01 Id: 1 名称: centos7_web01 UUID: d36d6ae8-74a6-46c6-aed3-1dd4916c6181 OS 类型: hvm 状态: running CPU: 1 CPU 时间: 23.1s 最大内存: 1048576 KiB 使用的内存: 1048576 KiB 持久: 是 自动启动: 禁用 管理的保存: 否 安全性模式: none 安全性 DOI: 0 |
调整内存
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | # 开机状态只能调整小,单位Kib [root@localhost ~]# virsh setmem centos7_web01 524288 virsh setmem centos_server01 --size 524288 --live --config virsh setmaxmem centos_server01 1572864 --config --config # 影响下一次引导,会更新配置文件 --live # 影响运行的域 --current # 影响当前域 [root@localhost ~]# virsh dominfo centos7_web01 Id: 1 名称: centos7_web01 UUID: d36d6ae8-74a6-46c6-aed3-1dd4916c6181 OS 类型: hvm 状态: running CPU: 1 CPU 时间: 148.9s 最大内存: 1048576 KiB 使用的内存: 180404 KiB 持久: 是 自动启动: 禁用 管理的保存: 否 安全性模式: none 安全性 DOI: 0 # 调大内存,需要虚拟机是在关机状态下,编辑主机配置文件。 [root@localhost ~]# virsh edit centos7_web01 <memory unit='KiB'>2097152</memory> #内存的总大小 <currentMemory unit='KiB'>1048576</currentMemory> #当前可以使用的内存的大小 <vcpu placement='static'>2</vcpu> #cpu的核心 [root@localhost ~]# virsh dominfo centos7_web01 Id: 4 名称: centos7_web01 UUID: d36d6ae8-74a6-46c6-aed3-1dd4916c6181 OS 类型: hvm 状态: running CPU: 1 CPU 时间: 16.0s 最大内存: 2097152 KiB 使用的内存: 2097152 KiB 持久: 是 自动启动: 禁用 管理的保存: 否 安全性模式: none 安全性 DOI: 0 #命令行修改最大内存,需要虚拟机状态是关闭状态。 [root@kvm01 /opt]# virsh shutdown centos7_web01 Domain centos7_web01 is being shutdown [root@kvm01 /opt]# virsh setmaxmem centos7_web01 1048576 [root@kvm01 /opt]# virsh dominfo centos7_web01 Id: - Name: centos7_web01 UUID: 158ca26b-eebd-41f3-8dc0-5752bd98d151 OS Type: hvm State: shut off CPU(s): 2 Max memory: 1048576 KiB Used memory: 1048576 KiB Persistent: yes Autostart: disable Managed save: no Security model: none Security DOI: 0 #查看CPU的信息,只有1核所以只显示了一个核心数 [root@localhost ~]# virsh vcpuinfo centos7_web01 VCPU: 0 CPU: 3 状态: running CPU 时间: 349.5s CPU关系: yyyy |
重命名虚拟机
1 2 3 4 5 6 7 8 9 10 11 | # 需要虚拟机在关机状态下 [root@localhost ~]# virsh list --all Id 名称 状态 ---------------------------------------------------- - centos7_web01 关闭 [root@localhost ~]# virsh domrename centos7_web01 centOS7 Domain successfully renamed [root@localhost ~]# virsh list --all |grep cent - centOS7 关闭 |
恢复和挂起
1 2 3 4 5 6 7 8 9 10 11 12 13 | [root@localhost ~]# virsh suspend centOS7 域 centOS7 被挂起 [root@localhost ~]# virsh list Id 名称 状态 ---------------------------------------------------- 2 centOS7 暂停 [root@localhost ~]# virsh resume centOS7 域 centOS7 被重新恢复 [root@localhost ~]# virsh list|grep run 2 centOS7 running |
参考
https://mp.weixin.qq.com/s/joZLR_d5MjC4PKy8qSMtRw
https://www.cnblogs.com/gshelldon/p/16463562.html
https://so.csdn.net/so/search?q=KVM&t=blog&u=weixin_40228200