A Data Caching Service in Kubernetes

A Data Caching Service in Kubernetes

Deeptiman Pattnaik's photo
Deeptiman Pattnaik
·Sep 30, 2020·

5 min read

Subscribe to my newsletter and never miss my upcoming articles

In this article, I will try to explain building a data caching service with MongoDB and Redis deploying under a minikube environment. The web application is written using the Go programming language.

Github: https://github.com/Deeptiman/go-cache-kubernetes


The development environment should have Go as a programming language, MongoDB as persistence storage, and Redis as data caching service.

Note: The following installation of MongoDB, Redis Cache is only supported in the local development environment and the Kubernetes deployment environment for each module follows a different setup.


$ sudo apt-get update
$ sudo apt-get install golang-go

Setup the GOPATH

$ export GOPATH=$HOME/go
$ export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin
$ source ~/.profile
$ go version
$ go version go1.15 linux/amd64


The dependency module of MongoDB is required to install a *mongo-go-driver *that has all the Go APIs to query a MongoDB database.

$ go get go.mongodb.org/mongo-driver/mongo

mongodb/mongo-go-driver The MongoDB supported driver for Go. Go 1.10 or higher. We aim to support the latest supported versions of go. MongoDB…github.com

Redis Cache

The *go-redis* Go client library will work as a dependency module to integrate the data caching service.

$ go get github.com/go-redis/redis/v8

go-redis/redis ❤️ Uptrace.dev - distributed traces, logs, and errors in one place API docs…github.com

redis-server: The development environment also required to install a Redis server that will provide various data structures, caching mechanisms and a message broker.

 $ wget [http://download.redis.io/releases/redis-6.0.8.tar.gz](http://download.redis.io/releases/redis-6.0.8.tar.gz)
 $ tar xzf redis-6.0.8.tar.gz
 $ cd redis-6.0.8
 $ make 


Build the Docker images

The web application has been built under a docker container environment that will deploy into the Kubernetes.


$ sudo apt install docker.io
$ sudo apt install docker-compose

Build the image

$ docker build -t go-cache-kubernetes-v1 .

Tag the image

$ docker tag go-cache-kubernetes-v1 deeptiman1991/go-cache-kubernetes-v1:1.0.0

Login to Docker Hub

$ docker login

Type Username and Password to complete the instruction

Push the image to Docker Hub

$ docker push deeptiman1991/go-cache-kubernetes-v1:1.0.0

Kubernetes Tools

The minikube and kubectl two major Kubernetes tools that required to be installed in the local environment.

*minikube** will be running as a single-node Kubernetes cluster under a Virtual machine. The computer must support Virtualization and [HyperVisor](en.wikipedia.org/wiki/Hypervisor#:~:text=A%.. needs to be enabled. [Hyperkit](minikube.sigs.k8s.io/docs/drivers/hyperkit/)* is a recommended virtualization kit to start the minikube.

$ sudo install minikube
$ minikube start

Installing Kubernetes with Minikube Minikube is a tool that makes it easy to run Kubernetes locally. Minikube runs a single-node Kubernetes cluster inside…kubernetes.io

*kubectl* is a command-line tool that will provide several useful commands to deploy, manage, and troubleshoot a Kubernetes Cluster.

curl -LO “[https://storage.googleapis.com/kubernetes-release/release/$(curl](https://storage.googleapis.com/kubernetes-release/release/$(curl) -s [https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl](https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl)"

Install and Set Up kubectl The Kubernetes command-line tool, kubectl, allows you to run commands against Kubernetes clusters. You can use kubectl…kubernetes.io

Kubernetes Deployment

The web application will require different layers of deployment strategies to create a Kubernetes cluster running under a minikube environment.

Deployments of Web App

  1. PersistentVolumeClaim

The web application will require 1GB of the storage volume to integrate the docker container inside the Pod.

YAML: go-cache-poc-pvc.yaml

$ kubectl apply -f go-cache-poc-pvc.yaml

Persistent Volume Claims metadata of go-cache-kubernetes-pvc-v1-pocPersistent Volume Claims metadata of go-cache-kubernetes-pvc-v1-poc

2. Deployment

The web application will run as a Deployment inside a Kubernetes cluster.

YAML: go-cache-poc-app.yaml

$ kubectl apply -f go-cache-poc-app.yaml

Check the Deployment status

$ kubectl get deployments

NAME                         READY UP-TO-DATE AVAILABLE AGE
go-cache-kubernetes-app-poc  1/1   1          1         14s

Deployment metadata of go-cache-kubernetes-app-pocDeployment metadata of go-cache-kubernetes-app-poc

3. Service (LoadBalancer)

There will be *LoadBalancer* running as a Service to create an endpoint to connect with external traffic.

YAML: go-cache-poc-svc.yaml

$ kubectl apply -f go-cache-poc-svc.yaml

Service runs as LoadBalancer for go-cache-kubernetes-svc-pocService runs as LoadBalancer for go-cache-kubernetes-svc-poc

After the Service created successfully, in another terminal execute the following command.

$ minikube tunnel

The command requires root privileges and it will create a network route to allow the host to connect to external traffic via a Cluster IP gateway.

MongoDB Replica Set as a Kubernetes StatefulSet

The Kubernetes provides an architecture to create N-replicas with a unique identifier for all kinds of stateful applications. A brief explanation about StatefulSet and the configuration setup of MongoDB as StatefulSet in Kubernetes that you can find in my other medium article.

Secret Management in Kubernetes

This web application also uses the Secret management technique to store MongoDB credentials. I have explained the Secret management setup for both Kubernetes and *HashiCorp Vault* in my other medium article.

Deploy Redis in Kubernetes

The Redis docker image is required to be run in the local environment.

$ docker run -p 6379:6379 redislabs/redismod

Redis Deployment

YAML: [redis-deployment.yaml](github.com/Deeptiman/go-cache-kubernetes/bl..)

$ kubectl apply -f redis-deployment.yaml

Deployment metadata of redismodDeployment metadata of redismod

Redis Service

YAML: [redis-service.yaml](github.com/Deeptiman/go-cache-kubernetes/bl..)

$ kubectl apply -f redis-service.yaml

Service metadata of redis-serviceService metadata of redis-service

Deploy the redismod image into the Kubernetes

$ kubectl run redismod — image=redislabs/redismod — port=6379

Expose the deployment

$ kubectl expose deployment redismod — type=NodePort

Now verify the Redis connection

$ redis-cli -u $(minikube service — format “redis://{{.IP}}:{{.Port}}” — url redismod)

You must be getting an ip address with a port that can be used as a connection string for Redis.

minikube dashboard with all kinds in running statusminikube dashboard with all kinds in running status

Troubleshoot with kubectl

kubectl tool can be used debug, analyze Pods, Services in the Kubernetes cluster.

Few useful commands

  1. kubectl get pods

  2. kubectl describe pods

  3. kubectl logs

  4. kubectl exec -ti — bash

So, this is the overview of building a Data Caching Service in Kubernetes using Microservice architecture. If you are a beginner then, this project will help you get started with Kubernetes and understanding deployment in a minikube environment.

I hope you find this article useful :)


Share this