三种探针
针对运行中的容器,kubelet 可以选择是否执行以下三种探针,以及如何针对探测结果作出反应:
startupProbe
指示容器中的应用是否已经启动。如果提供了启动探针,则所有其他探针都会被 禁用,直到此探针成功为止。如果启动探测失败,kubelet 将杀死容器,而容器依其 重启策略进行重启。
如果容器没有提供启动探测,则默认状态为 Success。
livenessProbe
指示容器是否正在运行。如果存活态探测失败,则 kubelet 会杀死容器, 并且容器将根据其重启策略决定未来。
如果容器不提供存活探针, 则默认状态为 Success。
readinessProbe
指示容器是否准备好为请求提供服务。如果就绪态探测失败, 端点控制器将从与 Pod 匹配的所有服务的端点列表中删除该 Pod 的 IP 地址。 初始延迟之前的就绪态的状态值默认为 Failure。
如果容器不提供就绪态探针,则默认状态为 Success。
部署探针
创建一个nginx Deployment,包含三种探针:
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: myserver-myapp-frontend-deployment
namespace: myserver
spec:
replicas: 1
selector:
matchLabels:
app: myserver-myapp-frontend-label
template:
metadata:
labels:
app: myserver-myapp-frontend-label
spec:
terminationGracePeriodSeconds: 60
containers:
- name: myserver-myapp-frontend-label
image: nginx:1.20.2
ports:
- containerPort: 80
startupProbe:
httpGet:
path: /index.html
port: 80
initialDelaySeconds: 5 #首次检测延迟5s
failureThreshold: 3 #从成功转为失败的次数
periodSeconds: 3 #探测间隔周期3s
readinessProbe:
httpGet:
path: /index.html
port: 80
initialDelaySeconds: 5 #首次检测延迟5s
periodSeconds: 3 #探测间隔周期3s
timeoutSeconds: 5 #检测超时时间5s
successThreshold: 1 #从失败转为成功的次数
failureThreshold: 3 #从成功转为失败的次数
livenessProbe:
httpGet:
path: /index.html
port: 80
initialDelaySeconds: 5 #首次检测延迟5s
periodSeconds: 3 #探测间隔周期3s
timeoutSeconds: 5 #检测超时时间5s
successThreshold: 1 #从失败转为成功的次数
failureThreshold: 3 #从成功转为失败的次数
---
apiVersion: v1
kind: Service
metadata:
name: myserver-myapp-frontend-service
namespace: myserver
spec:
ports:
- name: http
port: 80
targetPort: 80
nodePort: 30080
protocol: TCP
type: NodePort
selector:
app: myserver-myapp-frontend-label
查看Pod中容器三种探针的配置情况:
kubectl describe pod myserver-myapp-frontend-deployment-556f9595c5-6sbsw -n myserver
...
Liveness: http-get http://:80/index.html delay=5s timeout=5s period=3s #success=1 #failure=3
Readiness: http-get http://:80/index.html delay=5s timeout=5s period=3s #success=1 #failure=3
Startup: http-get http://:80/index.html delay=5s timeout=1s period=3s #success=1 #failure=3
...