我有以下python
代码:
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")
我创建了一个包含上面Python代码的映像的部署。
当我使用kubectl delete -f sig.yaml
删除部署时,我的Python代码不会创建sig文件指示,也不会打印“已完成”消息。
在这个链接:https://cloud.google.com/blog/products/containers-kubernetes/kubernetes-best-practices-terminating-with-grace,我看到k8s发送SIGINT,SIGTERM信号,但是在我的应用程序中什么也没有发生。
如何使k8s向我的应用程序发送信号?我做错了什么?
发布于 2021-03-09 13:50:09
问题在于Python代码。您需要调整类参数并使用Stopper.stop = True
(带有大写的T
)。
以下代码与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")
https://stackoverflow.com/questions/66547469
复制相似问题