Terminate a pod when container dies

2/6/2019

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?

-- drifter
kubernetes

1 Answer

2/6/2019

There's a way, you have to add livenessProbe configuration with restartPolicy never in your pod config.

  1. The livenessProbe listen to container failures
  2. When the container dies, as 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

-- Laksitha Ranasingha
Source: StackOverflow