docker如何进行资源控制

CPU 资源控制

cgroups,是一个非常强大的linux内核工具,他不仅可以限制被 namespace 隔离起来的资源, 还可以为资源设置权重、计算使用量、操控进程启停等等。 所以 cgroups( Control groups) 实现了对资源的配额和度量。

cgroups四大功能

功能说明
资源限制可以对任务使用的资源总额进行限制
优先级分配通过分配的cpu时间片数量以及磁盘IO带宽大小,实际上相当于控制了任务运行优先级
资源统计可以统计系统的资源使用量,如cpu时长,内存用量等
任务控制cgroup可以对任务执行挂起、恢复等操作

设置CPU使用率上限

Linux 通过 CFS(Completely Fair Scheduler,完全公平调度器)来调度各个进程对 CPU 的使用。CFS 默认的调度周期是 100ms。
我们可以设置每个容器进程的调度周期,以及在这个周期内各个容器最多能使用多少 CPU 时间。

使用 --cpu-period 即可设置调度周期,使用 --cpu-quota 即可设置在每个周期内容器能使用的 CPU 时间。两者可以配合使用。
CFS 周期的有效范围是 1ms~1s,对应的 --cpu-period 的数值范围是 1000~100000。
而容器的 CPU 配额必须不小于 1ms,即 --cpu-quota 的值必须 >= 1000。
docker run -itd --name saber centos:7 /bin/bash

docker ps -a
CONTAINER ID   		IMAGE     	COMMAND       CREATED      	STATUS       PORTS     NAMES
477a35a7ef4c  	 	centos:7  	"/bin/bash"  7 seconds ago      Up 6 seconds          saber

cd /sys/fs/cgroup/cpu/docker/477a35a7ef4c8bb5a3dd867232b06d76079f6c52cfa1120292d30e2eb4b73286/
cat cpu.cfs_quota_us 
-1
cat cpu.cfs_period_us 
100000

在这里插入图片描述

#cpu.cfs_period_us:cpu分配的周期(微秒,所以文件名中用 us 表示),默认为100000。
#cpu.cfs_quota_us:表示该control group限制占用的时间(微秒),默认为-1,表示不限制。 如果设为50000,表示占用50000/100000=50%的CPU。

进行CPU压力测试

docker exec -it 477a35a7ef4c /bin/bash

vi /cpu.sh
#!/bin/bash
i=0
while true
do
let i++
done

chmod +x /cpu.sh
./cpu.sh
exit
top     #可以看到这个脚本占了很多的cpu资源

在这里插入图片描述
在这里插入图片描述

设置50%的比例分配CPU使用时间上限

yum -y install epel-release
yum -y install stress

docker run -itd --name wu1 --cpu-quota 50000 centos:7 /bin/bash #可以重新创建一个容器并设置限额
或者
cd /sys/fs/cgroup/cpu/docker/3ed82355f81151c4568aaa6e7bc60ba6984201c119125360924bf7dfd6eaa42b/
echo 50000 > cpu.cfs_quota_us

docker exec -it 0be3b04ff49e /bin/bash
./cpu.sh
exit
top     #可以看到cpu占用率接近50%,cgroups对cpu的控制起了效果

在这里插入图片描述

设置CPU资源占用比(设置多个容器时才有效)

Docker 通过--cpu-shares 指定 CPU 份额,默认值为1024,值为1024的倍数。
#创建两个容器为 c1 和 c2,若只有这两个容器,设置容器的权重,使得c1和c2的CPU资源占比为1/32/3。

docker run -itd --name sun1 --cpu-shares 512 centos:7 
docker run -itd --name sun2 --cpu-shares 1024 centos:7

在这里插入图片描述

分别进入容器,进行压力测试

yum install -y epel-release
yum install stress -y

docker exec -it 77042a766bc9 bash
docker exec -it a3d65d227564 bash

stress -c 4    #产生四个进程,每个进程都反复不停的计算随机数的平方根
exit

top

在这里插入图片描述

查看容器运行状态(动态更新)

docker stats
CONTAINER ID   NAME      CPU %     MEM USAGE / LIMIT     MEM %     NET I/O          BLOCK I/O         PIDS
a3d65d227564   sun2      0.00%     141.6MiB / 3.844GiB   3.60%     27.5MB / 324kB   12.1MB / 50.5MB   2
77042a766bc9   sun1      0.00%     125.6MiB / 3.844GiB   3.19%     26.2MB / 430kB   16MB / 50.5MB     2
0be3b04ff49e   wu1       0.00%     908KiB / 3.844GiB     0.02%     732B / 0B        1.03MB / 14.3kB   1
477a35a7ef4c   saber     0.00%     98.32MiB / 3.844GiB   2.50%     26.3MB / 451kB   18.8MB / 50.5MB   1

在这里插入图片描述

设置容器绑定指定的CPU

先分配虚拟机4个CPU核数
在这里插入图片描述

docker run -itd --name archer --cpuset-cpus 1,3 centos:7 /bin/bash
#进入容器,进行压力测试
yum install -y epel-release
yum install stress -y
stress -c 4
exit
#退出容器,执行 top 命令再按 1 查看CPU使用情况。

在这里插入图片描述

绑定指定的CPU错误简析

分配核数时将会报错,这是需要重启虚拟机: init 6

Error response from daemon: 0CI runtime create failed: container_ linux.go:367: starting container process caused

process_ linux . g0:340: applying cgroup configuration for process caused: failed to Write "1,3": write /sys/fs/cgroup/ cpuset/docker/e4d199f ad942ed91f2b10f5df4c3bc5978d0bb9eeda8aac05c4304c29e280396/cpuset .cpus:permission denied: unknown
Error:failed to start containers:archer

这是docker将不能使用yum命令进行安装

Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=container error was
14: curl#6 - "Could not resolve host: mirrorlist.centos.org; Unknown error"

One of the configured repositories failed (Unknown),
 and yum doesn't have enough cached data to continue. At this point the only
 safe thing yum can do is fail. There are a few ways to work "fix" this:
 1. Contact the upstream for the repository and get them to fix the problem.
 
2. Reconfigure the baseurl/etc. for the repository, to point to a working

upstream. This is most often useful if you are using a newer
        distribution release than is supported by the repository (and the
        packages for the previous distribution release still work).

3. Run the command with the repository temporarily disabled
            yum --disablerepo=<repoid> ...
  
4. Disable the repository permanently, so yum won't use it by default. Yum
        will then just ignore the repository until you permanently enable it
        again or use --enablerepo for temporary usage:
    yum-config-manager --disable <repoid>
        or
            subscription-manager repos --disable=<repoid>

 5. Configure the failing repository to be skipped, if it is unavailable.
        Note that yum will try to contact the repo. when it runs most commands,
        so will have to try and fail each time (and thus. yum will be be much
        slower). If it is a very temporary problem though, this is often a nice
        compromise:
 yum-config-manager --save --setopt=<repoid>.skip_if_unavailable=true
 
#Cannot find a valid baseurl for repo: base/7/x86_64               

这时我们需要启动docker容器

systemctl restart docker

对内存使用的限制

docker run -itd --name sun1 -m 512m centos:7 bash
docker stats

在这里插入图片描述

对磁盘IO配额控制(blkio)的限制

--device-read-bps:限制某个设备上的读速度bps(数据量),单位可以是kb、mb(M)或者gb。

例:docker run -itd --name sun2 --device-read-bps /dev/sda:1M  centos:7 /bin/bash
--device-write-bps : 限制某个设备上的写速度bps(数据量),单位可以是kb、mb(M)或者gb。

例:docker run -itd --name sun3 --device-write-bps /dev/sda:1mb centos:7 /bin/bash
--device-read-iops :限制读某个设备的iops(次数)
--device-write-iops :限制写入某个设备的iops(次数)

创建容器,并限制写速度

docker run -it --name sun2 --device-write-bps /dev/sda:1mb centos:7 /bin/bash

通过dd来验证写速度

dd if=/dev/zero of=test.out bs=1M count=10 oflag=direct    #添加oflag参数以规避掉文件系统cache

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值