postStart  and  preStop  hook invocation mechanisms are similar to the  Health Probes described in Chapter 4 and support these handler types: exec Runs a command directly in the container httpGet Executes an HTTP GET request against a port opened by one Pod container You have to be very careful what critical logic you execute in the  postStart  hook as there are no guarantees for its execution. Since the hook is running in parallel with the container process, it is possible that the hook may be executed before the con‐ tainer has started. Also, the hook is intended to have at-least once semantics, so the implementation has to take care of duplicate executions. Another aspect to keep in mind is that the platform does not perform any retry attempts on failed HTTP requests that didn’t reach the handler. The  preStop  hook is a blocking call sent to a container before it is terminated. It has the same semantics as the SIGTERM signal and should be used to initiate a graceful shutdown of the container when reacting to SIGTERM is not possible. The  preStop action in Example 5-2 must complete before the call to delete the container is sent to the container runtime, which triggers the SIGTERM notification. Call out to a  /shutdown  endpoint running within the application Even though  preStop  is blocking, holding on it or returning a nonsuccessful result does not prevent the container from being deleted and the process killed.  preStop  is only a convenient alternative to a SIGTERM signal for graceful application shutdown 44  |  Chapter 5: Managed Lifecycle Example 5-2. A container with a preStop hook Prestop Hook apiVersion : v1 kind : Pod metadata : name : pre-stop-hook spec : containers : -  image : k8spatterns/random-generator:1.0 name : random-generator lifecycle : preStop : httpGet : port : 8080 path : /shutdown