GRPC正在成为云原生微服务之间通信的通用语言。如果您今天要将gRPC应用程序部署到Kubernetes,您可能想知道配置运行状况检查的最佳方法。在本文中,我们将讨论grpc-health-probe,一种Kubernetes本地健康检查gRPC应用程序的方法。

grpc-health-probe 解决方案

为了标准化上面提到的“exec探针”方法,我们需要:

  • 标准的健康检查“协议”,可以轻松地在任何gRPC服务器中实现。
  • 标准的健康检查“工具”,可以轻松查询健康协议。

得庆幸的是,gRPC有一个标准的健康检查协议。它可以从任何语言轻松使用。生成的代码和用于设置运行状况的实用程序几乎都在gRPC的所有语言实现中提供。
如果在gRPC应用程序中实现此运行状况检查协议,则可以使用标准/通用工具调用此Check()方法来确定服务器状态。
下来你需要的是“标准工具”,它是grpc-health-probe
image.png
使用此工具,您可以在所有gRPC应用程序中使用相同的运行状况检查配置。这种方法需要你:

  • 选择您喜欢的语言找到gRPC“health”模块并开始使用它(例如Go库)。
  • 将grpc_health_probe二进制文件打到容器中。
  • 配置Kubernetes“exec”探针以调用容器中的“grpc_health_probe”工具。

示例

您可以将静态编译的grpc_health_probe打在容器映像中。或选择二进制版本并将其下载到Dockerfile中:

  1. RUN GRPC_HEALTH_PROBE_VERSION=v0.2.0 && \
  2. 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 && \
  3. chmod +x /bin/grpc_health_probe

在你的 Kubernetes Pod manifest中,指定容器的 livenessProbe and/or readinessProbe 。

  1. spec:
  2. containers:
  3. - name: server
  4. image: "[YOUR-DOCKER-IMAGE]"
  5. ports:
  6. - containerPort: 5000
  7. readinessProbe:
  8. exec:
  9. command: ["/bin/grpc_health_probe", "-addr=:5000"]
  10. initialDelaySeconds: 5
  11. livenessProbe:
  12. exec:
  13. command: ["/bin/grpc_health_probe", "-addr=:5000"]
  14. initialDelaySeconds: 10

成功:

  1. $ grpc_health_probe -addr=localhost:5000
  2. healthy: SERVING

失败:

  1. $ grpc_health_probe -addr=localhost:9999 -connect-timeout 250ms -rpc-timeout 100ms
  2. failed to connect service at "localhost:9999": context deadline exceeded
  3. exit status 2