[toc]

资源限制介绍

官网:TP

带有内存、CPU 和 GPU 的运行时选项

默认情况下,容器没有资源限制,可以使用主机内核调度程序允许的尽可能多的给定资源。Docker 提供了控制容器可以使用多少内存或 CPU 的方法,设置docker run命令的运行时配置标志。

何时应该设置此类限制以及设置这些限制可能出现哪些影响?

其中许多功能需要您的内核支持 Linux 功能。要检查支持,您可以使用该 docker info命令。
如果您的内核中禁用了某个功能,您可能会在输出的末尾看到如下警告:WARNING: no swap limit support

警告示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 检查功能是否完成
docker info
Client: Docker Engine - Community
Version: 24.0.2
Context: default
Debug Mode: false
Plugins:
buildx: Docker Buildx (Docker Inc.)
Version: v0.10.5
Path: /usr/libexec/docker/cli-plugins/docker-buildx
compose: Docker Compose (Docker Inc.)
Version: v2.18.1
Path: /usr/libexec/docker/cli-plugins/docker-compose

Server:
Containers: 11
Running: 5
Paused: 0
Stopped: 6
Images: 23
Server Version: 24.0.2
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Using metacopy: false
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Cgroup Version: 1
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc io.containerd.runc.v2
Default Runtime: runc
Init Binary: docker-init
containerd version: 3dce8eb055cbb6872793272b4f20ed16117344f8
runc version: v1.1.7-0-g860f061
init version: de40ad0
Security Options:
seccomp
Profile: builtin
Kernel Version: 3.10.0-957.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 972.6MiB
Name: elkstack02
ID: 157d082e-1c28-4abf-8857-2e2d9bacbdf1
Docker Root Dir: /var/lib/docker
Debug Mode: false
Experimental: false
Insecure Registries:
10.0.0.82
127.0.0.0/8
Registry Mirrors:
https://pgz00k39.mirror.aliyuncs.com/
Live Restore Enabled: false

WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled

资源限制示例

Docker内存限制

内存限制相关语法

1
2
3
4
5
6
# 选项
-m # 允许容器使用的最大内存,单位:k、m、g
--vm # 指定启动几个压测内存的进程(一个进程占用内存256m)

# 语法格式
docker run -m 200m -it busybox /bin/sh

内存限制示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 查看docker资源占用信息
docker stats
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
3ccba8015e98 test_mem1 0.00% 56KiB / 200MiB 0.03% 656B / 0B 0B / 0B 1
e4ccf0b811bb harbor-portal 0.00% 1.375MiB / 972.6MiB 0.14% 1.54kB / 0B 266kB / 0B 2
8adfdf10d542 registryctl 0.00% 3.078MiB / 972.6MiB 0.32% 1.54kB / 0B 2.61MB / 0B 6
e454f59c7e0c registry 0.00% 5.043MiB / 972.6MiB 0.52% 1.5kB / 0B 3.02MB / 0B 7
9df3af7ae3de redis 0.12% 1.207MiB / 972.6MiB 0.12% 1.54kB / 0B 258kB / 0B 4
83445c67d178 harbor-log 0.00% 3.25MiB / 972.6MiB 0.33% 98.9kB / 59.5kB 1.5MB / 4.1kB 11

# 下载压测镜像
docker pull lorel/docker-stress-ng

# 不限制内存压测
docker run --rm --name test_mem4 -it lorel/docker-stress-ng --vm 3
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
9b375a58845b test_mem4 70.07% 770MiB / 3.84GiB 19.58% 656B / 0B 35MB / 5.71MB 7
6d524edf3a2f test_mem3 52.77% 401.4MiB / 500MiB 80.27% 656B / 0B 2.44GB / 2.63GB 7
7f81b0bb1fa4 jenkins 6.87% 187.2MiB / 3.84GiB 4.76% 656B / 0B 1.34GB / 9.81MB 46
99d88cd4a1e3 gitlab 57.24% 1.89GiB / 3.84GiB 49.22% 656B / 0B 3.43GB / 13.7MB 269

# 限制内存压测
docker run --rm --name test_mem3 -m 500m -it lorel/docker-stress-ng --vm 3
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
9b375a58845b test_mem4 70.07% 770MiB / 3.84GiB 19.58% 656B / 0B 35MB / 5.71MB 7
6d524edf3a2f test_mem3 52.77% 401.4MiB / 500MiB 80.27% 656B / 0B 2.44GB / 2.63GB 7
7f81b0bb1fa4 jenkins 6.87% 187.2MiB / 3.84GiB 4.76% 656B / 0B 1.34GB / 9.81MB 46
99d88cd4a1e3 gitlab 57.24% 1.89GiB / 3.84GiB 49.22% 656B / 0B 3.43GB / 13.7MB 269

Docker限制CPU

CPU限制相关语法

1
2
3
4
# 选项
--cpus # 限制该容器占用多少个CPU核心数
--vm # 指定启动几个压测内存的进程(一个进程占用内存256m)
--cpu # 压测CPU

CPU限制示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 下载压测镜像
docker pull lorel/docker-stress-ng

# 不限制CPU压测
docker run --rm --name test_cpu1 -it lorel/docker-stress-ng --cpu 8

CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
e5ae29cca3de test_cpu1 295.87% 17.88MiB / 3.84GiB 0.45% 656B / 0B 0B / 0B 9
7f81b0bb1fa4 jenkins 1.06% 139MiB / 3.84GiB 3.53% 656B / 0B 1.41GB / 9.81MB 46
99d88cd4a1e3 gitlab 25.94% 1.984GiB / 3.84GiB 51.67% 656B / 0B 4.02GB / 13.8MB 266

# 限制CPU压测
docker run --rm --name test_cpu1 --cpus 1 -it lorel/docker-stress-ng --cpu 8

CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
9a6f9487e33b test_cpu1 134.87% 17.85MiB / 3.84GiB 0.45% 656B / 0B 0B / 0B 9
7f81b0bb1fa4 jenkins 1.34% 139.3MiB / 3.84GiB 3.54% 656B / 0B 1.42GB / 9.81MB 46
99d88cd4a1e3 gitlab 6.55% 2.004GiB / 3.84GiB 52.20% 656B / 0B 4.05GB / 13.8MB 266

总结

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