<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:wfw="http://wellformedweb.org/CommentAPI/">
<channel>
<title>dwt&#039;s life - 资源限制</title>
<link>https://dwt.life/tag/%E8%B5%84%E6%BA%90%E9%99%90%E5%88%B6/</link>
<atom:link href="https://dwt.life/feed/tag/%E8%B5%84%E6%BA%90%E9%99%90%E5%88%B6/" rel="self" type="application/rss+xml" />
<language>zh-CN</language>
<description></description>
<lastBuildDate>Sun, 04 Jul 2021 01:23:27 +0800</lastBuildDate>
<pubDate>Sun, 04 Jul 2021 01:23:27 +0800</pubDate>
<item>
<title>docker内存限制</title>
<link>https://dwt.life/archives/27/</link>
<guid>https://dwt.life/archives/27/</guid>
<pubDate>Sun, 04 Jul 2021 01:23:27 +0800</pubDate>
<dc:creator>Ricky</dc:creator>
<description><![CDATA[-m 和 --memory-swapdocker run -it --rm -m 100M --memory-swap -1 ubuntu-stress:latest /bin/bash指定限制...]]></description>
<content:encoded xml:lang="zh-CN"><![CDATA[
<p>-m 和 --memory-swap</p><p><code>docker run -it --rm -m 100M --memory-swap -1 ubuntu-stress:latest /bin/bash</code></p><p>指定限制内存大小并且设置 memory-swap 值为 -1，表示容器程序使用内存受限，而 swap 空间使用不受限制（宿主 swap 支持使用多少则容器即可使用多少。如果 --memory-swap 设置小于 --memory则设置不生效，使用默认设置）。</p><p><code>docker run -it --rm -m 100M --memory-swap -1 centos /bin/bash</code></p><p>按照官方文档的理解，如果指定 -m 内存限制时不添加 --memory-swap 选项，则表示容器中程序可以使用 100M 内存和 100M swap 内存。默认情况下，–memory-swap 会被设置成 memory 的 2倍。</p><p><code>–memory-swappiness=0</code></p><p>表示禁用容器 swap 功能(这点不同于宿主机，宿主机 swappiness 设置为 0 也不保证 swap 不会被使用):</p><p><code>docker run -it --rm -m 100M --memory-swappiness=0 ubuntu-stress:latest /bin/bash</code></p><p>–memory-reservation</p><p>选项可以理解为内存的软限制。如果不设置 -m 选项，那么容器使用内存可以理解为是不受限的。按照官方的说法，memory reservation 设置可以确保容器不会长时间占用大量内存。</p><p>–oom-kill-disable</p><p><code>docker run -it --rm -m 100M --memory-swappiness=0 --oom-kill-disable ubuntu-stress:latest /bin/bash</code></p><p>–kernel-memory</p><p>内核内存与用户内存根本不同，因为内核内存无法换出。无法交换使容器可能通过占用过多的内核内存来阻止系统服务。内核内存包括：</p><p>stack pages<br>slab pages<br>sockets memory pressure<br>tcp memory pressure</p>
]]></content:encoded>
<slash:comments>0</slash:comments>
<comments>https://dwt.life/archives/27/#comments</comments>
<wfw:commentRss>https://dwt.life/feed/tag/%E8%B5%84%E6%BA%90%E9%99%90%E5%88%B6/</wfw:commentRss>
</item>
<item>
<title>docker容器的CPU资源限制</title>
<link>https://dwt.life/archives/26/</link>
<guid>https://dwt.life/archives/26/</guid>
<pubDate>Sun, 04 Jul 2021 01:17:00 +0800</pubDate>
<dc:creator>Ricky</dc:creator>
<description><![CDATA[准备centos7docker平台stress命令（模拟CPU负载）用到docker参数--cpu-period（周期），--cpu-quota（配额）单位是微秒；1秒=1000毫秒，1毫秒=1...]]></description>
<content:encoded xml:lang="zh-CN"><![CDATA[
<h1>准备</h1><ul><li>centos7</li><li>docker平台</li><li>stress命令（模拟CPU负载）</li><li>用到docker参数--cpu-period（周期），--cpu-quota（配额）单位是微秒；1秒=1000毫秒，1毫秒=1000微秒，1秒=1000000微秒</li></ul><h1>过程</h1><h2>运行容器</h2><p>（限制容器CPU占用比重为500 000/1 000 000=50%；1秒内最多使用0.5秒的CPU；最多不超过一个内核的50%）</p><pre><code>[root@server ~]# docker run -dit --name centos1 --cpu-period=1000000 --cpu-quota=500000 192.168.200.104:5000/centos/stress:latest bash
59c89af717d4606f4a9ec3843ad7fe4d72a81ab81bc299095eab07627aae3952</code></pre><h2>压力测试(尽量超载)：</h2><p>进入到容器中模拟负载，负载量为400%（1个内核=100%）：</p><pre><code>[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</code></pre><p>同时查看该容器的CPU资源占用：</p><pre><code>[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      0</code></pre><p>CPU使用率为50.07%，说明限制生效了。</p><h1>测试</h1><p>也可以设置上限为2个内核（200%）</p><pre><code>[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</code></pre><p>本机有四个内核，设置上限为2000%（20个内核）：</p><pre><code>[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</code></pre><h1>总结</h1><p>容器的CPU配额是通过period和quota之间的大小比重来确定内核的数量，而且是以时间为单位。</p><p>例如：1秒内可以使用0.5秒的CPU，等同于可以使用一个内核的50%；1秒内可以使用2秒的CPU，则是两个内核。</p><p>限制内核数为quota/period=n。</p>
]]></content:encoded>
<slash:comments>0</slash:comments>
<comments>https://dwt.life/archives/26/#comments</comments>
<wfw:commentRss>https://dwt.life/feed/tag/%E8%B5%84%E6%BA%90%E9%99%90%E5%88%B6/</wfw:commentRss>
</item>
</channel>
</rss>