Introduction
Throughout this article, I will describe how to create a data caching service using MongoDB and Redis in a minikube environment. The Go programming language is used to create the web application.
Go as a programming language, MongoDB as persistent storage, and Redis as a data caching Service should all be in the development environment.
Table of Contents
- An Introduction to Kubernetes Data Caching Service
- How is Kubernetes deployed?
- Essential Kubernetes Tools
Installation
The Kubernetes deployment environment for each module is set up differently and only supports Redis Cache in the local development environment. The installation of MongoDB is as follows.
GO
$ 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
MongoDB
MongoDB’s dependency module is necessary to install a mongo-go-driver, which contains all of the Go APIs for querying a MongoDB database.
$ go get go.mongodb.org/mongo-driver/mongo
Redis Cache
The go-redistribution To integrate the data caching Service, the Go client library will act as a dependent module.
$ go get github.com/go-redis/redis/v8
redis-server
Installing a Redis server, which offers a variety of data structures, caching techniques, and a message broker, was also necessary for the development environment.
- $ wget 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
https://redis.io/download#installation
Construct the Docker images.
The docker container environment in which the web application was created will be used for Kubernetes deployment.
Installation
$ sudo apt install docker.io
$ sudo apt install docker-compose
Build the image
$ docker build -t go-cache-kubernetes-v1
Label the image.
$ docker tag go-cache-kubernetes-v1 deeptiman1991/go-cache-kubernetes-v1:1.0.0
Sign in to Docker Hub.
$ docker loginType Username and Password to complete the instruction
Upload the image to the Docker Hub.
$ docker push deeptiman1991/go-cache-kubernetes-v1:1.0.0
Kubernetes Tools
Minikube and kubectl are two essential Kubernetes tools that must be installed in the local environment.
minikube will run as a single-node Kubernetes cluster within a Virtual machine. Virtualization must be supported by the device, and HyperVisor must be enabled. Hyper kit is a suggested virtualization kit for getting started with the minikube.
$ sudo install minikube
$ minikube start
kubectl is a command-line utility that may be used to deploy, administer, and debug a Kubernetes Cluster.
curl -LO “https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl“
Deployment of Kubernetes
To construct a Kubernetes cluster running in a minikube environment, the web application will require multiple levels of deployment strategies.
$ kubectl apply -f go-cache-poc-pvc.yaml
Deployment
The web application will be deployed within a Kubernetes cluster.
$ kubectl apply -f go-cache-poc-app.yaml
Check the Deployment status
$ kubectl get deploymentsNAME READY UP-TO-DATE AVAILABLE AGE
go-cache-kubernetes-app-poc 1/1 1 1 14s
Service (LoadBalancer)
LoadBalancer will operate as a service to generate an endpoint to connect with external traffic.
$ kubectl apply -f go-cache-poc-svc.yaml
After creating the Service, execute the following command in a different terminal.
$ minikube tunnel
The command, which must be run with root permissions, will set up a network path that will enable the host to connect to external traffic through a Cluster IP gateway.
MongoDB Replica Set as a Kubernetes StatefulSet
Kubernetes provides an architecture to build N-replicas with a distinctive identity for all types of stateful applications.
You can read my previous Medium blog for a brief explanation of StatefulSet and the configuration setting of MongoDB as StatefulSet in Kubernetes.
Kubernetes Secret Management
This web application additionally stores MongoDB credentials using the Secret management approach. I have described the secret management configuration for both Kubernetes and HashiCorp Vault.
Redis should be deployed in Kubernetes.
The Redis docker image must execute in the local environment.
$ docker run -p 6379:6379 redislabs/redismod
Redis Deployment
$ kubectl apply -f redis-deployment.yaml
The Redis Service
$ kubectl apply -f redis-service.yaml
Deploy the redismod image into the Kubernetes
$ kubectl run redismod — image=redislabs/redismod — port=6379
Expose the deployment
$ kubectl expose deployment redismod — type=NodePort
Check the Redis connection now.
$ redis-cli -u $(minikube service — format “redis://{{.IP}}:{{.Port}}” — url redismod)
You must be receiving an IP address and a port that Redis may utilize as part of a connection string.
Use kubectl to troubleshoot
The Kubernetes cluster’s kubectl tool can be used to debug and examine its services and pods.
So that’s an overview of how to develop a Data Caching Service in Kubernetes utilizing Microservice architecture.
This post will help you get started with Kubernetes and understand deployment in a minikube environment if you are a novice.