Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kubernetes Pod with Sleep command takes time to get deleted

Currently it takes quite a long time before the pod can be terminated after a kubectl delete command. I have the feeling that it could be because of the sleep command.

How can I make the container stop faster? What best practices should I use here?

apiVersion: apps/v1
kind: Deployment
...
spec:
  template:
    spec:
      containers:
        - image: alpine
          ..
          command:
            - /bin/sh
            - -c
            - |
              trap : TERM INT 
              while true; do
                # some code to check something
                sleep 10
              done

Is my approach with "trap: TERM INT" correct? At the moment I don't see any positive effect...

When I terminate the pod it takes several seconds for the command to come back.

kubectl delete pod my-pod
like image 212
user5580578 Avatar asked Dec 04 '25 04:12

user5580578


1 Answers

Add terminationGracePeriodSeconds to your spec will do:

...
spec:
  template:
    spec:
      terminationGracePeriodSeconds: 10  # <-- default is 30, can go as low as 0 to send SIGTERM immediately.
      containers:
        - image: alpine
like image 176
gohm'c Avatar answered Dec 07 '25 18:12

gohm'c