Deploy Golang & Mongo on Kubernetes (Minikube)

wahyu eko hadi saputro
3 min readApr 18, 2021

At this time i will make a tutorial how to deploy simple go application with minikube, before going further you can visit my old article

  1. about kubernetes on : :https://wahyu-ehs.medium.com/kubernetes-k8s-simple-introduction-6f01746a1398.
  2. About docker on :

https://wahyu-ehs.medium.com/golang-and-mongodb-in-docker-cab7773c04ce.

At firs we need a simple go application and i have already made it, and can be downloaded from : https://github.com/wahyueko22/golang-app/tree/master/go-mongo

On the repo i created many configuration file for deploying application and mongoDB

About what is deployment, service, pv (kubernetes volume), pvc (kubernetes volume claim) please refer to kubernetes doc : https://kubernetes.io/docs/concepts/storage/persistent-volumes/

Required software for deploying go app with minikube :

  1. Install virtual box to start minikube
  2. Install minikube
  3. Install kubectl
  4. Install docker
  5. Create account for docker hub
  6. Make sure those software are running well

Steps to deploy go app with kubernates :

  1. Install mongo DB in kubernates

Download file with mongo prefix from https://github.com/wahyueko22/golang-app/tree/master/go-mongo/kubernetes

Run below command sequantly:

  1. kubectl apply -f kubernetes/mongo-pv.yaml
  2. kubectl apply -f kubernetes/mongo-pvc.yaml
  3. kubectl apply -f kubernetes/mongo-deployment.yaml
  4. kubectl apply -f kubernetes/mongo-service.yaml
config deployment

The message should be created, not unchanged, because of i have already deployed those file so that the message is unchanged.

Result after mongo db config deployment

To see / check the service we run : minikube service service-name for example

minikube service mongodb

And base the picture we got link : http://192.168.99.100:32343

Open db using mongo db client that is Robo 3T

Create data according above picture, so that the json can be consumed by application.

{
"name" : "Wick",
"grade" : "5"
}

The sample code will query based on name, where the name is Wick.

2. Application deployment :

a. Create empty repo on docker hub

For example wahyuehs7/go-mongo

b. Create image

docker build -t wahyuehs7/go-mongo:1.0 .

c. Push image to docker hub

#docker push wahyuehs7/go-mongo:1.0
Result after pushing image to docker hub.

d. Run below command sequantly:

  • kubectl apply -f kubernetes/app-deployment.yaml
  • kubectl apply -f kubernetes/mongo-service.yaml
App config deployment
See all information with kubectl get all

To test app service, issues command :

minikube service goapp-srv
Result of application deployment

sources :

https://github.com/wahyueko22/golang-app/tree/master/go-mongo

https://kubernetes.io/docs/concepts/storage/persistent-volumes/

https://levelup.gitconnected.com/deploy-your-first-flask-mongodb-app-on-kubernetes-8f5a33fa43b4

https://keiran.scot/building-a-todo-api-with-golang-and-kubernetes-part-3-building-and-deploying-our-application-7857f2eeb680

--

--