2

I have the following python code:

import time
import os
import signal
from abc import abstractmethod

class Stopper:
    stop = False
    
    @staticmethod
    def safe_stop(*args):
        Stopper.stop = true


signal.signal(signal.SIGINT, Stopper.safe_stop)
signal.signal(signal.SIGTERM, Stopper.safe_stop)

while not Stopper.stop:
    print("Running...")
    time.sleep(1)

os.system("touch /mnt/pod/sig")
print("Done")

And I create a deployment that contains an image with the above Python code.

When I delete the deployment using kubectl delete -f sig.yaml my Python code does not create the sig file indication and it's not printing the "Done" message.

At this link: https://cloud.google.com/blog/products/containers-kubernetes/kubernetes-best-practices-terminating-with-grace I see that k8s send SIGINT, SIGTERM signals but nothing happens in my application.

How can I make k8s send the signal to my application? What am I doing wrong?

Mar Teen
  • 87
  • 1
  • 11

1 Answers1

0

The problem is in your Python code. You need to adjust the class arguments and use Stopper.stop = True (with capitalized T).

The following code worked with Python 3:

import time
import os
import signal
from abc import abstractmethod

class Stopper:
    stop = False
    
    @abstractmethod
    def safe_stop(self, args):
        Stopper.stop = True


signal.signal(signal.SIGINT, Stopper.safe_stop)
signal.signal(signal.SIGTERM, Stopper.safe_stop)

while not Stopper.stop:
    print("Running...")
    time.sleep(1)

os.system("touch /mnt/pod/sig")
print("Done")
Eduardo Baitello
  • 10,469
  • 7
  • 46
  • 74