您现在的位置是:首页 >技术教程 >python - kubernetes中grpc服务健康检查实现网站首页技术教程
python - kubernetes中grpc服务健康检查实现
简介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')
- 容器中测试
bingrpc-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/ -
风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。