title: Kubernetes - Indication of CPU limit less than available CPU
body: <h2>Objective</h2>
  
  <p>Clarify the behaviour of K8S container cpu usage when limit is set far below available CPU, and confirm if the understanding how to set limit is correct.</p>
  
  <h2>Background</h2>
  
  <p>I have a node of 2CPU, hence 2000m can be the upper limit. Every namespace is set with the LimitRange which limits CPU to 500m for container.</p>
  
  <pre><code>kind: LimitRange
  metadata:
    name: core-resource-limits
      spec:
        limits:
        - default:
            cpu: 500m
            memory: 2Gi
          defaultRequest:
            cpu: 100m
          type: Container
  </code></pre>
  
  <h2>Indication</h2>
  
  <p>Even when 2 CPU are available (no other process/container waiting) and a container is runnable, it can only use 0.5 CPU, and 1.5 CPU will be left unused. Is this correct?</p>
  
  <h2>How to set LimitRange</h2>
  
  <p>I believe I can set the limit such as 75-80% of available 2 CPU to better utilise the CPU resource. Because in case there are multiple containers trying to claim CPU more than requests, K8S will allocate the CPU among containers based on the request value of each containers, as per the documentations (some from OpenShift but believe it is the same with K8S). Is this correct?</p>
  
  <pre><code>kind: LimitRange
  metadata:
    name: core-resource-limits
      spec:
        limits:
        - default:
            cpu: 1500m
            memory: 2Gi
          defaultRequest:
            cpu: 100m
          type: Container
  </code></pre>
  
  <p><a href="https://docs.openshift.com/container-platform/3.5/dev_guide/compute_resources.html#dev-cpu-requests" rel="noreferrer">CPU Requests</a></p>
  
  <blockquote>
    <p>The CPU request represents a minimum amount of CPU that your container may consume, but <strong>if there is no contention for CPU, it can use all available CPU on the node. If there is CPU contention on the node, CPU requests provide a relative weight across all containers</strong> on the system for how much CPU time the container may use.</p>
  </blockquote>
  
  <p><a href="https://docs.openshift.com/container-platform/3.5/dev_guide/compute_resources.html#dev-cpu-limits" rel="noreferrer">CPU Limits</a></p>
  
  <blockquote>
    <p>Each container in a pod can specify the amount of CPU it is limited to use on a node. <strong>CPU limits control the maximum amount of CPU that your container may use independent of contention on the node</strong>. If a container attempts to exceed the specified limit, the system will throttle the container. This allows the container to have a consistent level of service independent of the number of pods scheduled to the node.</p>
  </blockquote>
  
  <p><a href="https://stackoverflow.com/questions/42326645/kubernetes-understanding-cpu-resources-limits">kubernetes / understanding CPU resources limits</a></p>
  
  <blockquote>
    <p>The 6% of CPU means 6% (CPU requests) of the nodes CPU time is reserved for this pod. So it guaranteed that it always get at lease this amount of CPU time. <strong>It can still burst up to 12% (CPU limits), if there is still CPU time left</strong>.</p>
  </blockquote>
  
  <p><a href="https://stackoverflow.com/questions/37975366/how-are-kubernetes-cpu-limits-enforced">How are Kubernetes CPU limits enforced?</a></p>
  
  <blockquote>
    <p>Checking one of my containers, I see that Kubernetes set CpuPeriod, CpuQuota for them. The Docker docs (<a href="https://docs.docker.com/engine/reference/run/" rel="noreferrer">https://docs.docker.com/engine/reference/run/</a>) mention the CFS documentation (<a href="https://www.kernel.org/doc/Documentation/scheduler/sched-bwc.txt" rel="noreferrer">https://www.kernel.org/doc/Documentation/scheduler/sched-bwc.txt</a>), and from the information there, it seems that these are hard limits</p>
  </blockquote>