失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > kuberbetes Pod 健康检查

kuberbetes Pod 健康检查

时间:2020-05-14 21:00:50

相关推荐

kuberbetes Pod 健康检查

判断 Pod 健康状态的两种方式

livenessProbe 存活性探针readinessProbe 就绪性探针
1.livenessProbe 存活性探针

用于判断容器是否存活,即 Running 状态。如果 livenessProbe 探针检测到容器不健康,则 kubelet 会杀死容器,并根据容器的重启策略进行容器的重启。如果容器没有定义 livenessProbe 探针,则 livenessProbe 返回状态永远为 Success。

2.readinessProbe 就绪性探针

用于判断容器服务(service)是否可用,即 Ready 状态。如果 Ready 状态,则表示Service 与 Pod EndPoint 建立了关联,并且将其保存到 EndPoint 列表中,以供服务调用;如果状态变为 Failure , Kubernetes 会将其从 EndPoint 列表中移除,保证通过 Service 访问时不会将流量路由到不健康的 Pod 上,等到状态恢复到 Ready 状态再将其对应的 EndPoint 加入EndPoint 列表中。如果容器没有定义 readinessProbe 探针,则 readiness 返回状态永远为 Success。

探针的配置方式

通过 kubectl 定期对容器进行诊断,判断容器状态是否健康或者存活。

livenessProbe 和 readinessProbe 探测方式
ExecActionTCPSocketActionHTTPGetAction
三种探针的可能返回结果
Success:探测成功Failure:探测失败,更具重启策略重启容器Unknow:探测失败(未找到),不采取任何行动

enjoy coding!

ExecAction

在容器中执行自定义命令以判断容器是否健康,命令退出时返回码为 0 ,则表示健康,其他值表示不健康。

例子:

apiVersion: v1kind: Podmetadata:labels:test: livenessname: liveness-execspec:containers:- name: livenessimage: innerpeacez/k8s.gcr.io-busyboxargs:- /bin/sh- -c- touch /tmp/healthy; sleep 20; rm -rf /tmp/healthy; sleep 60livenessProbe:exec:command:- cat- /tmp/healthyinitialDelaySeconds: 10periodSeconds: 5

这个例子中,busybox 镜像其中之后执行了touch /tmp/healthy创建出/tmp/healthy目录,30 s 后删除这个目录,livenessProbe 设置了initialDelaySeconds: 10,会在容器启动之后 10s 后进行初次探测,返回结果不出意外为 Success ,表示容器健康。使用以下命令查看 Pod Event

kubectl describe pod liveness-exec

一段时间后你可能会看到以下结果,显示目录不存在了。原因是periodSeconds: 5设置了 5s 后再次探测,但是目录已经被删除了,所以探测返回结果为:Failure

TCPSocketAction

通过容器的 IP 和 port 进行 TCP 检查,如果能够建立 TCP 连接,则表示容器健康。

例子:

apiVersion: v1kind: Podmetadata:name: liveness-tcpsocketlabels:app: liveness-tcpsocketspec:containers:- name: liveness-tcpsocketimage: innerpeacez/k8s.gcr.io-goproxy:0.1ports:- containerPort: 8080livenessProbe:tcpSocket:port: 8080initialDelaySeconds: 15periodSeconds: 20

执行命令:

vim livenessProbe-tcpSocket.yaml # 将上面内容粘贴进来kubectl apply -f livenessProbe-tcpSocket.yaml

例子中,livenessProbe 会与容器中的 localhost:8080 建立 TCP 连接,进行健康检查。因为容器启动时,容器端口被设定为 8080,所以可以建立 TCP 连接,livenessProbe 健康检查返回 Success 。我们可以手动将tcpSocket检查端口设置为 8081 ,此时将无法建立 TCP 连接,可以通过下面的命令查看 Pod Event。

kubectl describe pod liveness-tcpsocket

修改成8081再次查看Pod Event

可以看出unhealthy,证明tcpsocket检查返回值为failure

HTTPGetAction

通过容器的 IP Port 以及访问路径,发送 HTTP 请求进行调用,如果返回的状态码范围为 [200 , 400) ,则表示容易健康。

例子:

apiVersion: v1kind: Podmetadata:labels:test: livenessname: liveness-httpspec:containers:- name: livenessimage: innerpeacez/k8s.gcr.io-livenessargs:- /serverlivenessProbe:httpGet:path: /healthzport: 8080httpHeaders:- name: Custom-Headervalue: AwesomeinitialDelaySeconds: 3periodSeconds: 3

执行命令:

vim livenessProbe-httpget.yaml # 将上面内容粘贴进来kubectl apply -f livenessProbe-httpget.yaml

同时可以执行以下命令查看 Event

kubectl describe pod liveness-http

readinessProbe 探测

方式和 livenessProbe 是一样的,所以上述设置 livenessProbe 的地方同时也可以设置 readinessProbe 。如:

livenessProbe:exec:command:- cat- /tmp/healthyinitialDelaySeconds: 10periodSeconds: 5readinessProbe:exec:command:- cat- /tmp/healthyinitialDelaySeconds: 10periodSeconds: 5

livenessProbe:tcpSocket:port: 8080initialDelaySeconds: 15periodSeconds: 20readinessProbe:tcpSocket:port: 8080initialDelaySeconds: 15periodSeconds: 20

livenessProbe:httpGet:path: /healthzport: 8080httpHeaders:- name: Custom-Headervalue: AwesomeinitialDelaySeconds: 3periodSeconds: 3readinessProbe:httpGet:path: /healthzport: 8080httpHeaders:- name: Custom-Headervalue: AwesomeinitialDelaySeconds: 3periodSeconds: 3

livenessProbe 和 readinessProbe 相关配置

如上述的 initialDelaySeconds ,表示初次进行探测的时间,以下是完整的配置列表

不同类型探针的相关配置

ExecAction

只存在 command ,即只能以执行自定义命令的方式进行探测。

TCPSocketAction

只需要设置需要建立 TCP 连接的 port。

HTTPGetAction

Pod readiness gate

除了上述的两种方式,在 kubernetes 1.14 中 readiness gate 发布了 stable 版本。这个属性在 1.11 时加入,当时命令为Pod ready++

例子:

Kind: Pod...spec:readinessGates:- conditionType: "/feature-1"status:conditions:- type: Ready # this is a builtin PodConditionstatus: "False"lastProbeTime: nulllastTransitionTime: -01-01T00:00:00Z- type: "/feature-1" # an extra PodConditionstatus: "False"lastProbeTime: nulllastTransitionTime: -01-01T00:00:00ZcontainerStatuses:- containerID: docker://abcd...ready: true...

根据官网的文档,我们可以在 spec 定义期望 readinessGates 来检测 Pod 是否健康,需要在 status.conditions 设置type: "/feature-1"自定义一个condition,如果不设置,默认为 false , 并且这个condition 的 status 可以被外部修改,也就是说我们可以外部设置这个 Pod 是否能够被 Service 调度到。

设置了 readinessGates 之后判断一个 Pod 是否健康的条件
Pod 中所有的容器都为 ready 状态设置的所有的 ReadinessGates 状态都为 true

只有满足以上两个条件,这个 Pod 参可能被认为可暴露的,也就是说 Service 对应的 EndPoint 列表中才会包含这个 Pod 对应的 EndPoint。

参考

https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-probes/https://kubernetes.io/docs/reference/command-line-tools-reference/feature-gates/《kubernetes 权威指南》 第四版

如果觉得《kuberbetes Pod 健康检查》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。