gRPC is a high performance, language agnostic, open-source RPC framework. It has many benefits such as efficient Protobuf binary serialization, client/server/bi-directional streaming options, common service definitions with Protobuf with language agnostic implementations, and more.
In this lab, we will deploy a 'serverless' gRPC service with Knative.
Follow the instructions for your preferred language to create a simple gRPC server and client:
Build and push the Docker image (replace {username}
with your actual DockerHub):
docker build -t {username}/grpc-greeter:v1 .
docker push {username}/grpc-greeter:v1
Create a service.yaml file.
Notice how we need to define a port with h2c
(HTTP/2) for gRPC to work. More info on this in runtime-contract.
Deploy the service:
kubectl apply -f service.yaml
Check that the service is created and pods of the service are running:
kubectl get ksvc grpc-greeter
NAME
grpc-greeter
kubectl get pods
NAME
grpc-greeter-5tpwl-deployment-6fb423289c5-r5qmt
To test your service, go to the greeter client and change the url from localhost to the Knative service url.
-
C#
Inside GrpcGreeterClient, change Program.cs:
//httpClient.BaseAddress = new Uri("http://localhost:50051"); httpClient.BaseAddress = new Uri("http://grpc-greeter.default.34.77.201.183.xip.io");
Run the app:
dotnet run Greeting: Hello GreeterClient Press any key to exit...
-
Python
Get the address of your gRPC server:
GRPC_SERVER="$(kubectl get ksvc grpc-greeter -o jsonpath='{.status.url}')"
Run the app:
python greet_client.py -s $GRPC_SERVER -p 80 Greeting: Hello GreeterClient
Voilà! The gRPC client is now talking to a serverless gRPC service on Knative.