VirtualBox deploys KVM virtualization

What is KVM technology?

KVM (Kernel-based Virtual Machine) is an open source virtualization technology that is part of the Linux kernel. By converting the Linux kernel into a hypervisor, KVM allows multiple virtual machine instances to run on the same physical host, and each instance can exclusively share part of the system resources. The working principle of KVM is to extend the Linux kernel into a virtualization hypervisor, which runs directly on the hardware and provides the core functions required for virtualization. KVM utilizes CPU virtualization extensions such as Intel's VT-x and AMD's AMD-V to create and manage virtual machines. The virtual machine is simulated by QEMU (Quick EMUlator), and provides actual running hardware and devices, enabling the operating system and applications to run in the virtual machine.

Key features of KVM include:

1. Performance: KVM runs directly on the hardware of the physical host. Compared with the traditional emulation-based virtualization method, better performance can be obtained. Virtual machines can directly access hardware resources on the physical host and use hardware-assisted virtualization technology to improve performance.

2. Security: KVM utilizes hardware-assisted virtualization technology and uses the security functions of the Linux kernel to protect the isolation between virtual machines. Virtual machines cannot access each other, which increases security.

3. Flexibility: KVM can run a variety of operating systems, including Linux, Windows and other mainstream operating systems. It provides a wealth of management tools and APIs, enabling users to easily create, configure and manage virtual machines.

4. Scalability: KVM supports multiple virtual CPUs and a large amount of memory, which can meet the needs of different applications. It also supports migration and replication of virtual machines, which can balance load and improve availability among different physical hosts. In short, KVM is a powerful and flexible virtualization technology, which combines the powerful functions of the Linux kernel and the advantages of virtualization to provide users with high-performance, secure and flexible virtualization solutions. 

First of all, to use virtualbox to deploy KVM, first you need to enable the virtualization of hardware such as cpu,

Need to achieve the effect as shown in the figure:

Check if the CPU supports virtualization 

 Check whether the CPU can be virtualized: if one of vmx or svm appears, it means that the CPU supports virtualization.

 Turn off the firewall before proceeding, selinux

[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
[root@kvm yum.repos.d]# vim /etc/selinux/config 
SELINUX=disabled

Install KVM software dependencies:

yum install qemu-kvm libvirt virt-install bridge-utils -y

Check whether the KVM module is installed:

[root@kvm system]# lsmod | grep kvm
kvm_intel             188740  0 
kvm                   637289  1 kvm_intel
irqbypass              13503  1 kvm

Check whether the virtualization management tool libvirtd service is started

[root@kvm system]# systemctl status libvirtd
● libvirtd.service - Virtualization daemon
   Loaded: loaded (/usr/lib/systemd/system/libvirtd.service; enabled; vendor preset: enabled)
   Active: active (running) since 三 2023-06-28 16:04:31 CST; 30min ago
     Docs: man:libvirtd(8)
           https://libvirt.org
 Main PID: 2235 (libvirtd)
    Tasks: 19 (limit: 32768)
   CGroup: /system.slice/libvirtd.service
           ├─2235 /usr/sbin/libvirtd
           ├─2351 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --l...
           └─2352 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --l...

6月 28 16:11:42 kvm dnsmasq[2351]: using nameserver 114.114.114.114#53
6月 28 16:11:42 kvm dnsmasq[2351]: using nameserver 2400:3200::1#53
6月 28 16:13:45 kvm dnsmasq[2351]: reading /etc/resolv.conf
6月 28 16:13:45 kvm dnsmasq[2351]: using nameserver 2400:3200::1#53
6月 28 16:13:45 kvm dnsmasq[2351]: no servers found in /etc/resolv.conf, will retry
6月 28 16:13:45 kvm dnsmasq[2351]: reading /etc/resolv.conf
6月 28 16:13:45 kvm dnsmasq[2351]: using nameserver 114.114.114.114#53
6月 28 16:13:48 kvm dnsmasq[2351]: reading /etc/resolv.conf
6月 28 16:13:48 kvm dnsmasq[2351]: using nameserver 114.114.114.114#53
6月 28 16:13:48 kvm dnsmasq[2351]: using nameserver 2400:3200::1#53
[root@kvm system]# 

Set up KVM network

First, configure the configuration file in the currently used network card configuration file as follows:

Two modes of KVM network:

① NAT: Default setting, data packets are transmitted by NAT through the interface of the host, and 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 , the external machine can directly access the inside of the virtual machine, but it needs the support of the network card (generally, the wired network card supports it)

[root@kvm network-scripts]# cat ifcfg-enp0s3 
TYPE="Ethernet"
BOOTPROTO="static"
NAME="enp0s3"
UUID="3bdb5b05-6bd2-401c-93f5-bae94e5c1cd5"
DEVICE="enp0s3"
ONBOOT="yes"
#IPADDR=192.168.126.100
#GATEWAY=192.168.126.1
#PREFIX=24
#DNS1=114.114.114.114
BRIDGE=br0

Set the br0 network card:

[root@kvm network-scripts]# cat 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.126.100
NETMASK=255.255.255.0
GATEWAY=192.168.126.1
DNS1=114.114.114.114

Restart the network service:

service network restart

Check the ip configuration again and find that there is an extra br0 network card

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br0 state UP group default qlen 1000
    link/ether 08:00:27:dd:13:32 brd ff:ff:ff:ff:ff:ff
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 52:54:00:42:58:b4 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 1000
    link/ether 52:54:00:42:58:b4 brd ff:ff:ff:ff:ff:ff
6: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 08:00:27:dd:13:32 brd ff:ff:ff:ff:ff:ff
    inet 192.168.126.100/24 brd 192.168.126.255 scope global noprefixroute br0
       valid_lft forever preferred_lft forever
    inet6 2408:8640:8fe:f5:6033:8508:1cb4:4393/64 scope global noprefixroute dynamic 
       valid_lft 2592000sec preferred_lft 604800sec
    inet6 fe80::1f5a:3c2d:6054:f2d5/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

# 测试网络

[root@kvm network-scripts]# ping www.baidu.com
PING www.a.shifen.com (112.80.248.75) 56(84) bytes of data.
64 bytes from 112.80.248.75 (112.80.248.75): icmp_seq=1 ttl=55 time=13.5 ms
64 bytes from 112.80.248.75 (112.80.248.75): icmp_seq=2 ttl=55 time=12.5 ms

KVM deployment and management

Create a directory for KVM storage and mirroring, and upload a centos7 mirroring file as a mirroring file for creating a KVM virtual machine

mkdir -p /data/data_kvm/iso
mkdir -p /data/data_kvm/store
[root@kvm ~]# cd /data/data_kvm/iso/
[root@kvm iso]# ls
CentOS-7-x86_64-DVD-2009.iso

Create a virtual machine in command line mode

The meaning of the virt-install option:

--name: Specifies the name of the created virtual machine.

--ram: Specify the memory size of the virtual machine.

--vcpu: Specifies the number of virtual CPUs for the virtual machine.

--disk: Specify the disk path and size of the virtual machine.

--size: Specifies the size of the virtual machine disk, generally followed by --disk, separated by .

--network: Specifies the network settings for the virtual machine.

--cdrom: Specifies the CD path of the virtual machine for installing the operating system.

--graphics: Specify the graphics settings of the virtual machine, such as VNC connections.

--os-variant: Specifies the operating system type of the virtual machine.

--location: Specifies the OS installation source of the virtual machine.

virt-install --connect qemu:///system --virt-type kvm --name KVM_test --memory 512 --vcpu 1 --disk /data/data_kvm/store/KVM_test.qcow2,size=10 --location /data/data_kvm/iso/CentOS-7-x86_64-DVD-2009.iso  --network bridge=br0 --graphics none --extra-args='console=ttyS0'

Enter the installation page: you need to change [!] to [X] before you can continue to install the system. (In fact, the installation order of centos is changed from a graphical interface to a character interface.)

Enter the corresponding command to configure the system. 

system installation

Check the system load at this time:

[root@kvm ~]# virsh list
 Id    名称                         状态
----------------------------------------------------
 1     KVM_test                       running

Guess you like

Origin blog.csdn.net/qq_48391148/article/details/131439561