concept pre - stop hook in category kubernetes

This is an excerpt from Manning's book Kubernetes in Action, Second Edition.
When you run Nginx in a Kubernetes pod, you can use a pre-stop lifecycle hook to run this command and ensure that the pod shuts down gracefully. The following listing shows the definition of this pre-stop hook. You can find it in the fortune-prestop.yaml pod manifest.
Listing 6.19 Defining a pre-stop hook for Nginx lifecycle: #A preStop: #A exec: #B command: #B - nginx #C - -s #C - quit #CWhenever a container using this pre-stop hook is terminated, the command nginx -s quit is executed in the container before the main process of the container receives the TERM signal.
Note that the same problem occurs if you use a shell script in your container to run the application. In this case, you must either intercept and pass signals to the application or use the exec shell command to run the application in your script.

This is an excerpt from Manning's book Kubernetes in Action.
Configuring a pre-stop hook in a pod manifest isn’t very different from adding a post-start hook. The previous example showed a post-start hook that executes a command, so we’ll look at a pre-stop hook that performs an HTTP GET request now. The following listing shows how to define a pre-stop HTTP GET hook in a pod.
Listing 17.6. A pre-stop hook YAML snippet: pre-stop-hook-httpget.yaml
lifecycle: preStop: #1 httpGet: #1 port: 8080 #2 path: shutdown #2The pre-stop hook defined in this listing performs an HTTP GET request to http://POD_IP:8080/shutdown as soon as the Kubelet starts terminating the container. Apart from the port and path shown in the listing, you can also set the fields scheme (HTTP or HTTPS) and host, as well as httpHeaders that should be sent in the request. The host field defaults to the pod IP. Be sure not to set it to localhost, because localhost would refer to the node, not the pod.
In contrast to the post-start hook, the container will be terminated regardless of the result of the hook—an error HTTP response code or a non-zero exit code when using a command-based hook will not prevent the container from being terminated. If the pre-stop hook fails, you’ll see a FailedPreStopHook warning event among the pod’s events, but because the pod is deleted soon afterward (after all, the pod’s deletion is what triggered the pre-stop hook in the first place), you may not even notice that the pre-stop hook failed to run properly.