How to develop Go gRPC microservices and deploy in Kubernetes

Shalauddin Ahamad Shuza
HackerNoon.com
5 min readFeb 2, 2019

--

A few month back, I started my journey learning gRPC. This article is to demonstrate how we can use gRPC to develop microservices using Go and deploy them in kubernetes cluster.
We will develop two microservice. One microservice will be responsible for calculating summation of two integer and other will serve a public REST API.

Prerequisites

There are many many way to run kubernetes cluster in local machine. I am going to use Minikube for this article. We also need to install kubectl, Docker and Protobuf Compiler.

To start Minikube you have to run following command with root privileges

Define communication protocol

As an underlying transport protocol we will use gRPC. For that we need to write definitions for message types and services in Protocol Buffer’s interface definition language and compile them. In your project root directory create a file named add.proto inside pb directory.

To compile this proto file navigate to pb directory and run the following command

After successful compilation it will produce add.pb.go file in the same directory.

Implement summation service

To implement summation service we need to use auto-generated code. Now create main.go file inside add directory and make sure you import correct packages

Now implement the Compute handler function that will add two integer from using auto-generated pb.AddServiceClient interface.

Noe in the main function, register a server type which will handle requests. Then start the gRPC server.

API Service

API service use Gorilla Mux to serve REST API response to the client and route them.Create a client to communicate with the Add Service. To communicate with Add Service we will use service name add-service because later we will deploy our service in kubernetes cluster. Kubernetes has a built-in DNS service, so we can access by service name.

Here I have declared a handler for /add/{a}/{b} endpoint which reads parameters A and B and then call Add Service for summation.

Build Docker Images

Now your services are ready but need to containerize them to deploy in kubernetes cluster. For Add Service create a Dockerfile inside add directory

To build and push summation-service image in DockerHub navigate to add directory and run following commands

For Api Service create Dockerfile inside api directory

To build and push api-service image navigate to api directory and run following commands

Deploying to Kubernetes cluster

For each service we need to configure two object in kubernetes Deployment and Service. Deployment will create poda inside kubernetes cluster and manage desire status of those pods to make sure we have our application running to serve traffic. Service will provide fixed address to access those pods.
For Summation service create add-service.yaml file and insert following commands

For api-service create api-service.yaml file

Main difference between two service definition is in api-service we have declared it as NodePort type as it can be accessible from outside of kubernetes cluster.
Now create those resources by running following commands

Wait until all the pods become available or in running state

Conclusion

Let’s verify our system. To get URL of our api-service run

Make a request to the service using previously found IP address

And it will show the summation result.

You can find entire source code on GitHub.

Originally published at shuza.ninja on February 2, 2019.

--

--