Kubernetes | 是否有任何可用于Pod重新启动的钩子?

Moh*_*pta 4 kubernetes google-kubernetes-engine kubectl kubernetes-pod

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

Jak*_*jny 5

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

站在文档中时,有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"]
Run Code Online (Sandbox Code Playgroud)

编辑:所以我检查以下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
Run Code Online (Sandbox Code Playgroud)

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

command: ["/bin/sh"]
args: ["-c", "your-application-executable; your-logs-upload"]
Run Code Online (Sandbox Code Playgroud)

因此,您的日志上传可执行文件将在您的应用程序可执行崩溃/结束后执行

  • @MohitGupta如果您可以测试一下并通过接受答案来确认是否可以解决您的问题,我将不胜感激 (2认同)