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.
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
KVM installation steps
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
[ root@kvm-node1 ~]# yum install -y virt-install ###Used to create virtual machines
[ 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.
Use KVM to create a virtual machine (CentOS7)
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
[ 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
Manage KVM
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.
[ 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 start CentOS-7-x86_64 ##Start the virtual machine
Domain CentOS-7-x86_64 started
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
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:
[ 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