库伯内斯 |任何可用于 Pod 重启的挂钩?

Kubernetes | Any hooks available for Pod restarts?

是否有任何可用于 Pod 生命周期事件的挂钩?具体来说,我想 运行 一个命令来在 pod 重启时上传日志。

编辑:PreStop 挂钩不适用于容器重启 - 请参阅下面的其余答案

documentation 中有 PreStopPostStart 事件,您可以附加到它们。

来自文档的示例:

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","-c","nginx -s quit; while killall -0 nginx; do sleep 1; done"]

编辑: 因此,我检查了以下 POC 是否在容器崩溃时执行了 preStop 挂钩,结论是:NOT

apiVersion: v1
kind: Pod
metadata:
  name: lifecycle-demo
spec:
  containers:
  - name: lifecycle-demo-container
    volumeMounts:
    - mountPath: /data
      name: test-volume
    image: nginx
    command: ["/bin/sh"]
    args: ["-c", "sleep 5; exit 1"]
    lifecycle:
      postStart:
        exec:
          command: ["/bin/sh", "-c", "echo Hello from the postStart handler > /data/postStart"]
      preStop:
        exec:
          command: ["/bin/sh","-c","echo preStop handler! > /data/preStop"]

  volumes:
  - name: test-volume
    hostPath:
      path: /data
      type: Directory

作为您的解决方案,我建议以这种方式为您的容器覆盖命令部分:

command: ["/bin/sh"]
args: ["-c", "your-application-executable; your-logs-upload"]

所以 your-logs-upload 可执行文件将在 your-application-executable crash/end

之后执行