KVMKernel-based Virtual Machine的简称,是一个开源的虚拟化模块,今天我将在CentOS7的操作系统上安装KVM

一.环境信息

系统: CentOS 7.7 IP: 10.0.0.41/24

在虚拟机安装时,需要启用CPU的虚拟化功能

kvm虚拟机怎么开启内核虚拟化 kvm虚拟机怎么用_Ethernet

二. KVM安装步骤

1. 安装qemu-kvm和libvirt

#qemu-kvm用来创建虚拟机硬盘,libvirt用来管理虚拟机
[root@c7-41 ~]# yum install -y qemu-kvm libvirt

2. 安装virt-install

#用来创建虚拟机
[root@c7-41 ~]# yum install -y virt-install

3. 启动libvirtd,并将它设为开机启动,

#启动后使用ifconfig查看,发现会多出来一块virbr0的网卡,ip默认为192.168.122.1/24,说明libvirtd启动成功,如果默认没有ifconfig命令,使用yum install -y net-tools安装
[root@c7-41 ~]# systemctl start libvirtd && systemctl enable libvirtd
[root@c7-41 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.0.41  netmask 255.255.255.0  broadcast 10.0.0.255
        inet6 fe80::20c:29ff:fe49:48c7  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:49:48:c7  txqueuelen 1000  (Ethernet)
        RX packets 35522  bytes 50991746 (48.6 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 5373  bytes 579842 (566.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.1.41  netmask 255.255.0.0  broadcast 172.16.255.255
        inet6 fe80::20c:29ff:fe49:48d1  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:49:48:d1  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 14  bytes 1016 (1016.0 B)
        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 1000  (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:0b:43:69  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

经过以上三步,KVM安装成功,下面开始使用KVM创建虚拟机.

三. 使用KVM创建虚拟机(Centos7)

1. 使用qemu命令创建一个10G的硬盘

#最小10,G,可以更多,硬盘的名称为: CentOS-7-x86_64.raw
[root@c7-41 ~]# qemu-img create -f raw /opt/CentOS-7-x86_64.raw 10G
Formatting '/opt/CentOS-7-x86_64.raw', fmt=raw size=10737418240 
[root@c7-41 ~]# ll -h /opt
total 0
-rw-r--r-- 1 root root 10G May  9 18:03 CentOS-7-x86_64.raw

2. 使用virt-install创建名称为CentOS-7-x86_64的虚拟机

#在创建之前,先上传一个CentOS7的ISO镜像,最好在根目录下创建一个ISO的目录文件夹
[root@c7-41 ~]# mkdir /ISO
[root@c7-41 ~]# cd /ISO/
[root@c7-41 ISO]# ls
CentOS-7-x86_64-DVD-1908.iso
[root@c7-41 ISO]# virt-install --virt-type kvm --name CentOS-7-x86_64 --ram 1024 --cdrom=/ISO/CentOS-7-x86_64-DVD-1908.iso --disk path=/opt/CentOS-7-x86_64.raw --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole

Starting install...
Domain installation still in progress. You can reconnect to 
the console to complete the installation process.

vpn下载地址 这时候使用VNC工具,连接主机IP 10.0.0.41,设置安装操作系统的网卡名称为eth0,如图所示

kvm虚拟机怎么开启内核虚拟化 kvm虚拟机怎么用_kvm虚拟机怎么开启内核虚拟化_02


使用tab

kvm虚拟机怎么开启内核虚拟化 kvm虚拟机怎么用_Ethernet_03


回车

安装步骤和我们平时安装操作系统的方法一样,安装完成后,可以使用virsh list --all显示KVM上所有的虚拟机

[root@c7-41 ISO]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 -     CentOS-7-x86_64                shut off            # 可以看到虚拟机的名称和状态

四. 管理KVM

1. 使用virsh启动KVM中的虚拟机

[root@c7-41 ISO]# virsh start CentOS-7-x86_64
Domain CentOS-7-x86_64 started

启动后,使用VNC连接工具,登录后可以看到网卡的名称为eth0,默认没有ip地址,是因为网卡没有激活,将ONBOOT改为yes,重启网卡,可以看到ip地址为192.168.122.0网段的

kvm虚拟机怎么开启内核虚拟化 kvm虚拟机怎么用_Domain_04


当KVM中的虚拟机启动后,可以在/etc/libvirt/qemu目录下,看到启动的虚拟机的信息

root@c7-41 ISO]# cd /etc/libvirt/qemu
[root@c7-41 qemu]# pwd
/etc/libvirt/qemu
[root@c7-41 qemu]# ls -lh
total 8.0K
-rw------- 1 root root 4.2K May  9 18:11 CentOS-7-x86_64.xml
drwx------ 3 root root   42 May  9 18:08 networks

CentOS-7-x86_64.xml就是启动的虚拟机的信息,当我们使用vim编辑器查看时,会提示如下信息:

<!--
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.
-->

看到提示需要修改虚拟机的信息,需要使用命令 virsh edit CentOS-7-x86_64,是用这条命令后,我们可以看到很多虚拟机的信息,包括vcpu,memory

[root@c7-41 qemu]# virsh edit CentOS-7-x86_64
<domain type='kvm'>
  <name>CentOS-7-x86_64</name>
  <uuid>e7c924f2-2ebf-4eca-9124-b6740b70a4fb</uuid>
  <memory unit='KiB'>1048576</memory>
  <currentMemory unit='KiB'>1048576</currentMemory>
  <vcpu placement='auto' current='1'>4</vcpu>
  <os>
    <type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
    <boot dev='hd'/>
  </os>
  <features>
    <acpi/>
    <apic/>
  </features>
  <cpu mode='custom' match='exact' check='partial'>
    <model fallback='allow'>Broadwell-noTSX-IBRS</model>
    <feature policy='require' name='md-clear'/>
    <feature policy='require' name='spec-ctrl'/>
    <feature policy='require' name='ssbd'/>
  </cpu>
  <clock offset='utc'>
    <timer name='rtc' tickpolicy='catchup'/>
    <timer name='pit' tickpolicy='delay'/>
    <timer name='hpet' present='no'/>
  </clock>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>destroy</on_crash>
  <pm>
    <suspend-to-mem enabled='no'/>
    <suspend-to-disk enabled='no'/>
  </pm>
  <devices>
"/tmp/virsh1kbtgk.xml" 107L, 4024C written
Domain CentOS-7-x86_64 XML configuration edited.

2. CPU热添加(

#KVM支持CPU的热添加,不过局限是宿主机和虚拟机的系统相同才行,这里宿主机是CentOS7,虚拟机也是CentOS7,所以可以模拟CPU热添加,不过真实生产环境不建议这样操作)
[root@c7-41 qemu]# virsh edit CentOS-7-x86_64
 <vcpu placement='static'>1</vcpu> 修改为: 
 <vcpu placement='auto' current='1'>4</vcpu> 
 #修改成这个##意思是当前cpu为1核,最多可以有4核,修改完虚拟机的配置文件,必须关闭虚拟机再启动,配置才能生效
[root@c7-41 qemu]# virsh shutdown CentOS-7-x86_64      #关闭虚拟机
Domain CentOS-7-x86_64 is being shutdown

[root@c7-41 qeum ~]# virsh list --all            ##查看虚拟机状态 
Id          Name            State
----------------------------------------------------
-      CentOS-7-x86_64          shut off

[root@c7-41 qemu]# virsh start CentOS-7-x86_64       #启动虚拟机
Domain CentOS-7-x86_64 started


[root@c7-41 qemu]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 3     CentOS-7-x86_64                running

先通过VNC工具查看虚拟机的cpu信息

kvm虚拟机怎么开启内核虚拟化 kvm虚拟机怎么用_Domain_05


在宿主机上通过命令添加核数

[root@c7-41 qemu]# virsh setvcpus CentOS-7-x86_64 2 --live     
#虚拟机的cpu添加到2core

再通过VNC工具查看虚拟机CPU信息

kvm虚拟机怎么开启内核虚拟化 kvm虚拟机怎么用_Domain_06

3. 内存热添加

(还是不建议生产中使用,知道功能就行了)

通过VNC连接虚拟机,查看虚拟机内存

kvm虚拟机怎么开启内核虚拟化 kvm虚拟机怎么用_Ethernet_07

[root@c7-41 qemu]# virsh qemu-monitor-command CentOS-7-x86_64 --hmp --cmd info balloon
balloon: actual=1024
# 查看当前虚拟机的内存

[root@c7-41 qemu]# virsh qemu-monitor-command CentOS-7-x86_64 --hmp --cmd balloon 512
# 修改当前虚拟机的内存为512

[root@c7-41 qemu]# virsh qemu-monitor-command CentOS-7-x86_64 --hmp --cmd info balloon
balloon: actual=512
# 查看修改的内存信息已经改变成了512

使用VNC工具连接虚拟机,查看内存是否有变:

kvm虚拟机怎么开启内核虚拟化 kvm虚拟机怎么用_kvm虚拟机怎么开启内核虚拟化_08

4. 网卡配置

(如何将KVM虚拟机中的网卡改为桥接模式,让局域网的其他用户可以正常访问)

[root@c7-41 qemu]# brctl show
bridge name	bridge id		STP enabled	interfaces
virbr0		8000.5254003a7202	yes		virbr0-nic
							vnet0

创建一个脚本

#创建一个脚本,删除ens33网卡的ip信息,新建一个桥接网卡,并且将ens33网卡的ip信息富裕新建的桥接网卡
[root@c7-41 qemu]# cd
[root@c7-41 ~]# cat br0.sh 
#!/bin/bash
brctl addbr br0            #创建一个桥接网卡br0
brctl addif br0 ens33               #将ens33网卡的桥接信息转到br0网卡
ip addr del dev ens33 10.0.0.41/24          #将ens33网卡的ip信息删除
ifconfig br0 10.0.0.41/24 up              #将ens33网卡的ip信息这只到br0网卡上
route add default gw 10.0.0.2            #添加网关
[root@c7-41 ~]# chmod u+x br0.sh
[root@c7-41 ~]# ./br0.sh

#使用ifocnfig查看网卡的ip信息,可以看到br0的地址已经变为了10.0.0.41/24
[root@c7-41 ~]# ifconfig
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.0.41  netmask 255.255.255.0  broadcast 10.0.0.255
        inet6 fe80::20c:29ff:fe49:48c7  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:49:48:c7  txqueuelen 1000  (Ethernet)
        RX packets 42  bytes 2438 (2.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 63  bytes 4762 (4.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::20c:29ff:fe49:48c7  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:49:48:c7  txqueuelen 1000  (Ethernet)
        RX packets 3523503  bytes 4923288560 (4.5 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 313418  bytes 52607195 (50.1 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.1.41  netmask 255.255.0.0  broadcast 172.16.255.255
        inet6 fe80::20c:29ff:fe49:48d1  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:49:48:d1  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 14  bytes 1016 (1016.0 B)
        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 1000  (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:75:79:e4  txqueuelen 1000  (Ethernet)
        RX packets 81  bytes 6682 (6.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 46  bytes 5691 (5.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vnet0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::fc54:ff:fedb:1c4c  prefixlen 64  scopeid 0x20<link>
        ether fe:54:00:db:1c:4c  txqueuelen 1000  (Ethernet)
        RX packets 19  bytes 1712 (1.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 147  bytes 8262 (8.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

使用命令修改KVM虚拟机的信息

[root@c7-41 ~]# virsh edit CentOS-7-x86_64
 72  <interface type='network'>  修改为:
     <interface type='bridge'>

 74   <source network='default'/> 修改为:
      <source bridge='br0'/>

修改完成后关闭虚拟机,启动虚拟机,通过VNC工具修改ip为10.0.0.88,尝试ping宿主机是否可以ping通,并通过ssh连接

[root@c7-41 ~]# virsh shutdown CentOS-7-x86_64
Domain CentOS-7-x86_64 is being shutdown

[root@c7-41 ~]# virsh start CentOS-7-x86_64 
Domain CentOS-7-x86_64 started

kvm虚拟机怎么开启内核虚拟化 kvm虚拟机怎么用_kvm虚拟机怎么开启内核虚拟化_09


kvm虚拟机怎么开启内核虚拟化 kvm虚拟机怎么用_kvm虚拟机怎么开启内核虚拟化_10


通过本地的Xshell软件,尝试连接,可以连接,并且可以看到IP信息

kvm虚拟机怎么开启内核虚拟化 kvm虚拟机怎么用_Ethernet_11


kvm虚拟机怎么开启内核虚拟化 kvm虚拟机怎么用_kvm虚拟机怎么开启内核虚拟化_12