How to monitor executing of `preStop` command?

10/2/2017

I'm trying to use pod's lifecycle event. Problem is that command from preStop doesn't run at all. Is there any way to monitor if it was started? Log of the container is empty.

      lifecycle:
        preStop:
          exec:
            command: [ "/bin/sh", "-c", "/clean.sh" ]
-- tse
kubernetes

2 Answers

10/2/2017

From https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/#debugging-hook-handlers

The logs for a Hook handler are not exposed in Pod events. If a handler fails for some reason, it broadcasts an event. [...] For PreStop, this is the FailedPreStopHook event. You can see these events by running kubectl describe pod <pod_name>. Here is some example output of events from running this command [...]

-- Janos Lenart
Source: StackOverflow

5/24/2020

I just want to add for the preStop hook, the pod may be terminated and not available to describe.

Another way to see the preStop error log is via kubectl events:

kubectl get events | grep FailedPreStopHook

Example:

kubectl get events | grep FailedPreStopHook                                    
5m33s       Warning   FailedPreStopHook   pod/pod-name-59988c4675-79q4p                              
Exec lifecycle hook ([/bin/kill -s SIGQUIT 1]) for Container "container_name" in Pod "pod-name-59988c4675-79q4p_namespace(556dc3d2-9da4-11ea-bca3-00163e01eb9a)" failed - error: 
command '/bin/kill -s SIGQUIT 1' exited with 1: kill: can't kill pid 1: Operation not permitted
-- David Thomas
Source: StackOverflow