Skip to main content

Version: latest

Proxy gRPC Traffic

Google Remote Procedure Call (gRPC) is an open-source, high-performance RPC framework built on the HTTP/2 protocol and using Protocol Buffers (protobuf) as its Interface Description Language (IDL).

This guide will show you how to configure the gateway to proxy gRPC traffic using the Ingress Controller.

Prerequisites

  1. Complete Set Up Ingress Controller and Gateway.
  2. Install gRPCurl to verify gRPC traffic proxying.

Start an Example Upstream Service

Create a Kubernetes manifest file for the gRPC server deployment and service:

grpc-server.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: aic
name: grpc-service
spec:
replicas: 1
selector:
matchLabels:
app: grpc-service
template:
metadata:
labels:
app: grpc-service
spec:
containers:
- name: grpc-service
image: api7/grpc-server-example:1.0.0
ports:
- containerPort: 50051
---
apiVersion: v1
kind: Service
metadata:
namespace: aic
name: grpc-service
spec:
type: ClusterIP
selector:
app: grpc-service
ports:
- name: grpc
port: 50051
targetPort: 50051

Apply the configuration to your cluster:

kubectl apply -f grpc-server.yaml

To verify whether the gRPC server is running, first, port forward the gRPC server's service port to your local machine:

kubectl port-forward svc/grpc-service 50051:50051 &

Send a request to the service to list all available gRPC services and methods:

grpcurl -plaintext 127.0.0.1:50051 list

If everything is ok, you should see the following response:

grpc.reflection.v1alpha.ServerReflection
helloworld.Greeter
helloworld.TestImport

List all the available methods for the helloworld.Greeter service:

grpcurl -plaintext 127.0.0.1:50051 list helloworld.Greeter

If everything is ok, you should see the following response:

helloworld.Greeter.GetErrResp
helloworld.Greeter.Plus
helloworld.Greeter.SayHello
helloworld.Greeter.SayHelloAfterDelay
helloworld.Greeter.SayHelloBidirectionalStream
helloworld.Greeter.SayHelloClientStream
helloworld.Greeter.SayHelloServerStream

Create a Route

Create a Kubernetes manifest file to configure a GRPCRoute to the gRPC service:

grpc-route.yaml
apiVersion: gateway.networking.k8s.io/v1
kind: GRPCRoute
metadata:
namespace: aic
name: grpc-route
spec:
parentRefs:
- name: apisix
rules:
- matches:
- method:
service: helloworld.Greeter
method: SayHello
backendRefs:
- name: grpc-service
port: 50051

Apply the configurations to your cluster:

kubectl apply -f grpc-route.yaml

Update Gateway Configuration for HTTP/2

By default, the gateway supports TLS-encrypted HTTP/2 on port 9443. For non-encrypted HTTP/2, you can add a new port to the gateway configuration.

In this section, you will be updating the gateway configuration and deployment to support non-encrypted HTTP/2 on 9081.

To update the gateway's HTTP2 configuration, first export all values (including defaults):

helm get values -n aic apisix --all > values.yaml

In the values file, update the following section values as such:

values.yaml
service:
http:
containerPort: 9080
enabled: true
servicePort: 80
additionalContainerPorts:
- port: 9081
enable_http2: true

Upgrade the release:

helm upgrade -n aic apisix apisix/apisix -f values.yaml

Verify

Download the helloworld.proto file here.

This example uses the helloworld.proto file to ensure the gRPCurl CLI tool aligns the request and response format with the gRPC service definition.

Expose the gateway’s HTTP/2 service port to your local machine:

# replace with your gateway’s service name
kubectl port-forward svc/<gateway-service-name> 9081:9081 &

Send a request to the route:

grpcurl -plaintext \
-proto helloworld.proto \
-d '{"name":"World"}' \
"127.0.0.1:9081" \
"helloworld.Greeter.SayHello"

You should see the following output:

{
"message": "Hello World"
}
API7.ai Logo

The digital world is connected by APIs,
API7.ai exists to make APIs more efficient, reliable, and secure.

Sign up for API7 newsletter

Product

API7 Gateway

SOC2 Type IIISO 27001HIPAAGDPRRed Herring

Copyright © APISEVEN PTE. LTD 2019 – 2025. Apache, Apache APISIX, APISIX, and associated open source project names are trademarks of the Apache Software Foundation