DEV Community

karan singh
karan singh

Posted on • Originally published at Medium

Deploy MongoDB on OpenShift using Helm

Image description

Introduction

This is yet another blog post on deploying Blahblahblah on OpenShift and this time its MongoDB. In this post you will learn deployment of MongoDB on OpenShift using Helm Chart.

Lets' get started

  • Install helm CLI on your local machine (see docs)
  • Login to OpenShift CLI
  • Create a new Project on OpenShift
oc new-project ksingh
Enter fullscreen mode Exit fullscreen mode
  • Add helm repository
helm repo add bitnami https://charts.bitnami.com/bitnami
Enter fullscreen mode Exit fullscreen mode
  • Set root user password and replica-set-key as environment variables
export MONGODB_ROOT_PASSWORD=root123
export MONGODB_REPLICA_SET_KEY=root123
Enter fullscreen mode Exit fullscreen mode
  • Using helm install MongoDB on OpenShift. Make sure to set the required SecurityContext , so that helm can deploy MongoDB on OpenShift
helm install mongodb bitnami/mongodb --set podSecurityContext.fsGroup="",containerSecurityContext.runAsUser="1001080001",podSecurityContext.enabled=false,architecture=replicaset,auth.replicaSetKey=$MONGODB_REPLICA_SET_KEY,auth.rootPassword=$MONGODB_ROOT_PASSWORD
Enter fullscreen mode Exit fullscreen mode
  • Wait for the deployment to be ready, you can run oc get po
  • Get the root password (optional)
export MONGODB_ROOT_PASSWORD=$(kubectl get secret --namespace ksingh mongodb -o jsonpath="{.data.mongodb-root-password}" | base64 --decode)
Enter fullscreen mode Exit fullscreen mode
  • Create a MongoDB Client container and verify that connectivity and DB access
kubectl run --namespace ksingh mongodb-client --rm --tty -i --restart='Never' --env="MONGODB_ROOT_PASSWORD=$MONGODB_ROOT_PASSWORD" --image docker.io/bitnami/mongodb:4.4.13-debian-10-r9 --command -- bash
Enter fullscreen mode Exit fullscreen mode
  • From the client container shell, connect to the MongoDB cluster
## Option-1 Using host addres
mongo admin --host "mongodb-0.mongodb-headless.ksingh.svc.cluster.local:27017,mongodb-1.mongodb-headless.ksingh.svc.cluster.local:27017" --authenticationDatabase admin -u root -p $MONGODB_ROOT_PASSWORD
Enter fullscreen mode Exit fullscreen mode
## Option-2 Using MongoDB URI
mongo "mongodb://mongodb-0.mongodb-headless.ksingh.svc.cluster.local:27017,mongodb-1.mongodb-headless.ksingh.svc.cluster.local:27017" --authenticationDatabase admin  -u root -p $MONGODB_ROOT_PASSWORD
Enter fullscreen mode Exit fullscreen mode
  • List the databases show dbs
  • Create a database mydb and create a document in collection post
use mydb

db.post.insert([
  {
    title: "MongoDB to Kafka testing",
    description: "Debezium connector",
    by: "Karan",
    url: "http://redhat.com",
    tags: ["mongodb", "debezium", "ROSAK"],
    likes: 100
  }
])
Enter fullscreen mode Exit fullscreen mode
  • Verify the document is created
show dbs
db.post.find()
Enter fullscreen mode Exit fullscreen mode

Exit the mongodb client container

  • If you want to connect to MongoDB cluster from localhost, then forward to port
kubectl port-forward service/mongodb-external-0 27017 &
Enter fullscreen mode Exit fullscreen mode
  • Connect using MongoDB CLI from localhost
mongo --host 127.0.0.1 --authenticationDatabase admin -u root -p $MONGODB_ROOT_PASSWORD
Enter fullscreen mode Exit fullscreen mode
  • (optionally) Connect using MongoDB Compass or MongoDB Shell
# MongoDB Compass > New Connection
mongodb://root:JvckncuMto@127.0.0.1:27017
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
marcomoscatelli profile image
Marco Moscatelli

Great article!