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

  • 概述
    kubernetes本身不支持gRPC健康检查,本文记录使用 ‘grpc-health-probe’ 实现grpc服务的健康检查
    ‘grpc-health-probe’,这是 Kubernetes 原生的健康检查 gRPC 应用程序的方法
    python - kubernetes中grpc服务健康检查实现_第1张图片

    官方参考文档: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/

你可能感兴趣的:(Python,python,kubernetes,开发语言)