docker 8 : 容器资源限制管理

docker对容器系统资源的限制,实际使用的是系统发cgroup功能。

启动一个nginx容器,可以使用如下命令查看镜像的资源利用率。

[root@localhost ~]# docker stats  85e199af8f02 
CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT   MEM %               NET I/O             BLOCK I/O           PIDS
85e199af8f02        angry_williams      0.00%               1.43MiB / 919MiB    0.16%               1.28kB / 1.27kB   

一. CPU利用率控制

  1. 容器可以占用CPU核数编号,从0开始编号,例如:0-3表示4核,在容器运行的时候可以对CPU的使用进行限制。

[root@localhost ~]# docker run --name nginx-cputest --cpuset-cpus=0 -d -P nginx

  2. 已运行的容器的CPU控制,若某些容器已经运行,不方便停止重启的话,可以在线控制。

    a.首先查找当前运行容器的ID,这样可以找到对应CPU配置文件的位置。

      

[root@localhost ~]# docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                   NAMES
85e199af8f02        nginx               "/docker-entrypoint.…"   About an hour ago   Up About an hour    0.0.0.0:32768->80/tcp   angry_williams
[root@localhost ~]# docker inspect 85e199af8f02 
[
    {
        "Id": "85e199af8f02134e7d4f7a72d6f28d8c54222d7139142472fe552f752c068945",

  

[root@localhost ~]# cd /sys/fs/cgroup/cpu/docker/85e199af8f02134e7d4f7a72d6f28d8c54222d7139142472fe552f752c068945/
查看对CPU的限制。
[root@localhost 85e199af8f02134e7d4f7a72d6f28d8c54222d7139142472fe552f752c068945]# cat cpu.cfs_quota_us 
-1
这里-1表示没有限制。
[root@localhost 85e199af8f02134e7d4f7a72d6f28d8c54222d7139142472fe552f752c068945]# ll
total 0
-rw-r--r--. 1 root root 0 Apr 18 06:47 cgroup.clone_children
--w--w--w-. 1 root root 0 Apr 18 06:47 cgroup.event_control
-rw-r--r--. 1 root root 0 Apr 18 06:47 cgroup.procs
-r--r--r--. 1 root root 0 Apr 18 06:47 cpuacct.stat
-rw-r--r--. 1 root root 0 Apr 18 06:47 cpuacct.usage
-r--r--r--. 1 root root 0 Apr 18 06:47 cpuacct.usage_percpu
-rw-r--r--. 1 root root 0 Apr 18 06:47 cpu.cfs_period_us
-rw-r--r--. 1 root root 0 Apr 18 06:47 cpu.cfs_quota_us
-rw-r--r--. 1 root root 0 Apr 18 06:47 cpu.rt_period_us
-rw-r--r--. 1 root root 0 Apr 18 06:47 cpu.rt_runtime_us
-rw-r--r--. 1 root root 0 Apr 18 06:47 cpu.shares
-r--r--r--. 1 root root 0 Apr 18 06:47 cpu.stat
-rw-r--r--. 1 root root 0 Apr 18 06:47 notify_on_release
-rw-r--r--. 1 root root 0 Apr 18 06:47 tasks

    b. 可以通过修改参数,来实现对CPU的限制。

      #说明
      cpu.cfs_quota_us 的内容为 -1,表示默认情况下并没有限制容器的 CPU 使用.
      cpu.cfs_period_us 取值范围1000~1000000:1ms ~ 1s,cfs_quota_us的最小值为1000

      #其他限制参数
      #限制参数
      -cpu-period 指定容器对CPU的重新分配时间。
      -cpu-quota 在指定的周期内,最多有多少时间用运行容器。

      #单位微秒(μs)
      cfs_quota_us/cfs_period_us 表示容器实际可用的最多的CPU核数

      1.限制只能使用1个CPU(每250ms能使用250ms的CPU时间)
      # echo 250000 > cpu.cfs_quota_us /* quota = 250ms */
      # echo 250000 > cpu.cfs_period_us /* period = 250ms */

      2.限制使用2个CPU(内核)(每500ms能使用1000ms的CPU时间,即使用两个内核)
      # echo 1000000 > cpu.cfs_quota_us /* quota = 1000ms */
      # echo 500000 > cpu.cfs_period_us /* period = 500ms */

      3.限制使用1个CPU的20%(每50ms能使用10ms的CPU时间,即使用一个CPU核心的20%)
      # echo 10000 > cpu.cfs_quota_us /* quota = 10ms */
      # echo 50000 > cpu.cfs_period_us /* period = 50ms */

 

二、内存利用率的限制

  1. 同样在容器运行启动的时候,可以通过参数限制。运行一个容器,CPU设置为1,内存设置为300Mb。

[root@localhost /]# docker run --name nginx-memoytest -d --cpuset-cpus=0 -m 300M -P nginx
08f296dfd2c92864b41a4ba59fe72eaf2d403ffe4cdd478c00591c14ff2659b4

  2.针对已经运行的容器,内存限制也可以在cgroup文件上直接限制。

[root@localhost /]# cd /sys/fs/cgroup/memory/docker/08f296dfd2c92864b41a4ba59fe72eaf2d403ffe4cdd478c00591c14ff2659b4
[root@localhost 08f296dfd2c92864b41a4ba59fe72eaf2d403ffe4cdd478c00591c14ff2659b4]# cat memory.limit_in_bytes 
314572800

  可以看到当前内存限制为300M,可以直接修改值来调整容器的内存使用量。

[root@localhost /]# docker stats 08f296dfd2c9
CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT   MEM %               NET I/O             BLOCK I/O           PIDS
08f296dfd2c9        nginx-memoytest     0.00%               1.406MiB / 300MiB   0.47%               648B / 0B           20.5kB / 12.3kB     2

  

 

三、磁盘空间利用率控制

  1.同样在容器运行启动的时候,可以通过参数限制,如下:

[root@localhost ~]# docker run -it --storage-opt size=12m alpine:latest
docker: Error response from daemon: --storage-opt is supported only for overlay over xfs with 'pquota' mount option.
See 'docker run --help'.

  如上限制的时候出现报错,说明磁盘没有是能配额管理,需要先是能才行。

  2. 这里我们重新挂载一块5G的硬盘,来完成磁盘管理。

    a. 格式化磁盘

[root@localhost data]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xd29bccbe.

The device presents a logical sector size that is smaller than
the physical sector size. Aligning to a physical sector (or optimal
I/O) size boundary is recommended, or performance may be impacted.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 
First sector (2048-10485759, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-10485759, default 10485759): 
Using default value 10485759
Partition 1 of type Linux and of size 5 GiB is set

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

    b. 格式化磁盘

[root@localhost data]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1              isize=512    agcount=4, agsize=327616 blks
         =                       sectsz=4096  attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=1310464, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=4096  sunit=1 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

    c. 挂载磁盘

[root@localhost data]# ll /dev/disk/by-uuid/*
lrwxrwxrwx. 1 root root 10 Apr 18 05:35 /dev/disk/by-uuid/5f40a6a2-9d44-4346-a096-8914a822f52a -> ../../sdb1

[root@localhost data]#mkdir /data

[root@localhost data]# vim /etc/fstab 
UUID=5f40a6a2-9d44-4346-a096-8914a822f52a /data xfs rw,pquota 0 0
~
"/etc/fstab" 13L, 607C written                                                                                                                                                                                             
 
[root@localhost data]# mount -a
[root@localhost data]# cat /proc/mounts | grep sdb
/dev/sdb1 /data xfs rw,seclabel,relatime,attr2,inode64,prjquota 0 0

    d. 修改守护进程配置文件

[root@localhost docker]# vi daemon.json
{
  "registry-mirrors": ["https://plqjafsr.mirror.aliyuncs.com"],
   "data-root": "/data/docker",
    "storage-driver": "overlay2",
      "overlay2.size=1G"    #要求容器大小不要超过1G
    ]
}
~

    e. 验证

[root@localhost docker]# docker run -it centos /bin/bash
Unable to find image 'centos:latest' locally
latest: Pulling from library/centos

Digest: sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
Status: Downloaded newer image for centos:latest
[root@a9d2544aa25b /]# dd if=/dev/zero of=/test.txt bs=1100M count=1   

dd: error writing '/test.txt': No space left on device

  

 

posted on 2021-04-18 20:43  torotoise512  阅读(513)  评论(0编辑  收藏  举报