pod资源的健康检查-liveness探针的exec使用

2022-11-06,,,,

使用探针的方式对pod资源健康检查

探针的种类

livenessProbe:健康状态检查,周期性检查服务是否存活,检查结果失败,将重启容器

readinessProbe:可用性检查,周期性检查服务是否可用,不可用将从service的endpoints中移除

探针的检测方法

* exec:执行一段命令

* httpGet:检测某个 http 请求的返回状态码

* tcpSocket:测试某个端口是否能够连接

liveness探针的exec使用

#cat nginx_pod_exec.yaml

apiVersion: v1
kind: Pod
metadata:
  name: exec2
  labels:
        app: my-dep5
spec:
  containers:
    - name: nginx
      image: centos-nginx:1.23.0
      imagePullPolicy: Never
      ports:
        - containerPort: 80
      args:
        - /bin/sh
        - -c
        - touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600
      livenessProbe:
        exec:
          command:
            - cat
            - /tmp/healthy
        initialDelaySeconds: 5
        periodSeconds: 5
        timeoutSeconds: 3
        successThreshold: 1
        failureThreshold: 2

参数说明

initialDelaySeconds:容器启动后第一次执行探测是需要等待多少秒。

periodSeconds:执行探测的频率。默认是10秒,最小1秒。

timeoutSeconds:探测超时时间。默认1秒,最小1秒。

successThreshold:探测失败后,最少连续探测成功多少次才被认定为成功。默认是1。对于liveness必须是1。最小值是1。

failureThreshold:探测成功后,最少连续探测失败多少次才被认定为失败。默认是3。最小值是1。

创建pod

[root@k8s-master1 tangzheng]# kubectl create -f nginx_pod_exec.yaml 
pod/exec2 created

[root@k8s-master1 tangzheng]# kubectl get pods
NAME                       READY   STATUS    RESTARTS   AGE
exec2                      1/1     Running   0          9s

[root@k8s-master1 ~]# kubectl describe pod exec2

只复制了events事件

过了几秒后,探针检测到文件不存在,健康状态标注为unhealthy

开始kill掉容器,重启

探针检测失败次数太多,容器崩溃

pod状态变为CrashLoopBackOff

[root@k8s-master1 tangzheng]# kubectl get pods 
NAME                       READY   STATUS             RESTARTS   AGE
exec2                      0/1     CrashLoopBackOff   11         29m

pod资源的健康检查-liveness探针的exec使用的相关教程结束。

《pod资源的健康检查-liveness探针的exec使用.doc》

下载本文的Word格式文档,以方便收藏与打印。