Skip to content

jmservera/grpc

Repository files navigation

gRPC in 3 minutes (C#)

Modified example from the official gRPC repo, adapted to .NET 5, Docker and Kubernetes.

You can compile it and run it with Visual Studio 2022 and you will find a Kubernetes deployment file that will run the service and one client inside a k8s cluster.

MODIFICATIONS TO THE ORIGINAL EXAMPLE

This example has been modified to demonstrate a deployment into a K8s cluster:

  • The client runs in a loop and takes the server address as an environment value called GRPCSERVER.
  • The server now is bound to the 0.0.0.0 address and sends the server name into the response to easily check if there is load balancing present.
  • Two Dockerfile have been created to allow containerizing the solution, they have been autogenerated with Visual Studio, and modified to workaround a runtime issue that needs the c++ dev tools
  • A K8s deployment file has been created to demonstrate how to run it in Kubernetes, it has the needed labels to allow Linkerd to load-balance the calls.

PREREQUISITES

HOW TO RUN IN K8s

In Visual Studio right click on the GreeterServer project and click into Publish to publish it to your Container Registry. Do the same for the GreeterClient.

Once you have the containers uploaded to the CR, modify the deployment.yaml file with the image names of your registry and run kubectl apply -f deployment.yaml

Once all the deployment is up and running you can check the logs of the client:

kubectl logs pod/greeter-client greeter-client -n greeter -f

LOAD BALANCING

When you deploy this service to an AKS cluster and test it you will notice that even thought you are deploying 3 replicas of the service, the client is always connecting to the same one. This is a side effect of the gRPC connection using HTTP/2, that tries to have a single long-lived TCP connection as explained in the Kubernetes documentation.

To enable load balancing you will need to add a Service Mesh like Linkerd. The deployment file already contains the annotations to add the Linkerd sidecar proxies to your containers, so you only need to follow the install guide provided by Linkerd, and then redeploy the service:

kubectl delete -f deployment.yaml ; kubectl apply -f deployment.yaml

Original content below:


BACKGROUND

This is a version of the helloworld example using the dotnet SDK tools to compile helloworld.proto in a common library, build the server and the client, and run them.

PREREQUISITES

You can also build the solution Greeter.sln using Visual Studio 2017, but it's not a requirement.

BUILD AND RUN

  • Build and run the server

    > dotnet run -p GreeterServer
    
  • Build and run the client

    > dotnet run -p GreeterClient
    

Tutorial

You can find a more detailed tutorial about Grpc in gRPC Basics: C#

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors