Proxy gRPC traffic using GRPCRoute

Incompatible with
konnect
Related Documentation
Minimum Version
Kong Operator - 2.3
TL;DR

Annotate the backend Service with konghq.com/protocol=grpcs, add an HTTPS listener to your Gateway, then create a GRPCRoute resource. Kong Operator converts the GRPCRoute into a Kong Gateway Service and Route.

Prerequisites

  1. Add the Kong Helm charts:

    helm repo add kong https://charts.konghq.com
    helm repo update
  2. Install Kong Operator using Helm:

    helm upgrade --install kong-operator kong/kong-operator -n kong-system \
      --create-namespace \
      --set image.tag=2.3 \

    If you want cert-manager to issue and rotate the admission and conversion webhook certificates, install cert-manager to your cluster and enable cert-manager integration by passing the following argument while installing, in the next step:

    --set global.webhooks.options.certManager.enabled=true

    If you do not enable this, the chart will generate and inject self-signed certificates automatically. We recommend enabling cert-manager to manage the lifecycle of these certificates. Kong Operator needs a certificate authority to sign the certificate for mTLS communication between the control plane and the data plane. This is handled automatically by the Helm chart. If you need to provide a custom CA certificate, refer to the certificateAuthority section in the values.yaml of the Helm chart to learn how to create and reference your own CA certificate.

Apply a KongLicense. This assumes that your license is available in ./license.json

echo "
apiVersion: configuration.konghq.com/v1alpha1
kind: KongLicense
metadata:
 name: kong-license
rawLicenseString: '$(cat ./license.json)'
" | kubectl apply -f -

Install gRPCurl, which is a CLI that lets you interact with gRPC servers. This tutorial requires gRPCurl for validation.

GRPCRoute is a Kubernetes Gateway API resource for routing gRPC traffic. This guide shows how to configure Kong Operator to proxy gRPC traffic secured with TLS to a backend Service.

Create the kong namespace

Create the kong namespace in your Kubernetes cluster, which is where the demo will run:

kubectl create namespace kong

Configure the Gateway

Create a GatewayConfiguration, GatewayClass, and Gateway with an HTTP listener:

echo '
apiVersion: gateway-operator.konghq.com/v2beta1
kind: GatewayConfiguration
metadata:
  name: kong-gateway-configuration
  namespace: kong
spec:
  dataPlaneOptions:
    deployment:
      podTemplateSpec:
        spec:
          containers:
            - image: kong/kong-gateway:3.15
              name: proxy
---
apiVersion: gateway.networking.k8s.io/v1
kind: GatewayClass
metadata:
  name: kong-grpc
spec:
  controllerName: konghq.com/gateway-operator
  parametersRef:
    group: gateway-operator.konghq.com
    kind: GatewayConfiguration
    name: kong-gateway-configuration
    namespace: kong
---
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
  name: kong-grpc-gateway
  namespace: kong
spec:
  gatewayClassName: kong-grpc
  listeners:
    - name: http
      port: 80
      protocol: HTTP' | kubectl apply -f -

Deploy a gRPC backend

Install the grpcbin Service, which implements a simple gRPC test API:

kubectl apply -f https://developer.konghq.com/manifests/kic/grpcbin-service.yaml -n kong

Annotate the Kubernetes Service

Kong Gateway assumes Services are HTTP or HTTPS by default. Add the konghq.com/protocol annotation so Kong Operator configures the backend Service as gRPC over TLS instead:

kubectl annotate service -n kong grpcbin 'konghq.com/protocol=grpcs'

Generate a TLS certificate

  1. Create a test certificate for the example.com hostname. This will be used to secure TLS traffic.

    Older OpenSSL versions, including the version provided with macOS Monterey, require using the alternative version of this command.

  2. Create a Secret containing the certificate:
     kubectl create secret -n kong tls example.com --cert=./server.crt --key=./server.key
  3. Kong Operator only watches certificate Secrets that carry the konghq.com/secret="true" label. Add the label so Kong Operator picks up the Secret you just created:

    kubectl label secret example.com -n kong konghq.com/secret="true"

Route gRPC traffic

  1. Re-apply the kong-grpc-gateway Gateway with an additional HTTPS listener for gRPC traffic:

    Warning: Applying this Gateway replaces the listener list. Include every listener you want to keep.

    echo 'apiVersion: gateway.networking.k8s.io/v1
    kind: Gateway
    metadata:
      name: kong-grpc-gateway
      namespace: kong
    spec:
      gatewayClassName: kong-grpc
      listeners:
        - name: http
          port: 80
          protocol: HTTP
        - name: grpc
          port: 443
          protocol: HTTPS
          hostname: example.com
          tls:
            certificateRefs:
              - group: ""
                kind: Secret
                name: example.com' | kubectl apply -f -
  2. Create a GRPCRoute:

    echo 'apiVersion: gateway.networking.k8s.io/v1
    kind: GRPCRoute
    metadata:
      name: grpcbin
      namespace: kong
    spec:
      parentRefs:
        - name: kong-grpc-gateway
          sectionName: grpc
      hostnames:
        - "example.com"
      rules:
        - backendRefs:
            - name: grpcbin
              port: 9001
    ' | kubectl apply -f -

This configuration instructs Kong Gateway to forward gRPC requests for example.com on port 443 to the grpcbin Service on port 9001.

Validate

  1. Wait for the Gateway to be programmed:

    kubectl wait gateway/kong-grpc-gateway -n kong \
      --for=condition=Programmed=True \
      --timeout=5m
  2. Get the Gateway’s external IP:

    export PROXY_IP=$(kubectl get gateway kong-grpc-gateway -n kong -o jsonpath='{.status.addresses[0].value}')
    echo $PROXY_IP
  3. Call the grpcbin test service through the proxy:

    grpcurl -insecure -authority example.com \
      -d '{"greeting": "Kong"}' \
      $PROXY_IP:443 hello.HelloService.SayHello

    The results should look like this:

    {
      "reply": "hello Kong"
    }

Help us make these docs great!

Kong Developer docs are open source. If you find these useful and want to make them better, contribute today!