启动探测 startupProbe
资源清单配置如下:
apiVersion: v1
kind: Pod
metadata:
name: check
namespace: default
labels:
app: check
spec:
containers:
- name: check
image: busybox:1.28
imagePullPolicy: IfNotPresent
command:
- /bin/sh
- -c
- sleep 10; exit
kubectl get pods -w: 持续观察 pod 状态. 上面的资源清单创建的 pod 每 10s 退出重启一次.
livenessProbe:用于探测容器是否运行。如果存活探测失败,则 kubelet 会杀死容器,并且容器将受到其重启策略的影响决定是否重启。如果容器不提供存活探针,则默认状态为 Success。
readinessProbe:一般用于探测容器内的程序是否健康,容器是否准备好服务请求。如果就绪探测失败,endpoint将从与 Pod 匹配的所有 Service 的端点中删除该 Pod 的 IP 地址。初始延迟之前的就绪状态默认为 Failure。如果容器不提供就绪探针,则默认状态为 Success。
startupProbe: 探测容器中的应用是否已经启动。如果提供了启动探测(startup probe),则禁用所有其他探测,直到它成功为止。如果启动探测失败,kubelet 将杀死容器,容器服从其重启策略进行重启。如果容器没有提供启动探测,则默认状态为成功Success。
目前 livenessProbe, readinessProbe, startupProbe 探测都支持下面三种探针:
- exec: 在容器中执行指定的命令, 如果执行成功, 退出码为 0 则探测成功.
- TCPSocket: 通过容器的 IP 地址和端口号执行 TCP 检查, 如果能够建立 TCP 连接, 则表明容器健康.
- HTTPGet: 通过容器的 IP 地址, 端口号及路径调用 HTTP Get 方法, 如果响应的状态码大于等于 200 小于 400, 则认为容器健康.
添加启动探测资源清单示例:
apiVersion: v1
kind: Pod
metadata:
name: startupprobe
spec:
containers:
- name: startup
image: tomcat
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080
startupProbe:
exec:
command:
- "/bin/sh"
- "-c"
- "ps aux | grep tomcat"
initialDelaySeconds: 20 # 容器启动后多久开始探测
periodSeconds: 20 # 执行探测的时间间隔
timeoutSeconds: 10 # 探针执行检测请求后. 等待响应的超时时间
successThreshold: 1 # 成功多少次才算成功
failureThreshold: 3 # 失败多少次才算失败
kubectl explain pods.spec.containers.startupProbe: 查看编写启动探测的字段帮助信息.
容器探测间隔说明
不同的探针探测时间可能不一样. exec 和 tcp 探测完成的时间可能不一样. httpGet 第一次探测成功就算成功了.
启动, 就绪, 存活探测混合使用
apiVersion: v1
kind: Service
metadata:
name: springboot-live
labels:
app: springboot
spec:
type: NodePort
ports:
- name: server
port: 8080
targetPort: 8080
nodePort: 31180
- name: management
port: 8081
targetPort: 8081
nodePort: 31181
selector:
app: springboot
---
apiVersion: v1
kind: Pod
metadata:
name: springboot-live
labels:
app: springboot
spec:
containers:
- name: springboot
image: mydlqclub/springboot-helloworld:0.0.1
imagePullPolicy: IfNotPresent
ports:
- name: server
containerPort: 8080
- name: management
containerPort: 8081
readinessProbe: # 就绪和存活在启动探测之后一起启动
initialDelaySeconds: 20
periodSeconds: 5
timeoutSeconds: 10
httpGet:
scheme: HTTP
port: 8081
path: /actuator/health
livenessProbe:
initialDelaySeconds: 20
periodSeconds: 5
timeoutSeconds: 10
httpGet:
scheme: HTTP
port: 8081
path: /actuator/health
startupProbe: # 先启动
initialDelaySeconds: 20
periodSeconds: 5
timeoutSeconds: 10
httpGet:
scheme: HTTP
port: 8081
path: /actuator/health
kubectl describe svc springboot-live: 查看服务信息.kubectl exec -it springboot-live -- /bin/bash: 启动一个容器终端.