46

k8s与健康检查--grpc服务健康检查最佳实践

 5 years ago
source link: https://studygolang.com/articles/18609?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

前言

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

果您不熟悉,Kubernetes 健康检查 (liveness and readiness probes)就是让您的应用程序在您睡觉时保持可用的原因。他们检测到没有响应的pod,将它们标记为不健康,并导致这些pod重新启动或重新调度。

kubernetes本身不支持gRPC健康检查。这使得gRPC开发人员在部署到Kubernetes时有以下三种方法:

bVbpc2Z?w=2700&h=967

  • httpGet probe: 不能与gRPC原生使用。您需要重构您的应用程序以同时提供gRPC和HTTP / 1.1协议(在不同的端口号上)。
  • tcpSocket probe: 打开套接字到gRPC服务器是没有意义的,因为它无法读取响应正文。
  • exec probe: 这会定期调用容器生态系统中的程序。对于gRPC,这意味着您自己实现健康RPC,然后使用编写客户端工具,并将客户端工具与容器打包到一起。

grpc-health-probe 解决方案

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

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

得庆幸的是,gRPC有一个 标准的健康检查协议 。它可以从任何语言轻松使用。生成的代码和用于设置运行状况的实用程序几乎都在gRPC的所有语言实现中提供。

如果在gRPC应用程序中实现此运行状况检查协议,则可以使用标准/通用工具调用此Check()方法来确定服务器状态。

下来你需要的是“标准工具”,它是 grpc-health-probe

bVbpc3P?w=2100&h=750

使用此工具,您可以在所有gRPC应用程序中使用相同的运行状况检查配置。这种方法需要你:

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

示例

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

RUN GRPC_HEALTH_PROBE_VERSION=v0.2.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 Pod manifest中,指定容器的 livenessProbe and/or readinessProbe 。

spec:
  containers:
  - name: server
    image: "[YOUR-DOCKER-IMAGE]"
    ports:
    - containerPort: 5000
    readinessProbe:
      exec:
        command: ["/bin/grpc_health_probe", "-addr=:5000"]
      initialDelaySeconds: 5
    livenessProbe:
      exec:
        command: ["/bin/grpc_health_probe", "-addr=:5000"]
      initialDelaySeconds: 10

服务器健康检查的代码实现,主要部分如下:

hsrv := health.NewServer()
    hsrv.SetServingStatus("", healthpb.HealthCheckResponse_SERVING)
    healthpb.RegisterHealthServer(s, hsrv)

完整代码,请查看 git仓库


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK