管理KVM虚拟机

工具:libvirt  

官网:http://libvirt.org/

介绍:Libvirt 库是一种实现 Linux 虚拟化功能的 Linux® API,它支持各种虚拟机监控程序,包括 Xen 和 KVM,以及 QEMU 和用于其他操作系统的一些虚拟产品。

工作原理如下图

centos7 kvm虚拟机 管理面板 kvm虚拟机管理网页_xml

 

配置文件

KVM创建的虚拟机配置文件路径: /etc/libvirt/qemu,它是xml后缀文件,如之前我们创建的 CentOS-7-X86_64,它的配置文件是CentOS-7-X86_64.xml

备注:这个文件不能使用vi、vim命令进行修改,可以通过命令virsh edit CentOS-7-X86_64修改。

centos7 kvm虚拟机 管理面板 kvm虚拟机管理网页_CentOS_02

配置文件样例说明:

centos7 kvm虚拟机 管理面板 kvm虚拟机管理网页_xml_03

centos7 kvm虚拟机 管理面板 kvm虚拟机管理网页_CentOS_04

1 <domain type='kvm'>
 2   <name>CentOS-7-X86_64</name>  名称
 3   <uuid>ae53ced4-573c-4de0-9bd9-0852451b8573</uuid>  唯一
 4   <memory unit='KiB'>2097152</memory>   内存
 5   <currentMemory unit='KiB'>2097152</currentMemory>   当前内存
 6   <vcpu placement='static'>1</vcpu>   CPU
 7   <os>
 8     <type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>  hvm表示硬件虚拟机
 9     <boot dev='hd'/>  启动硬盘
10   </os>
11   <features>
12     <acpi/>
13     <apic/>
14   </features>
15   <cpu mode='custom' match='exact'>
16     <model fallback='allow'>Broadwell-noTSX</model>
17   </cpu>
18   <clock offset='utc'>
19     <timer name='rtc' tickpolicy='catchup'/>
20     <timer name='pit' tickpolicy='delay'/>
21     <timer name='hpet' present='no'/>
22   </clock>
23   <on_poweroff>destroy</on_poweroff>
24   <on_reboot>restart</on_reboot>
25   <on_crash>restart</on_crash>
26   <pm>
27     <suspend-to-mem enabled='no'/>
28     <suspend-to-disk enabled='no'/>
29   </pm>
30   <devices>
31     <emulator>/usr/libexec/qemu-kvm</emulator>
32     <disk type='file' device='disk'>  硬盘
33       <driver name='qemu' type='raw'/>
34       <source file='/opt/CentOS-7-x86_64.raw'/>
35       <target dev='vda' bus='virtio'/>
36       <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
37     </disk>
38     <disk type='file' device='cdrom'>
39       <driver name='qemu' type='raw'/>
40       <target dev='hda' bus='ide'/>
41       <readonly/>
42       <address type='drive' controller='0' bus='0' target='0' unit='0'/>
43     </disk>
44     <controller type='usb' index='0' model='ich9-ehci1'>
45       <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x7'/>
46     </controller>
47     <controller type='usb' index='0' model='ich9-uhci1'>
48       <master startport='0'/>
49       <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0' multifunction='on'/>
50     </controller>
51     <controller type='usb' index='0' model='ich9-uhci2'>
52       <master startport='2'/>
53       <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x1'/>
54     </controller>
55     <controller type='usb' index='0' model='ich9-uhci3'>
56       <master startport='4'/>
57       <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x2'/>
58     </controller>
59     <controller type='pci' index='0' model='pci-root'/>
60     <controller type='ide' index='0'>
61       <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
62     </controller>
63     <controller type='virtio-serial' index='0'>
64       <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
65     </controller>
66     <interface type='network'>  网络
67       <mac address='52:54:00:d0:dd:c9'/>
68       <source network='default'/>
69       <model type='virtio'/>
70       <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
71     </interface>
72     <serial type='pty'>
73       <target port='0'/>
74     </serial>
75     <console type='pty'>
76       <target type='serial' port='0'/>
77     </console>
78     <channel type='unix'>
79       <target type='virtio' name='org.qemu.guest_agent.0'/>
80       <address type='virtio-serial' controller='0' bus='0' port='1'/>
81     </channel>
82     <input type='tablet' bus='usb'>
83       <address type='usb' bus='0' port='1'/>
84     </input>
85     <input type='mouse' bus='ps2'/>
86     <input type='keyboard' bus='ps2'/>
87     <graphics type='vnc' port='-1' autoport='yes' listen='0.0.0.0'>
88       <listen type='address' address='0.0.0.0'/>
89     </graphics>
90     <video>
91       <model type='cirrus' vram='16384' heads='1' primary='yes'/>
92       <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
93     </video>
94     <memballoon model='virtio'>
95       <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
96     </memballoon>
97   </devices>
98 </domain>

xml配置

 

重要命令

  • 帮助命令
virsh  --help
  • dumpxml导出命令,将虚拟机配置文件导出做备份
virsh dumpxml CentOS-7-X86_64 > backup.xml
  • 恢复备份,如果虚拟机通过undefine命令误删除,但是xml还存在,可以通过这个命令恢复
virsh define backup.xml
  • 创建快照
virsh snapshot-create CentOS-7-X86_64

 

CPU动态扩容

centos 7上,kvm支持CPU动态扩容

1、在创建时需要设置maxcpus,如下图

centos7 kvm虚拟机 管理面板 kvm虚拟机管理网页_CentOS_05

2、对于已创建的虚拟机,修改它的xml文,例

virsh edit CentOS-7-X86_64

将 <vcpu placement='static'>1</vcpu>

修改成 <vcpu placement='auto' current='1'>4</vcpu>

表示动态VCPU,当前CPU为1,最大CPU为4

重启

virsh shutdown CentOS-7-X86_64
virsh start CentOS-7-X86_64

将虚拟机CPU改成2

virsh setvcpus CentOS-7-X86_64 2 --live

查看CPU个数,cat /proc/cpuinfo

查看CPU状态是否工作,cat /sys/devices/system/cpu/cpu1/online

 

内存动态扩容

修改内存为512

virsh qemu-monitor-command CentOS-7-X86_64 --hmp --cmd balloon 512

备注:内存最大值为创建时设置的值

查看虚拟机内存

virsh qemu-monitor-command CentOS-7-X86_64 --hmp --cmd info balloon

 

KVM磁盘

硬盘格式总体上分为两种:1为全镜像格式,2为稀疏格式

  • 全镜像格式(典型代表raw格式),其特点:设置多大就是多大,写入速度快,方便的转换为其他格式,性能最优,但是占用空间大,不支持快照
  • 稀疏模式(典型代表qcow2格式),其特点:支持压缩,快照,镜像,更小的存储空间(即用多少占多少)

查看文件格式

qemu-img info CentOS-7-x86_64.raw

raw转化成qcow2

qemu-img convert -f raw -0 qcow2 /opt/CentOS-7-x86_64.raw /opt/test.qcow2

  

KVM网络

1、查看网络

brctl show

2、创建桥接网络

brctl addbr br0
brctl addbrif br0 eth0
ip addr del eth0 192.168.137.11/24
ifconfig br0 192.168.137.11/24 up
route add default gw 192.168.137.2

virsh edit CentOS-7-X86_64

<interface type='network'>
      <mac address='52:54:00:d0:dd:c9'/>
      <source network='default'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </interface>

改成

<interface type='bridge'>
      <mac address='52:54:00:d0:dd:c9'/>
      <source bridge='br0'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </interface>

重启

virsh shutdown CentOS-7-X86_64
virsh start CentOS-7-X86_64

 

KVM优化

三方面:CPU、内存、IO

1、CPU优化

taskset --help  帮助命令

taskset -cp 0 pid

centos7 kvm虚拟机 管理面板 kvm虚拟机管理网页_CentOS_06

指定6573进程使用CPU0,减少cache miss

2、内存优化

  • 开启大页内存 
  • 内存合并  cat /sys/kernel/mm/transparent_hugepage/enabled

3、IO优化

查看IO调度算法  cat /sys/block/sda/queue/scheduler,CentOS 7调度算法三种:noop、deadline、cfq

  • noop  什么都不做,适用于SSD硬盘
  • deadline  “最后期限”算法
  • cfq  完全公平队列

centos7 kvm虚拟机 管理面板 kvm虚拟机管理网页_CentOS_07