KVM deployment (complete virtualization platform)

1. Virtualization technology

Through virtualization technology, a computer is virtualized into multiple logical computers. Multiple logical computers
are allowed on one computer at the same time. Each logical computer can run a different operating system, and application programs can run in independent spaces. Do not affect each other, thereby improving the efficiency of the computer

2. Development of Virtualization Technology

In 1961, the IBM709 machine implemented a time-sharing system, which divided the CPU usage into multiple extremely short time slices (1/100sec). Each time slice performed different tasks. By polling these time slices, a CPU Disguised as multiple CPUs.
In 1972, IBM officially named the system370 time-sharing system as a virtual machine.
In 1990, IBM introduced the system390 machine to support logical partitioning (a CPU is divided into multiple copies, independent of each other, that is, logical division)
Xen 2003 Coming out in 2007, it is an external hypervisor/VMM program (virtual machine management program) that can control the host and allocate resources to multiple clients.
KVM: Coming out in 2007,
the virtualization technology supported by Xen , which is now built into the kernel kernel : Full virtualization, paravirtualization
KVM support virtualization technology: full virtualization

Three, virtualization type

1. Full virtualization: All physical hardware resources are abstracted by software, and finally
the method of invoking is used: using hypervisor (VMM) software, the principle is to establish an abstraction layer between the underlying hardware and the server, and based on The core virtual machine is an open source hypervisor (VMM) for Linux systems that can capture CPU instructions and act as an intermediary for instructions to access hardware controllers and peripherals.
2. Paravirtualization; need to modify the operating system
3. Pass-through: use physical hardware resources directly (support is needed, not perfect yet)

Four, virtualization features

1. Advantages

Centralized management (remote management, maintenance)
improves hardware utilization (low utilization of physical resources such as peaks, virtualization solves "idle" capacity)
dynamic adjustment of machine/resource configuration (virtualization separates system applications and service hardware, Improved flexibility)
High reliability (additional functions and solutions can be deployed to improve application environments such as transparent load balancing, migration, and recovery replication)

2. Disadvantages

The high upfront cost (initial hardware support)
reduces hardware utilization (in certain scenarios-for example, applications that are extremely resource-intensive may not be suitable for virtualization)
greater error impact (local physical machine down opportunities cause virtual machines to be unavailable, and at the same time It is possible that all the files in the virtual machine are damaged) The
implementation configuration is complicated, the management is complicated (management personnel operation and maintenance, troubleshooting is difficult)
certain restrictions (virtualization technology involves various restrictions, and must be compatible with virtualized servers and applications Used in conjunction with suppliers)
Security (the security risks of virtualization technology itself)

V. Introduction to KVM

1. Generalized KVM

KVM (Kernel-based Vritual Machine)-Kernel-based virtual machine
KVM is an open source Linux native full virtualization solution based on virtualized X86 hardware (requires CPU to support Intel-VT-x or AMD-V)
KVM embedded In the kernel module, the processor and memory are simulated to support the operation of the
virtual machine. The virtual machine is implemented as a regular Linux process, which is scheduled by the standard Linux scheduler;
each virtual CPU of the virtual machine is implemented as a regular Linux process. This allows KMV to use the existing functions of the Linux kernel,
but KVM itself does not perform any simulation. A client space program (virtual machine) is required to set the address space of a client virtual server through the /dev/kvm (this virtual device needs to be turned on hardware-assisted virtualization) interface,
and it is scheduled by Qemu simulation /O (ioctl) Resource and maintenance management
Libvit: KVM management tool, in addition to managing VMMs such as KVM, it can also manage Xen, VirtualBox, and even the bottom layer of OpenStack.
Libvirt contains 3 components: the background daemon program libvirtd. API library, command line I have virsh

2. KVM virtualization architecture and three modes

1. Guest mode (guestOS): The OS in the VM is the mode in which the GuestOS
guest runs in the operating system. The guest is divided into kernel mode and user mode.
2. User mode:
provides users with user space tools for virtual machine management and Perform I/O on behalf of the user, Qemu works in this mode (the main function of Qemu)
3. Linux kernel mode
Simulates CPU and memory, realizes client mode switching, handles the launch of client mode, and KVM runs in this mode

3. KVM principle

1. Guest: The guest system, including CPU (vCPU), memory, and drivers (Console, network card, I/O device driver, etc.)
are put in a restricted CPU mode by KVM to run.
2. The KVM kernel module simulates the processor and memory to support virtual machine operation
3. Qemu mainly handles I/O and provides customers with a user space /dev/kvm tool libvirt for virtual machine management
ioctl (definition) dedicated to device input and output Operating system call
libvirt: KVM management tool

The above constitutes a complete virtualization platform.
Simple understanding:
KVM driver provides processor, memory virtualization, and guest I/O interception. After guest I/O is intercepted, it is handled by Qemu and
Qemu uses the interface libkvm to call ( ioctl) virtual machine device interface /dev/kvm to allocate resources, manage and maintain virtual machines

4. KVM workflow

The user-mode Qemu uses the interface libkvm to enter the kernel mode through the ioctl system call. The KVM driver creates a virtual CPU and virtual memory for the virtual machine, then executes the VMLAU-NCH instruction to enter the guest mode, loads the Guest OS and runs. If an abnormality occurs during the operation of the Guest OS, the operation of the Guest OS is suspended, the current state is saved, and the kernel mode is exited to handle these abnormalities.
When kernel mode handles these exceptions, if V/O is not needed, then re-enter client mode after the processing is complete. If V/O is needed, then enter the user mode, then Qemu will handle the I/O, after the processing is completed, enter the kernel mode, and then enter the client mode

Six, KVM host deployment

1. Experimental environment

CPU: Dual-core dual-thread-CPU virtualization enabled
Memory: 8G
Hard disk: 300G
Network card: Single network card
Operating system: Centos 7.6 (1810)
Insert picture description here
1) Modify the host name

hostnamectl set-hostname kvm 
su -

2) Set the mirrored disc to auto/permanently mount

vim /etc/fstab
/dev/cdrom 		/mnt		 iso9660 		defaults 0 0
mount -a
df -Th

Insert picture description here
3) Environment optimization
Set up DNS reverse resolution

#是否反解DNS,设置为NO可以让客户端连接服务器更快
vim /etc/ssh/sshd_config 
#取消DNS注释
UseDNS no

Insert picture description here
4) Make a local YUM warehouse

cd /etc/yum.repos.d/
mkdir repos.bak
mv * repos.bak

vim local.repo
[kvm]
name=kvm
baseurl=file:///mnt
gpgcheck=0
enabled=1

yum clean all 
yum repolist

Insert picture description here
Insert picture description here
5) Turn off the firewall and core protection

systemctl stop firewalld
systemctl disable firewalld
setenforce 0

2. Install KVM

# 安装 GNOME 桌面环境  如果装了图形界面可以不需要装
yum groupinstall -y "GNOME Desktop"
# KVM 模块
yum -y install qemu-kvm
# 安装KVM 调试工具,可不安装
yum -y install qemu-kvm-tools
# 构建虚拟机的命令行工具
yum -y install virt-install
# qemu 组件,创建磁盘、启动虚拟机等
yum -y install qemu-img
# 网络支持工具
yum -y install bridge-utils
# 虚拟机管理工具
yum -y install libvirt
# 图形界面管理虚拟机	
yum -y install virt-manager

cat /proc/cpuinfo | grep vmx			# 检测CPU是否支持虚拟化

lsmod | grep kvm		#查看KVM模块是否已安装;Lsmod:显示已载入的系统模块

ln -sf /lib/systemd/system/graphical.target /etc/systemd/system/default.target			#设置开启启动界面的显示模式

Insert picture description here
Insert picture description here

3. Set up the KVM network

Two modes of KVM network:

  • NAT: The default setting, data packets are transmitted through the host's interface by NAT, which can access the external network, but cannot access the virtual machine network from the outside

  • Bridge: This mode allows the virtual machine to have a network like an independent host, and external machines can directly access the inside of the virtual machine, but need network card support (generally wired network cards support)

# 使用BRIDGE网桥模式进行部署
vim /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none     #将static改成none
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=ens33
UUID=db69c99f-d72c-44de-90fc-a296479c66ef
DEVICE=ens33
ONBOOT=yes
#IPADDR=192.168.220.3
#NETMASK=255.255.255.0
#GATEWAY=192.168.220.2
#DNS1=8.8.8.8
BRIDGE=br0   #删除/注释原先地址,设置为网桥模式,关联br0网卡
# 创建、编辑桥接网卡
vim /etc/sysconfig/network-scripts/ifcfg-br0
TYPE=Bridge
BOOTPROTO=static
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=br0
DEVICE=br0
ONBOOT=yes
IPADDR=192.168.220.3
NETMASK=255.255.255.0
GATEWAY=192.168.220.2

systemctl restart network

4. KVM deployment and management

1) Create a directory for KVM storage and mirroring data, upload a centos7 mirror

mkdir -p /data_kvm/iso
mkdir -p /data_kvm/store
mount.cifs //192.168.1.191/jingxiang /opt   #此处/jingxiang为自己真机中需要共享的目录名称
Password for root@//192.168.1.191/jingxiang:      #root的密码
cd /opt
cp -p CentOS-7-x86_64-DVD-1810-7.6.iso /data_kvm/iso

Insert picture description here
2) Use virtual system manager to manage virtual machines.
Creation ideas:
① Create storage pool (ISO, STORE)
② Add storage volume
③ Create virtual machine

virt-manager

① Step 1: Create a storage pool
Double-click to open QEMU/KVM-storage-add (plus sign)-fill in the name-forward-browse-other locations-computer-data_kvm-store-open-finish, use the same method to create a mirrored storage pool, The selected folder is /data_kvm/iso
Insert picture description here
Insert picture description here

② Create a storage volume
Insert picture description here

③ Create a virtual machine
Open the virtual system manager-right-click QEMU/KVM-select New
Insert picture description here

Local installation media-forward

Insert picture description here

Choose to use ISO image-browse to select the image in the storage pool of the image just created-go forward
Insert picture description here

Configure memory and CPU-go ahead
Insert picture description here

Management-select the storage volume in the storage pool just created-go forward
Insert picture description here

Click to customize configuration before installation-select network (bridge)-complete
Insert picture description here

Click the boot option-check Start the virtual machine when the host boots-start the installation-a prompt appears to select yes

Insert picture description here
Insert picture description here

to sum up:

1. Introduction to KVM technology
What technology is it: virtualization

2. KVM technology positioning (practical application)
workstation, virtualbox
simulate hardware resources/equipment/operating system

3. The core function of KVM virtualization technology (in principle)
QEMU (working in the user layer, controlling the ibkvm tool (tool function, controlling KVM in the kernel) to call physical virtualization resources
(the method of calling physical virtualization resources is ioctl, providing Virtual machine)
KVM (working at the kernel layer, virtualizing/abstracting physical hardware resources, providing Qemu component calls)

4. KVM core component functions
QEMU
function: control I/O virtualization, call hardware resources
KVM
function: provide CPU, memory (hardware resources) virtualization for virtual machines

Guess you like

Origin blog.csdn.net/Pyy0928/article/details/114874163