Kubernetes is an open-source container orchestration platform that enables developers to manage, deploy, and scale containerized applications. MongoDB is a popular NoSQL database that offers high scalability, flexibility, and performance.
In this article, we will explore how to deploy MongoDB on Kubernetes and take advantage of its features to manage and scale MongoDB clusters.
Before we start, ensure that you have the following:
- A Kubernetes cluster up and running
- kubectl CLI tool installed
- Helm CLI tool installed
Step 1: Create a Namespace
To create a separate namespace for our MongoDB deployment, use the following command:
kubectl create namespace mongo
Step 2: Add the Bitnami Helm Repository
Bitnami provides a Helm chart for deploying MongoDB on Kubernetes. To add the Bitnami Helm repository, use the following command:
helm repo add bitnami https://charts.bitnami.com/bitnami
Step 3: Install MongoDB
To install MongoDB using the Bitnami Helm chart, use the following command:
helm install mongo bitnami/mongodb --namespace mongo --set auth.rootPassword=password
This command deploys a single MongoDB replica set with authentication enabled. The root password is set to 'password.' You can change it by modifying the --set auth.rootPassword parameter.
Step 4: Verify the Deployment
To verify that the MongoDB deployment is successful, use the following command:
kubectl get pods -n mongo
This command lists all the running pods in the mongo namespace. You should see the MongoDB pod running.
Step 5: Connect to MongoDB
To connect to the MongoDB instance, use the following command:
kubectl port-forward svc/mongo-mongodb 27017:27017 -n mongo
This command forwards the port 27017 from the MongoDB service to your local machine. You can now connect to the MongoDB instance using a MongoDB client.
Step 6: Scale the MongoDB Deployment
To scale the MongoDB deployment, use the following command:
kubectl scale statefulset mongo-mongodb --replicas=3 -n mongo
This command scales the MongoDB statefulset to three replicas. You can change the number of replicas by modifying the --replicas parameter.
Step 7: Backup and Restore MongoDB
Bitnami provides a tool called Velero for backing up and restoring Kubernetes resources. To install Velero, use the following command:
velero install --provider aws --plugins velero/velero-plugin-for-aws:v1.0.0 --bucket <your-bucket-name> --secret-file ./credentials-velero
This command installs Velero with the AWS provider plugin and sets up a backup bucket. You need to provide your AWS credentials in the credentials-velero file.
To backup the MongoDB deployment, use the following command:
velero backup create mongo-backup --include-namespaces mongo
This command creates a Velero backup of the mongo namespace.
To restore the MongoDB deployment, use the following command:
velero restore create --from-backup mongo-backup
This command restores the MongoDB deployment from the mongo-backup Velero backup.
In this article, we learned how to deploy MongoDB on Kubernetes using the Bitnami Helm chart. We also explored how to scale the MongoDB deployment and backup and restore MongoDB using Velero.
Kubernetes provides a robust platform for managing and scaling containerized applications, and MongoDB is an excellent choice for high-performance NoSQL databases. By combining the two, we can build scalable and resilient applications.
Related Searches and Questions asked:
That's it for this post. Keep practicing and have fun. Leave your comments if any.