KVM installation and preliminary use under CentOS 7.2

KVM is the abbreviation of Kernel-based Virtual Machine. It is an open source virtualization module. Today I will install KVM on the CentOS7 operating system. The following are my installation steps.

  1. Environmental Information

System: CentOS 7.2 IP: 10.0.0.12/24

When installing the virtual machine, you need to enable the virtualization function of the CPU

  1. KVM installation steps

  2. Install qemu-kvm and libvirt

[ root@kvm-node1 ~]# yum install -y qemu-kvm libvirt   ###qemu-Kvm is used to create virtual machine hard disks,libvirt is used to manage virtual machines
  1. Install virt-install
[ root@kvm-node1 ~]# yum install -y virt-install    ###Used to create virtual machines
  1. Start libvirtd and set it to boot up. After starting, use ifconfig to check and find that there will be an extra virbr0 network card. The default ip is 192.168.122.1/24, indicating that libvirtd has started successfully. If there is no ifconfig command by default, use yum install- y net-tools installation
[ root@kvm-node1 ~]# systemctl start libvirtd && systemctl enable libvirtd
[ root@kvm-node1 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
  inet 10.0.0.12  netmask 255.255.255.0  broadcast 10.0.0.255
  inet6 fe80::5505:5aa:3f96:f3f8  prefixlen 64  scopeid 0x20<link>
  ether 00:0c:29:51:09:70  txqueuelen 1000(Ethernet)
  RX packets 22459  bytes 29147955(27.7 MiB)
  RX errors 0  dropped 0  overruns 0  frame 0
  TX packets 5161  bytes 587757(573.9 KiB)
  TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
  inet 127.0.0.1  netmask 255.0.0.0
  inet6 ::1  prefixlen 128  scopeid 0x10<host>
  loop  txqueuelen 1(Local Loopback)
  RX packets 0  bytes 0(0.0 B)
  RX errors 0  dropped 0  overruns 0  frame 0
  TX packets 0  bytes 0(0.0 B)
  TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
  inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
  ether 52:54:00:fb:b1:55  txqueuelen 1000(Ethernet)
  RX packets 0  bytes 0(0.0 B)
  RX errors 0  dropped 0  overruns 0  frame 0
  TX packets 0  bytes 0(0.0 B)
  TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

After the above three steps, the KVM installation is successful, and then start using KVM to create a virtual machine.

  1. Use KVM to create a virtual machine (CentOS7)

  2. Use the qemu command to create a 10G hard disk (minimum 10, G, you can more), the name of the hard disk is: CentOS-7-x86_64.raw

[ root@kvm-node1 ~]# qemu-img create -f raw /opt/CentOS-7-x86_64.raw 10G
Formatting '/opt/CentOS-7-x86_64.raw', fmt=raw size=10737418240[root@kvm-node1 ~]# ll -h /opt
total 0-rw-r--r--1 root root 10G Aug 2207:59 CentOS-7-x86_64.raw
  1. Use virt-install to create a virtual machine named CentOS-7-x86_64. Before creating it, upload an ISO image of CentOS7. It is best to create an ISO directory folder in the root directory.

[ root@kvm-node1 ~]# virt-install --virt-type kvm --name CentOS-7-x86_64 --ram 1024--cdrom=/ISO/CentOS-7-x86_64-Everything-1611.iso --disk path=/opt/CentOS-7-x86_64.raw --network network=default--graphics vnc,listen=0.0.0.0--noautoconsole
 
Starting install...
Creating domain...|0 B  00:03     
Domain installation still in progress. You can reconnect to 
the console to complete the installation process.

At this time, use the TightVNC tool to connect to the host IP 10.0.0.12, and set the name of the network card to install the operating system to eth0, as shown in the figure

The installation steps are the same as we usually install the operating system. After the installation is complete, you can use virsh list --all to display all virtual machines on KVM.

[ root@kvm-node1 ~]# virsh list --all
 Id    Name                           State
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  CentOS-7-x86_64                shut off                ###You can see the name and status of the virtual machine
  1. Manage KVM

  2. Use virsh to start the virtual machine in KVM

[ root@kvm-node1 ~]# virsh start CentOS-7-x86_64
Domain CentOS-7-x86_64 started

After startup, use the VNC connection tool. After logging in, you can see that the name of the network card is eth0, and there is no ip address by default. This is because the network card is not activated. Change ONBOOT to yes, restart the network card, and you can see that the ip address is the 192.168.122.0 network segment. of

When the virtual machine in KVM is started, you can see the information of the started virtual machine in the /etc/libvirt/qemu directory

[ root@kvm-node1 ~]# cd /etc/libvirt/qemu
[ root@kvm-node1 qemu]# pwd
/etc/libvirt/qemu
[ root@kvm-node1 qemu]# ls -lh
total 4.0K
- rw-------1 root root 3.8K Aug 2208:18 CentOS-7-x86_64.xml    
drwx------3 root root   42 Aug 2207:51 networks

CentOS-7-x86_64.xml is the information of the started virtual machine. When we use the vim editor to view it, the following information will be prompted:

<!--
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:
 virsh edit CentOS-7-x86_64
or other application using the libvirt API.-->

When you see the prompt that you need to modify the information of the virtual machine, you need to use the command virsh edit CentOS-7-x86_64. After using this command, we can see a lot of virtual machine information, including vcpu, memory, etc.

  1. CPU hot addition (KVM supports CPU hot addition, but the limitation is that [Host] (https://cloud.tencent.com/product/cdh?from=10680) and the virtual machine system are the same. (This is not recommended for production environments)
[ root@kvm-node1 ~]# virsh edit CentOS-7-x86_64
6< vcpu placement='static'>1</vcpu>change into:<vcpu placement='auto' current="1">4</vcpu>
## Means that the current cpu is 1 core,Up to 4 cores,After modifying the configuration file of the virtual machine,The virtual machine must be shut down and restarted,Configuration to take effect

[ root@kvm-node1 ~]# virsh shutdown CentOS-7-x86_64 ##Close the virtual machine
Domain CentOS-7-x86_64 is being shutdown


[ root@kvm-node1 ~]# virsh list --all ##View virtual machine status
Id          Name            State


[ root@kvm-node1 ~]# virsh start CentOS-7-x86_64 ##Start the virtual machine
Domain CentOS-7-x86_64 started


[ root@kvm-node1 ~]# virsh list --all 
Id          Name          State

3        CentOS-7-x86_64     running

First view the cpu information of the virtual machine through the VNC tool

Add cores by command on the host

[ root@kvm-node1 ~]# virsh setvcpus CentOS-7-x86_64 2--live ##Add the cpu of the virtual machine to 2core

Then use the VNC tool to view the virtual machine CPU information

  1. Memory hot addition (still not recommended for use in production, just know the function)

Connect to the virtual machine through VNC and view the virtual machine memory

[ root@kvm-node1 ~]# virsh qemu-monitor-command CentOS-7-x86_64 --hmp --cmd info balloon   ##View the memory of the current virtual machine
balloon: actual=1024[root@kvm-node1 ~]# virsh qemu-monitor-command CentOS-7-x86_64 --hmp --cmd balloon 512   ##Modify the memory of the current virtual machine to 512[root@kvm-node1 ~]# virsh qemu-monitor-command CentOS-7-x86_64 --hmp --cmd info balloon  ##View the modified memory information has been changed to 512
balloon: actual=512

Use the VNC tool to connect to the virtual machine and check whether the memory has changed:

  1. Network card configuration (how to change the network card in the KVM virtual machine to bridge mode so that other users on the LAN can access it normally)
[ root@kvm-node1 ~]# brctl show  ##View existing bridges
bridge name     bridge id               STP enabled     interfaces
virbr0          8000.525400fbb155       yes             virbr0-nic
              vnet0

Create a script to delete the ip information of the ens33 network card, create a new bridge network card, and enrich the ip information of the ens33 network card.

[ root@kvm-node1 ~]# vim br0.sh
#! /bin/bash
brctl addbr br0         #Create a bridged network card br0
brctl addif br0 ens33   #Transfer the bridging information of the ens33 network card to the br0 network card
ip addr del dev ens33 10.0.0.12/24  #Delete the ip information of the ens33 network card
ifconfig br0 10.0.0.12/24 up        #Change the ip information of the ens33 network card to the br0 network card
route add default gw 10.0.0.2       #Add gateway
[ root@kvm-node1 ~]# chmod u+x br0.sh
[ root@kvm-node1 ~]# ./br0.sh

Use ifocnfig to view the ip information of the network card, you can see that the address of br0 has changed to 10.0.0.12/24

[ root@kvm-node1 ~]# ifconfig 
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
  inet 10.0.0.12  netmask 255.255.255.0  broadcast 10.0.0.255
  inet6 fe80::20c:29ff:fe51:970  prefixlen 64  scopeid 0x20<link>
  ether 00:0c:29:51:09:70  txqueuelen 1000(Ethernet)
  RX packets 553  bytes 41723(40.7 KiB)
  RX errors 0  dropped 0  overruns 0  frame 0
  TX packets 680  bytes 63511(62.0 KiB)
  TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
  inet6 fe80::5505:5aa:3f96:f3f8  prefixlen 64  scopeid 0x20<link>
  ether 00:0c:29:51:09:70  txqueuelen 1000(Ethernet)
  RX packets 6107998  bytes 8708309251(8.1 GiB)
  RX errors 0  dropped 0  overruns 0  frame 0
  TX packets 534420  bytes 68906654(65.7 MiB)
  TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
  inet 127.0.0.1  netmask 255.0.0.0
  inet6 ::1  prefixlen 128  scopeid 0x10<host>
  loop  txqueuelen 1(Local Loopback)
  RX packets 0  bytes 0(0.0 B)
  RX errors 0  dropped 0  overruns 0  frame 0
  TX packets 0  bytes 0(0.0 B)
  TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
  inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
  ether 52:54:00:fb:b1:55  txqueuelen 1000(Ethernet)
  RX packets 6685  bytes 278745(272.2 KiB)
  RX errors 0  dropped 0  overruns 0  frame 0
  TX packets 6981  bytes 34351624(32.7 MiB)
  TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vnet0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
  inet6 fe80::fc54:ff:fe6b:5fee  prefixlen 64  scopeid 0x20<link>
  ether fe:54:00:6b:5f:ee  txqueuelen 1000(Ethernet)
  RX packets 16  bytes 1650(1.6 KiB)
  RX errors 0  dropped 0  overruns 0  frame 0
  TX packets 987  bytes 52236(51.0 KiB)
  TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Use commands to modify the information of the KVM virtual machine

[ root@kvm-node1 ~]# virsh edit CentOS-7-x86_64
69< interfacetype='network'>change into:<interfacetype='bridge'>  ##bridging
71< source network='default'/>change into:<source bridge='br0'/>

After the modification is completed, close the virtual machine, start the virtual machine, modify the ip to 10.0.0.14/24 through the VNC tool, try to ping the host machine to see if it can be pinged, and connect via ssh

[ root@kvm-node1 ~]# virsh shutdown CentOS-7-x86_64 
Domain CentOS-7-x86_64 is being shutdown

[ root@kvm-node1 ~]# virsh start CentOS-7-x86_64   
Domain CentOS-7-x86_64 started

Through the local ssh software, try to connect, you can connect, and you can see the IP information

The above is the installation and simple management of KVM in CentOS7.

This article permanently updates the link address: http://www.linuxidc.com/Linux/2017-08/146414.

Recommended Posts

KVM installation and preliminary use under CentOS 7.2
centos7 kvm installation and use
Installation and use of Mysql under CentOS
Centos7 elk7.1.1 installation and use
Use Nginx and u under CentOS
Centos6.5 installation and deployment of KVM
Java-JDK installation and configuration under CentOS
Install and use docker under CentOS 6.8
Erlang 20.2 installation and deployment under CentOS 7
Tomcat installation and configuration under CentOS 7 (Tomcat startup)
MySQL 8.0 installation, deployment and configuration under CentOS 6/7
Installation and configuration of redis under centos7
Zabbix installation and deployment and localization under CentOS
Jenkins installation and deployment tutorial under CentOS 7
Redis3 installation under Centos7
CentOS 7 installation and configuration graphic tutorials under VMware10
MySQL 8.0 installation and deployment under CentOS, super detailed!
Installation and cracking of confluence6.3 operation records under Centos
Redis cluster installation under CentOS
Centos7 installation and configuration prometheus
Installation and cracking of Jira7 operation records under Centos
CentOS 7 installation and configuration PPTP
Installation and use of GDAL in Python under Ubuntu
CentOS installation and configuration cmake
Centos7.5 installation and configuration MongoDB4.0.4
Redis cluster installation under CentOS
CentOS 7 installation and configuration PPTP
GitLab installation and basic use
Detailed explanation of Spark installation and configuration tutorial under centOS7
Use Jexus 5.8.2 to deploy and run Asp.net core under Centos
Centos7 installation and configuration of Jenkins
Install and use dig under ubuntu/debian
Compile and install LAMP under Centos 5.2
Installation under centos6.9 of jenkins learning
CentOS6 minimal installation KVM detailed tutorial
CentOS7 installation and maintenance of Gitlab
Upgrade OpenSSL and OpenSSH under CentOS7
CentOS7.3 install iptables and detailed use
Deploy and optimize Tomcat under Centos
Redis installation under ubuntu and windows
CentOS 7.X system installation and optimization
Install Python3 and ansible under CentOS8
CentOS 7 Tomcat service installation and configuration
CentOS7 install and use SQL Server
CentOS NTP server installation and configuration
Install Python3 and Py under CentOS7
CentOs7 installation and deployment Zabbix3.4 original
Centos7 mysql database installation and configuration
Install Mono 3.2 and Jexus 5.4 under CentOS 6.3
2019-07-09 CentOS7 installation
centos7_1708 installation
CentOS 7 system installation and configuration graphic tutorial
Compile and install libmodbus library under CentOS7
Centos-6.5 installation and deployment of LNMP environment
Linux kernel compilation and CentOS system installation
Centos7.6 operating system installation and optimization record
Centos7 installation and deployment of gitlab server
Installation and uninstallation of CUDA under Ubuntu 16.04
Installation and use of Win10 subsystem Ubuntu
Centos python3 compile installation and compile gcc upgrade
Centos7 hive stand-alone mode installation and configuration