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: liveness
name: liveness-exec
spec:
containers:
- name: liveness
image: harbor.dinginx.org/hostos/alpine:latest
args:
- /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.org
Normal 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 directory
Normal Killing 33s kubelet spec.containers{liveness}: Container liveness failed liveness probe, will be restarted
Normal 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 created
Normal Started 3s (x2 over 56s) kubelet spec.containers{liveness}: Container started
Normal 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: liveness
name: liveness-http
spec:
containers:
- name: liveness
image: harbor.dinginx.org/dinginx/app-dinginx:v1
livenessProbe:
# 使用httpGet的方式去做健康检查
httpGet:
# 指定访问路径
path: /index.html
# 指定访问端口
port: 80
# 失败重试次数三次
failureThreshold: 3
# 第一次检测时间10s
initialDelaySeconds: 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-liveness
image: harbor.dinginx.org/dinginx/app-dinginx:v1
ports:
- containerPort: 80
livenessProbe:
# 使用tcpSocket的方式去做健康检查
tcpSocket:
port: 80
# 失败重试次数三次
failureThreshold: 3
# 第一次检测时间10s
initialDelaySeconds: 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.4
HTTP/1.1 502 Bad Gateway # 资源失效,探测失败
Connection: keep-alive
Keep-Alive: timeout=4
Proxy-Connection: keep-alive
Content-Length: 0
HTTP/1.1 502 Bad Gateway
Connection: keep-alive
Keep-Alive: timeout=4
Proxy-Connection: keep-alive
Content-Length: 0
HTTP/1.1 502 Bad Gateway
Connection: keep-alive
Keep-Alive: timeout=4
Proxy-Connection: keep-alive
Content-Length: 0
HTTP/1.1 502 Bad Gateway
Connection: keep-alive
Keep-Alive: timeout=4
Proxy-Connection: keep-alive
Content-Length: 0
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: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

浙公网安备 33010602011771号