设为首页 收藏本站
查看: 2451|回复: 0

[经验分享] kvm限制虚拟机资源

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-5-28 08:17:40 | 显示全部楼层 |阅读模式
我们为什么需要资源控制?
在实际的生产环境中,希望每台虚拟机对资源的消耗是可控的,避免过度的消耗资源,造成对其他虚拟机的影响。

测试环境:
CentOS 6.2


操作步骤
1. 升级系统到最新
yum update -y
2. 安装epel源
rpm -Uvh http://mirrors.sohu.com/fedora-e ... ease-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时间周期。 www.it165.net
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>


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-71299-1-1.html 上篇帖子: qemu-kvm安装以及简单使用 下篇帖子: kvm install 报错could not open disk imageXXX: Permission denied 虚拟机 资源
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表