<p>I have a deployment with memory limits</p>
<pre><code>resources:
  limits:
    memory: 128Mi
</code></pre>
<p>But my app starts to fail when it is near to the limit, so, there is any way to restart the pod before it reaches a percentage of the memory limit?</p>
<p>For example if the limit is 128Mi, restart the pod when it reach 85% of it.</p>

<p>I am going to address this question from the Kuberentes side.</p>
<p>As already mentioned by arjain13, the solution you thought of is not the way to go as it is against the idea of <a href="https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/#requests-and-limits" rel="nofollow noreferrer">Requests and limits</a>:</p>
<blockquote>
<p>If you set a memory limit of 4GiB for that Container, the kubelet (and
container runtime) enforce the limit. The runtime prevents the
container from using more than the configured resource limit. For
example: when a process in the container tries to consume more than
the allowed amount of memory, the system kernel terminates the process
that attempted the allocation, with an out of memory (OOM) error.</p>
</blockquote>
<p>You can also find an example of <a href="https://kubernetes.io/docs/tasks/configure-pod-container/assign-memory-resource/#exceed-a-container-s-memory-limit" rel="nofollow noreferrer">Exceeding a Container's memory limit</a>:</p>
<blockquote>
<p>A Container can exceed its memory request if the Node has memory
available. But a Container is not allowed to use more than its memory
limit. If a Container allocates more memory than its limit, the
Container becomes a candidate for termination. If the Container
continues to consume memory beyond its limit, the Container is
terminated. If a terminated Container can be restarted, the kubelet
restarts it, as with any other type of runtime failure.</p>
</blockquote>
<p>There are two things I would like to recommend you to try in your current use case:</p>
<ol>
<li><p>Debug your application in order to eliminate the memory leak which looks like to be the source of this issue.</p>
</li>
<li><p>Use a <a href="https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#container-probes" rel="nofollow noreferrer">livenessProbe</a>:</p>
</li>
</ol>
<blockquote>
<p>Indicates whether the container is running. If the liveness probe
fails, the kubelet kills the container, and the container is subjected
to its restart policy.</p>
</blockquote>
<p>It can be configured using the fields below:</p>
<ul>
<li><p><code>initialDelaySeconds</code>: Number of seconds after the container has started before liveness or readiness probes are initiated. Defaults to 0 seconds. Minimum value is 0.</p>
</li>
<li><p><code>periodSeconds</code>: How often (in seconds) to perform the probe. Default to 10 seconds. Minimum value is 1.</p>
</li>
<li><p><code>timeoutSeconds</code>: Number of seconds after which the probe times out. Defaults to 1 second. Minimum value is 1.</p>
</li>
<li><p><code>successThreshold</code>: Minimum consecutive successes for the probe to be considered successful after having failed. Defaults to 1. Must be 1 for liveness. Minimum value is 1.</p>
</li>
<li><p><code>failureThreshold</code>: When a probe fails, Kubernetes will try <code>failureThreshold</code> times before giving up. Giving up in case of liveness probe means restarting the container. In case of readiness probe the Pod will be marked Unready. Defaults to 3. Minimum value is 1.</p>
</li>
</ul>
<p>If you set the minimal values for <code>periodSeconds</code>, <code>timeoutSeconds</code>, <code>successThreshold</code> and <code>failureThreshold</code> you can expect more frequent checks and faster restarts.</p>
<p>Below you will find some useful sources and guides:</p>
<ul>
<li><p><a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/" rel="nofollow noreferrer">Configure Liveness, Readiness and Startup Probes</a></p>
</li>
<li><p><a href="https://cloud.google.com/blog/products/containers-kubernetes/kubernetes-best-practices-resource-requests-and-limits" rel="nofollow noreferrer">Kubernetes best practices: Resource requests and limits</a></p>
</li>
<li><p><a href="https://cloud.google.com/blog/products/containers-kubernetes/kubernetes-best-practices-setting-up-health-checks-with-readiness-and-liveness-probes" rel="nofollow noreferrer">Kubernetes best practices: Setting up health checks with readiness and liveness probes</a></p>
</li>
</ul>
