Go and Redis running on Kubernetes with Minukube


Go is a simple, fast and powerful programing language. Go is growing a lot into the DevOps Engineering scene like Hashicorp Stack or even Kubernetes.  One of the main advantages of GO is the fact that you can generate a single binary with all you needed, bundled in a single file. This makes distribution so much easier.

Go is also very compact for some use cases and you can write so less code and still very very efficient and get best of performance.  Today we will see how to create a very, very, very simple service in go. This service will access redis to increment how many times it was called.  We will use Minikube in order to run kubernetes locally and we will store our data in Redis.




The Go Service

Let's get down to the code.

Here we are using an external library called go-redis. We need this library to communicate with Redis using Go. You can run this code on your machine right now but first, you need to install the Redis driver you can do it so by running $ go get -u github.com/go-redis/redis .Now you can simply run the app with $  go run main.go .Keep in mind this won't work right now because we don't have Redis running. You can download, install and run Redis or you can use docker. We will run redis with docker but on Kubernetes using Minikube.

As you can see we are exposing a service on the port :9090 and he have a handler function which takes care of the HTTP request to this very server. We want to avoid double counting for this service and some user might call it from the browser so we need to ignore the /favicon.ico request that's why we use the if on the very first lines of the code.

We are connecting on Redis but we are reading Redis URL from an OS env var. I'm doing this so we can run this code in many ways like locally, docker, docker-compose, and kubernetes. IF you have Redis locally now you can do $ REDIS_URL=localhost:6379 go run main.go .For this simple service, we are using Redis commands INCR and GET in order to increment keys(+1) and get the current value of the key.

Go Dockerfile

Right now we need to create a Dockerfile to run our go simple service. In order to do that we will create a Dockerfile file and add the following content:

Dockerfile is quite straightforward we are using GO lang 1.8 and we are installing the Redis driver and we copy the go source code to the container.

Kubernetes deployment

Now we need create 2 yaml files in order to describe the kuerbenetes deployment and Service for the go web app. However before doing that we will create 2 other yaml files in order to deploy Redis so our redis docker container runs on kubernetes as well. We also link connect this 2 containers using kubernetes internal DNS service.

Redis Deployment on Kubernetes

Let's create a file called: redis-deployment.yaml.

We also need to create a service. So create a file called redis-service.yaml.

The most important information here is that we are using the image called redis. We are also setting the spec on the service for LoadBalancing on the port 6379 and this means we will be able to access outside of the cluster. This is not required but is quite useful so you can use redis-cli and access the redis instance on Kubernetes.

Right now we can create the deployment and service yaml files for the web go simple service application.  So first create a file called: webappgo-deployment.yaml


Then, create another file called webappgo-service.yaml.

Here we have some important things that need to be noted such as the image which is: webappgo:v1. This image does not exist on public docker repo so we need to build on our local machine and send the docker image to minikube dockers registry.

When we write the go code we were expecting to receive the redis URL via OS ENV var. We can set this vars via Kubernetes and kubernetes will forward this vars to Docker we did this on the property called env. You might notice some specific name for the redis url which is: redis.default.svc.cluster.local:6379 .This is the kubernetes internal DNS. Where redis is the name of the service, the default is the default namespace and cluster.local is the domain.

Another important thing to notice is that the spec type on the service in LoadBalancer so we are exposing the port 9090.

kubectl and Minikube

okay, now we can go on and deploy theses files in kubernetes on our minikube local cluster.

There are some important steps here.  We are doing eval on minikube docker-env so we can use docker commands on out machine but send docker images to minikube docker registry. We are also baking a docker images for the go service application.

You might notify we are using kubectl create -f and passing a directory. This is created because you don't need to specify a file by files and long as you have redis and go deployment and services files properly in each respective directory.

Now we can run the GO simple service in our browser. You might need to run  $ kubectl describe services webappgo to get the proper port mapping.



IF you go to the minikube dashboard(http://192.168.99.100:30000/#!/pod?namespace=default) you can the see the GO service LOGs, like this:



You can get all the files in my github.

Cheers,
Diego Pacheco

Popular posts from this blog

Telemetry and Microservices part2

Installing and Running ntop 2 on Amazon Linux OS

Fun with Apache Kafka