Setup mongo using Kubernetes community operator in local dev box

Sairam Krish
2 min readApr 27, 2022

Setting up mongo in local dev box is little tricky. The official documents are good place to start. But I faced issues with getting them all working in a local setup for development purpose.

Setup mongo operator

helm repo add mongodb https://mongodb.github.io/helm-charts
helm install community-operator mongodb/community-operatorSetup mongo database resource
  • Let’s setup mongo database resource. Save the below file as mongo.yaml. Later we will revisit this area and move this into our helm-charts repository.
---
apiVersion: mongodbcommunity.mongodb.com/v1
kind: MongoDBCommunity
metadata:
name: mongo
spec:
members: 1
type: ReplicaSet
version: "4.2.6"
security:
authentication:
modes: ["SCRAM"]
users:
- name: my-user
db: admin
passwordSecretRef: # a reference to the secret that will be used to generate the user's password
name: my-user-password
roles:
- name: clusterAdmin
db: admin
- name: userAdminAnyDatabase
db: admin
- name: readWriteAnyDatabase
db: admin
scramCredentialsSecretName: my-scram
additionalMongodConfig:
storage.wiredTiger.engineConfig.journalCompressor: zlib
statefulSet:
spec:
serviceName: mongo
template:
spec:
containers:
- name: mongodb
image: mongo
env:
- name: MONGO_INITDB_DATABASE
value: platformdb
- name: MONGO_INITDB_ROOT_USERNAME
value: admin
- name: MONGO_INITDB_ROOT_PASSWORD
value: password
- name: mongodb-agent
readinessProbe:
exec:
command:
- ls
enabled: false
failureThreshold: 50
initialDelaySeconds: 300
periodSeconds: 300
timeoutSeconds: 300
# 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: my-user-password
type: Opaque
stringData:
password: password
  • Run following command
kubectl apply -f mongo.yaml
# operator will pick this kind of resource
# and setup the all the mongo requirements

Connecting desktop mongo clients with mongo

  • we need to port forward mongo
kubectl port-forward service/mongo 27017:27017
  • connection URL based on above flow:
mongodb://admin:password@localhost:27017/?authSource=admin&authMechanism=SCRAM-SHA-256&directConnection=true
  • We are all set to use the mongo

--

--