gpt4 book ai didi

Kubernetes |任何可用于 Pod 重启的钩子(Hook)?

转载 作者:行者123 更新时间:2023-12-03 16:28:15 26 4
gpt4 key购买 nike

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

最佳答案

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

如站在 documentationPreStopPostStart事件,您可以附加到它们。

来自文档的示例:

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),结论是: 不是
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 后执行

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com