当前位置: 首页 > news >正文

Kubernetes探针之livenessProbe探针

1 Kubernetes探针简介

官方链接

# 官方文档
https://kubernetes.io/zh-cn/docs/concepts/configuration/liveness-readiness-startup-probes/# 配置文档
https://kubernetes.io/zh-cn/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/

Kubernetes 允许你定义探针来持续监控 Pod 中容器的健康状况。

探针分为三种类型,每种类型都有不同的用途:

  • 启动探针
  • 存活探针
  • 就绪探针
# 启动探针启动探针检查容器内的应用是否已启动。 启动探针可以用于对慢启动容器进行存活性检测,避免它们在启动运行之前就被 kubelet 杀掉。
如果配置了这类探针,它会禁用存活检测和就绪检测,直到启动探针成功为止。
这类探针仅在启动时执行,不像存活探针和就绪探针那样周期性地运行。
- 更多细节参阅[配置存活探针、就绪探针和启动探针](https://kubernetes.io/zh-cn/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes)。# 存活探针存活探针决定何时重启容器。 例如,当应用在运行但无法取得进展时,存活探针可以捕获这类死锁。
如果一个容器的存活探针失败多次,kubelet 将重启该容器。
存活探针不会等待就绪探针成功。 如果你想在执行存活探针前等待,你可以定义 `initialDelaySeconds`,或者使用[启动探针](https://kubernetes.io/zh-cn/docs/concepts/configuration/liveness-readiness-startup-probes/#startup-probe)。# 就绪探针
就绪探针决定容器何时准备好接受流量。 这种探针在等待应用执行耗时的初始任务时非常有用; 例如:建立网络连接、加载文件和预热缓存。在容器的生命周期后期, 就绪探针也很有用,例如,从临时故障或过载中恢复时。
如果就绪探针返回的状态为失败,Kubernetes 会将该 Pod 从所有对应服务的端点中移除。
就绪探针在容器的整个生命期内持续运行。

2 存活探针(livenessProbe

2.1 exec-liveness案例

2.1.1 资源清单

# 资源清单
[root@k8s-master01 /data/manifests/pods/probe]# cat 01-exec-liveness.yaml 
apiVersion: v1
kind: Pod
metadata:labels:test: livenessname: liveness-exec
spec:containers:- name: livenessimage: harbor.dinginx.org/hostos/alpine:latestargs:- /bin/sh- -c- touch /tmp/healthy; sleep 20; rm -f /tmp/healthy; sleep 600# 配置探针livenessProbe:exec:command:- cat- /tmp/healthy#  kubelet 在执行第一次探测前应该等待 5 秒initialDelaySeconds: 5# 指定了探针的频率 kubelet 应该每 1 秒执行一次存活探测periodSeconds: 1# 检测服务失败次数的累加值,默认值是3次,最小值是1。当检测服务成功后,该值会被重置!failureThreshold: 3# 检测服务成功次数的累加值,默认值为1次,最小值1.successThreshold: 1# 一次检测周期超时的秒数,默认值是1秒,最小值为1.timeoutSeconds: 1
#
21 fail --- 23 fail
24 reboot
44s fail --- 47s fail
48 reboot --- 49s successed

2.1.2 创建资源并监测状态

[root@k8s-master01 /data/manifests/pods/probe]# kubectl apply -f 01-exec-liveness.yaml# 监听资源状态
[root@k8s-node01 ~]# kubectl get pods -w -owide
NAME            READY   STATUS    RESTARTS   AGE   IP       NODE     NOMINATED NODE   READINESS GATES
liveness-exec   0/1     Pending   0          0s    <none>   <none>   <none>           <none>
liveness-exec   0/1     Pending   0          0s    <none>   k8s-node03.dinginx.org   <none>           <none>
liveness-exec   0/1     ContainerCreating   0          0s    <none>   k8s-node03.dinginx.org   <none>           <none>
liveness-exec   1/1     Running             0          2s    10.244.3.126   k8s-node03.dinginx.org   <none>           <none>
liveness-exec   1/1     Running             1 (1s ago)   55s   10.244.3.126   k8s-node03.dinginx.org   <none>           <none>
liveness-exec   1/1     Running             2 (1s ago)   108s   10.244.3.126   k8s-node03.dinginx.org   <none>           <none>
liveness-exec   1/1     Running             3 (0s ago)   2m40s   10.244.3.126   k8s-node03.dinginx.org   <none>           <none>
...[root@k8s-node02 ~]# kubectl describe pods liveness-exec 
#...
Events:Type     Reason     Age                From               Message----     ------     ----               ----               -------Normal   Scheduled  56s                default-scheduler  Successfully assigned default/liveness-exec to k8s-node03.dinginx.orgNormal   Pulled     56s                kubelet            spec.containers{liveness}: Successfully pulled image "harbor.dinginx.org/hostos/alpine:latest" in 72ms (72ms including waiting). Image size: 8322300 bytes.
# 21s左右第一次探测失败Warning  Unhealthy  33s (x3 over 35s)  kubelet            spec.containers{liveness}: Liveness probe failed: cat: can't open '/tmp/healthy': No such file or directoryNormal   Killing    33s                kubelet            spec.containers{liveness}: Container liveness failed liveness probe, will be restartedNormal   Pulling    3s (x2 over 56s)   kubelet            spec.containers{liveness}: Pulling image "harbor.dinginx.org/hostos/alpine:latest"Normal   Created    3s (x2 over 56s)   kubelet            spec.containers{liveness}: Container createdNormal   Started    3s (x2 over 56s)   kubelet            spec.containers{liveness}: Container startedNormal   Pulled     3s                 kubelet            spec.containers{liveness}: Successfully pulled image "harbor.dinginx.org/hostos/alpine:latest" in 62ms (62ms including waiting). Image size: 8322300 bytes.

2.2 http-liveness案例

2.2.1 编写资源清单

# 资源清单
[root@k8s-master01 /data/manifests/pods/probe]# cat 02-http-liveness.yaml 
apiVersion: v1
kind: Pod
metadata:labels:test: livenessname: liveness-http
spec:containers:- name: livenessimage: harbor.dinginx.org/dinginx/app-dinginx:v1livenessProbe:# 使用httpGet的方式去做健康检查httpGet:# 指定访问路径path: /index.html # 指定访问端口port: 80# 失败重试次数三次failureThreshold: 3# 第一次检测时间10sinitialDelaySeconds: 10# 每1s检测一次periodSeconds: 1# 成功检测一次成功即成功successThreshold: 1# 一次检测周期超时的秒数,默认值是1秒,最小值为1.timeoutSeconds: 1

2.2.2 创建资源及资源监测

[root@k8s-master01 /data/manifests/pods/probe]# kubectl apply -f 02-http-liveness.yaml # 资源监测
[root@k8s-node02 ~]# while true ;do curl 10.244.3.132 && sleep 1 ;done
<!-- 文件: nginx-site/index.html -->
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Welcome to Nginx!</title>
</head>
<body><h1>Hello, Dinginx's website v1.0</h1><p>欢迎访问www.dinginx.org</p>
</body>
</html><!-- 文件: nginx-site/index.html -->
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Welcome to Nginx!</title>
</head>
<body><h1>Hello, Dinginx's website v1.0</h1><p>欢迎访问www.dinginx.org</p>
</body>
</html>
curl: (7) Failed to connect to 10.244.3.132 port 80 after 1 ms: Connection refused
curl: (7) Failed to connect to 10.244.3.132 port 80 after 1 ms: Connection refused
curl: (7) Failed to connect to 10.244.3.132 port 80 after 0 ms: Connection refused
#...
<!-- 文件: nginx-site/index.html -->
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Welcome to Nginx!</title>
</head>
<body><h1>Hello, Dinginx's website v1.0</h1><p>欢迎访问www.dinginx.org</p>
</body>
</html><!-- 文件: nginx-site/index.html -->
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Welcome to Nginx!</title>
</head>
<body><h1>Hello, Dinginx's website v1.0</h1><p>欢迎访问www.dinginx.org</p>
</body>
</html>
......

2.2.3 模拟故障自愈

[root@k8s-master01 /data/manifests/pods/probe]# kubectl exec -it pods/liveness-http -- rm -rf /usr/share/nginx/html/index.html## 监测pod状态
[root@k8s-node01 ~]# kubectl get pods -w -owide
NAME            READY   STATUS    RESTARTS      AGE     IP             NODE                     NOMINATED NODE   READINESS GATES
liveness-http   1/1     Running   1 (14s ago)   7m30s   10.244.3.132   k8s-node03.dinginx.org   <none>           <none>
liveness-http   1/1     Running   2 (0s ago)    8m30s   10.244.3.132   k8s-node03.dinginx.org   <none>           <none>
liveness-http   0/1     CrashLoopBackOff   2 (1s ago)    8m50s   10.244.3.132   k8s-node03.dinginx.org   <none>           <none>
liveness-http   1/1     Running            3 (23s ago)   9m12s   10.244.3.132   k8s-node03.dinginx.org   <none>           <none>## 监测访问状态
[root@k8s-node02 ~]# while true ;do curl 10.244.3.132 && sleep 1 ;done
<!-- 文件: nginx-site/index.html -->
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Welcome to Nginx!</title>
</head>
<body><h1>Hello, Dinginx's website v1.0</h1><p>欢迎访问www.dinginx.org</p>
</body>
</html><!-- 文件: nginx-site/index.html -->
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Welcome to Nginx!</title>
</head>
<body><h1>Hello, Dinginx's website v1.0</h1><p>欢迎访问www.dinginx.org</p>
</body>
</html>
curl: (7) Failed to connect to 10.244.3.132 port 80 after 1 ms: Connection refused
curl: (7) Failed to connect to 10.244.3.132 port 80 after 1 ms: Connection refused
curl: (7) Failed to connect to 10.244.3.132 port 80 after 0 ms: Connection refused
#...
<!-- 文件: nginx-site/index.html -->
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Welcome to Nginx!</title>
</head>
<body><h1>Hello, Dinginx's website v1.0</h1><p>欢迎访问www.dinginx.org</p>
</body>
</html><!-- 文件: nginx-site/index.html -->
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Welcome to Nginx!</title>
</head>
<body><h1>Hello, Dinginx's website v1.0</h1><p>欢迎访问www.dinginx.org</p>
</body>
</html>
......

2.3 tcp-liveness案例

第三种类型的存活探测是使用 TCP 套接字。 使用这种配置时,kubelet 会尝试在指定端口和容器建立套接字链接。

2.3.1 编写资源清单

# 资源清单apiVersion: v1
kind: Pod
metadata:name: tcp-liveness
spec:containers:- name: tcp-livenessimage: harbor.dinginx.org/dinginx/app-dinginx:v1ports:- containerPort: 80livenessProbe:# 使用tcpSocket的方式去做健康检查tcpSocket:port: 80# 失败重试次数三次failureThreshold: 3# 第一次检测时间10sinitialDelaySeconds: 10# 每1s检测一次periodSeconds: 1# 成功检测一次成功即成功successThreshold: 1# 一次检测周期超时的秒数,默认值是1秒,最小值为1.timeoutSeconds: 1

2.3.2 创建资源及查看资源信息

[root@k8s-master01 /data/manifests/pods/probe]# kubectl apply -f 03-tcp-liveness.yaml # 检查资源信息
[root@k8s-node01 ~]# kubectl get pods -owide
NAME           READY   STATUS    RESTARTS     AGE   IP            NODE                     NOMINATED NODE   READINESS GATES
tcp-liveness   1/1     Running   3 (7s ago)   44s   10.244.2.87   k8s-node02.dinginx.org   <none>           <none># 资源访问测试
[root@k8s-node02 ~]# curl 10.244.2.87
<!-- 文件: nginx-site/index.html -->
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Welcome to Nginx!</title>
</head>
<body><h1>Hello, Dinginx's website v1.0</h1><p>欢迎访问www.dinginx.org</p>
</body>
</html>

2.3.3 模拟故障自愈

[root@k8s-master01 /data/manifests/pods/probe]# kubectl exec -it pods/tcp-liveness -- nginx  -s stop## 资源访问监测
[root@k8s-node02 ~]# while true ;do curl 10.244.2.87 && sleep 1 ;done
HTTP/1.1 200 OK
Content-Length: 332
Accept-Ranges: bytes
Connection: keep-alive
Content-Type: text/html
Date: Thu, 07 May 2026 13:14:15 GMT
Etag: "695e3551-14c"
Keep-Alive: timeout=4
Last-Modified: Wed, 07 Jan 2026 10:28:33 GMT
Proxy-Connection: keep-alive
Server: nginx/1.29.4HTTP/1.1 502 Bad Gateway		# 资源失效,探测失败
Connection: keep-alive
Keep-Alive: timeout=4
Proxy-Connection: keep-alive
Content-Length: 0HTTP/1.1 502 Bad Gateway
Connection: keep-alive
Keep-Alive: timeout=4
Proxy-Connection: keep-alive
Content-Length: 0HTTP/1.1 502 Bad Gateway
Connection: keep-alive
Keep-Alive: timeout=4
Proxy-Connection: keep-alive
Content-Length: 0HTTP/1.1 502 Bad Gateway
Connection: keep-alive
Keep-Alive: timeout=4
Proxy-Connection: keep-alive
Content-Length: 0HTTP/1.1 200 OK			# 资源重启,故障自愈
Content-Length: 332
Accept-Ranges: bytes
Connection: keep-alive
Content-Type: text/html
Date: Thu, 07 May 2026 13:14:38 GMT
Etag: "695e3551-14c"
Keep-Alive: timeout=4
Last-Modified: Wed, 07 Jan 2026 10:28:33 GMT
Proxy-Connection: keep-alive
Server: nginx/1.29.4
http://www.jsqmd.com/news/772647/

相关文章:

  • 自托管AI网关HydeClaw:整合28种AI模型与多平台接入的智能体编排平台
  • AISMM模型实战手册:从技术债评估、场景优先级排序到资源动态分配的完整闭环
  • 别再为CUDA内存错误发愁了!MMDetection3D复现MVXNet时调小学习率的实战避坑
  • 告别复制粘贴!用STM32CubeMX快速配置STM32F407的GPIO(附LED闪烁和按键检测例程)
  • SAP DB02隐藏玩法:除了性能监控,它还是你的“轻量级SQL查询器”(支持排序、分组、聚合)
  • Cursor编辑器右键菜单插件开发:提升开发者效率的VSCode扩展实践
  • 智能车硬件新手避坑:从AMS1117到TPS5450,我的5V/3.3V供电方案选择与实战踩坑记录
  • 智能体技能库设计:模块化构建AI应用执行能力的工程实践
  • 核心组件大换血:Backbone与Neck魔改篇:YOLO26替换分类头骨干:利用Conformer网络实现全局与局部特征的动态握手
  • 审稿人视角看KBS:我审了两篇稿后,给投稿人的5条Latex与回复建议
  • 跨平台直播聚合架构重构:SimpleLive性能突破与企业级实践指南
  • 从URDF到控制器:深入解读ros2_control中lt;ros2_controlgt;标签的完整配置语法与最佳实践
  • 【AISMM模型深度解码】:20年架构师首曝开源策略落地的5大致命误区与避坑指南
  • 别再用记事本学汇编了!手把手教你用DOSBox+DEBUG玩转8086指令(附完整实验流程)
  • 基于MCP协议的AI数据抓取工具dataclaw-mcp实战指南
  • 保姆级教程:用VASP+VASPKIT 1.5.1计算铝在400K下的弹性模量(AIMD应力应变法)
  • 一次处理Linux处理器和内存双高问题的经历
  • 保姆级教程:用Pinia+Axios拦截器搞定Vue3电商项目的登录状态管理
  • 【稀缺首发】AISMM v3.2增强版ROI引擎白皮书核心节选:新增ESG衰减因子与流动性折价模块(仅限本周开放下载)
  • IL-10/IL-10RA信号通路:从免疫调控枢纽到生物医药创新靶点
  • Claude API逆向工程:Python封装库原理、实战与自动化应用
  • 别再踩坑了!用HT7533给ESP32/STM32供电,这个电源细节必须检查
  • 【大白话说Java面试题】【Java基础篇】第37题:final、finally、finalize的区别
  • LuaDec51 完全指南:如何高效反编译 Lua 5.1 字节码的 3 大核心策略
  • Word安全防护:宏病毒与漏洞的攻防战
  • 深入StbM模块:从Time Base Status状态字节看AUTOSAR时间同步的健壮性设计
  • 别急着换手机!手把手教你给旧安卓(Android 5/6)装上最新版Termux,还能跑C程序
  • 如何在Obsidian中无缝嵌入B站视频:Media Extended插件完整教程
  • 如何用PE-bear轻松分析Windows可执行文件:3个实用技巧让你成为逆向分析高手
  • WeakAuras Companion技术架构深度解析:自动化同步机制与跨平台实现