I want to terminate a pod when container dies but I did not find a efficient way of doing it.
I can kill the pod using kubctl but I want pod should get killed/restart automatically whenever any container restarts in a pod.
Can this task be achieved using operator?
There's a way, you have to add livenessProbe
configuration with restartPolicy
never
in your pod config.
restartPolicy
is never, pod status becomes Failed
.For example;
apiVersion: v1
kind: Pod
metadata:
labels:
test: liveness
name: liveness-http
spec:
restartPolicy: Never
containers:
- args:
- /server
image: k8s.gcr.io/liveness
livenessProbe:
httpGet:
# when "host" is not defined, "PodIP" will be used
# host: my-host
# when "scheme" is not defined, "HTTP" scheme will be used. Only "HTTP" and "HTTPS" are allowed
# scheme: HTTPS
path: /healthz
port: 8080
httpHeaders:
- name: X-Custom-Header
value: Awesome
initialDelaySeconds: 15
timeoutSeconds: 1
name: liveness
Here's the reference; https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#pod-phase