设置容器终止消息
更新时间: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接收。
Plain Text
1apiVersion: v1
2kind: Pod
3metadata:
4 annotations:
5 myannotation: "myannotation"
6 labels:
7 app: bci-test-vk
8 mylabel: "mylabel"
9 name: termination-test
10 namespace: default
11spec:
12 nodeSelector:
13 type: virtual-kubelet
14 tolerations:
15 - effect: NoSchedule
16 key: virtual-kubelet.io/provider
17 operator: Equal
18 value: baidu
19 - effect: NoExecute
20 key: node.kubernetes.io/not-ready
21 operator: Exists
22 tolerationSeconds: 300
23 - effect: NoExecute
24 key: node.kubernetes.io/unreachable
25 operator: Exists
26 tolerationSeconds: 300
27 containers:
28 - image: hub.baidubce.com/cce/nginx-alpine-go
29 imagePullPolicy: IfNotPresent
30 name: c01
31 workingDir: /work
32 ports:
33 - containerPort: 8080
34 protocol: TCP
35 resources:
36 limits:
37 cpu: 250m
38 memory: 512Mi
39 requests:
40 cpu: 250m
41 memory: 512Mi
42 terminationMessagePath: "/tmp/termination-log"
此外,您还可以设置容器的terminationMessagePolicy字段,进一步自定义容器终止消息。该字段默认值为:File,即仅从终止消息文件中检索终止消息。您可以根据需要设置为:FallbackToLogsOnError,即在容器因错误退出时,如果终止消息文件为空,则使用容器日志输出的最后一部分内容来作为终止消息。
Plain Text
1apiVersion: v1
2kind: Pod
3metadata:
4 annotations:
5 myannotation: "myannotation"
6 labels:
7 app: bci-test-vk
8 mylabel: "mylabel"
9 name: volume-test
10 namespace: default
11spec:
12 nodeSelector:
13 type: virtual-kubelet
14 tolerations:
15 - effect: NoSchedule
16 key: virtual-kubelet.io/provider
17 operator: Equal
18 value: baidu
19 - effect: NoExecute
20 key: node.kubernetes.io/not-ready
21 operator: Exists
22 tolerationSeconds: 300
23 - effect: NoExecute
24 key: node.kubernetes.io/unreachable
25 operator: Exists
26 tolerationSeconds: 300
27 containers:
28 - image: hub.baidubce.com/cce/nginx-alpine-go
29 imagePullPolicy: IfNotPresent
30 name: c01
31 workingDir: /work
32 ports:
33 - containerPort: 8080
34 protocol: TCP
35 resources:
36 limits:
37 cpu: 250m
38 memory: 512Mi
39 requests:
40 cpu: 250m
41 memory: 512Mi
42 terminationMessagePath: "/tmp/termination-log"
43 terminationMessagePolicy: "FallbackToLogsOnError"