0
votes

I have a deployment template as below. but the prestop hook is never been executed at all. the idea here is set the zookeeper node offline before the pod is terminated.

I am running kubectl rollout to restart the pods. and old pod is when it terminates the prestop is not run. could someone please check whats wrong ?

Basically how its prestop executed in case of successful stop ? I need this feature because the zookeeper is involved here and the api connects to zookeeper to send the requests.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: abcd
  labels:
    app: abcd
spec:
  replicas: 1
  selector:
    matchLabels:
      app: abcd
  template:
    metadata:
      labels:
        app: abcd
    spec:
      hostNetwork: true
      dnsPolicy: ClusterFirstWithHostNet
#      terminationGracePeriodSeconds: 1
      containers:
      - name: se
        image: "xxx"
        lifecycle:
          preStop:
            exec:
              command: ["zookeepercli","--servers","zk-hs", "-c", "set", "$HOSTNAME", "offline"]
        ports:
        - containerPort: 2345
      - name: pe-1
        image: "xxx"
        lifecycle:
          preStop:
            exec:
              command: ["zookeepercli","--servers","zk-hs", "-c", "set", "$HOSTNAME", "offline"]
        ports:
        - containerPort: 2313
1
could please share the logs at termination? @user2511126 - hariK
I looked at the kubectl describe pod but it just shows (Reason: Killing) Message: stopping se and stopping pe-1 - user2511126
Hi, what k8s version do You use? - Piotr Malec
Client Version: version.Info{Major:"1", Minor:"15", GitVersion:"v1.15.3", GitCommit:"2d3c76f9091b6bec110a5e63777c332469e0cba2", GitTreeState:"clean", BuildDate:"2019-08-19T11:13:54Z", GoVersion:"go1.12.9", Compiler:"gc", Platform:"linux/amd64"} Server Version: version.Info{Major:"1", Minor:"17", GitVersion:"v1.17.4", GitCommit:"8d8aa39598534325ad77120c120a22b3a990b5ea", GitTreeState:"clean", BuildDate:"2020-03-12T20:55:23Z", GoVersion:"go1.13.8", Compiler:"gc", Platform:"linux/amd64"} - user2511126
the preStop hook doesnt uses env variables. I moved to bash script and works now - user2511126

1 Answers

0
votes

As user2511126 mentioned in his/her comment:

the preStop hook doesnt uses env variables. I moved to bash script and works now

According to kubernetes documentation:

PreStop

This hook is called immediately before a container is terminated due to an API request or management event such as liveness probe failure, preemption, resource contention and others. A call to the preStop hook fails if the container is already in terminated or completed state. It is blocking, meaning it is synchronous, so it must complete before the call to delete the container can be sent. No parameters are passed to the handler.

A more detailed description of the termination behavior can be found in Termination of Pods.

No parameters can be passed to the handler, this includes environmental variables.