Configure container termination messages

更新时间:
复制 MD 格式

When a container exits, Kubernetes reads a termination message from a designated file and exposes it in the pod status. Set terminationMessagePath to control where the container writes the message, and set terminationMessagePolicy to control how Kubernetes retrieves the termination message. This lets you retrieve diagnostic information after a container terminates, including in crash scenarios where the container cannot write to the message file.

Parameters

ParameterDescriptionDefault
terminationMessagePathPath to the file where the container writes its termination message. Kubernetes reads this file when the container exits./dev/termination-log
terminationMessagePolicyControls how Kubernetes retrieves the termination message.File

`terminationMessagePolicy` values:

  • `File` — Kubernetes retrieves termination messages only from the termination message file.

  • `FallbackToLogsOnError` — Kubernetes reads the termination message from the file. If the file is empty and the container exited with an error, Kubernetes uses the last portion of the container log output as the termination message instead.

A single container's termination message cannot exceed 4 KB. The total size across all containers in a pod cannot exceed 12 KB. If the total exceeds 12 KB, Kubernetes divides the limit equally among all containers. For example, a pod with four init containers and eight application containers allows 1 KB per container.

Configure termination messages

Set a custom termination message path

Set terminationMessagePath to a path where your container process writes diagnostic output on exit. Kubernetes reads that file after the container terminates.

apiVersion: v1
kind: Pod
metadata:
  name: msg-path-demo
spec:
  containers:
  - name: msg-path-demo-container
    image: registry.cn-shanghai.aliyuncs.com/eci_open/nginx:1.14.2
    terminationMessagePath: "/tmp/termination-log"

Use log fallback on error

Set terminationMessagePolicy to FallbackToLogsOnError so that Kubernetes uses container log output as the termination message when the container exits with an error and the termination message file is empty. This is useful when a container crashes before it can write to the termination message file.

apiVersion: v1
kind: Pod
metadata:
  name: msg-path-demo
spec:
  containers:
  - name: msg-path-demo-container
    image: registry.cn-shanghai.aliyuncs.com/eci_open/nginx:1.14.2
    terminationMessagePath: "/tmp/termination-log"
    terminationMessagePolicy: "FallbackToLogsOnError"