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

最佳答案

编辑:PreStop 钩子(Hook)对容器重启不起作用 - 请参阅下面 的其余答案

站在 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 钩子(Hook),结论是: 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 崩溃/结束后执行

关于Kubernetes |任何可用于 Pod 重启的钩子(Hook)?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56391316/

10-11 07:13