Determine the Reason for Pod Failure

    Termination messages provide a way for containers to write information about fatal events to a location where it can be easily retrieved and surfaced by tools like dashboards and monitoring software. In most cases, information that you put in a termination message should also be written to the general Kubernetes logs.

    You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. It is recommended to run this tutorial on a cluster with at least two nodes that are not acting as control plane hosts. If you do not already have a cluster, you can create one by using minikube or you can use one of these Kubernetes playgrounds:

    In this exercise, you create a Pod that runs one container. The manifest for that Pod specifies a command that runs when the container starts:

    debug/termination.yaml

    1. Create a Pod based on the YAML configuration file:

      1. Display information about the Pod:

        Repeat the preceding command until the Pod is no longer running.

      2. Display detailed information about the Pod:

        1. kubectl get pod termination-demo --output=yaml

        The output includes the “Sleep expired” message:

      3. Use a Go template to filter the output so that it includes only the termination message:

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

      Kubernetes retrieves termination messages from the termination message file specified in the terminationMessagePath field of a Container, which has a default value of /dev/termination-log. By customizing this field, you can tell Kubernetes to use a different file. Kubernetes use the contents from the specified file to populate the Container’s status message on both success and failure.

      The termination message is intended to be brief final status, such as an assertion failure message. The kubelet truncates messages that are longer than 4096 bytes.

      The total message length across all containers is limited to 12KiB, divided equally among each container. For example, if there are 12 containers ( or containers), each has 1024 bytes of available termination message space.

      The default termination message path is /dev/termination-log. You cannot set the termination message path after a Pod is launched.

      In the following example, the container writes termination messages to /tmp/my-log for Kubernetes to retrieve:

      1. apiVersion: v1
      2. kind: Pod
      3. name: msg-path-demo
      4. containers:
      5. - name: msg-path-demo-container
      6. image: debian
      7. terminationMessagePath: "/tmp/my-log"
      • See the terminationMessagePath field in .
      • Learn about Go templates.