go gRPC routing without specifying port

4/24/2018

New to gRPC:

Having a gRPC client, how do you use routing?

My gRPC server is at this local path 10.0.1.6/hw.

It is running and listening in a Kubernetes pod and it is working internally, if I run it in a pod. But I am now accessing it via url and ingress.

But how do I test that it actually is working with the routing without a port number?

const (
    address     = "10.0.1.6:80/hw"
    defaultName = "World"
)

var (
    conn *grpc.ClientConn
    c    pb.GreeterClient
)

func setupRPC() {

    var err error
    conn, err = grpc.Dial(address, grpc.WithInsecure())
    c = pb.NewGreeterClient(conn)
    if err != nil {
        log.Fatalf("did not connect: %v", err)
    }

}

I get this error:

could not greet: rpc error: code = Unavailable desc = all SubConns are in TransientFailure, latest connection error: connection error: desc = "transport: Error while dialing dial tcp: lookup tcp/80/hw: nodename nor servname provided, or not known"

Do you know how? Or how to test gRPC endpoints?

-- Chris G.
go
grpc
istio
kubernetes

1 Answer

9/28/2019

If it is working internally in the pod, then you only need to expose it. You can do it by adding a yaml definition for a service. Like the following.

apiVersion: v1
kind: Service
metadata:
  namespace: $YOUR_NAMESPCAE
  labels:
    app: $YOUR_SERVICE_NAME
  name: $YOUR_SERVICE_NAME
spec:
  ports:
  - name: $YOUR_GRCP_PORT
    port: $YOUR_GRCP_PORT
    targetPort: $YOUR_GRCP_PORT
  selector:
    app: $YOUR_SERVICE_NAME
status:
  loadBalancer: {}

Note that where it says $YOUR_GRCP_PORTyou define the grcp port you will expose.

Now you only need to edit the ingress you mentioned. It should be something like the following.

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: vizix-ingress
  namespace: preprod-vizix-io
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: "/"
spec:
  rules:
  - http:
      paths:
      - path: /
        backend:
          serviceName: $YOUR_SERVICE_NAME
          servicePort: $YOUR_GRCP_PORT
-- Rodrigo Loza
Source: StackOverflow