管理KVM虚拟机

工具:libvirt

官网:http://libvirt.org/

介绍: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  完全公平队列

管理KVM虚拟机(二)的更多相关文章

  1. Linux 中使用 virsh 管理 KVM 虚拟机 (转)

    术语 虚拟化指的是:在相同的物理(硬件)系统上,同时运行多个操作系统,且这几个系统相互隔离的可能性,而那个硬件在虚拟化架构中被称作宿主机(host).虚拟机监视器(也被称为虚拟机管理程序(hyperv ...

  2. 使用Wok管理kvm虚拟机

    [Centos7.4] !!!测试环境我们首关闭防火墙和selinux [root@localhost ~]# systemctl stop firewalld [root@localhost ~]# ...

  3. 使用 libvirt创建和管理KVM虚拟机

      1. libvirt介绍    Libvirt是一个软件集合,便于使用者管理虚拟机和其他虚拟化功能,比如存储和网络接口管理等等.Libvirt概括起来包括一个API库.一个 daemon(libv ...

  4. 使用cockpit管理kvm虚拟机

    在Centos8.2主机上部署kvm,使用cockpit管理 首先检测cpu是否支持虚拟化 [root@localhost ~]# cat /proc/cpuinfo | grep vmx flags ...

  5. 通过python-libvirt管理KVM虚拟机 代码实现

    初步代码 <span style="font-size:18px;">''''' Work with virtual machines managed by libvi ...

  6. 安装libvirt管理套件(C/S架构模式,用户管理kvm虚拟机)

    # 1:安装程序包 yum install -y libvirt virt-manager virt-viewer virt-install qemu-kvm   # 2:启动libvirtd守护进程 ...

  7. 通过python-libvirt管理KVM虚拟机 源码

    版本:0.9.13 libvirt库可真是大,先看看该版本里面都有哪些类和方法,验证过的方法我会用O开头,|开头的标示还没亲自验证过. <span style="font-size:1 ...

  8. Kvm04 kvm虚拟机热迁移,可视化管理

    目录 1.kvm虚拟机热迁移 1). 冷迁移 修改配置文件,将定义的格式raw,改成qcow2格式! 修改完成,重启服务 2).热迁移 2.Virt-manager管理Kvm虚拟机 1.kvm虚拟机热 ...

  9. kvm虚拟机管理(3)

    一.远程管理kvm虚拟机 (1)上一节我们通过 virt-manager 在本地主机上创建并管理 KVM 虚机.其实 virt-manager 也可以管理其他宿主机上的虚机.只需要简单的将宿主机添加进 ...

随机推荐

  1. Redis简介、安装、配置、启用学习笔记

    前一篇文章有介绍关系型数据库和非关系型数据库的差异,现在就来学习一下用的较广的非关系型数据库:Redis数据库 Redis 简介 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-v ...

  2. 让docker中的mysql启动时自动执行sql文件

    本文提要 本文目的不仅仅是创建一个MySQL的镜像,而是在其基础上再实现启动过程中自动导入数据及数据库用户的权限设置,并且在新创建出来的容器里自动启动MySQL服务接受外部连接,主要是通过Docker ...

  3. 501. Find Mode in Binary Search Tree【LeetCode by java】

    Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred ...

  4. jupyter notebook 更改工作环境和浏览器

    转载自:https://blog.csdn.net/u011141114/article/details/78556227 1 修改默认目录 最近刚刚开始学习Python,比较好的一个IDE就是jup ...

  5. Python包下载超时问题解决

    pip下载模块慢解决办法 由于pip安装默认的访问地址为 http://pypi.python.org/simple/经常会有网络不稳定和速度慢的现象,出现timeout报错,因此可以改为访问国内的地 ...

  6. 假设检验,alpha,p值 通俗易懂的的理解。

    假设检验: 一般原假设H0 :表是为 XXX和YYYY无显著差异,H1,是有显著差异. 如果我们定义alpha的值是0.05.意味着我们接受H0是真的但是我们却认为他是假的的概率. 这里你想想,这个值 ...

  7. PAT甲题题解-1033. To Fill or Not to Fill (25)-模拟

    模拟先说一下例子,最后为方便起见,在目的地安增加一个费用为0的加油站0 1 2 3 4 5 6 7 87.1 7.0 7.2 6.85 7.5 7.0 7.3 6.0 00 150 200 300 4 ...

  8. python数据分析Titanic_Survived预测

    import pandas as pd import matplotlib.pyplot as plt # matplotlib画图注释中文需要设置from matplotlib.font_manag ...

  9. week7:个人博客作业

    这周有2个需要注意的地方. 1,课上 这周是由张翼飞老师讲的课,上周已经听过老师讲的一节课了,这周发现,这个老师讲课是理论+案例,我每节课都在第一排坐的,我不知道其他人是什么想法,我是一上课,老师讲理 ...

  10. java 封装,继承,多态基础

    什么是封装? 1,对象数据和在.操作该对象的指令都是对象自身的一部分,能够实现尽可能对外部隐藏数据. 2,实际项目开发中,使用封装最多的就是实体类. 什么是继承? 1,继承是面向对象程序设计能提高效率 ...