管理KVM虚拟机
工具:libvirt
介绍:Libvirt 库是一种实现 Linux 虚拟化功能的 Linux® API,它支持各种虚拟机监控程序,包括 Xen 和 KVM,以及 QEMU 和用于其他操作系统的一些虚拟产品。
工作原理如下图
配置文件
KVM创建的虚拟机配置文件路径: /etc/libvirt/qemu,它是xml后缀文件,如之前我们创建的 CentOS-7-X86_64,它的配置文件是CentOS-7-X86_64.xml
备注:这个文件不能使用vi、vim命令进行修改,可以通过命令virsh edit CentOS-7-X86_64修改。
配置文件样例说明:
<domain type='kvm'>
<name>CentOS--X86_64</name> 名称
<uuid>ae53ced4-573c-4de0-9bd9-0852451b8573</uuid> 唯一
<memory unit='KiB'></memory> 内存
<currentMemory unit='KiB'></currentMemory> 当前内存
<vcpu placement='static'></vcpu> CPU
<os>
<type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type> hvm表示硬件虚拟机
<boot dev='hd'/> 启动硬盘
</os>
<features>
<acpi/>
<apic/>
</features>
<cpu mode='custom' match='exact'>
<model fallback='allow'>Broadwell-noTSX</model>
</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>restart</on_crash>
<pm>
<suspend-to-mem enabled='no'/>
<suspend-to-disk enabled='no'/>
</pm>
<devices>
<emulator>/usr/libexec/qemu-kvm</emulator>
<disk type='file' device='disk'> 硬盘
<driver name='qemu' type='raw'/>
<source file='/opt/CentOS-7-x86_64.raw'/>
<target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
</disk>
<disk type='file' device='cdrom'>
<driver name='qemu' type='raw'/>
<target dev='hda' bus='ide'/>
<readonly/>
<address type='drive' controller='' bus='' target='' unit=''/>
</disk>
<controller type='usb' index='' model='ich9-ehci1'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x7'/>
</controller>
<controller type='usb' index='' model='ich9-uhci1'>
<master startport=''/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0' multifunction='on'/>
</controller>
<controller type='usb' index='' model='ich9-uhci2'>
<master startport=''/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x1'/>
</controller>
<controller type='usb' index='' model='ich9-uhci3'>
<master startport=''/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x2'/>
</controller>
<controller type='pci' index='' model='pci-root'/>
<controller type='ide' index=''>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
</controller>
<controller type='virtio-serial' index=''>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
</controller>
<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>
<serial type='pty'>
<target port=''/>
</serial>
<console type='pty'>
<target type='serial' port=''/>
</console>
<channel type='unix'>
<target type='virtio' name='org.qemu.guest_agent.0'/>
<address type='virtio-serial' controller='' bus='' port=''/>
</channel>
<input type='tablet' bus='usb'>
<address type='usb' bus='' port=''/>
</input>
<input type='mouse' bus='ps2'/>
<input type='keyboard' bus='ps2'/>
<graphics type='vnc' port='-1' autoport='yes' listen='0.0.0.0'>
<listen type='address' address='0.0.0.0'/>
</graphics>
<video>
<model type='cirrus' vram='' heads='' primary='yes'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
</video>
<memballoon model='virtio'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
</memballoon>
</devices>
</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,如下图
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
指定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 完全公平队列