Kubernetes mongo Installation

wahyu eko hadi saputro
2 min readApr 3, 2022

Ok this time we will discuss about installation mongo DB on kubernetes. 2 main things that is very important in installing database on kubernetes / k8s are persistence volume and statefulset. StatefulSet maintains a sticky identity for each of their Pods, so that when the pod is broken or deleted, it is like will restart / create new pod with same pod name as before.

persistence volume is needed to store the database date. persistence volume is a directory that mounted to pod and the directory is persistent even though the pod is deleted. The idea is storage for database must be persistence so that what ever happened to database application we still able to backup the database data.

step to deploy mongo db on kubernetes :

  1. persistence volume
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mongodb-sit-claim
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 40G

2. Create mongo deployment

apiVersion: apps/v1
kind: StatefulSet
metadata:
name: sit-ml-mongo
spec:
replicas: 1
selector:
matchLabels:
app: sit-ml-mongo
serviceName: sit-ml-mongo
template:
metadata:
labels:
app: sit-ml-mongo
spec:
nodeSelector:
purpose: ml_database
environment: ml_sit
volumes:
- name: data
persistentVolumeClaim:
claimName: mongodb-sit-claim
containers:
- command:
- numactl
- --interleave=all
- mongod
- --bind_ip
- 0.0.0.0
- --auth
- --setParameter
- authenticationMechanisms=SCRAM-SHA-1
- --wiredTigerCacheSizeGB
- "0.25"
image: mongo:4.4
imagePullPolicy: IfNotPresent
name: mongo
ports:
- containerPort: 27017
protocol: TCP
resources:
limits:
cpu: "2"
memory: 7G
requests:
cpu: "0.5"
memory: 2G
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /data/db
name: data
restartPolicy: Always

example of result :

base on picture above, we have /data/db where database data is stored. the folder /data/db comes from

volumeMounts:
- mountPath: /data/db
name: data

--

--