zoukankan      html  css  js  c++  java
  • [zz]通过Libvirt控制KVM虚拟机资源的方法

    转载自: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>

  • 相关阅读:
    webserver的使用
    vs2010旗舰版中.net framework 4安装不成功的解决办法
    测量距离
    Mybatis初学习
    Mybatis的一些优化
    开博想法
    matplotlib 画图中的basemap安装问题
    Ubuntu 提示sudo: java: command not found解决办法
    django 中静态文件项目加载问题
    Velocity组装Xml报文
  • 原文地址:https://www.cnblogs.com/zhangzhang/p/2852248.html
Copyright © 2011-2022 走看看