Virtualization (KVM)

Virtualization (KVM)

1. Introduction to virtualization

1 Introduction

  • Virtualization refers to virtualizing a computer into multiple logical computers through virtualization technology.
  • Virtualization uses software to redefine the division of IT resources, which can realize dynamic allocation, flexible scheduling, and cross-domain sharing of IT resources, and improve IT resource utilization.

2. Classification of virtualization

  • Full virtualization: The most popular virtualization method uses a software called Hypervisor to establish an abstraction layer between the virtual server and the underlying hardware. Hypervisor can capture CPU instructions and act as an intermediary for instructions to access hardware controllers and peripherals. Therefore, full virtualization technology allows almost any operating system to be installed on a virtual server without modification, and they do not know that they are running in a virtualized environment. The main disadvantage is that the hypervisor will put a lot of load on the processor.
  • Paravirtualization: Full virtualization is a processor-intensive technology because it requires the Hypervisor to manage each virtual server and make them independent of each other. One way to reduce this burden is to change the client operating system and modify the guessos kernel so that guessos can directly use CPU resources without translating instructions, thus saving resources and making it think that it is running in a virtual environment. , Can work with Hypervisor. This method is called para-virtualization (para-virtualization) Xen.

3. Virtualization architecture

  • The hypervisor is installed directly on the physical machine, and multiple virtual machines run on the hypervisor. Xen and VMWare's ESXi belong to this type.
  • First install conventional operating systems on the physical machine, such as RedHat, Ubuntu, and Windows. Hypervisor runs as a program module on the OS and manages virtual machines. KVM, VirtualBox and VMWare Workstation all fall into this category.

Two, KVM introduction

1 Introduction

  • KVM (Kernel-Based Virtual Machines) is a virtualization technology based on the Linux kernel. KVM is a module of the Linux kernel. It can directly convert the Linux kernel into a Hypervisor (system management program) so that the Linux kernel can directly manage virtual machines. Call the memory management and process management subsystems in the Linux kernel to manage the virtual machine.

  • KVM virtualization requires hardware support (such as [Intel VT](https://baike.baidu.com/item/Intel VT) technology or AMD V technology). It is full virtualization based on hardware.

  • KVM appears as a process in the Linux operating system and is scheduled by the standard Linux scheduler, which enables KVM to use the existing functions of the Linux kernel

  • QEMU is an open source virtualization software, pure software, can virtualize all hardware, performance is not strong

  • Based on QEMU, KVM has developed a tool QEMU-KVM that can run in user space.

    Disks, network devices, etc. are all simulated by the tool QEMU-KVM

    KVM and QEMU-KVM communication is achieved through /dev/kvm

    libvirt is an API used to manage KVM virtual machines, and its command is virsh

2. Architecture

Insert picture description here

Three, install KVM

1. Adjust the virtual machine

  • Add a disk of at least 20G
  • Edit virtual machine settings >> check the first two in the virtualization engine on the right side of the processor

2. Check the number of CPUs

# free 

             total        used        free      shared  buff/cache   available
Mem:        1868660      109492     1521452        8868      237716     1601524
Swap:       2097148           0     2097148
  • Check whether the cpu is enabled for virtualization support:

    # grep -Ei "vmx|svm"  /proc/cpuinfo    //有内容输出即为支持
    

3. Mount the new disk

# lsblk   //查看是否有新的磁盘 sdb
# mkfs.ext4 /dev/sdb //格式化磁盘
# lsblk -f //查看格式化是否成功,有 UUID	J即为成功
# mkdir /kvm_data //创建过载目录
# mount /dev/sdb /kvm_data //将 sdb 挂载到 /kvm/data/
# lsblk -f   //查看挂载是否成功
# vim /etc/fstab
[输入内容如下]
/dev/sdb     /kvm_data           ext4      defaults    0 0
[精致的结尾]

4. Turn off the firewall and selinux

# systemctl stop firewalld
# systemctl disable firewalld
# vi /etc/selinux/config
[修改内容如下]
SELINUX=disabled 
[精致的结尾]
#重启虚拟机
# getenforce //打印 Disabled

5. Install KVM

]# yum install -y  virt-*  libvirt  bridge-utils qemu-img

Fourth, start KVM

1. Configure the network card

  • Add bridged network card

    # cd /etc/sysconfig/network-scripts/
    # cp ifcfg-eno16777736 ifcfg-br0
    
  • Modify the content of the bridged network card ifcfg-br0

    [root@kvm network-scripts]# cat ifcfg-br0 
    [配置内容如下]
    TYPE=Bridge
    BOOTROTO=none
    NAME=br0
    DEVICE=br0
    ONBOOT=yes
    IPADDR=192.168.235.131
    NETMASK=255.255.255.0
    GATEWAY=192.168.235.2
    DNS1=119.29.29.29
    [配置内容如下]
    
  • Modify the NAT network card ifcfg-eno16777736

    [root@kvm network-scripts]# cat ifcfg-eno16777736 
    [配置内容如下]
    TYPE=Ethernet
    BOOTPROTO=none
    NAME=eno16777736
    DEVICE=eno16777736
    ONBOOT=yes
    BRIDGE=br0
    [配置内容如下]
    
  • Restart the network card service and view the network card information

    [root@kvm network-scripts]# service network restart
    Restarting network (via systemctl):                        [  OK  ]
    [root@kvm network-scripts]# ifconfig 
    br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 192.168.235.131  netmask 255.255.255.0  broadcast 192.168.235.255
            inet6 fe80::20c:29ff:fe08:cf47  prefixlen 64  scopeid 0x20<link>
            ether 00:0c:29:08:cf:47  txqueuelen 0  (Ethernet)
            RX packets 36  bytes 2182 (2.1 KiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 29  bytes 2582 (2.5 KiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            ether 00:0c:29:08:cf:47  txqueuelen 1000  (Ethernet)
            RX packets 5607  bytes 1419872 (1.3 MiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 1360  bytes 124245 (121.3 KiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    

2. Start the libvirtd service

  • Check whether the KVM module is loaded

    [root@kvm ~]# lsmod | grep kvm     //lsmod 用于显示已载入系统的模块
    kvm_intel             162153  0 
    kvm                   525259  1 kvm_intel
    
  • Start libvirtd and check if it started successfully

    # systmctl start libvirtd
    # ps -ef | grp libvirt //有多行输出
    
  • After successful startup, you can see two network cards

    [root@kvm ~]# brctl show
    bridge name	bridge id		STP enabled	interfaces
    br0		8000.000c2908cf47	no		eno16777736
    virbr0		8000.000000000000	yes		
    

3. Install CentOS 7 from the command line

# virt-install --name=test --memory=512,maxmemory=1024 --vcpus=1,maxvcpus=2 --os-type=linux --os-variant=rhel7 --location=/tmp/CentOS-7-x86_64-DVD-1511.iso --disk path=/kvm_data/test.img,size=10 --bridge=br0 --graphics=none --console=pty,target_type=serial  --extra-args="console=tty0 console=ttyS0"
  • Command explanation:
    --name:指定虚拟机的名称。
    --memory:指定分配给虚拟机的内存资源大小。

    maxmemory:指定可调节的最大内存资源大小,因为KVM支持热调整虚拟机的资源。

    --vcpus:指定分配给虚拟机的CPU核心数量。

    maxvcpus:指定可调节的最大CPU核心数量。

    --os-type:指定虚拟机安装的操作系统类型。

    --os-variant:指定系统的发行版本。

    --location:指定ISO镜像文件所在的路径,支持使用网络资源路径,也就是说可以使用URL。

    --disk path:指定虚拟硬盘所存放的路径及名称,size则是指定该硬盘的可用大小,单位是G。

    --bridge:指定使用哪一个桥接网卡,也就是说使用桥接的网络模式。

    --graphics:指定是否开启图形。

    --console:定义终端的属性,target_type 则是定义终端的类型。

    --extra-args:定义终端额外的参数。

  • The following operations are omitted

Five, virtual machine management

1. Basic KVM management

  • virsh list --all View all virtual machines

  • virsh console testEnter the specified virtual machine
    if any error: operation failed: Active console session exists for this domainerror:

    # ps -ef |grep console
    root      11167       console test
    # kill -9 11167
    
  • virsh shutdown test Shut down the virtual machine

  • virsh start test Turn on the virtual machine

  • virsh destroy test Force shutdown of the virtual machine

  • virsh undefine test Completely destroy the virtual machine, the virtual machine configuration file will be deleted

  • virsh autostart test The host computer boots the virtual machine also boots

  • virsh suspend test Hang up

  • virsh autostart --disable testDeactivate boot

  • virsh resume test restore

2. Clone the virtual machine

# virsh shutdown test  //关闭虚拟机,不然会报错
# virt-clone  --original test --name test02 --file 
/kvm_data/test02.img
  • Command explanation:
    • --original:指定克隆源虚拟机。
    • --name:指定克隆后的虚拟机名字。
    • --file:指定目标虚拟机的虚拟磁盘文件。

3. Snapshot management

  • virsh snapshot-create test 创建test虚拟机的快照
  • virsh snapshot-list test 列出test虚拟机的所有快照
  • virsh snapshot-current test 查看当前虚拟机快照的详细信息
  • ls /var/lib/libvirt/qemu/snapshot/test/查看test虚拟机所有的快照配置文件
  • virsh snapshot-revert test 1588485687恢复指定快照
  • virsh snapshot-delete test 1588485687 删除指定快照

4. Disk format

  • qemu-img ifno /kvm_data/test.img 查看虚拟磁盘格式

  • qemu-img create -f raw /kvm_data/test_1.img 2G 创建2GB的 RAW 格式磁盘

    # virsh edit test
    [在 </disk> 下增加如下内容]
    <disk type='file' device='disk'>
          <driver name='qemu' type='raw'/>
          <source file='/kvm_data/test02_3.raw'/>
          <target dev='vdb' bus='virtio'/>
          <address type='pci' domain='0x0000' bus='0x00'slot='0x09' function='0x0'/>
    </disk>
    [主要修改 file 字段 、 slot 字段 dev 字段]
    
  • qemu-img convert -O qcow2 /kvm_data/test_1.img /kvm_data/test_1.qcow2 更改磁盘格式

    # virsh edit test02
    [找到这一部分,并修改如下]
    <disk type='file' device='disk'>
          <driver name='qemu' type='raw'/>
          <source file='/kvm_data/test02_2.img'/>
          <target dev='vda' bus='virtio'/>
          <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
    </disk>
    [主要更改 file 段和 type 段]
    

5. Disk expansion

  • emu-img resize /kvm_data/test02_2.img +2G 扩容两个G

6. Adjust the CPU and network card

  • virsh dominfo test 查看test 虚拟机配置信息

  • Edit virtual machine memory

    # virsh edit test
    [找到如下内容,并修改]
    <memory unit='KiB'>1048576</memory>
    //最大内存
      <currentMemory unit='KiB'>624288</currentMemory>
    //可用内存
      <vcpu placement='static' current='1'>2</vcpu>
    //最大cpu
    [精致的结尾]
    # virsh destory test  //关闭虚拟机
    # virsh start test  //开启虚拟机
    # virsh dominfo test //查看配置是否成功
    
  • Dynamic modification

    # virsh setmem test 800m //修改动态内存
    # virsh setvcpus test 2 //动态修改cpu,只可以增加不可以减少
    # virsh dumpxml test > /etc/libvirt/qemu/test.xml //需要把配置写入到配置文件里
    
  • Add a new network card

    # virsh domiflist test  //查看网卡列表
    #virsh attach-interface test --type bridge  --source virbr0  //  virbr0 NAT 模式,br0 桥接
    # virsh dumpxml test > /etc/libvirt/qemu/test.xml //备份配置文件
    

7. Migrate virtual machines

# virsh shutdown test
# virsh domblklist test  //查看虚拟机磁盘所在目录
# virsh dumpxml test > /etc/libvirt/qemu/test03.xml  //创造备份文件
# rsync -av /kvm_data/test.img  /kvm_data/test03.img
# vi /etc/libvirt/qemu/test03.xml 
[修改如下内容]
//修改domname: 
//修改uuid(随便改一下数字,位数不要变)
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/kvm_data/test03.img'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
    </disk>
//修改磁盘路径
[精致的结尾]
# virsh define /etc/libvirt/qemu/test03.xml //定义新的虚拟机
# virsh list --all  //查看是否有 test03

Guess you like

Origin blog.csdn.net/weixin_54898062/article/details/114983438