Traefik gRPC Load Balancing and Traces Propagation

- kubernetes gRPC Go devops

Following my recent blog post on setting up a dev environment in Kubernetes, here are some tips to use Traefik as a gRPC load balancer.

Traefik can be used on the edge and route incoming HTTP traffic to your Kubernetes cluster, but it’s also supporting gRPC.

gRPC Load Balancing with Traefik

Here I have a gRPC service I want to expose on the edge.

apiVersion: v1
kind: Service
metadata:
  name: myapp
  labels:
    name: "myapp"
    type: "grpc"
spec:
  ports:
    - port: 9200
      name: "grpc"
      targetPort: grpc
      protocol: TCP
  selector:
    app: "myapp"
  clusterIP: None

Note the clusterIP: None, it’s a headless service.

It will create a non loadbalanced service, pod’s services can be accessed directly.

myapp.default.svc.cluster.local.    2       IN      A       172.17.0.19
myapp.default.svc.cluster.local.    2       IN      A       172.17.0.10
myapp.default.svc.cluster.local.    2       IN      A       172.17.0.16

Here is the ingress for Traefik.

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: myapp-ingress
  namespace: default
  labels:
    name: "myapp"
  annotations:
    ingress.kubernetes.io/protocol: h2c
spec:
  rules:
  - host: myapp-lb.minikube
    http:
      paths:
      - path: /
        backend:
          serviceName: myapp
          servicePort: 9200

Note the h2c prefix, indicating HTTP2 protocol without TLS to your backend !

Traefik

Tracing

Traefik can be configured to emit tracing.

I’m using ocgrpc Opencensus, for gRPC metrics & traces.
It automatically emits several counters for gRPC and traces using the StatsHandler.

Unfortunately ocgrpc does not yet propagate Jaeger traces, I’ve temporary forked it to support Jaeger.

As you can see you can follow the request from Traefik down to your services.

Jaeger

Happy tracing !