k8s版本:1.25
pod的生命周期
验证上述结果的测试文件如下:
apiVersion: v1
kind: Pod
metadata:
labels:
test: liveness
name: test-probe
namespace: test-cronjob
spec:
initContainers:
- name: init-myservice
image: busybox:1.28
command: ['sh', '-c', 'echo this is initContainers']
containers:
- name: test-probe
image: busybox:1.28
args:
- /bin/sh
- -c
- sleep 100
lifecycle:
postStart:
exec:
command: ["sh", "-c", "echo this is poststart && sleep 2 && echo this is poststart"]
preStop:
exec:
command: ["sh","-c","echo this is prestop && sleep 2 && echo this is prestop"]
readinessProbe:
exec:
command: ['sh', '-c', 'echo this is readinessProbe && sleep 2 && echo this is readinessProbe']
initialDelaySeconds: 2
periodSeconds: 2
livenessProbe:
exec:
command: ['sh', '-c', 'echo this is livenessProbe && sleep 2 && echo this is livenessProbe']
initialDelaySeconds: 2
periodSeconds: 2
startupProbe:
exec:
command: ['sh', '-c', 'echo this is startupProbe && sleep 2 && echo this is startupProbe']
initialDelaySeconds: 2
periodSeconds: 2
启动后StartupProbe探针报错如下:
Startup probe errored: rpc error: code = Unknown desc = deadline exceeded ("DeadlineExceeded"): context deadline exceeded
经测试startupProbe、readinessProbe、livenessProbe这三个探针并不能使用上面格式执行命令探测。并且除了Init容器的echo能输出到前台日志,其他echo命令并不会输出到前台日志,修改后文件如下:
apiVersion: v1
kind: Pod
metadata:
labels:
test: liveness
name: test-probe
namespace: test-cronjob
spec:
initContainers:
- name: init-myservice
image: busybox:1.28
command: ['sh', '-c', 'echo "this is initContainers" >> /root/test.txt']
containers:
- name: test-probe
image: busybox:1.28
args:
- /bin/sh
- -c
- sleep 500
lifecycle:
postStart:
exec:
command: ["sh", "-c", "echo 'this is poststart' >> /root/test.txt"]
preStop:
exec:
command: ["sh","-c","echo 'this is prestop' >> /root/test.txt"]
readinessProbe:
exec:
command:
- /bin/sh
- -c
- echo 'this is readinessProbe' >> /root/test.txt
initialDelaySeconds: 2
periodSeconds: 2
livenessProbe:
exec:
command:
- /bin/sh
- -c
- echo 'this is livenessProbe' >> /root/test.txt
initialDelaySeconds: 2
periodSeconds: 2
startupProbe:
exec:
command:
- /bin/sh
- -c
- echo 'this is startupProbe' >> /root/test.txt
initialDelaySeconds: 2
periodSeconds: 2
查看日志如下。
root@k8s-master01:~/xyh# kubectl exec -it test-probe -n test-cronjob -- cat /root/test.txt
Defaulted container "test-probe" out of: test-probe, init-myservice (init)
this is poststart
this is startupProbe
this is readinessProbe
this is readinessProbe
this is livenessProbe
this is readinessProbe
this is livenessProbe
this is livenessProbe
this is readinessProbe
this is readinessProbe
this is livenessProbe
this is readinessProbe
.....
中间另开终端手动删除这个pod,查看文件变化情况
.....
this is readinessProbe
this is livenessProbe
this is readinessProbe
this is readinessProbe
this is livenessProbe
this is livenessProbe
this is readinessProbe
this is prestop
this is readinessProbe
this is readinessProbe
this is readinessProbe
this is readinessProbe
this is readinessProbe
this is readinessProbe
this is readinessProbe
this is readinessProbe
command terminated with exit code 137