Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to monitor executing of `preStop` command?

Tags:

kubernetes

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" ]
like image 283
tse Avatar asked Oct 02 '17 12:10

tse


2 Answers

I was looking for something, so I added some logging that help to see the logs of the script in the pod's stdout/stderr logs.

so for me, this approach help me

  • To write logs to the centralized logging system (help me to check logs in datadog)
  • Verify the script is executed properly
          lifecycle:
            preStop:
              exec:
                command: ["/bin/sh", "-c", "/clean.sh > /proc/1/fd/1"]          

and was able to verify the logs

kubectl get pods

kubectl logs -f my_stohook_pod

/proc/PID/fd/1 will help us to redirect script logs stdout/stderr of the container main process.

like image 72
Adiii Avatar answered Nov 13 '22 13:11

Adiii


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
like image 20
David Thomas Avatar answered Nov 13 '22 14:11

David Thomas