Skip to main content

Self-Healing Kubernetes Services Hide Failures Unless You Record Every Restart

A pod restarts itself at 2 a.m., the dashboard is green by morning and nobody opens a ticket. Did the system heal, or did it hide a defect? Kubernetes makes automatic recovery so easy that it is tempting to count every restart as a success. This guide is a framework for deciding what a restart can actually fix, how to configure probes so they do not cause outages of their own, and how to make sure every automatic recovery leaves a record someone reads.

A restart only fixes problems that live in process memory

Before adding any automatic recovery, ask one question: is the broken state disposable? A restart discards everything in the container's memory, so it clears deadlocks, wedged connection pools and corrupted in-process caches. It does nothing for a bad schema, an expired credential, a poisoned message or an upstream outage. If a fresh process will fail the same way, the restart is noise.

Three criteria help for any self-healing mechanism:

  • Is the failure local to the process? If a clean start would not change it, do not restart on it.
  • Can the work be repeated safely? A restart can interrupt work in progress, so the work has to tolerate that.
  • Will a human learn it happened? Recovery that leaves no trace turns a defect into a permanent feature.

Give each probe exactly one job

Kubernetes has three probes, and each triggers a different action. A startup probe holds back the other two until the application has started. Repeated liveness failures make the kubelet restart the container. A failed readiness probe removes the pod from the endpoints of matching services, without restarting anything. Liveness probes do not wait for readiness, so without a startup probe or an initial delay a slow-starting service can be killed before it finishes booting.

Probe On failure What it should check
StartupContainer is restarted if it never succeedsThat the application has finished booting
LivenessContainer is restartedOnly whether the process can still make progress
ReadinessPod stops receiving trafficWhether it can serve requests, including dependencies

The mistake worth avoiding most is putting a dependency check, such as the database or a downstream API, in the liveness probe. This is a design argument rather than a documented rule: if the dependency goes down, every replica fails liveness at about the same time and the kubelet restarts the whole fleet. Restarting does not bring the dependency back. Dependency health belongs in readiness, where the consequence is only that traffic pauses.

startupProbe:
  httpGet:
    path: /health/live
    port: 8080
  periodSeconds: 5
  failureThreshold: 36      # up to ~180s to boot before liveness starts
livenessProbe:
  httpGet:
    path: /health/live      # process-internal only: can it still make progress?
    port: 8080
  periodSeconds: 10
  failureThreshold: 3       # ~30s of failure before a restart
readinessProbe:
  httpGet:
    path: /health/ready     # may check dependencies; failure only removes traffic
    port: 8080
  periodSeconds: 10
  failureThreshold: 3

The values are placeholders to tune against measured startup time. The structure is what matters: the startup probe absorbs slow boots so liveness can stay strict, /health/live answers only whether the process is progressing, and /health/ready is the one allowed to look outward.

For a reporting or batch-style workload, the useful liveness signal is often a heartbeat that the worker loop updates. If the loop stops advancing, the endpoint fails. A bare "the web server answers" check can stay green while the code that matters is stuck.

Backoff turns a hidden fault into a slow one

A container that keeps failing is not restarted in a tight loop. The kubelet applies an exponential backoff delay between restarts, and the pod shows CrashLoopBackOff while it waits. Historically the delay started at 10 seconds, doubled after each failure and was capped at five minutes, and it resets after the container has run for ten minutes without problems. Recent Kubernetes releases add a reduced default delay and a configurable one, so the exact numbers depend on your version and on how your cluster (including managed Azure Kubernetes Service) is configured. Check the Pod Lifecycle page for your version before quoting figures.

Two consequences hold regardless of the numbers:

  • A broken pod can stay broken quietly. Backoff keeps the cluster calm, but a bad configuration can sit in a retry loop for hours without anyone touching it.
  • A slow crash can look healthy. If a service crashes just after the reset window, the backoff keeps restarting from the beginning, and a check that only looks at current state sees a running pod.

The restart policy decides whether any of this applies. The default, Always, restarts a container however it exited, even after a clean exit. OnFailure restarts only on a non-zero exit, and Never does not restart. For run-to-completion work such as report generation, that choice decides whether finished work gets repeated, so set it deliberately. Jobs accept OnFailure or Never.

Exit code 137 is evidence, not a diagnosis

When a container restarts, the first fact to find is why it died. Exit code 137 means 128 plus signal 9, SIGKILL, which a process cannot catch. The usual cause is the kernel killing the container for exceeding its memory limit, which kubectl describe pod reports as OOMKilled in the last state. But the same code also appears when a process ignores SIGTERM and is force-killed once the termination grace period (30 seconds by default) runs out, including after a liveness-probe kill. The exit code alone does not tell you which one you have, so read the reason field next to it.

This matters for the self-healing story. If a memory limit sits below the application's real working set, restarts become routine and look like healing, while every replacement pod hits the same wall. A recurring OOMKilled is a sizing question or a leak to investigate, not proof that automation works.

Every automatic recovery should leave a record someone reads

The fix for hidden failures is not to disable restarts. It is to make each one visible. Restart counts and last-termination reasons are exposed as metrics by kube-state-metrics, and Azure Monitor and other platforms can ingest them. A simple alert on a rate, with an illustrative threshold:

- alert: PodRestartingRepeatedly
  expr: increase(kube_pod_container_status_restarts_total[1h]) > 3
  for: 10m
  annotations:
    summary: "{{ $labels.namespace }}/{{ $labels.pod }} restarted more than 3 times in an hour"

Beyond the alert, a few practices make restarts findable and meaningful:

  1. Alert on a rate, not on the existence of one restart. One restart a week may be fine. A trend is a defect report.
  2. Record the termination reason. Keep OOMKilled, error exits and probe kills apart, since each points at a different fix.
  3. Log how and when each process started. A restart should be findable in application logs, not only in cluster events.
  4. Alert on user-visible symptoms too. A report that did not arrive on schedule matters even if every pod looks healthy.
  5. Make interrupted work safe to repeat. A restart can cut a job off mid-run, so a rerun must not duplicate or lose output.
  6. Set a review trigger. If the same workload restarts for the same reason twice, open a defect instead of tuning the probe.

The fifth point is worth verifying first in any design. Self-healing without safe reruns converts one failure into duplicated or partial output.

A sensible order of work

  1. List which failures a restart can really fix, and which it cannot.
  2. Keep liveness to a process-internal check, with a startup probe for slow boots.
  3. Move dependency checks to readiness.
  4. Choose the restart policy on purpose for long-running and run-to-completion workloads.
  5. Size memory limits from measurements, and treat repeated OOMKilled as a defect.
  6. Alert on restart rate and on missing output, and make reruns idempotent.

Use restarts for what they do well, which is clearing a stuck process, and treat every restart as an event worth counting. A service that heals quietly should still tell you what it healed, because that record is how you find the defect that restarting never fixes. Probe behavior and defaults vary by Kubernetes version and managed platform, so confirm them for your own cluster rather than relying on any general rule.

Comments