[zz]通过Libvirt控制KVM虚拟机资源的方法

本文详细介绍了如何在CentOS 6.3环境下通过cgroup、libvir和qemu实现虚拟机资源(磁盘、网络、CPU、内存)的精细化控制,包括权重设置、带宽限制、权重分配和内存参数调整等。

转载自:http://xiaoli110.blog.51cto.com/1724/1070201

我们为什么需要资源控制?

在实际的生产环境中,希望每台虚拟机对资源的消耗是可控的,避免过度的消耗资源,造成对其他虚拟机的影响。

 

测试环境:

Centos 6.3 update到 2012年11月 25日。

 

 

操作步骤

1. 升级系统到最新

yum update -y

2. 安装epel

rpm -Uvh http://mirrors.sohu.com/fedora-epel/6/x86_64/epel-release-6-7.noarch.rpm

3. 安装cgroup

yum install libcgroup libgroup-devel -y

启动服务

/etc/init.d/cgconfig start

停止服务

/etc/init.d/cgconfig stop

查看状态

/etc/init.d/cgconfig status

服务启动后,就可以根目录下查看到cgroup目录

ls /cgroup/

blkio cpu cpuacct cpuset devices freezer memory net_cls

设置默认启动服务

chkconfig cgconfig on

4. 磁盘资源的控制

磁盘的io控制有两种方式:

1) 在整体中的权重,数值范围在100-1000。

2) 限制具体的io

设置磁盘的权重

设置虚拟机的权重为700,并立即生效。

virsh blkiotune 25 --weight 700 --live

xml 文档为

<blkiotune>

<weight>700</weight>

</blkiotune>

使用blkdeviotune可以设置虚拟机的读写速度和iops

参数如下:

--total-bytes-sec <number> total throughput limit in bytes per second

--read-bytes-sec <number> read throughput limit in bytes per second

--write-bytes-sec <number> write throughput limit in bytes per second

--total-iops-sec <number> total I/O operations limit per second

--read-iops-sec <number> read I/O operations limit per second

--write-iops-sec <number> write I/O operations limit per second

--config affect next boot

--live affect running domain

--current affect current domain

例子

virsh blkdeviotune 8 hda --total_bytes_sec 10240

--total-bytes-sec 10240 --read-bytes-sec 4096 --write-bytes-sec 2028 \

--total-iops-sec 30 --read-iops-sec 15 --write-iops-sec 15

 

Xml文档格式为

<iotune>

<total_bytes_sec>10000000</total_bytes_sec>

<read_iops_sec>400000</read_iops_sec>

<write_iops_sec>100000</write_iops_sec>

</iotune>

注意:实际在centos6.3的环境下,使用blkdeviotune,会出现虚拟机不能启动的情况,经过查询redhat官网,这个是libvirt和qemu之间配合的一个bug,在fedora17上做了同样的测试,有同样的问题。

virsh # start w2k3.64.e1000

error: Failed to start domain w2k3.64.e1000

error: unsupported configuration: block I/O throttling not supported with this QEMU binary

如果要使用这个功能,可能需要等待官方的修复。

5. 网络带宽的控制

Xml文档格式为

<devices>

<interface type='network'>

<source network='default'/>

<target dev='vnet0'/>

<link state='down'/>

</interface>

<bandwidth>

<inbound average='1000' peak='5000' burst='1024'/>

<outbound average='128' peak='256' burst='256'/>

</bandwidth>

单位kilobytes

命令行命令为:

virsh # help attach-interface

NAME

attach-interface - attach network interface

 

SYNOPSIS

attach-interface <domain> <type> <source> [<target>] [<mac>] [<script>] [<model>] [--persistent] [<inbound>] [<outbound>]

 

DESCRIPTION

Attach new network interface.

 

OPTIONS

[--domain] <string> domain name, id or uuid

[--type] <string> network interface type

[--source] <string> source of network interface

[--target] <string> target network name

[--mac] <string> MAC address

[--script] <string> script used to bridge network interface

[--model] <string> model type

--persistent persist interface attachment

[--inbound] <string> control domain's incoming traffics

[--outbound] <string> control domain's outgoing traffics

6. CPU资源的控制

Xml文档格式为

 

<domain>

...

<cputune>

<vcpupin vcpu="0" cpuset="1-4,^2"/>

<vcpupin vcpu="1" cpuset="0,1"/>

<vcpupin vcpu="2" cpuset="2,3"/>

<vcpupin vcpu="3" cpuset="0,4"/>

<emulatorpin cpuset="1-3"/>

<shares>2048</shares>

<period>1000000</period>

<quota>-1</quota>

<emulator_period>1000000</emulator_period>

<emulator_quota>-1</emulator_quota>

</cputune>

...

</domain>

主要参数说明:

shares cpu权重,没有固定的数值,和其他的虚拟机相比较,那个的数值大,那个可以使用的cpu资源就多,比如设置2048值得虚拟机,就比设置1024数值的虚拟机可以多使用2倍的cpu资源。

period vcpu强制间隔的时间周期,单位微妙,范围[1000, 1000000],每一个vcpu不能使用超过period时间周期。

quota vcpu最大允许带宽,单位微妙,范围[1000, 18446744073709551]

emulator_period 强制间隔的时间周期,单位微妙,范围[1000, 1000000],虚拟机进程(qemu)不能使用超过period时间周期。

emulator_quota 虚拟机进程(qemu)最大允许带宽,单位微妙,范围[1000, 18446744073709551]。

7. 内存资源的控制

Xml文档格式为

<memory unit='KiB'>2097152</memory>

<currentMemory unit='KiB'>2097152</currentMemory>

<memtune>

<hard_limit unit='KiB'>4194304</hard_limit>

<soft_limit unit='KiB'>8388608</soft_limit>

<min_guarantee unit='Kib'>1024000</min_guarantee>>

<swap_hard_limit unit='KiB'>4194304</swap_hard_limit>

</memtune>

 

命令行命令为

virsh # help memtune

NAME

memtune - Get or set memory parameters

 

SYNOPSIS

memtune <domain> [--hard-limit <number>] [--soft-limit <number>] [--swap-hard-limit <number>] [--min-guarantee <number>] [--config] [--live] [--current]

 

DESCRIPTION

Get or set the current memory parameters for a guest domain.

To get the memory parameters use following command:

 

virsh # memtune <domain>

 

OPTIONS

[--domain] <string> domain name, id or uuid

--hard-limit <number> 强制最大内存 (default KiB)

--soft-limit <number> 可用最大内存 (default KiB)

--swap-hard-limit <number> 强制最大swap使用大小(default KiB)

--min-guarantee <number> 最小保证的内存 (default KiB)

--config 下次重启生效

--live 在线生效

--current 只在当前生效

 

例子:

memtune 6 --hard-limit 4194304 --soft-limit 8388608 --swap-hard-limit 4194304 --config

 

 

8. 其他资源或者功能的控制

Libvirt支持的资源

virsh # nwfilter-list

UUID Name

----------------------------------------------------------------

52cc08e0-59e9-8abc-a47f-292de158e50e allow-arp

ba62872c-f502-b186-a1d5-d42eca52020a allow-dhcp

2aac52ff-622d-4204-ee20-76914ed37050 allow-dhcp-server

069307c6-4e9e-64aa-3ceb-de11fe18742a allow-incoming-ipv4

6d1c441f-fb72-ee3d-ee23-f36f00289169 allow-ipv4

1670e7ba-0f88-6a07-8396-55fb205e60dc clean-traffic

2ef4e0ad-0b93-ff6d-0bea-db1bc40cb48e no-arp-ip-spoofing

f0913202-6548-2f6c-bb57-e4bb769a3f34 no-arp-mac-spoofing

924af240-4684-a33c-109e-84ac19c34809 no-arp-spoofing

cff39883-7766-5b16-e74b-0d4df1df188c no-ip-multicast

13c6ba86-4d9b-ca81-80f5-f73136a5b36d no-ip-spoofing

188b5f06-0c92-b3d0-a7ab-31b06dd3611d no-mac-broadcast

df609586-8ec5-b7de-234f-9411bb545cc0 no-mac-spoofing

9808a971-ef37-d5f6-8bfc-344beb9aeb3a no-other-l2-traffic

667b6824-5c51-a00e-21d2-33a69eb231d2 no-other-rarp-traffic

d29435d2-c5a1-78c3-8d4a-a0f8fe70b8b2 qemu-announce-self

409c14b9-7cf1-7259-1ce0-879a92197a7a qemu-announce-self-rarp

 

Arp资源的一个例子

virsh # nwfilter-dumpxml allow-arp

<filter name='allow-arp' chain='arp' priority='-500'>

<uuid>52cc08e0-59e9-8abc-a47f-292de158e50e</uuid>

<rule action='accept' direction='inout' priority='500'/>

</filter>

 

可以virsh nwfilter-edit命令编辑,例如

virsh nwfilter-edit allow-dhcp

<filter name='allow-dhcp' chain='ipv4' priority='-700'>

<uuid>ba62872c-f502-b186-a1d5-d42eca52020a</uuid>

<rule action='accept' direction='out' priority='100'>

<ip srcipaddr='0.0.0.0' dstipaddr='255.255.255.255' protocol='udp' srcportstart='68' dstportstart='67'/>

</rule>

<rule action='accept' direction='in' priority='100'>

<ip protocol='udp' srcportstart='67' dstportstart='68'/>

</rule>

</filter>

转载于:https://www.cnblogs.com/zhangzhang/archive/2013/01/09/2852248.html

libvirt是一种用于管理虚拟化平台的开源工具,可以通过它来管理KVM虚拟机libvirt提供了一组命令行工具和API来管理虚拟机,可以使用它来抓取KVM虚拟机的监控指标数据。 以下是使用libvirt抓取KVM虚拟机监控指标数据的步骤: 1. 安装libvirt工具及相关依赖 ```shell sudo apt-get install libvirt-bin libvirt-dev qemu-kvm ``` 2. 打开libvirt的API libvirt提供了多种语言的API,如C、Python、Java等,这里以Python为例,打开libvirt的API: ```python import libvirt conn = libvirt.open('qemu:///system') ``` 3. 获取虚拟机对象 使用`conn.listDomainsID()`函数获取所有虚拟机的ID列表,然后使用`conn.lookupByID(domain_id)`函数获取指定虚拟机的对象。 ```python domain_id_list = conn.listDomainsID() if len(domain_id_list) == 0: print('No active domains') else: for domain_id in domain_id_list: domain = conn.lookupByID(domain_id) ``` 4. 获取监控指标数据 可以使用`domain.info()`函数获取虚拟机的基本信息,如CPU使用率、内存使用情况等,也可以使用`domain.blockStats()`和`domain.interfaceStats()`函数获取虚拟机的磁盘和网络IO数据。 ```python # 获取虚拟机CPU使用率和内存使用情况 cpu_stats = domain.info()[4] mem_stats = domain.memoryStats() # 获取虚拟机磁盘IO数据 disk_stats = {} for disk in domain.listDisks(): block_info = domain.blockStats(disk) disk_stats[disk] = {'read_bytes': block_info[1], 'write_bytes': block_info[3]} # 获取虚拟机网络IO数据 net_stats = {} for interface in domain.interfaceAddresses(libvirt.VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_AGENT, 0): stats = domain.interfaceStats(interface) net_stats[interface] = {'rx_bytes': stats[0], 'tx_bytes': stats[4]} ``` 5. 关闭libvirt连接 ```python conn.close() ``` 以上就是使用libvirt抓取KVM虚拟机监控指标数据的步骤,可以根据自己的需求来调整获取的监控指标数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值