<p>I am using docker/K8s to deploy some pods.</p>
<p>Now, I need to limit total CPU that worker can use for running container (not limit CPU for each Container).</p>
<p>For example: we have 24 cores Linux Server. But I want to only use 20 cores for docker runtime.</p>
<p>Could we do that?</p>

<p>You can limit the resources for docker in your OS:</p>
<ol>
<li><p>Create the file <code>/etc/systemd/system/docker_limit.slice</code> with the following content</p>
<pre><code>[Unit]
Description=Slice that limits docker resources
Before=slices.target

[Slice]
CPUAccounting=true
CPUQuota=2000% 
</code></pre>
</li>
<li><p>Start unit: <code>systemctl start docker_limit.slice</code></p>
</li>
<li><p>Edit <code>/etc/docker/daemon.json</code></p>
<pre><code>{
  &quot;cgroup-parent&quot;: &quot;/docker_limit.slice&quot;
}
</code></pre>
</li>
<li><p>Restart Docker daemon: <code>systemctl restart docker</code></p>
</li>
</ol>
<p>credits to <a href="https://unix.stackexchange.com/a/550954/295368">@Leletir</a></p>
<p><strong>Hint:</strong>
we take a quota of <code>2000%</code>, because the <code>CPUQuota</code> relates to <strong>one</strong> CPU (in your case 20 CPUs * 100%):</p>
<blockquote>
<p>CPUQuota=</p>
<p>Assign the specified CPU time quota to the processes executed. Takes a
percentage value, suffixed with &quot;%&quot;. The percentage specifies how much
CPU time the unit shall get at maximum, relative to the total CPU time
available on one CPU. <strong>Use values &gt; 100% for allotting CPU time on more
than one CPU.</strong> This controls the &quot;cpu.max&quot; attribute on the unified
control group hierarchy and &quot;cpu.cfs_quota_us&quot; on legacy. For details
about these control group attributes, see Control Groups v2 and
sched-bwc.txt.</p>
<p>Example: CPUQuota=20% ensures that the executed processes will never
get more than 20% CPU time on one CPU.</p>
</blockquote>
<p>you can get more details about the parameters from the man page of <code>systemd.resource-control</code>:</p>
<pre><code>$ man systemd.resource-control
</code></pre>
<p>or on <a href="https://www.freedesktop.org/software/systemd/man/systemd.resource-control.html" rel="nofollow noreferrer">https://www.freedesktop.org/software/systemd/man/systemd.resource-control.html</a></p>
