pod的探针顺序下的生命周期

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值