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

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

 

测试环境:

CentOS 6.2

 

 

操作步骤

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 文档为

700

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

参数如下:

--total-bytes-sec total throughput limit in bytes per second

--read-bytes-sec read throughput limit in bytes per second

--write-bytes-sec write throughput limit in bytes per second

--total-iops-sec total I/O operations limit per second

--read-iops-sec read I/O operations limit per second

--write-iops-sec 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文档格式为

10000000

400000

100000

注意:实际在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文档格式为

单位kilobytes

命令行命令为:

virsh # help attach-interface

NAME

attach-interface - attach network interface

 

SYNOPSIS

attach-interface [] [] [