python - kubernetes中grpc服务健康检查实现

  • 概述
    kubernetes本身不支持gRPC健康检查,本文记录使用 ‘grpc-health-probe’ 实现grpc服务的健康检查
    ‘grpc-health-probe’,这是 Kubernetes 原生的健康检查 gRPC 应用程序的方法
    在这里插入图片描述

    官方参考文档:https://kubernetes.io/zh-cn/blog/2018/10/01/health-checking-grpc-servers-on-kubernetes/

  • 示例

    • 下载grpc-health-probe文件
      按需下载指定版本grpc-health-probe文件

      下载地址:https://github.com/grpc-ecosystem/grpc-health-probe/releases

    • 配置grpc-health-probe
      将静态编译的grpc_health_probe打在容器映像中
      Dockerfile:

      COPY ocr/data/grpc_health_probe_v0.4.1 /bin/grpc_health_probe
      
      RUN chmod +x /bin/grpc_health_probe
      
    • 健康检查接口实现

      • 创建health.proto文件
      syntax = "proto3";
      
      // 定义grpc请求消息体
      message HealthCheckRequest {
        string service = 1;
      }
      
      // 定义grpc返回消息体
      message HealthCheckResponse {
        enum ServingStatus {
          UNKNOWN = 0;
          SERVING = 1;
          NOT_SERVING = 2;
        }
        ServingStatus status = 1;
      }
      
      // 定义服务接口
      service Health {
        rpc Check(HealthCheckRequest) returns (HealthCheckResponse);
      }
      
      • 健康检查接口实现
       class HealthServer(health_pb2_grpc.HealthServicer):
          """
          定义类继承HealthServicer
          """
      
          def __init__(self):
              pass
      
          def Check(self, request, context):
              """
              健康检查接口,实现proto文件中的服务接口
              """
              return health_pb2.HealthCheckResponse(status=HealthCheckResponse.ServingStatus.SERVING)
      
      
          def run(host, port):
              """
              运行ocr服务
              :param host:
              :param port:
              :return:
              """
              server = grpc.server(futures.ThreadPoolExecutor(max_workers=5))
              health_pb2_grpc.add_HealthServicer_to_server(HealthServer(), server)
              server.add_insecure_port(f'{host}:{port}')
              server.start()
              print('Grpc server connect successful!')
              # 启动服务,等待退出
              try:
                  while True:
                      time.sleep(600)
              except KeyboardInterrupt:
                  server.stop(0)
      
      
          if __name__ == '__main__':
              run(host='0.0.0.0', port='8001')
      
      • 容器中测试
      \bin\grpc-health-probe -addr :8001
      返回 SERVING
      
      • kubernetes中配置
      spec:
        containers:
        - name: server
          image: "[YOUR-DOCKER-IMAGE]"
          ports:
          - containerPort: 8001
          readinessProbe:
            exec:
              command: ["/bin/grpc_health_probe", "-addr=:8001"]
            initialDelaySeconds: 5
          livenessProbe:
            exec:
              command: ["/bin/grpc_health_probe", "-addr=:8001"]
            initialDelaySeconds: 10
      


    参考文档:
    grpc_health_probe配置:https://github.com/grpc-ecosystem/grpc-health-probe/

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值