Monday, March 20, 2023

Deploy MongoDB Community on Kubernetes

 

In this post we will review the step to install MongoDB community version on a kubernetes cluster.

First, we install the MongoDB operator:


helm repo add mongodb https://mongodb.github.io/helm-charts
helm install community-operator mongodb/community-operator


Next, we create a replica.yaml file for the MongoDB custom resources:


---
apiVersion: mongodbcommunity.mongodb.com/v1
kind: MongoDBCommunity
metadata:
name: example-mongodb
spec:
members: 1
type: ReplicaSet
version: "5.0.16"
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
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: my-user-password
type: Opaque
stringData:
password: my-pass


And apply the resources:


kubectl apply -f replica.yaml


Now we can connect to the MongoDB using kubectl:


kubectl exec -it example-mongodb-0 -- mongosh "mongodb+srv://my-user:mongo@example-mongodb-svc.default.svc.cluster.local/admin?ssl=false"



No comments:

Post a Comment