Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do it create a shutdown sequence for containers in a pod?

when a SIGTERM is received from k8s I want my sidecar to die only after the main container is finished. How do I create a dependency chain for shutdown routine?

I can use a preStop and wait for say 120 secs, but it is going to a constant 120 secs for the pods to be cleaned up.

I am wondering if there is a way for my side car to check if my main container was killed. Or is it possible for the main container to signal my side car when it has finished its clean up (through k8s instead of a code change in my main container.

like image 746
RandomQuests Avatar asked Oct 19 '25 19:10

RandomQuests


1 Answers

In your preStop hook, check if your main container is still running (instead of just waiting a fixed amount of time.

If the main container has an existing healthprobe (for example an http endpoint) you can call that from within the preStop hook.

Another option is to edit the entrypoint script in your main container to include a trap command that creates a file on shutdown. The preStop hook of your sidecar to wait for this file be present.

trap "touch /lifecycle/main-pod-terminated" SIGTERM

If using the preStop hook, just keep the grace period countdown in mind and increase the default (30 seconds) if required by setting an appropriate value of terminationGracePeriodSeconds

Pod's termination grace period countdown begins before the preStop hook is executed, so regardless of the outcome of the handler, the container will eventually terminate within the Pod's termination grace period

like image 121
danielorn Avatar answered Oct 21 '25 10:10

danielorn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!