2016-11-18 01:07:34 +00:00
|
|
|
---
|
2017-06-08 20:41:57 +00:00
|
|
|
title: Determine the Reason for Pod Failure
|
2018-05-05 16:00:51 +00:00
|
|
|
content_template: templates/task
|
2016-11-18 01:07:34 +00:00
|
|
|
---
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% capture overview %}}
|
2016-11-18 01:07:34 +00:00
|
|
|
|
|
|
|
This page shows how to write and read a Container
|
|
|
|
termination message.
|
|
|
|
|
|
|
|
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
|
2017-04-14 23:33:05 +00:00
|
|
|
[Kubernetes logs](/docs/concepts/cluster-administration/logging/).
|
2016-11-18 01:07:34 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% /capture %}}
|
2016-11-18 01:07:34 +00:00
|
|
|
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% capture prerequisites %}}
|
2016-11-18 01:07:34 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
|
2016-11-18 01:07:34 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% /capture %}}
|
2016-11-18 01:07:34 +00:00
|
|
|
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% capture steps %}}
|
2016-11-18 01:07:34 +00:00
|
|
|
|
2017-01-18 18:18:37 +00:00
|
|
|
## Writing and reading a termination message
|
2016-11-18 01:07:34 +00:00
|
|
|
|
|
|
|
In this exercise, you create a Pod that runs one container.
|
|
|
|
The configuration file specifies a command that runs when
|
|
|
|
the container starts.
|
|
|
|
|
2018-07-02 18:17:19 +00:00
|
|
|
{{< codenew file="debug/termination.yaml" >}}
|
2016-11-18 01:07:34 +00:00
|
|
|
|
|
|
|
1. Create a Pod based on the YAML configuration file:
|
|
|
|
|
2018-07-02 18:17:19 +00:00
|
|
|
kubectl create -f https://k8s.io/examples/debug/termination.yaml
|
2016-11-18 01:07:34 +00:00
|
|
|
|
|
|
|
In the YAML file, in the `cmd` and `args` fields, you can see that the
|
|
|
|
container sleeps for 10 seconds and then writes "Sleep expired" to
|
|
|
|
the `/dev/termination-log` file. After the container writes
|
|
|
|
the "Sleep expired" message, it terminates.
|
|
|
|
|
|
|
|
1. Display information about the Pod:
|
|
|
|
|
2018-06-11 23:43:27 +00:00
|
|
|
kubectl get pod termination-demo
|
2016-11-18 01:07:34 +00:00
|
|
|
|
|
|
|
Repeat the preceding command until the Pod is no longer running.
|
|
|
|
|
|
|
|
1. Display detailed information about the Pod:
|
|
|
|
|
2018-06-11 23:43:27 +00:00
|
|
|
kubectl get pod --output=yaml
|
2016-11-18 01:07:34 +00:00
|
|
|
|
|
|
|
The output includes the "Sleep expired" message:
|
|
|
|
|
|
|
|
apiVersion: v1
|
|
|
|
kind: Pod
|
|
|
|
...
|
|
|
|
lastState:
|
|
|
|
terminated:
|
|
|
|
containerID: ...
|
|
|
|
exitCode: 0
|
|
|
|
finishedAt: ...
|
|
|
|
message: |
|
|
|
|
Sleep expired
|
|
|
|
...
|
|
|
|
|
|
|
|
1. Use a Go template to filter the output so that it includes
|
|
|
|
only the termination message:
|
2016-12-05 23:32:19 +00:00
|
|
|
|
2018-06-11 23:43:27 +00:00
|
|
|
kubectl get pod termination-demo -o go-template="{{range .status.containerStatuses}}{{.lastState.terminated.message}}{{end}}"
|
2016-11-18 01:07:34 +00:00
|
|
|
|
2017-11-13 03:34:02 +00:00
|
|
|
## Customizing the termination message
|
|
|
|
|
|
|
|
Kubernetes retrieves termination messages from the termination message file
|
|
|
|
specified in the `terminationMessagePath` field of a Container, which as 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.
|
|
|
|
|
|
|
|
In the following example, the container writes termination messages to
|
|
|
|
`/tmp/my-log` for Kubernetes to retrieve:
|
|
|
|
|
|
|
|
```yaml
|
|
|
|
apiVersion: v1
|
|
|
|
kind: Pod
|
|
|
|
metadata:
|
|
|
|
name: msg-path-demo
|
|
|
|
spec:
|
|
|
|
containers:
|
|
|
|
- name: msg-path-demo-container
|
|
|
|
image: debian
|
|
|
|
terminationMessagePath: "/tmp/my-log"
|
|
|
|
```
|
|
|
|
|
|
|
|
Moreover, users can set the `terminationMessagePolicy` field of a Container for
|
|
|
|
further customization. This field defaults to "`File`" which means the termination
|
|
|
|
messages are retrieved only from the termination message file. By setting the
|
|
|
|
`terminationMessagePolicy` to "`FallbackToLogsOnError`", you can tell Kubernetes
|
|
|
|
to use the last chunk of container log output if the termination message file
|
|
|
|
is empty and the container exited with an error. The log output is limited to
|
|
|
|
2048 bytes or 80 lines, whichever is smaller.
|
2016-11-18 01:07:34 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% /capture %}}
|
2016-11-18 01:07:34 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% capture whatsnext %}}
|
2016-11-18 01:07:34 +00:00
|
|
|
|
2016-12-05 23:32:19 +00:00
|
|
|
* See the `terminationMessagePath` field in
|
2018-05-05 16:00:51 +00:00
|
|
|
[Container](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#container-v1-core).
|
2017-03-23 03:01:18 +00:00
|
|
|
* Learn about [retrieving logs](/docs/concepts/cluster-administration/logging/).
|
2016-11-18 01:07:34 +00:00
|
|
|
* Learn about [Go templates](https://golang.org/pkg/text/template/).
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% /capture %}}
|
|
|
|
|
2016-11-18 01:07:34 +00:00
|
|
|
|
|
|
|
|