Let's first have an overview over the pieces of 'self-healing' mechanism by K8s.
ReplicaSet
owned by that Deployment ensures that number is kept. If a pod is deleted or a pod’s container exits, the ReplicaSet
(and controller manager) will create a replacement pod.restartPolicy
(for Deployments the value Always
is set by default). If the container process exits, kubelet will restart it according to that policy.SIGTERM
to processes (pid 1) in the container, waits terminationGracePeriodSeconds
(default 30s) for graceful exit, then SIGKILL
.
Lifecycle and hooks that are offered for Pods (and Containers) are points where your code can take actions. Kubernetes offers a number of places where you can provide explicit feedback to the system to have it operate as you’d like.
The lifecycle of a Pod is an aggregate of several components, as a Pod has a number of moving parts that can be in a variety of states as it operates. The representation of the lifecycle is how Kubernetes manages running your code for you, in conjunction with the control and feedback loops that work from the various controllers.
The states of a Pod's lifecycle are:
The two probes enabled in Kubernetes are the liveness probe and readiness Probe. They are complimentary, but different in intent and usage, and can be defined for each container within a Pod. In both cases, they provide a means for your code to influence how Kubernetes manages the containers.
The most basic probe is the Liveness probe. It provides a command or URL that Kubernetes can use to determine
whether a Pod is still operational. If the call succeeds, Kubernetes will assume the container is healthy. If it fails to respond,
then the Pod can be handled as the restartPolicy
is defined. The result is binary: either the probe succeeds, and Kubernetes
believes your Pod is running, or it fails, so Kubernetes believes your Pod is no longer functional.
In the latter case, it will check with the defined RestartPolicy
to choose what to do.
The default value for restartPolicy
is Always, meaning if a container within the Pod fails, Kubernetes will always attempt…
Based on „Kubernetes for Developers by Joseph Heck“