Guarantee execution of preStop in Kubernetes Job

6/6/2018

I have a Job as follows:

apiVersion: batch/v1
kind: Job
metadata:
  name: abcd-ca-installer-root
  namespace: abcd-sbenv-test
spec:
  backoffLimit: 2
  activeDeadlineSeconds: 30
  template:
    metadata:
      name: abcd-ca-installer-root
    spec:
      restartPolicy: Never
      activeDeadlineSeconds: 30
      containers:
      - name: abcd-ca-installer-root
        image: abc-dol-tools:5000/abcd-ca-installer:func-tst
        lifecycle:
         preStop:
          exec:
            command: ["/bin/sh", "/usr/local/yezdi/clean.sh"]
        imagePullPolicy: Always
        env:
...

basically, I would like for the clean.sh to execute after this job is executed successuly. I have configured preStop in the above, But, it doesn't look like its executing the clean.sh any ideas why? or how can I know if this is even trying to run. I have seen this post but it didn'thelp. another related question

Other version info I am using is

# kubectl version
Client Version: version.Info{Major:"1", Minor:"10", GitVersion:"v1.10.0", GitCommit:"fc32d2f3698e36b93322a3465f63a14e9f0eaead", GitTreeState:"clean", BuildDate:"2018-03-26T16:55:54Z", GoVersion:"go1.9.3", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"10", GitVersion:"v1.10.0", GitCommit:"fc32d2f3698e36b93322a3465f63a14e9f0eaead", GitTreeState:"clean", BuildDate:"2018-03-26T16:44:10Z", GoVersion:"go1.9.3", Compiler:"gc", Platform:"linux/amd64"}

docker --version
Docker version 17.12.1-ce, build 7390fc6

kubeadm version
kubeadm version: &version.Info{Major:"1", Minor:"10", GitVersion:"v1.10.2", GitCommit:"81753b10df112992bf51bbc2c2f85208aad78335", GitTreeState:"clean", BuildDate:"2018-04-27T09:10:24Z", GoVersion:"go1.9.3", Compiler:"gc", Platform:"linux/amd64"}
-- sbolla
kubernetes

1 Answer

6/7/2018

Each container has its lifecycle. You can manage the termination process with Lifecycle Hooks.

I've looked at configuration you provided. PreStop doesn't work the way you expect it.

The PreStop hook is called immediately before a container is terminated not when a pod is gracefully finishing lifecycle. 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.

You can use a shell script or call http request at the time when PreStop hook is started.

I find Kubernetes Termination of Pods documentation useful for this case.

-- d0bry
Source: StackOverflow