gRPC 健康探针 grpc_health_probe 是社区提供的一个工具,用来检查服务器的健康状态,服务器通过 gRPC 健康检查协议将它的状态暴露为服务。这是一个通用的客户端,能够与 gRPC 标准的健康检查服务通信。可以以 CLI 工具的方式来使用 grpc_health_probe,如下所示:
$ grpc_health_probe -addr=localhost:50051 ➊
healthy: SERVING
$ grpc_health_probe -addr=localhost:50052 -connect-timeout 600ms \
-rpc-timeout 300ms ➋
failed to connect service at "localhost:50052": context deadline exceeded
exit status 2
- ❶ 向本地端口 50051 运行的 gRPC 服务器发送健康检查请求。
- ❷ 带有连接性相关参数的健康检查请求。
如前面的 CLI 输出所示,grpc_health_probe
发送了一个对/grpc.health.v1.Health/Check
的 RPC 请求。如果它以 SERVING状态作为响应,那么 grpc_health_probe
会成功退出;否则,它退出时会给出一个非零的退出码。
如果是在 Kubernetes 上运行 gRPC 应用程序,那么就可以运行grpc_health_probe,作为 Kubernetes 中 gRPC 服务器端 pod 的存活性和就绪性状态检查。
为了实现这一点,可以将 gRPC 健康探针一起打包到 Docker 镜像中,如以下 Dockerfile 片段所示:
RUN GRPC_HEALTH_PROBE_VERSION=v0.3.0 && \
wget -qO/bin/grpc_health_probe \
https://github.com/grpc-ecosystem/grpc-health-probe/releases/download/
${GRPC_HEALTH_PROBE_VERSION}/grpc_health_probe-linux-amd64 && \
chmod +x /bin/grpc_health_probe
在 Kubernetes Deployment 的 pod 定义中,我们可以按照以下方式定义 livenessProbe 和 readinessProbe:
spec:
containers:
- name: server
image: "kasunindrasiri/grpc-productinfo-server"
ports:
- containerPort: 50051
readinessProbe:
exec:
command: ["/bin/grpc_health_probe", "-addr=:50051"] ➊
initialDelaySeconds: 5
livenessProbe:
exec:
command: ["/bin/grpc_health_probe", "-addr=:50051"] ➋
initialDelaySeconds: 10
- ❶ 声明 grpc_health_probe 作为就绪性探针。
- ❷ 声明 grpc_health_probe 作为存活性探针。
在将存活性探针和就绪性探针设置为 gRPC 健康探针之后,Kubernetes 就可以基于 gRPC 服务器的状态做决策了。
文档更新时间: 2023-09-02 08:11 作者:Minho