探针
K8S 提供了 3 种探针
startupProbe 启动检查(1.16 版本新增)
livenessProbe 存活检查
readinessProbe 就绪检查
startupProbe
kubernetes 1.16 版本新增功能,用于判断容器内应用程序是否已经启动,如果配置了 startuprobe,就会先禁用其他的探测,直到它成功为止,成功后将不再进行探测。
1 2 3 4 5 6 7 8 9 10 11
| startupProbe: failureThreshold: 3 httpGet: path: /health port: 8080 scheme: HTTP initialDelaySeconds: 20 periodSeconds: 10 successThreshold: 1 failureThreshold: 2 timeoutSeconds: 1
|
readinessProbe
一般用于探测容器内的程序是否健康,它的返回值如果为 success,那么就代表这个容器已经完成启动,并且程序已经是可以接受流量的状态. 这个时候就会被加到 service 的后端列表中,即可以对外提供服务。
livenessProbe
用于探测容器是否运行,如果探测失败,kubelet 会根据配置的重启策略进行相应的处理,如果没有配置该探针,默认就是 success
检查方式
httpget
kubelet 会向容器内运行的服务(服务在监听 8080 端口)发送一个 HTTP GET 请求来执行探测。 如果服务器上 /healthz 路径下的处理程序返回成功代码,则 kubelet 认为容器是健康存活的。 如果处理程序返回失败代码,则 kubelet 会杀死这个容器并将其重启。
返回大于或等于 200 并且小于 400 的任何代码都标示成功,其它返回代码都标示失败。
1 2 3 4 5 6 7
| livenessProbe: httpGet: path: /healthz port: 8080 httpHeaders: - name: Custom-Header value: Awesome
|
tcp 端口
端口通了则成功,不通则失败
1 2 3 4 5 6 7 8
| startupProbe: tcpSocket: port: 8080 initialDelaySeconds: 20 periodSeconds: 5 successThreshold: 1 failureThreshold: 3 timeoutSeconds: 3
|
执行命令
如果命令执行成功并且返回值为 0,kubelet 就会认为这个容器是健康存活的。 如果这个命令返回非 0 值,kubelet 会杀死这个容器并重新启动它。
1 2 3 4 5 6 7 8 9 10
| livenessProbe: exec: command: - cat - /tmp/healthy initialDelaySeconds: 5 timeoutSeconds: 2 successThreshold: 3 failureThreshold: 2 periodSeconds: 5
|
一个线上的配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| livenessProbe: failureThreshold: 3 httpGet: path: /actuator/info port: 8080 scheme: HTTP initialDelaySeconds: 60 periodSeconds: 10 successThreshold: 1 timeoutSeconds: 5 readinessProbe: failureThreshold: 3 httpGet: path: /actuator/info port: 8080 scheme: HTTP initialDelaySeconds: 60 periodSeconds: 10 successThreshold: 1 timeoutSeconds: 5 startupProbe: failureThreshold: 3 tcpSocket: port: 8080 initialDelaySeconds: 20 periodSeconds: 5 successThreshold: 1 timeoutSeconds: 3 resources: limits: cpu: "1" memory: 2560Mi requests: cpu: "1" memory: 2560Mi
|