Use the gRPC-Gateway plugin to proxy HTTP requests to a gRPC service
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
Kong Konnect
This is a Konnect tutorial and requires a Konnect personal access token.
-
Create a new personal access token by opening the Konnect PAT page and selecting Generate Token.
-
Export your token to an environment variable:
export KONNECT_TOKEN='YOUR_KONNECT_PAT'
-
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
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'
Copy and paste these into your terminal to configure your session.
Kong Gateway running
This tutorial requires Kong Gateway Enterprise. If you don’t have Kong Gateway set up yet, you can use the quickstart script with an enterprise license to get an instance of Kong Gateway running almost instantly.
-
Export your license to an environment variable:
export KONG_LICENSE_DATA='LICENSE-CONTENTS-GO-HERE'
-
Run the quickstart script:
curl -Ls https://get.konghq.com/quickstart | bash -s -- -e KONG_LICENSE_DATA
Once Kong Gateway is ready, you will see the following message:
Kong Gateway Ready
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
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'
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 kong-quickstart-gateway:/usr/local/kong
docker cp hello-gateway.proto $KONNECT_DP_CONTAINER:/usr/local/kong
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 -
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 -
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"
curl "http://localhost:8000/v1/messages/MyName"
- 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"
}'
curl -X POST "http://localhost:8000/v1/messages/" \
--json '{
"name": "MyName"
}'
Cleanup
Clean up Konnect environment
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.
Destroy the Kong Gateway container
curl -Ls https://get.konghq.com/quickstart | bash -s -- -d