当我使用 kubernetes api 来健康检查 grpc 服务器时,需要 proto 吗?还是不?

问题描述 投票:0回答:1
from kubernetes import client, config
from grpc_health.v1 import health_pb2, health_pb2_grpc
import grpc.experimental.aio as aio
import asyncio

async def check_grpc_health(ip, port):
    try:
        channel = aio.insecure_channel(f"{ip}:{port}")
        stub = health_pb2_grpc.HealthStub(channel)
        response = await stub.Check(health_pb2.HealthCheckRequest(service=""), timeout=1)  
        await channel.close()
        return response.status == health_pb2.HealthCheckResponse.SERVING
    except Exception as e:
        print(f"Error checking health of gRPC service at {ip}:{port}: {e}")
        return False

async def check_pod_health(pod):
    ip = pod.status.pod_ip
    port = None
    if pod.spec.containers and pod.spec.containers[0].ports:
        port = pod.spec.containers[0].ports[0].container_port
    if ip is not None and await check_grpc_health(ip, port):
        print(f"Pod {pod.metadata.name} is healthy.")
    else:
        print(f"Pod {pod.metadata.name} is unhealthy, consider restarting.")

async def main():
    config.load_kube_config()
    v1 = client.CoreV1Api()
    namespace = "apple-engine"
    label_selector = "grpc-health-check"

    print("finding...")
    pods = await v1.list_namespaced_pod(namespace=namespace, watch=False)
    print(len(pods.items))

    tasks = [check_pod_health(pod) for pod in pods.items]
    await asyncio.gather(*tasks)

if __name__ == "__main__":
    asyncio.run(main())

我正在使用 kubernetes api 编写此健康检查 python 代码,但不考虑 proto 文件。

如果我使用这个健康检查代码,是否需要proto文件? 只是想检查与 proto 的连接,而不是完全考虑命名空间和标签选择器

kubernetes protocol-buffers grpc health-check
1个回答
0
投票

gRPC 项目提供了 gRPC 运行状况检查协议PyPI 实现,称为

grpcio-health-checking
,您可以使用。

© www.soinside.com 2019 - 2024. All rights reserved.