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.
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.
- Visual Studio 2022 and Docker Desktop to build and run the Greeter.sln.
- An AKS cluster
- A Container Registry for storing the Docker images
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 -fWhen 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.yamlOriginal content below:
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.
You can also build the solution Greeter.sln using Visual Studio 2017,
but it's not a requirement.
-
Build and run the server
> dotnet run -p GreeterServer -
Build and run the client
> dotnet run -p GreeterClient
You can find a more detailed tutorial about Grpc in gRPC Basics: C#