<p>We're sort of new to the whole Kubernetes world, but by now have a number of services running in GKE. Today we saw some strange behaviour though, where one of the processes running inside one of our pods was killed, even though the Pod itself had plenty of resources available, and wasn't anywhere near its limits.</p>

<p>The limits are defined as such:</p>

<pre><code>resources:
  requests:
    cpu: 100m
    memory: 500Mi
  limits:
    cpu: 1000m
    memory: 1500Mi
</code></pre>

<p>Inside the pod, a Celery (Python) is running, and this particular one is consuming some fairly long running tasks.</p>

<p>During operation of one of the tasks, the celery process was suddenly killed, seemingly caused by OOM. The GKE Cluster Operations logs show the following:</p>

<pre><code>Memory cgroup out of memory: Kill process 613560 (celery) score 1959 or sacrifice child
Killed process 613560 (celery) total-vm:1764532kB, anon-rss:1481176kB, file-rss:13436kB, shmem-rss:0kB
</code></pre>

<p>The resource graph for the time period looks like the following:</p>

<p><a href="https://i.stack.imgur.com/zJEpZ.png" rel="noreferrer"><img src="https://i.stack.imgur.com/zJEpZ.png" alt="CPU and Memory usage for Pod"></a></p>

<p>As can be clearly seen, neither the CPU or the memory usage was anywhere near the limits defined by the Pod, so we're baffled as to why any OOMKilling occurred? Also a bit baffled by the fact that the process itself was killed, and not the actual Pod?</p>

<p>Is this particular OOM actually happening inside the OS and not in Kubernetes? And if so - is there a solution to getting around this particular problem?</p>

Tingyou
<p>About your statement:</p>

<blockquote>
  <p>Also a bit baffled by the fact that the process itself was killed, and
  not the actual Pod?</p>
</blockquote>

<p>Compute Resources (CPU/Memory) are configured for Containers, not for Pods.</p>

<p>If a Pod <strong>container is OOM killed</strong>, <a href="https://kubernetes.io/docs/tasks/administer-cluster/out-of-resource/#node-oom-behavior" rel="noreferrer">the Pod is not evicted</a>. The underlying container is restarted by the <code>kubelet</code> based on its <a href="https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#restart-policy" rel="noreferrer"><code>RestartPolicy</code></a>. The Pod will still exist on the same node, and the <code>Restart Count</code> will be incremented (unless you are using <code>RestartPolicy: Never</code>, which is not your case).</p>

<p>If you do a <code>kubectl describe</code> on your pod, the newly spawned container will be in <code>Running</code> state, but you can find the last restart cause in <code>Last State</code>. Also, <a href="https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/#my-container-is-terminated" rel="noreferrer">you can check</a> how many times it was restarted:</p>

<pre><code>State:          Running
  Started:      Wed, 27 Feb 2019 10:29:09 +0000
Last State:     Terminated
  Reason:       OOMKilled
  Exit Code:    137
  Started:      Wed, 27 Feb 2019 06:27:39 +0000
  Finished:     Wed, 27 Feb 2019 10:29:08 +0000
Restart Count:  5
</code></pre>

<hr>

<p>The Resource Graph visualization may deviate from the actual use of Memory. As it uses a <code>1 min interval (mean)</code> sampling, if your memory suddenly increases over the top limit, the container can be restarted before your <strong>average</strong> memory usage gets plotted on the graph as a high peak. If your Python container makes short/intermittent high memory usage, it's prone to be restarted even though the values are not in the graph.</p>

<p>With <code>kubectl top</code> you can view the last memory usage registered for the Pod. Although it will be more precisely to see the memory usage on a specific point in time, keep in mind that it fetches the values from <code>metrics-server</code>, which have a <a href="https://github.com/kubernetes-sigs/metrics-server#flags" rel="noreferrer"><code>--metric-resolution</code></a>:</p>

<blockquote>
  <p>The interval at which metrics will be scraped from Kubelets (defaults
  to 60s).</p>
</blockquote>

<p>If your container makes a "spiky" use of memory, you may still see it being restarting without even seeing the memory usage on <code>kubectl top</code>.</p>
