探针种类
- startupprobe(启动探针)
- livenessprobe(存活探针)
- readinessprobe(就绪探针)
startuprobe(启动探针)
- 只支持 tcp 和 http 检测
- 使用启动探针时, 会禁用另外两个探针, 确保不会被干扰
- 常用于针对启动缓慢的容器进行检查, 避免未启动就被杀死
- 程序最多有
failureThreshold
* periodSeconds
的时间来启动, 一旦探测成功, 交给其他探针进行后续的检测 - 如果时间内没有成功, 根据
restartPolicy
参数进行处理
- 启动探针完成后,交给就绪和存活探针
readnessprobe(就绪探针)
- 它是用来检测pod是否准备就绪,当检测成功时,加入到endpoints列表接入流量。所以默认状态为failed(未就绪),当检测成功时状态变更为success(已就绪)
livenessprobe(存活探针)
- 它是pod的正常运行后使用的探针,用于检测是否正常提供服务,失败则加入删除列表。所以默认状态为success(已就绪),当检测到失败时状态变更为failed(未就绪)
探测方式
exec
exec:
command:
- cat
- xxx.conf
http
httpGet:
path: /healthz
port: 8080
tcp
tcpSocket:
port: my-http
定义探针
readinessProbe:
httpGet:
path: /healthz
port: 9000
scheme: HTTP
httpHeaders:
- name: Accept
value: application/json
- name: token
value: ""
initialDelaySeconds: 20
periodSeconds: 5
timeoutSeconds: 10
failureThreshold: 3
successThreshold: 1
示例
pod 完整yaml
apiVersion: v1
kind: Pod
metadata:
namespace: yky
spec:
containers:
- name: xxx
image: xxx
ports:
- name: http
containerPort: 8080
protocol: TCP
startupProbe:
tcpSocket:
port: 8080
initialDelaySeconds: 30
periodSeconds: 3
timeoutSeconds: 10
failureThreshold: 3
livenessProbe:
exec:
command:
- cat
- xxx.conf
readinessProbe:
httpGet:
path: /healthz
port: http
查看pod探针信息
[root@10-100-0-30 pod]
...
Last State: Terminated
Reason: Error
Exit Code: 143
Started: Thu, 10 Jun 2021 18:51:39 +0800
Finished: Thu, 10 Jun 2021 18:52:09 +0800
Ready: True
Restart Count: 4
Liveness: http-get http://:8111/ delay=0s timeout=1s period=10s
...
补充
- 由kubelet进行使用
- 单独配置 readiness 不会使 pod 在失败时进行重启
- 单独配置 liveness 不会使 pod 在失败时移出 endpoint 列表, 导致请求失败
- 通常两者一起使用
- 使用go语言实现探针
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
duration := time.Now().Sub(started)
if duration.Seconds() > 10 {
w.WriteHeader(500)
w.Write([]byte(fmt.Sprintf("error: %v", duration.Seconds())))
} else {
w.WriteHeader(200)
w.Write([]byte("ok"))
}
})