ShixiongQi / spright-knative-grpc

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Serverless gRPC with Knative

gRPC

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.

Create a gRPC server & client

Follow the instructions for your preferred language to create a simple gRPC server and client:

Build and push Docker image

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

Deploy the Knative service

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

Test the service

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.

About


Languages

Language:Python 96.5%Language:Dockerfile 3.5%