健康检查配置
我们知道K8S本身提供了Liveness和Readiness机制来对Pod进行健康监控,同样我们在部署K8S Ingress Controller时也配置了LivenessProbe和ReadinessProbe对其进行健康检查,具体配置如下所示:
livenessProbe:
failureThreshold: 3
httpGet:
path: /healthz
port: 10254
scheme: HTTP
initialDelaySeconds: 10
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 1
readinessProbe:
failureThreshold: 3
httpGet:
path: /healthz
port: 10254
scheme: HTTP
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 1
那么Kubelet在对Nginx Ingress Controller Pod进行定期健康检查时,就会通过HTTP协议发送GET请求,类似于如下请求:
curl -XGET http://<NGINX_INGRESS_CONTROLLER_POD_ID>:10254/healthz
健康检查成功则会返回ok
,检查失败则返回失败信息。
原理剖析
那么当Kubelet发起对Ingress Controller Pod的健康检查时,Nginx Ingress Controller内部到底做了什么,以及为什么是10254端口和/healthz路径,今天我们简单剖析下K8S Ingress Controller内部的健康检查逻辑。
1、10254 和 /healthz
首先,Nginx Ingress Controller在启动时会通过goroutine启动一个HTTP Server:
// 初始化一个 HTTP Request Handler
mux := http.NewServeMux()
go registerHandlers(conf.EnableProfiling, conf.ListenPorts.Health, ngx, mux)
其中registerHandlers方法实现如下:
func registerHandlers(enableProfiling bool, port int, ic *controller.NGINXController, mux *http.ServeMux) {
// 注册健康检查Handler
healthz.InstallHandler(mux,
healthz.PingHealthz,
ic,
)
// 用于Prometheus抓取metrics信息
mux.Handle("/metrics", promhttp.Handler())
// 获取当前Ingress Controller版本信息
mux.HandleFunc("/build", func(w http.ResponseWriter, r *http.Request) {