docker的资源限制cpuset cpuquota memory

时间:2023-03-08 22:24:56

总结

  1. 目前,公司7u已经不再使用lxc,转而使用libcontainer 即native
  2. docker对cpuquota的支持目前是有问题的,一般大家使用docker的时候,主要是对memory,cpuset的限制,很少对 cpuquota去限制;
  3. cpuquota的限制的用法是这样的: #docker run --cpuset-cpus="0-3" --cpu-quota=400000 --cpu-period=100000 -m 100M -d --net=host reg.docker.alibaba-inc.com/hippo/hippo_alios7u2_base /sbin/init,这里总是要 --cpu-quota=--cpu-period=一起使用,这个意思是,--cpu-period=100000表示 cpu总共时间片有100ms,--cpu-quota=40000 表示 这个容器只能使用40ms,但是 这里--cpu-quota=400000 --cpu-period=100000 --cpu-quota的值 居然比 --cpu-period的值大的原因是, 这个是4核的。
  4. 一般的应用,我们可以使用systemd的方式,使用systemd的命令,调用systemd的dbus接口去写这个值。systemd是不会覆盖的。

1. 新生container A,使用docker参数中的cpuset cpuquota memory

[root@rs1l04637.et2sqa /home/ahao.mah]
#docker run --cpuset-cpus="0-3" --cpu-quota=400000 --cpu-period=100000 -m 100M -d --net=host reg.docker.alibaba-inc.com/hippo/hippo_alios7u2_base /sbin/init
9ba7a3ec25e4244a706f806d80799d73d4eb48e6938b35477df544250d334578

2. container A的 -m 100M 写入了systemd的配置中,但是 cpuquota的值没有写入

[root@rs1l04637.et2sqa /home/ahao.mah]
#cat /sys/fs/cgroup/cpu/system.slice/docker-9ba7a3ec25e4244a706f806d80799d73d4eb48e6938b35477df544250d334578.scope/cpuset.*
0
0-3
0
0
0
[root@rs1l04637.et2sqa /home/ahao.mah]
#systemctl cat docker-9ba7a3ec25e4244a706f806d80799d73d4eb48e6938b35477df544250d334578.scope
# /run/systemd/system/docker-9ba7a3ec25e4244a706f806d80799d73d4eb48e6938b35477df544250d334578.scope
# Transient stub

# /run/systemd/system/docker-9ba7a3ec25e4244a706f806d80799d73d4eb48e6938b35477df544250d334578.scope.d/50-BlockIOAccounting.conf
[Scope]
BlockIOAccounting=yes
# /run/systemd/system/docker-9ba7a3ec25e4244a706f806d80799d73d4eb48e6938b35477df544250d334578.scope.d/50-CPUAccounting.conf
[Scope]
CPUAccounting=yes
# /run/systemd/system/docker-9ba7a3ec25e4244a706f806d80799d73d4eb48e6938b35477df544250d334578.scope.d/50-DefaultDependencies.conf
[Unit]
DefaultDependencies=no
# /run/systemd/system/docker-9ba7a3ec25e4244a706f806d80799d73d4eb48e6938b35477df544250d334578.scope.d/50-Delegate.conf
[Scope]
Delegate=yes
# /run/systemd/system/docker-9ba7a3ec25e4244a706f806d80799d73d4eb48e6938b35477df544250d334578.scope.d/50-Description.conf
[Unit]
Description=docker container 9ba7a3ec25e4244a706f806d80799d73d4eb48e6938b35477df544250d334578
# /run/systemd/system/docker-9ba7a3ec25e4244a706f806d80799d73d4eb48e6938b35477df544250d334578.scope.d/50-MemoryAccounting.conf
[Scope]
MemoryAccounting=yes
# /run/systemd/system/docker-9ba7a3ec25e4244a706f806d80799d73d4eb48e6938b35477df544250d334578.scope.d/50-MemoryLimit.conf
[Scope]
MemoryLimit=104857600
# /run/systemd/system/docker-9ba7a3ec25e4244a706f806d80799d73d4eb48e6938b35477df544250d334578.scope.d/50-Slice.conf
[Scope]
Slice=system.slice
[root@rs1l04637.et2sqa /home/ahao.mah]
#cat /sys/fs/cgroup/cpu/system.slice/docker-9ba7a3ec25e4244a706f806d80799d73d4eb48e6938b35477df544250d334578.scope/cpu.cfs_quota_us
400000

3. 新生一个container B

[root@rs1l04637.et2sqa /home/ahao.mah]
#systemctl daemon-reload

[root@rs1l04637.et2sqa /home/ahao.mah]
#cat /sys/fs/cgroup/cpu/system.slice/docker-9ba7a3ec25e4244a706f806d80799d73d4eb48e6938b35477df544250d334578.scope/cpu.cfs_quota_us
400000

[root@rs1l04637.et2sqa /home/ahao.mah]
#docker run --cpuset-cpus="0-3" --cpu-quota=400000 --cpu-period=100000 -m 100M -d --net=host reg.docker.alibaba-inc.com/hippo/hippo_alios7u2_base /sbin/init
7cdfbed4b0719d5d584367b781a2981e6bff3f81ea0f8705e41995e15be27b76

4. 你会发现container A的cpuquota的值被reset,但是,cpuset memory的值都没有被reset ,分析一下像是 docker的--cpu-quota=400000 --cpu-period=100000 这两个参数没有将值正确的传给systemd的接口,使得持久化

5. 你可以使用 systemctl set-property testSpeed CPUQuota=10 的方式(这是systemd的方式)去修改产生的container的值,发现,经过相同的步骤是没有问题的;所以,经过分析后觉得,更像是docker没有处理好的一个bug

container A 的 cpu quota被reset了
[root@rs1l04637.et2sqa /home/ahao.mah]
#cat /sys/fs/cgroup/cpu/system.slice/docker-9ba7a3ec25e4244a706f806d80799d73d4eb48e6938b35477df544250d334578.scope/cpu.cfs_quota_us
-1

container A 的 cpu set没有reset
[root@rs1l04637.et2sqa /home/ahao.mah]
#cat /sys/fs/cgroup/cpu/system.slice/docker-9ba7a3ec25e4244a706f806d80799d73d4eb48e6938b35477df544250d334578.scope/cpuset.*
0
0-3
0
0

container A 的 memory没有reset
[root@rs1l04637.et2sqa /home/ahao.mah]
#cat /sys/fs/cgroup/memory/system.slice/docker-9ba7a3ec25e4244a706f806d80799d73d4eb48e6938b35477df544250d334578.scope/memory.limit_in_bytes
104857600

6.另外测试了,alidocker-1.12.3.1-976480.alios7.x86_64,alidocker-1.9.1.17-915487.alios7.x86_64都是一样的现象,另外,发现问题的docker都是使用native,lxc好像没有问题