限制容器对CPU的使用 - 每天5分钟玩转 Docker 容器技术(28)
<p class="wihudong" style="margin: 0"><span style="font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; color: rgba(56, 58, 66, 1)">上节学习了如何限制容器对内存的使用,本节我们来看CPU。</span></p><p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left"><br>默认设置下,所有容器可以平等地使用 host CPU 资源并且没有限制。</p>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">Docker 可以通过 <code style="font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12.96px; padding: 2px 4px; color: rgba(0, 0, 0, 1); border-radius: 3px; background: rgba(234, 234, 235, 1)"><span style="background-color: rgba(216, 216, 216, 1)">-c</span></code> 或 <code style="font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12.96px; padding: 2px 4px; color: rgba(0, 0, 0, 1); border-radius: 3px; background: rgba(234, 234, 235, 1)"><span style="background-color: rgba(216, 216, 216, 1)">--cpu-shares</span></code> 设置容器使用 CPU 的权重。如果不指定,默认值为 1024。</p>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">与内存限额不同,通过 <code style="font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12.96px; padding: 2px 4px; color: rgba(0, 0, 0, 1); border-radius: 3px; background: rgba(234, 234, 235, 1)"><span style="background-color: rgba(216, 216, 216, 1)">-c</span></code> 设置的 cpu share 并不是 CPU 资源的绝对数量,而是一个相对的权重值。某个容器最终能分配到的 CPU 资源取决于它的 cpu share 占所有容器 cpu share 总和的比例。</p>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">换句话说:<strong style="color: rgba(0, 0, 0, 1)">通过 cpu share 可以设置容器使用 CPU 的优先级</strong>。</p>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left"> 比如在 host 中启动了两个容器:<br>
</p>
<p style="text-align: left">
<span style="font-size: 13px; background-color: rgba(216, 216, 216, 1)">docker run --name "container_A" -c 1024 ubuntu</span>
</p>
<p style="text-align: left">
<span style="font-size: 13px; background-color: rgba(216, 216, 216, 1)">docker run --name "container_B" -c 512 ubuntu</span>
</p>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">
<span style="font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px"><br>container_A 的 cpu share 1024,是 container_B 的两倍。当两个容器都需要 CPU 资源时,container_A 可以得到的 CPU 是 container_B 的两倍。</span>
</p>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">
需要特别注意的是,这种按权重分配 CPU 只会发生在 CPU 资源紧张的情况下。如果 container_A 处于空闲状态,这时,为了充分利用 CPU 资源,container_B 也可以分配到全部可用的 CPU。
</p>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">
下面我们继续用 progrium/stress 做实验。
</p>
<ol class=" list-paddingleft-2" style="margin-bottom: 1.5em; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; line-height: 20.5714px; white-space: normal">
<li>
<p style="margin-top: 0; margin-bottom: 15px; line-height: 1.6; text-align: left">
启动 container_A,cpu share 为 1024: <br><img src="http://7xo6kd.com1.z0.glb.clouddn.com/upload-ueditor-image-20170613-1497354632260073313.jpg"><br><code style="font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12.96px; padding: 2px 4px; color: rgba(0, 0, 0, 1); border-radius: 3px; background: rgba(234, 234, 235, 1)"><span style="background-color: rgba(216, 216, 216, 1)">--cpu</span></code> 用来设置工作线程的数量。因为当前 host 只有 1 颗 CPU,所以一个工作线程就能将 CPU 压满。如果 host 有多颗 CPU,则需要相应增加 <code style="font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12.96px; padding: 2px 4px; color: rgba(0, 0, 0, 1); border-radius: 3px; background: rgba(234, 234, 235, 1)"><span style="background-color: rgba(216, 216, 216, 1)">--cpu</span></code> 的数量。
</p>
</li>
<li>
<p style="margin-top: 0; margin-bottom: 15px; line-height: 1.6; text-align: left">
启动 container_B,cpu share 为 512: <br><img src="http://7xo6kd.com1.z0.glb.clouddn.com/upload-ueditor-image-20170613-1497354632469060326.jpg">
</p>
</li>
<li>
<p style="margin-top: 0; margin-bottom: 15px; line-height: 1.6; text-align: left">
在 host 中执行 <code style="font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12.96px; padding: 2px 4px; color: rgba(0, 0, 0, 1); border-radius: 3px; background: rgba(234, 234, 235, 1)"><span style="background-color: rgba(216, 216, 216, 1)">top</span></code>,查看容器对 CPU 的使用情况: <br><img src="http://7xo6kd.com1.z0.glb.clouddn.com/upload-ueditor-image-20170613-1497354632659031922.jpg"><br>container_A 消耗的 CPU 是 container_B 的两倍。
</p>
</li>
<li>
<p style="margin-top: 0; margin-bottom: 15px; line-height: 1.6; text-align: left">
现在暂停 container_A: <br><img src="http://7xo6kd.com1.z0.glb.clouddn.com/upload-ueditor-image-20170613-1497354632995042802.jpg">
</p>
</li>
<li>
<p style="margin-top: 0; margin-bottom: 15px; line-height: 1.6; text-align: left">
<code style="font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12.96px; padding: 2px 4px; color: rgba(0, 0, 0, 1); border-radius: 3px; background: rgba(234, 234, 235, 1)"><span style="background-color: rgba(216, 216, 216, 1)">top</span></code> 显示 container_B 在 container_A 空闲的情况下能够用满整颗 CPU: <br><img src="http://7xo6kd.com1.z0.glb.clouddn.com/upload-ueditor-image-20170613-1497354633531037106.jpg">
</p>
</li>
</ol>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">
CPU限额就讨论到这里,下一节我们将学习如何限制容器对 Block IO 带宽资源的使用。<br>
</p>
<p>
<img src="http://7xo6kd.com1.z0.glb.clouddn.com/upload-ueditor-image-20170613-1497354633788030530.jpg"><br>
</p><br><br>
来源:https://www.cnblogs.com/CloudMan6/p/7003199.html
頁:
[1]