设置容器终止消息
更新时间:2025-02-13
设置容器终止消息
在Kubernetes中,terminationMessagePath和terminationMessagePolicy用于指定容器终止消息的来源路径和策略。本文介绍如何设置BCI容器的terminationMessagePath和terminationMessagePolicy字段,实现自定义设置容器终止消息。
配置说明
Kubernetes通过terminationMessagePath和terminationMessagePolicy管理容器终止消息。
字段 | 说明 |
---|---|
terminationMessagePath | 用于设置容器终止的消息来源。即当容器退出时,Kubernetes 从容器的terminationMessagePath字段中指定的终止消息文件中检索终止消息。默认值为 /dev/termination-log。 通过自定义设置terminationMessagePath,可以使得Kubernetes在容器运行成功或失败时,使用指定文件中的内容来填充容器的终止消息。终止消息内容最大为4 KB。 |
terminationMessagePolicy | 用于设置容器终止消息的策略。取值为:
|
说明:
Pod内所有容器的终止信息大小之和最大为12 KB。当总和超过12 KB时,Kubernetes的状态管理器会对其加以限制。例如:Pod内有4个InitContainer和8个应用Container,则状态管理器会限制每个容器的终止信息最大为1 KB,即截取每个Container终止信息的前1 KB。
操作指南
在以下示例中,配置了terminationMessagePath字段为:/tmp/termination-log,则容器将把终止消息写入/tmp/termination-log给Kubernetes接收。
apiVersion: v1
kind: Pod
metadata:
annotations:
myannotation: "myannotation"
labels:
app: bci-test-vk
mylabel: "mylabel"
name: termination-test
namespace: default
spec:
nodeSelector:
type: virtual-kubelet
tolerations:
- effect: NoSchedule
key: virtual-kubelet.io/provider
operator: Equal
value: baidu
- effect: NoExecute
key: node.kubernetes.io/not-ready
operator: Exists
tolerationSeconds: 300
- effect: NoExecute
key: node.kubernetes.io/unreachable
operator: Exists
tolerationSeconds: 300
containers:
- image: hub.baidubce.com/cce/nginx-alpine-go
imagePullPolicy: IfNotPresent
name: c01
workingDir: /work
ports:
- containerPort: 8080
protocol: TCP
resources:
limits:
cpu: 250m
memory: 512Mi
requests:
cpu: 250m
memory: 512Mi
terminationMessagePath: "/tmp/termination-log"
此外,您还可以设置容器的terminationMessagePolicy字段,进一步自定义容器终止消息。该字段默认值为:File,即仅从终止消息文件中检索终止消息。您可以根据需要设置为:FallbackToLogsOnError,即在容器因错误退出时,如果终止消息文件为空,则使用容器日志输出的最后一部分内容来作为终止消息。
apiVersion: v1
kind: Pod
metadata:
annotations:
myannotation: "myannotation"
labels:
app: bci-test-vk
mylabel: "mylabel"
name: volume-test
namespace: default
spec:
nodeSelector:
type: virtual-kubelet
tolerations:
- effect: NoSchedule
key: virtual-kubelet.io/provider
operator: Equal
value: baidu
- effect: NoExecute
key: node.kubernetes.io/not-ready
operator: Exists
tolerationSeconds: 300
- effect: NoExecute
key: node.kubernetes.io/unreachable
operator: Exists
tolerationSeconds: 300
containers:
- image: hub.baidubce.com/cce/nginx-alpine-go
imagePullPolicy: IfNotPresent
name: c01
workingDir: /work
ports:
- containerPort: 8080
protocol: TCP
resources:
limits:
cpu: 250m
memory: 512Mi
requests:
cpu: 250m
memory: 512Mi
terminationMessagePath: "/tmp/termination-log"
terminationMessagePolicy: "FallbackToLogsOnError"