Home > Enterprise >  Do Kubernetes PreStop hooks prevent ALL containers in a pod from stopping, or only the container it&
Do Kubernetes PreStop hooks prevent ALL containers in a pod from stopping, or only the container it&

Time:11-21

The Kubernetes documentation about PreStop hooks claims the following:

PreStop hooks are not executed asynchronously from the signal to stop the Container; the hook must complete its execution before the TERM signal can be sent.

However, it does not say anything about other containers in the pod. Suppose terminationGracePeriodSeconds has not yet passed.

Are all containers in a pod protected from termination until the PreStop hook for each container finishes? Or does each PreStop hook only protect its own container?

CodePudding user response:

I believe preStop hook only protects the container for which it's declared.

For example, in the following set up:

apiVersion: v1
kind: Pod
metadata:
  name: lifecycle-demo
spec:
  containers:
  - name: lifecycle-demo-container
    image: nginx
    lifecycle:
      preStop:
        exec:
          command: ["/bin/sh","-c","sleep 15"]
  - name: other-container
    image: mysql
    env:
    - name: MYSQL_ALLOW_EMPTY_PASSWORD
      value: "true"

If I terminate the pod, the mysql receives SIGTERM and shuts down immediately while the nginx container stays alive for extra 15 seconds due to its preStop hook

  • Related