设置容器终止消息
更新时间:2024-09-25
设置容器终止消息
本文介绍如何设置BCI容器的terminationMessagePath和terminationMessagePolicy字段,实现自定义设置容器终止消息。
操作指南
在以下示例中,配置了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"
Pod内所有容器的终止信息大小之和最大为12KB。当总和超过12KB时,Kubernetes的状态管理器会对其加以限制,例如:Pod内有4个InitContainer和8个应用Container,则状态管理器会限制每个容器的终止信息最大为1KB,即截取每个Container终止信息的前1KB。