Use the gRPC-Gateway plugin to proxy HTTP requests to a gRPC service

Uses: Kong Gateway decK
Minimum Version
Kong Gateway - 3.4
TL;DR

Create a Gateway Service with the grpc protocol, then create a Route and enable the gRPC-Gateway plugin. Specify the path to your Protobuf file in the config.proto parameter.

Prerequisites

This is a Konnect tutorial and requires a Konnect personal access token.

  1. Create a new personal access token by opening the Konnect PAT page and selecting Generate Token.

  2. Export your token to an environment variable:

     export KONNECT_TOKEN='YOUR_KONNECT_PAT'
    
    Copied to clipboard!
  3. Run the quickstart script to automatically provision a Control Plane and Data Plane, and configure your environment:

     curl -Ls https://get.konghq.com/quickstart | bash -s -- -k $KONNECT_TOKEN --deck-output
    
    Copied to clipboard!

    This sets up a Konnect Control Plane named quickstart, provisions a local Data Plane, and prints out the following environment variable exports:

     export DECK_KONNECT_TOKEN=$KONNECT_TOKEN
     export DECK_KONNECT_CONTROL_PLANE_NAME=quickstart
     export KONNECT_CONTROL_PLANE_URL=https://us.api.konghq.com
     export KONNECT_PROXY_URL='http://localhost:8000'
    
    Copied to clipboard!

    Copy and paste these into your terminal to configure your session.

Create a Protobuf definition

Use the following command to create a sample Protobuf definition:

echo 'syntax = "proto3";

package hello;

service HelloService {
 rpc SayHello(HelloRequest) returns (HelloResponse) {
   option (google.api.http) = {
     get: "/v1/messages/{name}"
     additional_bindings {
       get: "/v1/messages/legacy/{name=**}"
     }
     post: "/v1/messages/"
     body: "*"
   };
 }
}


// The request message containing the name.
message HelloRequest {
 string name = 1;
}

// The response message containing the greeting
message HelloResponse {
 string message = 1;
}' > hello-gateway.proto
Copied to clipboard!

This sample definition modifies the SayHello method available on grpcb.in and will return a message containing “Hello” followed by a name that can be specified either in the URL for GET requests or in the request body for POST requests.

Add the Protobuf definition to your Docker container

Since Konnect data plane container names can vary, set your container name as an environment variable:

export KONNECT_DP_CONTAINER='your-dp-container-name'
Copied to clipboard!

Use the following command to add hello-gateway.proto to the /usr/local/kong directory in your Kong Gateway Docker container:

docker cp hello-gateway.proto $KONNECT_DP_CONTAINER:/usr/local/kong
Copied to clipboard!

Create a Gateway Service and a Route

In this example, we want a Route that serves the HTTP protocol but proxies to a Gateway Service with the gRPC protocol. For testing purposes, we’ll use grpcb.in as the upstream service.

echo '
_format_version: "3.0"
services:
  - name: example-grpc-service
    protocol: grpc
    host: grpcb.in
    port: 9000
routes:
  - name: http-route
    protocols:
    - http
    paths:
    - "/"
    service:
      name: example-grpc-service
' | deck gateway apply -
Copied to clipboard!

Enable the gRPC-Gateway plugin

Configure the plugin to use the Protobuf definition we created:

echo '
_format_version: "3.0"
plugins:
  - name: grpc-gateway
    route: http-route
    config:
      proto: usr/local/kong/hello-gateway.proto
' | deck gateway apply -
Copied to clipboard!

Validate

To validate that the configuration is working as expected, you can:

  • Send a GET request to /v1/messages/ or /v1/messages/legacy/, with a name in the URL:
 curl "$KONNECT_PROXY_URL/v1/messages/MyName"
Copied to clipboard!
  • Send a POST request to /v1/messages/ with a name in the request body:
 curl -X POST "$KONNECT_PROXY_URL/v1/messages/" \
     --json '{
       "name": "MyName"
     }'
Copied to clipboard!

Cleanup

If you created a new control plane and want to conserve your free trial credits or avoid unnecessary charges, delete the new control plane used in this tutorial.

Did this doc help?

Something wrong?

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!