<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns="http://purl.org/rss/1.0/"
xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel rdf:about="https://dwt.life/feed/rss/tag/%E8%B5%84%E6%BA%90%E9%99%90%E5%88%B6/">
<title>dwt&#039;s life - 资源限制</title>
<link>https://dwt.life/tag/%E8%B5%84%E6%BA%90%E9%99%90%E5%88%B6/</link>
<description></description>
<items>
<rdf:Seq>
<rdf:li resource="https://dwt.life/archives/27/"/>
<rdf:li resource="https://dwt.life/archives/26/"/>
</rdf:Seq>
</items>
</channel>
<item rdf:about="https://dwt.life/archives/27/">
<title>docker内存限制</title>
<link>https://dwt.life/archives/27/</link>
<dc:date>2021-07-04T01:23:27+08:00</dc:date>
<description>-m 和 --memory-swapdocker run -it --rm -m 100M --memory-swap -1 ubuntu-stress:latest /bin/bash指定限制内存大小并且设置 memory-swap 值为 -1，表示容器程序使用内存受限，而 swap 空间使用不受限制（宿主 swap 支持使用多少则容器即可使用多少。如果 --memory-swap 设置小于 --memory则设置不生效，使用默认设置）。docker run -it --rm -m 100M --memory-swap -1 centos /bin/bash按照官方文档的理解，如果指定 -m 内存限制时不添加 --memory-swap 选项，则表示容器中程序可以使用 100M 内存和 100M swap 内存。默认情况下，–memory-swap 会被设置成 memory 的 2倍。–memory-swappiness=0表示禁用容器 swap 功能(这点不同于宿主机，宿主机 swappiness 设置为 0 也不保证 swap 不会被使用):docker run -it --rm -m 100M --memory-swappiness=0 ubuntu-stress:latest /bin/bash–memory-reservation选项可以理解为内存的软限制。如果不设置 -m 选项，那么容器使用内存可以理解为是不受限的。按照官方的说法，memory reservation 设置可以确保容器不会长时间占用大量内存。–oom-kill-disabledocker run -it --rm -m 100M --memory-swappiness=0 --oom-kill-disable ubuntu-stress:latest /bin/bash–kernel-memory内核内存与用户内存根本不同，因为内核内存无法换出。无法交换使容器可能通过占用过多的内核内存来阻止系统服务。内核内存包括：stack pagesslab pagessockets memory pressuretcp memory pressure</description>
</item>
<item rdf:about="https://dwt.life/archives/26/">
<title>docker容器的CPU资源限制</title>
<link>https://dwt.life/archives/26/</link>
<dc:date>2021-07-04T01:17:00+08:00</dc:date>
<description>准备centos7docker平台stress命令（模拟CPU负载）用到docker参数--cpu-period（周期），--cpu-quota（配额）单位是微秒；1秒=1000毫秒，1毫秒=1000微秒，1秒=1000000微秒过程运行容器（限制容器CPU占用比重为500 000/1 000 000=50%；1秒内最多使用0.5秒的CPU；最多不超过一个内核的50%）[root@server ~]# docker run -dit --name centos1 --cpu-period=1000000 --cpu-quota=500000 192.168.200.104:5000/centos/stress:latest bash
59c89af717d4606f4a9ec3843ad7fe4d72a81ab81bc299095eab07627aae3952压力测试(尽量超载)：进入到容器中模拟负载，负载量为400%（1个内核=100%）：[root@server ~]# docker exec -it centos1 bash
[root@59c89af717d4 /]# stress -c 4 
stress: info: [29] dispatching hogs: 4 cpu, 0 io, 0 vm, 0 hdd同时查看该容器的CPU资源占用：[root@server ~]# docker stats centos1
CONTAINER           CPU %               MEM USAGE / LIMIT    MEM %               NET I/O             BLOCK I/O           PIDS
centos1             50.07%              968 KiB / 3.86 GiB   0.02%               648 B / 648 B       5.895 MB / 0 B      0CPU使用率为50.07%，说明限制生效了。测试也可以设置上限为2个内核（200%）[root@server ~]# docker run -dit --name centos1 --cpu-period=1000000 --cpu-quota=2000000 192.168.200.104:5000/centos/stress:latest bash 
64428831a83319325d685c762e75306e5a45a75eeaa01979eeede758b5f68e65
root@server ~]# docexec -it centos1 bash
[root@64428831a833 /]# stress -c 4
stress: info: [28] dispatching hogs: 4 cpu, 0 io, 0 vm, 0 hdd
[root@server ~]# docker stats centos1
CONTAINER           CPU %               MEM USAGE / LIMIT    MEM %               NET I/O             BLOCK I/O           PIDS
centos1             201.48%             968 KiB / 3.86 GiB   0.02%               648 B / 648 B       5.886 MB / 0 B      0本机有四个内核，设置上限为2000%（20个内核）：[root@server ~]# docker run -dit --name centos1 --cpu-period=1000000 --cpu-quota=20000000 192.168.200.104:5000/centos/stress:latest bash
4ac343f7b5e8a87118e10821b0a252013aeb08e9cbfed86cd2a691b699464d19
[root@server ~]# docker exec -it centos1 bash
[root@4ac343f7b5e8 /]# stress -c 10
stress: info: [35] dispatching hogs: 10 cpu, 0 io, 0 vm, 0 hdd
[root@server ~]# docker stats centos1
CONTAINER           CPU %               MEM USAGE / LIMIT      MEM %               NET I/O             BLOCK I/O           PIDS
centos1             417.47%             1.098 MiB / 3.86 GiB   0.03%               648 B / 648 B       5.878 MB / 0 B      0总结容器的CPU配额是通过period和quota之间的大小比重来确定内核的数量，而且是以时间为单位。例如：1秒内可以使用0.5秒的CPU，等同于可以使用一个内核的50%；1秒内可以使用2秒的CPU，则是两个内核。限制内核数为quota/period=n。</description>
</item>
</rdf:RDF>