<p>I allocated resource to 1 pod only with 650MB/30% of memory (with other built-in pods, limit memory is 69% only)</p>

<p>However, when the pod handling process, the usage of pod is within 650MB but overall usage of node is 94%. </p>

<p>Why does it happen because it supposed to have upper limit of 69%? Is it due to other built-in pods which did not set limit? How to prevent this as sometimes my pod with error if usage of Memory > 100%?</p>

<p>My allocation setting (<code>kubectl describe nodes</code>):
<a href="https://i.stack.imgur.com/tDoZ6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tDoZ6.png" alt="enter image description here"></a></p>

<p>Memory usage of Kubernetes Node and Pod when idle:<br>
<code>kubectl top nodes</code><br>
<a href="https://i.stack.imgur.com/JtXgo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JtXgo.png" alt="enter image description here"></a><br>
<code>kubectl top pods</code><br>
<a href="https://i.stack.imgur.com/ijLHU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ijLHU.png" alt="enter image description here"></a></p>

<p>Memory usage of Kubernetes Node and Pod when running task:<br>
<code>kubectl top nodes</code><br>
<a href="https://i.stack.imgur.com/phCZS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/phCZS.png" alt="enter image description here"></a><br>
<code>kubectl top pods</code><br>
<a href="https://i.stack.imgur.com/7Ja9B.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7Ja9B.png" alt="enter image description here"></a></p>

<hr>

<p><strong>Further Tested behaviour:</strong><br>
1. Prepare deployment, pods and service under namespace <em>test-ns</em><br>
2. Since only <em>kube-system</em> and <em>test-ns</em> have pods, so assign 1000Mi to each of them (from <code>kubectl describe nodes</code>) aimed to less than 2GB<br>
3. Suppose memory used in <em>kube-system</em> and <em>test-ns</em> will be less than 2GB which is less than 100%, why memory usage can be 106%?  </p>

<p>In <em>.yaml file:</em>  </p>

<pre><code>    apiVersion: v1
    kind: LimitRange
    metadata:
      name: default-mem-limit
      namespace: test-ns
    spec:
      limits:
      - default:
          memory: 1000Mi
        type: Container
    ---
    apiVersion: v1
    kind: LimitRange
    metadata:
      name: default-mem-limit
      namespace: kube-system
    spec:
      limits:
      - default:
          memory: 1000Mi
        type: Container
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: devops-deployment
      namespace: test-ns
      labels:
        app: devops-pdf
    spec:
      selector:
        matchLabels:
          app: devops-pdf
      replicas: 2
      template:
        metadata:
          labels:
            app: devops-pdf
        spec:
          containers:
          - name: devops-pdf
            image: dev.azurecr.io/devops-pdf:latest
            imagePullPolicy: Always
            ports:
            - containerPort: 3000
            resources:
              requests:
                cpu: 600m
                memory: 500Mi
              limits:
                cpu: 600m
                memory: 500Mi
          imagePullSecrets:
          - name: regcred
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: devops-pdf
      namespace: test-ns
    spec:
      type: LoadBalancer
      ports:
      - port: 8007
      selector:
        app: devops-pdf
</code></pre>

<p><a href="https://i.stack.imgur.com/mjIKA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mjIKA.png" alt="enter image description here"></a>
<a href="https://i.stack.imgur.com/EGqCs.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EGqCs.png" alt="enter image description here"></a>
<a href="https://i.stack.imgur.com/BXpEe.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BXpEe.png" alt="enter image description here"></a></p>

<p>This effect is most likely caused by the 4 Pods that run on that node <strong>without</strong> a memory limit specified, shown as <code>0 (0%)</code>. Of course 0 doesn't mean it can't use even a single byte of memory as no program can be started without using memory; instead it means that there is no limit, it can use as much as available. Also programs running not in pod (ssh, cron, ...) are included in the total used figure, but are not limited by kubernetes (by cgroups).</p>

<p>Now kubernetes sets up the kernel oom adjustment values in a tricky way to favour containers that are under their memory  <em>request</em>, making it more more likely to kill processes in containers that are between their memory <em>request</em> and <em>limit</em>, and making it most likely to kill processes in containers with no memory <em>limits</em>. However, this is only shown to work fairly in the long run, and sometimes the kernel can kill your favourite process in your favourite container that is behaving well (using less than its memory <em>request</em>). See <a href="https://kubernetes.io/docs/tasks/administer-cluster/out-of-resource/#node-oom-behavior" rel="noreferrer">https://kubernetes.io/docs/tasks/administer-cluster/out-of-resource/#node-oom-behavior</a></p>

<p>The pods without memory limit in this particular case are coming from the aks system itself, so setting their memory limit in the pod templates is not an option as there is a reconciler that will restore it (eventually). To remedy the situation I suggest that you create a LimitRange object in the kube-system namespace that will assign a memory limit to all pods without a limit (as they are created):</p>

<pre><code>apiVersion: v1
kind: LimitRange
metadata:
  name: default-mem-limit
  namespace: kube-system
spec:
  limits:
  - default:
      memory: 150Mi
    type: Container
</code></pre>

<p>(You will need to delete the already existing <strong>Pods</strong> without a memory limit for this to take effect; they will get recreated)</p>

<p>This is not going to completely eliminate the problem as you might end up with an overcommitted node; however the memory usage will make sense and the oom events will be more predictable.</p>
