We are used to using VmWare or Virtual Box to implement virtualization on Windows platforms. Although they have Linux versions, most enterprises choose to use KVM to implement virtualization on Linux platforms. Therefore, learning KVM is an essential skill.

Install the KVM

Using centos as an example, the following commands are used to install KVM virtualization.

yum install -y qemu-kvm libvirt virt-install bridge-utils
Copy the code

What does all this software do?

software role
qemu-kvm A software that integrates QEMU and KVM.
libvirt QEMU encapsulates the interface, which can be more convenient to operate virtual machines, and provides many kinds of programming language SDK.
virt-install Command line tool used to create virtual machines.
bridge-utils Linux bridge, used to configure the vm bridge network.

What do KVM, qEMu, qemu-KVM and libvirt have to do with each other?

The Kernel Virtual Machine (KVM) is a Linux Kernel driver module. It requires CPU support and adopts hardware-assisted virtualization technologies such as Intel-VT and AMD-V. Memory-related technologies such as Intel’S EPT and AMD’s RVI make it possible to turn a Linux host into a Hypervisor.

QEMU is a virtual machine implemented purely by software. It can simulate CPU, memory, disk and other hardware to make virtual machines think that they are hardware at the bottom. In fact, all these are simulated by QEMU.

Qemu – KVM QEMU integrates KVM to virtualize CPU and memory over KVM and simulate I/O devices, such as network adapters and disks. The performance loss from this combination is significantly reduced, in the range of 1% to 2%, compared to using the hardware directly.

Libvirt is the most widely used tool and API for managing KVM virtual machines. Libvirtd is a daemon process. It can be invoked by the local or remote virsh. Libvirtd uses qemu-KVM to operate VMS.

Start the libvirt

systemctl start libvirtd
systemctl enable libvirtd
Copy the code

If you don’t want to use command line tools to manage virtual machines, you can install virt-Manager.

yum install -y virt-manager
Copy the code

You can start it by typing virt-manager directly on an SSH client that supports X11 forwarding (for example, MobaXterm).

Virtual network Type

And vmware, KVM also supports three types of networks.

  1. In NAT mode, a VM needs to send traffic to the host. The host machine translates network information before sending traffic, and external machines cannot detect the VM. In this way, the host is equivalent to a router. Therefore, the host has an IP address in the same network segment as the VM, and the GATEWAY address of the VM is the same IP address of the host.

  2. Host mode VMS can access each other but not the host. This mode is similar to the NAT mode, but it does not have an IP address in the same network segment as the VM. Therefore, the VM cannot use the host to access the external network.

  3. Bridge mode The VM and host are associated on the same network bridge, so the VM and host can reside on the same network segment. External VMS can directly access the VM, and VMS can use the bridge to access external networks.

    The other mode is widely used on cloud platforms such as openstack. Physical nics bound to Bridges do not have IP addresses. Configure the switch ports in trunk mode, connect VM ports to the bridge, and configure different VLAN tags for the ports to achieve isolation and interconnection.

NAT mode and host mode do not need to be configured separately, so let’s see how to configure a bridge network.

Configure the bridge network

After the physical network adapter is bound to the bridge, the network will be disconnected, so we need to configure the original IP address on the bridge.

CD /etc/sysconfig/network-scripts/ cp ifcfg-enp134s0f0 ifcfg-br0 copy the original NIC configuration file as the bridge NICCopy the code

Change TYPE=Ethernet in ifcfg-br0 to TYPE=Bridge. The final result is as follows:

DEVICE= BR0 ONBOOT=yes BOOTPROTO= None TYPE=Bridge IPADDR= PREFIX=16 GATEWAY= DNS1=114.114.114Copy the code

Modify ifcfg-enp134s0f0 file to delete IPADDR= NETMASK= GATEWAY= line, and add BRIDGE=br0 at the end, the final effect is as follows:

Copy the code

Finally, restart the network.

systemctl restart network
Copy the code

Viewing the network information, you can also see that the IP is configured on the bridge, and the bridge is associated with a physical nic.

[root@localhost network-scripts]# ip a8: enp134s0f0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq master br0 state UP group default qlen 1000 link/ether 74:a4:b5:01:04:22 brd ff:ff:ff:ff:ff:ff inet6 fe80::76a4:b5ff:fe01:422/64 scope link valid_lft forever preferred_lft forever 17: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000 link/ether 74:a4:b5:01:04:22 BRD FF :ff:ff:ff:ff: FF: FF INET BRD scope Global BR0 VALID_lft forever preferred_lft forever inet6 fe80::76a4:b5ff:fe01:422/64 scope link valid_lft forever preferred_lft forever [root@localhost network-scripts]# brctl showBridge Name Bridge ID STP Enabled interfaces BR0 8000.74A4B5010422 no ENP134s0f0Copy the code

Creating a VM

There are three options for creating a VM

  1. Virt-install Uses commands to create VMS, which is convenient and quick. However, it is not friendly to beginners, and many parameters are difficult to set.
  2. Libvirt is created using the Libvirt API, which is required for virtualization platform development.
  3. The virt-Manager uses a GRAPHICAL user interface (GUI) to create VMS. If you are familiar with vmware, you can use the virt-Manager.

Therefore, I prefer to use Virt-Manager to create virtual machines. The process is very simple, so I won’t post screenshots.

Managing VMS

Common commands used to operate VMS

# list the running virtual machines
virsh list
# list all virtual machines
virsh list --all
# Start the VM
virsh start test  
# Shut down the VM
virsh shutdown test  
# Forcibly stop the VM
virsh destroy test  
The vm configuration file will be deleted, but the virtual disk will not be deleted
virsh undefine test  
Set the vm to start when the host machine starts
virsh autostart test  
# Unboot boot
virsh autostart --disable test 
# Suspend the VM
virsh suspend test 
# Restore suspended VIRTUAL machines
virsh resume test 
After obtaining the VNC port, you can use tools such as vNCViewer to access the VM
virsh vncdisplay test
Export the VM XML configuration file
virsh dumpxml test >/root/test.xml
# Modify the VM configuration file
virsh edit test
Copy the code