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

使用探针的方式对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

posted @   IT运维成长笔记  阅读(223)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
点击右上角即可分享
微信分享提示