How To set up a MongoDB Kubernetes Installation

2023-03-16

Prerequisites

  • Basic Understanding of Kubernetes and Helm.
  • Minikube setup

How to Guide

Start up the Minikube cluster

1
2
minikube start --cpus 4 --memory 4096
kubectl get nodes

Install MongoDB Community Kubernetes Operator via Helm chart

There are so many helm charts versions of MongoDB, but we will be installing community-operator version.

1
2
3
4
5
6
7
8
9
10
#values.yaml
operator:
# Resources allocated to Operator Pod
resources:
limits:
cpu: 500m
memory: 200Mi
requests:
cpu: 250m
memory: 150Mi
1
2
3
export KUBE_NAMESPACE=infra
kubectl create namespace $KUBE_NAMESPACE
helm upgrade -f values.yaml --install community-operator mongodb/community-operator --namespace $KUBE_NAMESPACE

Deploy a MongoDB Replica Set

These are detailed guides on modifying your YAML files for deploying MongoDB replica sets.
I will modify the following guide to the YAML below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#mongo.yal
---
apiVersion: mongodbcommunity.mongodb.com/v1
kind: MongoDBCommunity
metadata:
name: mongodb
spec:
members: 3
type: ReplicaSet
version: "4.2.6"
security:
authentication:
modes: ["SCRAM"]
users:
- name: root-user
db: admin
passwordSecretRef: # a reference to the secret that will be used to generate the user's password
name: mongo-creds
roles:
- name: clusterAdmin
db: admin
- name: userAdminAnyDatabase
db: admin
scramCredentialsSecretName: my-scram
additionalMongodConfig:
storage.wiredTiger.engineConfig.journalCompressor: zlib

# the user credentials will be generated from this secret
# once the credentials are generated, this secret is no longer required
---
apiVersion: v1
kind: Secret
metadata:
name: mongo-creds
type: Opaque
stringData:
password: rtfTTUIgu7890ggt#ioo123
1
2
3
kubectl apply -f mongo.yaml  --namespace $KUBE_NAMESPACE
kubectl get mdbc --namespace $KUBE_NAMESPACE
kubectl get pods -n $KUBE_NAMESPACE

Output:

1
2
3
4
5
6
7
NAME      PHASE     VERSION
mongodb Running 4.2.6
NAME READY STATUS RESTARTS AGE
mongodb-0 2/2 Running 0 6m29s
mongodb-1 2/2 Running 0 87s
mongodb-2 2/2 Running 0 46s
mongodb-kubernetes-operator-6465d9cd7-qpkzs 1/1 Running 0 7m3s

Create a new user on the primary replica

Create a new user to avoid the following error MongoDB - admin user not authorized

  1. ssh into the 3 MongoDB replicas
1
2
3
kubectl exec -it mongodb-0 --namespace $KUBE_NAMESPACE -- sh
kubectl exec -it mongodb-1 --namespace $KUBE_NAMESPACE -- sh
kubectl exec -it mongodb-1 --namespace $KUBE_NAMESPACE -- sh
  1. When the # prompt appears, type: mongo
  2. Look out for the primary replica because this is the only member in the replica set that receives write operations.
1
2
3
4
5
6
MongoDB shell version v4.2.6
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("ccdd585d-2212-451c-b58f-9a42b4a52894") }
MongoDB server version: 4.2.6
........
mongodb:PRIMARY>
  1. I will terminate other 2 sessions and use kubectl exec -it mongodb-1 --namespace $KUBE_NAMESPACE -- sh, that is the primary replica
  2. initiate the replica set with rs.initiate()
  3. Switch to the test database: use admin
  4. Authenticate with the username and password specified in mongo.yaml file : db.auth('root-user','rtfTTUIgu7890ggt#ioo123')
  5. Create a new user : db.createUser({ user:"oluchi", pwd: "oluchitest", roles: [{role: "readWrite", db: "admin"}] })

Output

1
2
3
4
5
6
7
8
9
10
11
mongodb:PRIMARY> db.createUser({ user:"oluchi", pwd: "fGJYIIhhjkuchitest", roles: [{role: "readWrite", db: "admin"}]})
Successfully added user: {
"user" : "oluchi",
"roles" : [
{
"role" : "readWrite",
"db" : "admin"
}
]
}