一、什么是Docker资源限制?

默认情况下,Docker容器是没有资源限制的,它会尽可能地使用宿主机能够分配给它的资源。 如果不对容器资源进行限制,容器之间就会相互影响,一些占用硬件资源较高的容器会吞噬掉所有的硬件资源,从而导致其它容器无硬件资源可用,发生停服状态。其中许多功能需要您的内核支持 Linux 功能。要检查支持,您可以使用该 docker info命令。如果您的
内核中禁用了某个功能,您可能会在输出的末尾看到如下警告:
WARNING: no swap limit support

二、内存限制

stress:模拟系统资源压力的工具

选项参数:
-m:设置容器可用内存大小, 支持单位有 b, k, m 和 g.
--memory-swap:设置内存+swap的总大小, 配合 -m 一起使用, 该选项不指定的话默认是 -m 的 2 倍大小.
--oom-kill-disable:不允许容器因为 OOM 被系统杀掉, 建议配合 -m 选项使用, 避免宿主内存被耗光.

## 压测工具拉取
[root@harbor ~]# docker pull lorel/docker-stress-ng
## 没有限制内存
[root@harbor ~]# docker run --name mem_test -it lorel/docker-stress-ng --vm 2
## 查看状态
docker stats

image-20230915174218337

没有限制内存的情况下,发现光启动一个容器就使用972MB内存,

## 限制了内存  //限制内存300MB
[root@harbor ~]# docker run --name mem_test --rm -m 300m -it lorel/docker-stress-ng --vm 2
## 查看状态
[root@harbor ~]#  docker stats

image-20230915173952845

使用了内存限制,可以明显的看出内存数值始终不会超过300MB

三、CPU限制

尽量使用多核多G,效果比较明显

选项参数:
-c | --cpu-share:CPU 权重设置, Docker 会把 CPU 资源分成 1024 份, 如果对一个容器设置了 1024 意味它独占所有 CPU 资源, 如果多个容器同时进行了设置, 那么每个容器最后会通过各自占有的百分比来分配 CPU 资源.
--cpus:限制CPU核数. CPU 个数, 并且还可以指定如 1.5 之类的小数.
--cpuset-cpus:设置CPU亲和, 让容器绑定在指定的CPU上.

#没有限制
[root@harbor ~]# docker run --name cpu_test --rm -it lorel/docker-stress-ng --cpu 2
#状态查看
[root@harbor ~]# docker stats

image-20230915175811359

在没有限制的情况下,CPU使用率高达200%

[root@harbor ~]# docker run --name cpu_test --cpus 1 --rm -it lorel/docker-stress-ng --cpu 2
#状态查看
[root@harbor ~]# docker stats

image-20230915180017183

在使用了限制之后,即使压测测试2个CPU,但是CPU使用率保持在100~110左右

注意:我们资源限制不是目的,目的是我们要随时监控到我们的资源,能看到容器对宿主机资源的使用,才能更
好的做限制,不要盲目限制,不知道资源使用的情况下,就对容器资源限制,就是在作死。

四、硬盘 IO限制

选项参数 :
–device-read-bps:限制此设备上的读速度(bytes per second),单位可以是kb、mb或者gb。
–device-read-iops:通过每秒读IO次数来限制指定设备的读速度。
–device-write-bps: 限制此设备上的写速度(bytes per second),单位可以是kb、mb或者gb。
–device-write-iops:通过每秒写IO次数来限制指定设备的写速度。
–blkio-weight:容器默认磁盘IO的加权值,有效值范围为10-100。
–blkio-weight-device: 针对特定设备的IO加权控制。其格式为DEVICE_NAME:WEIGHT

# 使用下面的命令创建容器,并执行命令验证写速度的限制。限制最高写速度为 30M/S 
docker run -itd --name disk1 --device-write-bps /dev/sda:30mb centos:latest
#进入容器,通过dd来验证写速度,输出如下图示
[root@docker02 ~]# docker exec -it disk1  /bin/bash
[root@843b503dca54 /]# dd if=/dev/zero of=test.out bs=30M count=100 oflag=direct

image-20230915182415611

可以看到容器的写磁盘速度被成功地限制到了30MB/s。device-read-bps等其他磁盘IO限制参数可以使用类似的方式进行验证

容器空间大小限制

在docker使用devicemapper作为存储驱动时,默认每个容器和镜像的最大大小为10G。如果需要调整,可以在daemon启动参数中,使用dm.basesize来指定,但需要注意的是,修改这个值,不仅仅需要重启docker daemon服务,还会导致宿主机上的所有本地镜像和容器都被清理掉。

使用aufs或者overlay等其他存储驱动时,没有这个限制。

五、指定数据磁盘挂载

#磁盘分区
[root@web01 ~]# 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 0x86c3d215.

Command (m for help): 1
1: unknown command
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partitions system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

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): 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.
#查看分区结果
[root@web01 ~]# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0006026e

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM

Disk /dev/sdb: 5368 MB, 5368709120 bytes, 10485760 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x86c3d215

   Device Boot      Start         End      Blocks   Id  System      #新增磁盘分区
/dev/sdb1            2048    10485759     5241856   83  Linux

Disk /dev/mapper/centos-root: 16.1 GB, 16106127360 bytes, 31457280 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/mapper/centos-swap: 4290 MB, 4290772992 bytes, 8380416 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
#磁盘格式化
[root@web01 ~]# mkfs.xfs /dev/sdb1 
meta-data=/dev/sdb1              isize=512    agcount=4, agsize=327616 blks
         =                       sectsz=512   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=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

[root@web01 ~]# mount  /dev/sdb1  docker/
[root@web01 ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   15G  2.6G   13G  17% /
devtmpfs                 979M     0  979M   0% /dev
tmpfs                    991M     0  991M   0% /dev/shm
tmpfs                    991M  9.5M  981M   1% /run
tmpfs                    991M     0  991M   0% /sys/fs/cgroup
/dev/sda1               1014M  133M  882M  14% /boot
tmpfs                    199M     0  199M   0% /run/user/0
/dev/sdb1                5.0G   33M  5.0G   1% /root/docker

#修改systemd启动配置文件   // -g指定需挂载的数据目录
[root@web01 ~]# vim /usr/lib/systemd/system/docker.service
---
ExecStart=/usr/bin/dockerd -H fd:// -g /root/docker
---
[root@web01 ~]# systemctl daemon-reload 
[root@web01 ~]# systemctl restart docker
[root@web01 ~]# ll docker/
total 0
drwx------ 2 root root  6 Oct 19 20:12 containers
drwx------ 3 root root 22 Oct 19 20:12 image
drwxr-x--- 3 root root 19 Oct 19 20:12 network
drwx------ 3 root root 40 Oct 19 20:12 overlay2
drwx------ 4 root root 32 Oct 19 20:12 plugins
drwx------ 2 root root  6 Oct 19 20:12 swarm
drwx------ 2 root root  6 Oct 19 20:12 tmp
drwx------ 2 root root  6 Oct 19 20:12 trust
drwx------ 2 root root 25 Oct 19 20:12 volumes