Go Micro on Kubernetes: Getting started
By: Jm Robles
Tags: Go, Microservices, How To, Kubernetes, Golang

TL;DR: How to start to develop and deploy microservices with Go Micro ecosystem on a Kubernetes cluster.

Microservices in Go rules!

Introduction

Go micro is a microservices ecosystem for developing microservices without the “glue” boilerplate code (registry, authentication, broker, …)

Just focus on your business logic code (aka “the handlers”).

With its latest version, v3, it’s pretty easy to develop microservices.

It’s an ecosystem because go-micro beyond a framework. It includes several core services such as authentication, broker (pub/sub), KV storage, API proxy, gRPC proxy, etc.

The default message protocol is gRPC.

gRPC uses an efficient data binary encoding based in a data schema, protocol buffers. It minimizes message processing and its size.

You can use the out-of-the-box core microservices or specify yours (i.e. Kafka as broker).

Copied from the micro.mu website

Micro is released with a “special-half-open source” license. You can do what all you want except make a business offering as a service (avoid AWS, GCP, etc). It’s similar to the Confluence license.

Motivation

I’ve written this “Gettings started” guide, because there is documentation about getting started with Micro for development or in a local environment but not for the cloud with Kubernetes (at least in this moment with the v3 version).

We just launch a “helloworld” microservice and access it in our Kubernetes cluster.

Pre-requisites

You need to have an alive Kubernetes cluster.

If you don’t have one and you want one in the cloud, follow this post and create a cheap Kubernetes cluster on Hetzner.

Let’s go!

We start installing “micro” in our cluster. After that, we code and deploy our “hello world”. Finally, we create an ingress access and test it.

1. Installing Micro in our K8S cluster

You can install Micro with the helm chart o apply the following YAML (it’s the same included in the chart).

This YAML create several RoleBinding, PV, PVC and finally execute the “Micro” Job. All under the namespace “micro”.

NOTE: For the PV/PVC use “hcloud-storage” as CSI driver. This driver is specific for Hetzner CSI plugin. Change to yours.

After we apply it with the “apply” way

% kubectl apply -f micro-setup.yaml

We can find the core microservices pods running.

Rancher’s screen capture with the micro pods list

Sounds good!

It’s time to code our “hello world”.

2. Building the “hello world” microservice

The code for “Hello World” is included in the Micro documentation.

Just clone the repository https://github.com/micro/services and access to the folder “helloworld”.

The main.go is very simple.

The code is self-explanatory.

First, we create a new service with the name “helloworld” and version “latest” (BTW: “latest” is not a version, it’s a tag. Please, use semver instead).

In line 19, we register our micro server and our handler into the protocol buffers (pb) stub. We’ll talk about this now.

Finally, we enter the service loop.

The keys here are our protocol buffer micro stub (API) and the methods implemented in the handler (implementation).

I’m not going to go into details about gRPC. There is a lot of information on the Internet if you need to know more.

In gRPC (PB) all starts with the protocol specification.

For our microservice, the “proto” spec is quite simple.

This API definition just declares a method, Call. We just pass a “name” and we’ll receive a greeting message.

Before you can generate the stubs, you need to have installed the proto tool and its plugins (go and micro). For install it, just execute the next go get

% go get -u github.com/golang/protobuf/proto

% go get -u github.com/golang/protobuf/protoc-gen-go

% go get github.com/micro/micro/v3/cmd/protoc-gen-micro

(If you have troubles installing the protoc-gen-micro, set GO111MODULE=on )

We compile the API spec for Go stab and Micro plugin stab installing and executing the following command.

% protoc --proto_path=. --micro_out=. --go_out=:. proto/helloworld.proto

If all goes well, this command generates two files: helloworld.pb.go and helloworld.pb.micro.go .

It only remains to see the handler.

The hander just implements the Call method. It registers a log message and returns a greeting message.

Now we can compile it.

% CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o helloworld *.go

And generate our simple Docker container.

% docker build -t jmrobles/micro-helloworld .

Prior to use, we need to push to Docker Hub registry (or whatever you want).

% docker push jmrobles/micro-helloworld

NOTE: You can use directly this container if you want to skip the container generation and runs the micro service.

3. Deployment in Kubernetes

Before proceeding to install our pod in kubernetes, let’s see how microservices intercommunicate in kubernetes.

The authentication of the microservice in the “ring” of microservices is done using an API Key and API secret.

All the “ring” uses the same keys

All the microservices use the same MICRO_AUTH_PUBLIC_KEY and MICRO_AUTH_PRIVATE_KEY .

You can re-use the secret of a core services (API for example) or duplicate and create your own secret.

Our deployment and service YAML.

The important thing here is the environment variables for Micro. These tell you where the network proxy is, the authentication, etc.

NOTE: This environment vars are un-documented. They may not all be necessary or may change. An official documentation would be nice :)

We apply it.

% kubectl apply -f deploy-helloworld.yaml

If there is no problem, our microservice will be running.

It’s log must be similar to the following

Time to test it!

4. Testing from the CLI

We can test it from any service container.

Just execute a shell, login and do the call

Just works!

We can check the log too.

5. Access via API with Ingress

Finally, we are going to expose our microservices via ingress.

The API microservice lets to access to other microservices via REST JSON.

Automatically. You don’t need to write any further boilerplate code!

In my case, I use Let’s Encrypt SSL certificates (using cert-manager).

This is my ingress for this service

We test it with cURL