KVM (Kernel-based Virtual Machine) virtualization technology-super detailed theory + experiment! ! !

1. What is Virtualization Technology (KVM)?

  • KVM (Kernel-based Virtual Machine) is also known as virtualization technology. Through virtualization technology, a computer is converted into multiple logical computers. On one computer, colleagues run multiple logical computers, and each logical computer can run different operating systems. , Application programs can run in mutually independent spaces without affecting each other, thereby improving computer work efficiency

1.1 The specific role of KVM technology

  • 1. Simulate multiple operating systems (centos, win10, suse) in one operating system (win10), and each operating system can run different services (nginx+tomcat...), thereby realizing a host to build a cluster ( From the whole)
  • 2. Realize the functions of physical hardware through software/applications, for example:
    • ensp etc.

2. KVM technology development

Prototype

  • In 1961, the IBM 709 machine implemented a time-sharing system, which divided the CPU usage into multiple extreme time slices (1/100sec). Each time slice performed different tasks. By polling these time slices, a CPU Disguise as multiple CPUs

  • In 1972, IBM officially named the time-sharing system of the system370 machine as a virtual machine

  • In 1990, the system 390 machine introduced by IBM supports logical partitioning (a CPU is divided into multiple parts, independent of each other, that is, logical partitioning)

  • Xen came out in 2003 as an external hypervisor/VMM program (virtual machine management program) that can control the host and allocate resources to multiple clients

  • KVM: came out in 2007 and is now built into the kernel kernel

  • Virtualization technologies supported by Xen: full virtualization, paravirtualization

  • Virtualization technology supported by KVM: full virtualization

2.1 Types of virtualization

1. Full virtualization

  • All physical hardware resources are abstracted through software, and finally called
    (converting physical resources to virtualized resources)

  • Method used: Using hyperisor (VMM) software, the principle is to establish an abstraction layer between the underlying hardware and the server, and the core-based virtual machine is an open source product hypervisor (VMM) for Linux systems that can capture CPU instructions. Act as an intermediary for instructions to access hardware controllers and peripherals.

  • All the functions of physical resources are realized through software, and the physical resources are divided or divided to improve the utilization rate of a single physical resource

2. Paravirtualization

  • Need to modify the operating system and hardware support to make its performance and stability more in line with physical devices
  • Virtual engine: Turn on hardware assistance-----software virtualization function

3. Through

  • Use physical hardware resources directly (need support, not perfect yet)
    • Technical configuration difficulties

    • Kernel-level support is required. When the chip-level set is set, the underlying physical resources are directly used to give the virtual machine, and at the same time, logical division can be performed.

    • Directly call the underlying physical hardware support, the underlying architecture resources, and supply the virtual machine

Three, the three modes of KVM runtime

  • Client mode: It can be simply understood as the mode in which the client is running in the operating system. In addition, the client mode is also divided into kernel mode and user mode.

    • User mode: Provide users with user space tools for virtual machine management and perform I/O on behalf of users. Qemu runs in this mode.

    • Kernel mode: Simulate CPU and memory, realize switching of client mode, and handle exit from client mode. KVM kernel mode runs in this mode
      Insert picture description here

Four, KVM working principle

  • Qemu in user mode uses the interface libkvm to enter the kernel mode through ioctl system calls. KVMDriver creates virtual CPU and virtual memory for the virtual machine, and then executes the VMLAU NCH instruction to enter the guest mode, loads and runs the Guest OS, if an interrupt is sent during the guest OS operation, or Shadow page faults and other exceptions will suspend the operation of the Guest OS and save the current context. Exit to the kernel mode to handle these exceptions. If the kernel mode processes these exceptions if I/O is not required, then re-enter the guest mode after processing, if I/O is required O enters user mode, Qemu handles I/O, and enters kernel mode after processing is completed, and enters client mode
    Insert picture description here

Five, KVM features

5.1 Advantages

  • 1. Centralized management (remote management, maintenance)
  • 2. Improve hardware utilization (low utilization of physical resources-such as peak, virtualization solves "idle" capacity)
  • 3. Dynamic adjustment of machine/resource configuration (virtualization separates the system's application programs and service hardware to improve flexibility)
  • 4. High reliability (additional functions and solutions can be deployed to improve application environments such as transparent load balancing, migration, and recovery replication)

5.2 Disadvantages

  • 1. High initial cost (initial hardware support)
  • 2. Reduce hardware utilization (specific scenarios-for example, quarterly resource-consuming applications may not be suitable for virtualization)
  • 3. Larger error impact area (the local physical machine down may cause the virtual machine to be unavailable, and all the files in the virtual machine may be damaged)
  • 4. Implementation of configuration replication and complicated management (difficulty in operation and maintenance and troubleshooting of management personnel)
  • 5. Certain restrictions (virtualization technology involves various restrictions and must be used in conjunction with servers, applications and vendors that support/compatible virtualization)
  • 6. Security (the security risks of virtualization technology itself)

6. Comparison before and after virtualization

6.1 Before virtualization

  • 1. Each host has an operating system
  • 2. Close integration of software and hardware
  • 3. Running multiple applications on the same host usually creates conflicts
  • 4. Low utilization of system resources (for example: 5%)
  • 5. The hardware cost is high and not flexible enough

6.2 After virtualization

  • 1. Break the interdependence of operating system and hardware
  • 2. Through the technology encapsulated into the virtual machine, the management operating system and application programs are a single individual
  • 3. Powerful safety and fault isolation
  • 4. Virtual machines are independent of hardware, they can run on any hardware

Seven, KVM virtualization platform practical operation

7.1 Topology diagram

Insert picture description here

7.2 Virtual machine resource environment

First install the VMware software, and install the CentOS7 system, then set the virtual machine resource environment,
first close the virtual machine, set the virtual machine memory, etc., then check the KVM virtualization function, and the network card selects the NAT mode

CPU: Dual-core dual-thread-CPU virtualization enabled
Memory: 8G
Hard disk: 300G
Dual network card: Single network card
Operating system: Centos 7.6
Insert picture description here
1. Firewall optimization

[root@kvm yum.repos.d]# systemctl stop firewalld.service 
[root@kvm yum.repos.d]# systemctl disable firewalld.service 
[root@kvm yum.repos.d]# setenforce 0
[root@kvm yum.repos.d]# vim /etc/selinux/config 
SELINUX=disabled	'     #将enforcing修改为disabled

2. Set the mirrored disc to be automatically mounted

[root@kvm ~]# vim /etc/fstab 
/dev/sr0 /mnt iso9660 defaults 0 0	#尾行添加

3. Optimize ssh

[root@kvm ~]# vim /etc/ssh/sshd_config 
UseDNS no	#取消注释,并将yes改成no

4. Yum local warehouse construction

[root@kvm ~]# cd /etc/yum.repos.d/
[root@kvm yum.repos.d]# mkdir backup
[root@kvm yum.repos.d]# mv * backup
[root@kvm yum.repos.d]# ls
backup     local.repo
[root@kvm yum.repos.d]# vim local.repo

[local]
name=local
baseurl=file:///mnt
enabled=1
gpgcheck=0
[root@kvm yum.repos.d]# yum clean all	  #清空yum仓库
[root@kvm yum.repos.d]# mount /dev/sr0 /mnt	#因为没有重启,所以需要手动挂载一下光盘镜像
[root@kvm yum.repos.d]# yum makecache	#加载yum数据

5. After completing the deployment optimization, restart the system

[root@kvm ~]# reboot

7.3 Install KVM

1. Install the software package

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

2. Detect whether the CPU supports virtualization

[root@kvm ~]#  cat /proc/cpuinfo | grep vmx     

Insert picture description here
3. Check whether the KVM module is installed

[root@kvm ~]# lsmod | grep kvm
kvm_intel             170086  0 
kvm                   566340  1 kvm_intel
irqbypass              13503  1 kvm

4. Set the display mode of the startup interface

[root@kvm ~]# ln -sf /lib/systemd/system/graphical.target /etc/systemd/system/default.target
#将 系 统 的 默 认 运 行 target 更 改 为graphical.targe。 重启后系统将进入图形化界面。
[root@kvm ~]# systemctl start libvirtd    开启服务
[root@kvm ~]# systemctl enable libvirtd   自启动

7.4 Set up KVM network

  • After installing KVM on the host server, you must first set up the network. There are two ways to run KVM network in libvirt:
    NAT and Bridge, the default is NAT

    • 1. User mode, namely NAT mode. This mode is the default network setting. Data packets are transmitted through the interface of the host by NAT mode. You can access the external network, but you cannot access the virtual machine network from the outside.

    • 2. Network bridge, bridge mode, this mode allows the virtual machine to have a network like an independent host. External machines can directly access the inside of the virtual machine, but need network card support (generally wired network cards support)

1. Set and modify the ens33 network card

[root@kvm ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33 
#注释ip地址,子网掩码,网管,bns
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=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=e5cd1414-3df7-4052-864a-d561e7b3a836
DEVICE=ens33
ONBOOT=yes
#IPADDR=192.168.1.10
#NETMASK=255.255.255.0
#GATEWAY=192.168.1.2
#DNS=192.168.1.2
BRIDGE=br0

2. Set and modify the br0 network card (bridge mode)

[root@kvm ~]# vim /etc/sysconfig/network-scripts/ifcfg-br0	#配置桥接网卡
TYPE=Bridge
OTPROTO=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.1.10
NETMASK=255.255.255.0
GATEWAY=192.168.1.2

[root@kvm~]# systemctl restart network	#重启网卡

Note: After I configured the network card configuration here, I restarted the network card and reported an error Failed to start LSB: Bring up/down networking, you can refer to the link: Failed to start LSB: Bring up/down networking solution . This is a documented solution

7.5 KVM deployment and management

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

[root@kvm ~]# mkdir -p /data_kvm/iso   创建镜像文件存储文件
[root@kvm ~]#  mkdir -p /data_kvm/store 创建虚拟机存储
[root@kvm ~]# cp CentOS-7-x86_64-DVD-1708.iso /data_kvm/iso/
[root@kvm ~]# ls /data_kvm/iso/
CentOS-7-x86_64-DVD-1708.iso

1. Open the virtual system manager in the virtual machine

[root@kvm ~]# virt-manager 

2. Use virtual manager to manage and create virtual machines

Create a setup process:

1.创建存储池(ISOSTORE)
2.添加存储卷
3.创建虚拟机

Create storage pool tree
Double-click to open QEMU/KVM-->Storage-->Plus sign (+)-->Fill in name-->Forward-->Browse-->Other location-->Computer-->data_kvm--store-->Open-->Done
double-click to open QEMI /KVM
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
creates a storage pool in the same way. Apple
selects the /data_kvm/iso folder,
Insert picture description here
creates a storage volume,
selects the tree storage pool to create
, that is, the /data_kvm/store folder,
Insert picture description here
creates a virtual machine,
right-clicks QEMU/KVM-New-forward and
Insert picture description here
selects data/kvm/ The image in the storage pool apple in the iso
Insert picture description here
Configure the memory and CPU size
Insert picture description here
Configure the virtual machine storage volume
in the tree storage pool
Choose to /data_kvm/store/tree.qcow2
Insert picture description here
Select the network mode to (bridge) to
Insert picture description here
Insert picture description here
start the installation
Insert picture description here
Note: I'm here When I started installing CentOS7, I encountered the problem that the keyboard could not be used or moved. You can refer to: The keyboard cannot be used when installing the virtual machine using KVM graphical management to solve the problem.

8. Summary

  • VMM is a system software that can maintain multiple efficient and isolated program environments (virtual machines), and at the same time can manage the real resources of the computer system and provide ports for virtual machines;

  • The use of KVM virtualization technology greatly improves hardware utilization. Centralized management of multiple virtual machines, easy maintenance

Guess you like

Origin blog.csdn.net/F2001523/article/details/112544993