Search code examples
gogrpcgrpc-go

How to connect to the gRPC service inside k8s cluster from outside gRPC client


I have a gRPC server running on port 9000 with gRPC-gateway running on port 9080. I can make request to my service with postman using the following link: ```http://cluster1.example.com/api/v1/namespaces/default/services/my-service:9080/proxy

How can I connect to my service from gRPC client (on my local machine, which is outside of the cluster) using grpc.Dial()?

Example:

conn, err := grpc.Dial(...?, grpc.WithInsecure())
if err != nil {
    panic(err)
}

Solution

  • You should be able to connect to services in your k8s cluster from local with port forwarding:

    kubectl port-forward --context <mycontext> -n <mynamespace> svc/my-service 9000:9000

    And then you just pass the gRPC target into Dial with localhost and no scheme:

    conn, err := grpc.Dial("localhost:9000", grpc.WithInsecure())
    if err != nil {
        panic(err)
    }
    

    I might state the obvious, but of course the server also must be started in insecure mode (no credentials), otherwise you might get Unavailable response code.