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
- Add helm repository
helm repo add bitnami https://charts.bitnami.com/bitnami
- Set
root
user password andreplica-set-key
as environment variables
export MONGODB_ROOT_PASSWORD=root123
export MONGODB_REPLICA_SET_KEY=root123
- 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
- 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)
- 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
- 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
## 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
- List the databases
show dbs
- Create a database
mydb
and create a document in collectionpost
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
}
])
- Verify the document is created
show dbs
db.post.find()
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 &
- Connect using MongoDB CLI from localhost
mongo --host 127.0.0.1 --authenticationDatabase admin -u root -p $MONGODB_ROOT_PASSWORD
- (optionally) Connect using MongoDB Compass or MongoDB Shell
# MongoDB Compass > New Connection
mongodb://root:JvckncuMto@127.0.0.1:27017
Top comments (1)
Great article!