KVM installation and deployment (detailed teaching)

1. Virtualization technology

Virtualize a computer into multiple logical computers through virtualization technology, allowing multiple logical computers on one computer at the same time, each logical computer can run a different operating system, and application programs can run in independent spaces And do not affect each other, thereby improving the efficiency of the computer

Two, virtualization type

  1. Full virtualization: All physical hardware resources are abstracted through software, and finally called.
    Applicable method: apply hypervisor (VMM) software, the principle of which is to establish an abstraction layer between the underlying hardware and the server. The core-based 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. Through: Use physical hardware resources directly (need support, not perfect yet)

Three, virtualization features

1、Advantage

  1. Centralized management (remote management, maintenance)
  2. Improve hardware utilization (physical resource utilization is at a peak, virtualization solves "idle" capacity)
  3. Dynamic adjustment of machine/resource configuration (separation of application and service hardware of virtualized system improves flexibility)
  4. High reliability (additional functions and solutions can be deployed to improve application environments such as transparent load balancing, migration, and recovery replication)

2、Disadvantage

  1. High upfront cost (initial hardware support)
  2. Reduce hardware utilization (specific scenarios-such as extremely resource-intensive 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 at the same time, all the files in the virtual machine may be damaged)
  4. Complex implementation configuration 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) security (the security risks of virtualization technology itself)

Four, KVM introduction

  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 The operating system calls
    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, hand over Processed by Qemu
    Qemu uses the interface libkvm to call (ioctl) the virtual machine device interface /dev/kvm to allocate resources, manage, and maintain the virtual machine

  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

Five, 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)

1. Create a brand new virtual machine
Insert picture description here
first. 2. Experimental environment
1. Modify the host name

[root@kvm ~]# hostnamectl set-hostname kvm 
[root@kvm ~]# su

2. Set the mirrored disc to auto/permanently mount

[root@kvm ~]# vim /etc/fstab
/dev/cdrom /mnt iso9660 defaults 0 0
[root@kvm ~]# mount -a 
[root@kvm ~]# df -hT
/dev/sr0       iso9660   4.3G  4.3G     0  100% /mnt

Insert picture description here
Insert picture description here
3. Environmental optimization

① Set DNS reverse resolution
#Whether to reverse DNS, set to NO to make the client connect to the server faster
[root@kvm ~]# vim /etc/ssh/sshd_config #Cancel
DNS comment and change to NO
UseDNS no

Insert picture description here
4. Make a local YUM warehouse

[root@kvm ~]# mkdir /abc
[root@kvm ~]# cd /etc/yum.repos.d/
[root@kvm yum.repos.d]# ls
CentOS-Base.repo  CentOS-Debuginfo.repo  CentOS-Media.repo    CentOS-Vault.repo
CentOS-CR.repo    CentOS-fasttrack.repo  CentOS-Sources.repo
[root@kvm yum.repos.d]# mkdir bak
[root@kvm yum.repos.d]# mv CentOS-* bak
[root@kvm yum.repos.d]# ls
bak
[root@kvm yum.repos.d]# vim local.repo
[local]
name=kvm
baseurl=file:///abc
gpgcheck=0
enabled=1
[root@kvm yum.repos.d]# yum clean all 
[root@kvm yum.repos.d]# yum repolist

Insert picture description here

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

[root@kvm yum.repos.d]# systemctl stop firewalld
[root@kvm yum.repos.d]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@kvm yum.repos.d]# setenforce 0

Two, install KVM

① 安装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


#以下是安装的内容
yum groupinstall -y "GNOME Desktop"
yum -y install qemu-kvm
yum -y install qemu-kvm-tools
yum -y install virt-install
yum -y install qemu-img
yum -y install bridge-utils
yum -y install libvirt
yum -y install virt-manager

Insert picture description here

Check whether the CPU supports virtualization

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

Insert picture description here

Check whether the KVM module is installed

Lsmod:显示已载入的系统模块
[root@kvm ~]# lsmod | grep kvm
kvm_intel             183621  0 
kvm                   586948  1 kvm_intel
irqbypass              13503  1 kvm

Insert picture description here
② Set the display mode of the startup interface

[root@kvm ~]# ln -sf /lib/systemd/system/graphical.target /etc/systemd/system/default.target

Insert picture description here

Three, set up the KVM network

Two modes of KVM network:
① NAT: The default setting, data packets are
transmitted through the interface of the host 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 behave like one An independent host also has a network, and external machines can directly access the inside of the virtual machine, but need network card support (generally wired network cards support)

使用Bridge网桥模式进行部署

[root@bdqn ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=no
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=aac61931-47a6-40e5-9511-68adf20e97c5
DEVICE=ens33
ONBOOT=yes
#IPADDR=192.168.162.130
#PREFIX=24
#GATEWAY=192.168.162.2
BRIDGE=br0			#删除原先地址,设置为网桥模式,关联br0网卡

Insert picture description here

#Create and edit bridged network card

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.162.130
NETMASK=255.255.255.0
GATEWAY=192.168.162.2

Insert picture description here

Fourth, KVM deployment and management
#Create a directory for KVM storage and mirroring data, upload centos7 mirrors

[root@kvm network-scripts]# mkdir -p /data_kvm/iso
[root@kvm network-scripts]# mkdir -p /data/_kvm/store
[root@kvm network-scripts]# mkdir /abc
[root@kvm network-scripts]# mount.cifs //192.168.162.1/LAMP-C7 /mnt
Password for root@//192.168.162.1/LAMP-C7:  
[root@kvm network-scripts]# cd /abc
[root@kvm abc]# cp -p CentOS-7-x86_64-DVD-1806.iso /data_kvm/iso/

Insert picture description here

Insert picture description here

View mirror

[root@kvm abc]# ll /data_kvm/iso/
总用量 4481024
-rwxr-xr-x. 1 root root 4588568576 2月  20 2019 CentOS-7-x86_64-DVD-1810.iso

Insert picture description here

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

[root@kvm kvmdata]# virt-manager

Insert picture description here

① 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

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
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
Insert picture description here

Insert picture description here
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/weixin_51622156/article/details/114645698