[TOC]
[TOC]
0x00 基础环境
环境准备:
1) VMware Workstation 16
2) OS 版本&架构: CentOS/7.9.2009/x86_64/ 官网下载地址
代码语言:javascript复制# 需求选择:此处测试选择使用 `CentOS-7-x86_64-DVD-2009.iso` 进行安装桌面版本的操作,选择CentOS-7-x86_64-Minimal-2009.iso进行KVM虚拟机安装
CentOS-7-x86_64-DVD-2009.iso 04-Nov-2020 19:37 4G
CentOS-7-x86_64-DVD-2009.iso 04-Nov-2020 19:37 4G
CentOS-7-x86_64-DVD-2009.torrent 06-Nov-2020 22:44 176K
CentOS-7-x86_64-Everything-2009.iso 02-Nov-2020 23:18 10G
CentOS-7-x86_64-Everything-2009.torrent 06-Nov-2020 22:44 381K
CentOS-7-x86_64-Minimal-2009.iso 03-Nov-2020 22:55 973M
CentOS-7-x86_64-Minimal-2009.torrent 06-Nov-2020 22:44 39K
CentOS-7-x86_64-NetInstall-2009.iso 27-Oct-2020 00:26 575M
CentOS-7-x86_64-NetInstall-2009.torrent 06-Nov-2020 22:44 23K
0x01 基础步骤
描述: 当基础环境准备好后我们就需要开始创建 KVM-Server
的虚拟机了,在我们准备的 VMware Workstation 16 进行配置基础的操作系统,然后在此系统中安装KVM相应软件。
- Step 1.如图所示根据需求进行配置虚拟机,注意必须勾选虚拟化引擎来或者硬件辅助虚拟化的支持,例如
虚拟化 Intel VT-x/EPT 或 AMD-V/RVI(V)
WeiyiGeek.VM虚拟机创建
Step 2.选择创建带有桌面的CentOS 7的版本,并且开启网络和设置主机名称方便后续连接。
WeiyiGeek.KVM-Server虚拟配置
Step 3.安装完成启动系统并进入以下界面后,输入对于的命令进行简单设置与查看
代码语言:javascript复制# (0) 内核查看 (一般正常版本在2.6以上)
$ uname -a
Linux kvm-server 3.10.0-1160.el7.x86_64 #1 SMP Mon Oct 19 16:18:59 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
# (1) 关闭和禁用防火墙
[root@kvm-server ~]# systemctl stop firewalld.service
[root@kvm-server ~]# systemctl disable firewalld.service
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
# (2) 关闭selinux
[root@kvm-server ~]# sed -i "s/^SELINUX=enforcing/SELINUX=disabled/g" /etc/sysconfig/selinux
[root@kvm-server ~]# setenforce 0
# (3) 查看CPU是否支持VT技术
grep -E 'vmx|svm' /proc/cpuinfo
# 本机支持的vmx的虚拟化即Intel的处理器支持的。(有几个处理器就显示几个flags)
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq vmx
WeiyiGeek.简单设置与查看
Step 4.环境清理卸载系统原本存在的KVM以及安装新的软件
代码语言:javascript复制# 1.旧的版本
rpm -qa | egrep 'qemu|virt|kvm'
# libvirt-daemon-driver-nwfilter-4.5.0-36.el7.x86_64
# libvirt-daemon-driver-storage-mpath-4.5.0-36.el7.x86_64
# libvirt-libs-4.5.0-36.el7.x86_64
# libvirt-gobject-1.0.0-1.el7.x86_64
# qemu-guest-agent-2.12.0-3.el7.x86_64
# libvirt-daemon-driver-storage-logical-4.5.0-36.el7.x86_64
# libvirt-daemon-driver-interface-4.5.0-36.el7.x86_64
# libvirt-glib-1.0.0-1.el7.x86_64
# libgovirt-0.3.4-5.el7.x86_64
# virt-what-1.18-4.el7.x86_64
# libvirt-daemon-driver-storage-disk-4.5.0-36.el7.x86_64
# libvirt-daemon-driver-storage-gluster-4.5.0-36.el7.x86_64
# libvirt-daemon-driver-storage-4.5.0-36.el7.x86_64
# libvirt-daemon-driver-network-4.5.0-36.el7.x86_64
# libvirt-daemon-config-network-4.5.0-36.el7.x86_64
# libvirt-daemon-driver-nodedev-4.5.0-36.el7.x86_64
# qemu-img-1.5.3-175.el7.x86_64
# libvirt-daemon-driver-storage-core-4.5.0-36.el7.x86_64
# libvirt-daemon-driver-storage-iscsi-4.5.0-36.el7.x86_64
# libvirt-daemon-driver-storage-rbd-4.5.0-36.el7.x86_64
# qemu-kvm-common-1.5.3-175.el7.x86_64
# libvirt-daemon-driver-qemu-4.5.0-36.el7.x86_64
# libvirt-daemon-driver-secret-4.5.0-36.el7.x86_64
# ipxe-roms-qemu-20180825-3.git133f4c.el7.noarch
# qemu-kvm-1.5.3-175.el7.x86_64
# libvirt-daemon-driver-storage-scsi-4.5.0-36.el7.x86_64
# libvirt-daemon-kvm-4.5.0-36.el7.x86_64
# libvirt-gconfig-1.0.0-1.el7.x86_64
# libvirt-daemon-4.5.0-36.el7.x86_64
# 2.卸载
yum remove `rpm -qa | egrep 'qemu|virt|kvm'` -y
# 3.清空目录文件夹
rm -rf /var/lib/libvirt/ /etc/libvirt
# 4.安装软件(如果安装过程出错请检查是否是操作系统的问题-此处我是很顺利的)
yum install *qemu* *virt* librbd1-devel -y # CentOS 7
# yum upgrade rpm -y && yum install *qemu* *virt* librbd1-devel -y # CentOS 8
Step 5.在所谓的KVM技术中运用到的就是 QEMU KVM
的组合实现真正意义上的服务器虚拟化。
- KVM : 负责 CPU的虚拟化 内存的虚拟化 但却不能模拟其它设备。
- QEMU : 负责 IO设备的模拟例如磁盘 和 网络
# 安装的包简单介绍
1) qemu-kvm : 主包
2) libvirt : 调用KVM虚拟机技术的 Api 接口主要用于管理的作用。
3) virt-manager : 图形管理程序与hypervisor进行交互的作用。
Step 6.安装完成后启动libvirtd服务并查看KVM模块是是否安装
代码语言:javascript复制[root@kvm-server ~]# systemctl start libvirtd.service && systemctl enable libvirtd.service
[root@kvm-server ~]# lsmod | grep "kvm"
kvm_intel 188740 0
kvm 637289 1 kvm_intel
irqbypass 13503 1 kvm
0x02 安装 GuestOS
GuestOS安装的几种方式:
- 1.图形化方式
- 2.完全文本模式 (
现场配置虚拟机的规格
) - 3.命令行模式 (
模板镜像 配置文件
)
1.图形化方式
Step 1.可以通过命令行模式或者在界面中点击运行虚拟系统管理器都可以
代码语言:javascript复制virt-manager
WeiyiGeek.virt-manager
- Step 2.选择本地介质安装(采用前面我们下载的CentOS精简版镜像),其安装虚拟机方式与VMware类似,不在类似请看下图
WeiyiGeek.VM虚拟机新建
- Step 3.QEMU/KVM 创建的VM1虚拟机配置查看,并且点击开始安装,后续centos7安装流程大致相同此处不在讲述。
WeiyiGeek.QEMU/KVM
- Step 4.GuestOS 安装成功重启机器。
WeiyiGeek.GuestOS
Tips : 宿主机必须开启路由转发(才能使用桥接网络·重点)
之后重启一下 libvirtd.service
echo 'net.ipv4.ip_forward = 1' | tee -a /etc/sysctl.conf && sysctl -p
systemctl restart libvirtd.service
2.完全文本模式
描述: 此种方式应用环境是服务器与客户端均没有图形界面时的场景使用。
Step 1.安装前查看你KVM支持的OS版本;
代码语言:javascript复制root@kvm-server ~]# osinfo-query os | grep centos
centos-stream8 | CentOS Stream 8 | 8 | http://centos.org/centos-stream/8
centos5.0 | CentOS 5.0 | 5.0 | http://centos.org/centos/5.0
centos5.1 | CentOS 5.1 | 5.1 | http://centos.org/centos/5.1
centos5.10 | CentOS 5.10 | 5.10 | http://centos.org/centos/5.10
centos5.11 | CentOS 5.11 | 5.11 | http://centos.org/centos/5.11
centos5.2 | CentOS 5.2 | 5.2 | http://centos.org/centos/5.2
centos5.3 | CentOS 5.3 | 5.3 | http://centos.org/centos/5.3
centos5.4 | CentOS 5.4 | 5.4 | http://centos.org/centos/5.4
centos5.5 | CentOS 5.5 | 5.5 | http://centos.org/centos/5.5
centos5.6 | CentOS 5.6 | 5.6 | http://centos.org/centos/5.6
centos5.7 | CentOS 5.7 | 5.7 | http://centos.org/centos/5.7
centos5.8 | CentOS 5.8 | 5.8 | http://centos.org/centos/5.8
centos5.9 | CentOS 5.9 | 5.9 | http://centos.org/centos/5.9
centos6.0 | CentOS 6.0 | 6.0 | http://centos.org/centos/6.0
centos6.1 | CentOS 6.1 | 6.1 | http://centos.org/centos/6.1
centos6.10 | CentOS 6.10 | 6.10 | http://centos.org/centos/6.10
centos6.2 | CentOS 6.2 | 6.2 | http://centos.org/centos/6.2
centos6.3 | CentOS 6.3 | 6.3 | http://centos.org/centos/6.3
centos6.4 | CentOS 6.4 | 6.4 | http://centos.org/centos/6.4
centos6.5 | CentOS 6.5 | 6.5 | http://centos.org/centos/6.5
centos6.6 | CentOS 6.6 | 6.6 | http://centos.org/centos/6.6
centos6.7 | CentOS 6.7 | 6.7 | http://centos.org/centos/6.7
centos6.8 | CentOS 6.8 | 6.8 | http://centos.org/centos/6.8
centos6.9 | CentOS 6.9 | 6.9 | http://centos.org/centos/6.9
centos7.0 | CentOS 7 | 7 | http://centos.org/centos/7.0
centos8 | CentOS 8 | 8 | http://centos.org/centos/8
Step 2.安装命令;
代码语言:javascript复制# 1.帮助查看
man virt-install
# 2.采用FTP的格式进行安装(注意需要将OS镜像挂载到此ftp目录下)
virt-install --connnect qemu:///system
-n vm3 -r 2048 --vcpus=1 --disk-path=/var/lib/libvirt/images/vm9.img,size=7
--os-type=linux --os-variant=centos7.0 -x console=ttyS0 --nographics
--location=ftp://192.168.10.230/centos7/
TIPS : 安装时给与的内存必须是2G及以上否则将报如下错误 Dracut-Initqueue[552]:/sbin/dmsquash-live-root:line 273: printf: write error : no space left on device
。
3.命令行模式 (推荐方式)
描述: 通过此魔术我们可以轻松的创建我们的虚拟机,此种模式创建虚拟机依赖于虚拟机配置文件和虚拟机虚拟机img介质。
Step 1.虚拟机组成部分
代码语言:javascript复制[root@kvm-server ~]# ls /etc/libvirt/qemu
networks vm1.xml
[root@kvm-server ~]# ls /var/lib/libvirt/images/
vm1.qcow2
Step 2.根据现有模板镜像配置文件创建虚拟机即复制磁盘镜像与配置文件
代码语言:javascript复制[root@kvm-server ~]# cp /etc/libvirt/qemu/vm1.xml /etc/libvirt/qemu/vm2.xml
[root@kvm-server ~]# cp /var/lib/libvirt/images/vm1.qcow2 /var/lib/libvirt/images/vm2.qcow2
Step 3.配置文件修改
代码语言:javascript复制# 1.虚拟机UUID修改
[root@kvm-server ~]# uuidgen
960cc2bb-9731-4101-94e6-160c4263bace
# 2.虚拟机配置文件
<!--
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using: virsh edit vm2 or other application using the libvirt API.
-->
<domain type='kvm'>
<!-- 修改点1.名称与UUID -->
<name>vm2</name>
<uuid>960cc2bb-9731-4101-94e6-160c4263bace</uuid>
<memory unit='KiB'>2097152</memory>
<currentMemory unit='KiB'>2097152</currentMemory>
<vcpu placement='static'>2</vcpu>
<os>
<type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
<boot dev='hd'/>
</os>
<features>
<acpi/>
<apic/>
</features>
<cpu mode='custom' match='exact' check='partial'>
<model fallback='allow'>Broadwell-noTSX-IBRS</model>
<feature policy='require' name='md-clear'/>
<feature policy='require' name='spec-ctrl'/>
<feature policy='require' name='ssbd'/>
</cpu>
<clock offset='utc'>
<timer name='rtc' tickpolicy='catchup'/>
<timer name='pit' tickpolicy='delay'/>
<timer name='hpet' present='no'/>
</clock>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>destroy</on_crash>
<pm>
<suspend-to-mem enabled='no'/>
<suspend-to-disk enabled='no'/>
</pm>
<devices>
<emulator>/usr/libexec/qemu-kvm</emulator>
<!-- 修改点2.虚拟机磁盘镜像 -->
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='/var/lib/libvirt/images/vm2.qcow2'/>
<target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
</disk>
<disk type='file' device='cdrom'>
<driver name='qemu' type='raw'/>
<target dev='hda' bus='ide'/>
<readonly/>
<address type='drive' controller='0' bus='0' target='0' unit='0'/>
</disk>
<controller type='usb' index='0' model='ich9-ehci1'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x7'/>
</controller>
<controller type='usb' index='0' model='ich9-uhci1'>
<master startport='0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0' multifunction='on'/>
</controller>
<controller type='usb' index='0' model='ich9-uhci2'>
<master startport='2'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x1'/>
</controller>
<controller type='usb' index='0' model='ich9-uhci3'>
<master startport='4'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x2'/>
</controller>
<controller type='pci' index='0' model='pci-root'/>
<controller type='ide' index='0'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
</controller>
<controller type='virtio-serial' index='0'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
</controller>
<!-- 修改点3.网络Mac地址(virtio == NAT 转发) -->
<interface type='network'>
<mac address='52:54:00:64:97:40'/>
<source network='default'/>
<model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
<serial type='pty'>
<target type='isa-serial' port='0'>
<model name='isa-serial'/>
</target>
</serial>
<console type='pty'>
<target type='serial' port='0'/>
</console>
<channel type='unix'>
<target type='virtio' name='org.qemu.guest_agent.0'/>
<address type='virtio-serial' controller='0' bus='0' port='1'/>
</channel>
<channel type='spicevmc'>
<target type='virtio' name='com.redhat.spice.0'/>
<address type='virtio-serial' controller='0' bus='0' port='2'/>
</channel>
<input type='tablet' bus='usb'>
<address type='usb' bus='0' port='1'/>
</input>
<input type='mouse' bus='ps2'/>
<input type='keyboard' bus='ps2'/>
<graphics type='spice' autoport='yes'>
<listen type='address'/>
<image compression='off'/>
</graphics>
<sound model='ich6'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
</sound>
<video>
<model type='qxl' ram='65536' vram='65536' vgamem='16384' heads='1' primary='yes'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
</video>
<redirdev bus='usb' type='spicevmc'>
<address type='usb' bus='0' port='2'/>
</redirdev>
<redirdev bus='usb' type='spicevmc'>
<address type='usb' bus='0' port='3'/>
</redirdev>
<memballoon model='virtio'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>
</memballoon>
<rng model='virtio'>
<backend model='random'>/dev/urandom</backend>
<address type='pci' domain='0x0000' bus='0x00' slot='0x09' function='0x0'/>
</rng>
</devices>
</domain>
Step 4.虚拟机创建
代码语言:javascript复制[root@kvm-server ~]# virsh define /etc/libvirt/qemu/vm2.xml
# 定义域 vm2(从 /etc/libvirt/qemu/vm2.xml)
- Step 5.宿主机开启路由转发
(才能使用桥接网络重点)
之后重启一哈libvirtd.service
echo 'net.ipv4.ip_forward = 1' | tee -a /etc/sysctl.conf && sysctl -p
systemctl restart libvirtd.service
- Step 6.虚拟机运行结果
WeiyiGeek.成功创建并连接
4.飞机舱cockpit管理KVM服务器
Tips : 我们可以使用飞机驾驶舱安装与管理虚拟机实际上就是通过一个Web界面来管理服务器,安装完成后浏览器访问服务地址: 9090
代码语言:javascript复制# 安装&启用
yum install cockpit -y;
systemctl start cockpit
# 服务地址查看
ip addr | grep " inet "
# inet 127.0.0.1/8 scope host lo
# inet 192.168.228.128/24 brd 192.168.228.255 scope global noprefixroute dynamic ens33
# 停止服务
systemctl stop cockpit.socket
# 端口配置
vim /etc/systemd/system/cockpit.socket.d/listen.conf
# [Unit]
# Description=Cockpit Web Service Socket
# Documentation=man:cockpit-ws(8)
# Wants=cockpit-motd.service
# [Socket]
# ListenStream=9090
# ExecStartPost=-/usr/share/cockpit/motd/update-motd '' localhost
# ExecStartPost=-/bin/ln -snf active.motd /run/cockpit/motd
# ExecStopPost=-/bin/ln -snf /usr/share/cockpit/motd/inactive.motd /run/cockpit/motd
# [Install]
# WantedBy=sockets.target
sudo systemctl daemon-reload
sudo systemctl restart cockpit.socket
sudo systemctl restart cockpit.service
WeiyiGeek.cockpit-Web
0x03 升级GuestOS
描述: 在实际的应用场景中,我们常常要对已安装的GuestOS
进行添加硬件设备和升级现有的资源配置。
实现方式说明
- 1.图像界面操作(简单)
- 2.配置文件操作(手动-无界面的情况下)
图像界面操作
虚拟机添加网卡设备流程
- Step 1.首先需要关闭要添加硬件的虚拟机
- Step 2.双击虚拟机在打开的
对话框点击上方的 View 并点击 Details(详情),点击 Add Hardware
就可以选择要添加的虚拟硬件; - Step 3.此处以添加网卡为例。
- Step 4.删除添加的虚拟硬件步骤点击要删除的硬件, 例如
NIC:20:b9:da
然后点击右下角的删除即可。
配置文件操作
虚拟机添加磁盘设备流程
Step 1.同样需要先关闭要添加硬件的虚拟机VM2;
Step 2.创建要添加的磁盘镜像文件(qcow2格式);
代码语言:javascript复制$ qemu-img create -f qcow2 /var/lib/libvirt/images/vm2-1.qcow2 1G
> Formatting '/var/lib/libvirt/images/vm2-1.qcow2', fmt=qcow2 size=1073741824 encryption=off cluster_size=65536 lazy_refcounts=off
Step 3.编辑VM2虚拟主机的配置文件,找到定义磁盘镜像的<disk>
标签, 将该标签及其包含的内容复制一份在其后。
$ virsh edit vm2
37 <disk type='file' device='disk'>
38 <driver name='qemu' type='qcow2'/>
39 <source file='/var/lib/libvirt/images/vm2.qcow2'/>
40 <target dev='vda' bus='virtio'/>
41 <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
42 </disk>
43 <disk type='file' device='disk'>
44 <driver name='qemu' type='qcow2'/>
45 <source file='/var/lib/libvirt/images/vm2-1.qcow2'/> <!--修改点: 磁盘镜像名称-->
46 <target dev='vdb' bus='virtio'/> <!--修改点: 物理磁盘名称-->
47 <address type='pci' domain='0x0000' bus='0x00' slot='0x17' function='0x0'/> <!--修改点: 一定插槽值不能重复-->
48 </disk>
# 编辑了域 vm2 XML 配置。
Step 4.修改完VM2虚拟机配置后保存:wq
之后重启libvirtd服务, 之后采用命令启动虚拟机
[root@kvm-server ~]# systemctl restart libvirtd.service
[root@kvm-server ~]# virsh list --all
Id 名称 状态
----------------------------------------------------
- vm1 关闭
- vm2 关闭
[root@kvm-server ~]# virsh start vm2
域 vm2 已开始
WeiyiGeek.VM2配置升级