To restart the entire pod if the probe in one container returns false in Kubernetes, you can use a liveness or readiness probe with a failure threshold and period.
First, specify the liveness or readiness probe in the pod definition for each container that requires it. The probe should check the health of the container’s application and return success (HTTP 200-399) if it is healthy, or failure (HTTP 400-599) if it is not.
Next, set the failureThreshold
and periodSeconds
properties in the pod spec to define how many consecutive failures are allowed before considering the container unhealthy and triggering a restart.
For example:
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: app-container
image: my-app-image
livenessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 5
periodSeconds: 10
failureThreshold: 3
- name: sidecar-container
image: my-sidecar-image
# Restart policy specifies what to do when a container fails.
restartPolicy: Always
In this example, we have set failureThreshold
to three consecutive failures within a periodSeconds
of ten seconds for our livenessProbe
. This means that if our app has been failing its health checks for more than thirty seconds straight, Kubernetes will trigger an automatic restart of all containers within that pod.
The restartPolicy
specifies what action to take if any of these containers fail. In this case, we’ve set it to ‘Always’, which ensures that Kubernetes will always attempt to restart failed containers indefinitely until they come back up again successfully.