【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