我可以在硬驱逐之前使用 prestop 钩子优雅地终止进程吗?

问题描述

我的应用程序在收到 SIGTERM 时正常停止。优雅停止操作是删除 ZooKeeper 中的一个 znode。

但我尝试的是在 Kubernetes 驱逐我的 pod 之前优雅地自动终止进程。

我想将 prestop 钩子设置为“kill 1”,以便在 pod 被驱逐之前优雅地停止。

我不期望 prestop hook 在所有情况下都有效,但我认为,这样我会有一些保证。

不知道我的想法是否可行。

解决方法

是的 preStop 是可能的 - 并且涵盖您的用例。下面是 k8s docs 示例:

apiVersion: v1
kind: Pod
metadata:
  name: lifecycle-demo
spec:
  containers:
  - name: lifecycle-demo-container
    image: nginx
    lifecycle:
      postStart:
        exec:
          command: ["/bin/sh","-c","echo Hello from the postStart handler > /usr/share/message"]
      preStop:
        exec:
          command: ["/bin/sh","nginx -s quit; while killall -0 nginx; do sleep 1; done"]