Can LivenessProbe control error response(e.g. 500)

Hi,

If livenessprobe is configured as below, the Pod will be verified (every specified interval) against sublink /healthz and port 8080. If the response is 200, the Pod is considered to be working fine and any other response greater then or equal to 400 is considered to be failed and the Pod gets restarted by the k8s. However my question is - if I want to perform certain action(e.g. trigger a command inside Pod) if the liveness probe response is 500, is it possible to configure so while creating the Pod?

livenessProbe:
      httpGet:
        path: /healthz
        port: 8080

The k8s document provides the below snippets however this seems to be done separately on the server (i.e. not from the yaml file).

https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/

http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
    duration := time.Now().Sub(started)
    if duration.Seconds() > 10 {
        w.WriteHeader(500)
        w.Write([]byte(fmt.Sprintf("error: %v", duration.Seconds())))
    } else {
        w.WriteHeader(200)
        w.Write([]byte("ok"))
    }
})

Hello @debu3645,

You can attach handlers to Container lifecycle events [postStart, preStop]. Kubernetes sends the postStart event immediately after a Container is started, and it sends the preStop event immediately before the Container is terminated.

Here is an example configuration file for the Pod:

apiVersion: v1
kind: Pod
metadata:
  name: lifecycle-demo
spec:
  containers:
  - name: lifecycle-demo-container
    image: nginx
    lifecycle:
      postStart:
        exec:
          command: ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]
      preStop:
        exec:
          command: ["/bin/sh","-c","nginx -s quit; while killall -0 nginx; do sleep 1; done"]

For more information:

Thank you @Ayman
I will have a try.