【K8s任务】确定 Pod 失败的原因


参考:https://kubernetes.io/zh/docs/tasks/debug-application-cluster/determine-reason-pod-failure/

读写终止消息

显示 Pod 的详细信息:

kubectl get pod --output=yaml

输出结果包含 "Sleep expired" 消息:

apiVersion: v1
kind: Pod
...
    lastState:
      terminated:
        containerID: ...
        exitCode: 0
        finishedAt: ...
        message: |
                    Sleep expired
        ...

使用 Go 模板过滤输出结果,使其只含有终止消息:

kubectl get pod termination-demo -o go-template="{{range .status.containerStatuses}}{{.lastState.terminated.message}}{{end}}"

定制终止消息

Kubernetes 从容器的 terminationMessagePath 字段中指定的终止消息文件中检索终止消息, 默认值为 /dev/termination-log。 通过定制这个字段,您可以告诉 Kubernetes 使用不同的文件。 Kubernetes 使用指定文件中的内容在成功和失败时填充容器的状态消息。

在下例中,容器将终止消息写入 /tmp/my-log 给 Kubernetes 来接收:

apiVersion: v1
kind: Pod
metadata:
  name: msg-path-demo
spec:
  containers:
  - name: msg-path-demo-container
    image: debian
    terminationMessagePath: "/tmp/my-log"

此外,用户可以设置容器的 terminationMessagePolicy 字段,以便进一步自定义。 此字段默认为 "File",这意味着仅从终止消息文件中检索终止消息。 通过将 terminationMessagePolicy 设置为 "FallbackToLogsOnError",你就可以告诉 Kubernetes,在容器因错误退出时,如果终止消息文件为空,则使用容器日志输出的最后一块作为终止消息。 日志输出限制为 2048 字节或 80 行,以较小者为准。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM