当我使用kubernetes API对grpc服务器进行健康检查时,是否需要proto?

shstlldc  于 2024-01-06  发布在  Kubernetes
关注(0)|答案(1)|浏览(158)
  1. from kubernetes import client, config
  2. from grpc_health.v1 import health_pb2, health_pb2_grpc
  3. import grpc.experimental.aio as aio
  4. import asyncio
  5. async def check_grpc_health(ip, port):
  6. try:
  7. channel = aio.insecure_channel(f"{ip}:{port}")
  8. stub = health_pb2_grpc.HealthStub(channel)
  9. response = await stub.Check(health_pb2.HealthCheckRequest(service=""), timeout=1)
  10. await channel.close()
  11. return response.status == health_pb2.HealthCheckResponse.SERVING
  12. except Exception as e:
  13. print(f"Error checking health of gRPC service at {ip}:{port}: {e}")
  14. return False
  15. async def check_pod_health(pod):
  16. ip = pod.status.pod_ip
  17. port = None
  18. if pod.spec.containers and pod.spec.containers[0].ports:
  19. port = pod.spec.containers[0].ports[0].container_port
  20. if ip is not None and await check_grpc_health(ip, port):
  21. print(f"Pod {pod.metadata.name} is healthy.")
  22. else:
  23. print(f"Pod {pod.metadata.name} is unhealthy, consider restarting.")
  24. async def main():
  25. config.load_kube_config()
  26. v1 = client.CoreV1Api()
  27. namespace = "apple-engine"
  28. label_selector = "grpc-health-check"
  29. print("finding...")
  30. pods = await v1.list_namespaced_pod(namespace=namespace, watch=False)
  31. print(len(pods.items))
  32. tasks = [check_pod_health(pod) for pod in pods.items]
  33. await asyncio.gather(*tasks)
  34. if __name__ == "__main__":
  35. asyncio.run(main())

字符串
我正在使用kubernetes API编写这个健康检查python代码,但不考虑proto文件。
如果我使用这个健康检查代码,是否需要proto文件?只是想检查与proto的连接,不完全考虑命名空间和标签选择器

相关问题