This repository is deprecated and no longer actively maintained. It contains outdated code examples or practices that do not align with current MongoDB best practices. While the repository remains accessible for reference purposes, we strongly discourage its use in production environments. Users should be aware that this repository will not receive any further updates, bug fixes, or security patches. This code may expose you to security vulnerabilities, compatibility issues with current MongoDB versions, and potential performance problems. Any implementation based on this repository is at the user's own risk. For up-to-date resources, please refer to the MongoDB Developer Center.
This guide outlines the steps to deploy a MongoDB replica set using the MongoDB Kubernetes Operator. The MongoDB Kubernetes Operator simplifies the deployment and management of MongoDB clusters on Kubernetes.
Before proceeding with the deployment, ensure that you have the following prerequisites:
- Kubernetes cluster up and running
kubectl
command-line tool installed and configured- MongoDB Kubernetes Operator installed in your cluster
Create a Kubernetes namespace to isolate the MongoDB resources:
kubectl create namespace my-mongodb
Create a Kubernetes secret to store the MongoDB admin credentials:
kubectl create secret generic my-mongodb-credentials
--namespace my-mongodb
--from-literal=mongodb-admin-username=<admin_username>
--from-literal=mongodb-admin-password=<admin_password>
Replace <admin_username>
and <admin_password>
with the desired MongoDB admin username and password.
Create a YAML file, for example mongodb-replicaset.yaml
, with the following content:
apiVersion: mongodb.com/v1
kind: MongoDB
metadata:
name: my-mongodb
namespace: my-mongodb
spec:
members: 3
type: ReplicaSet
version: "6.3.1"
security:
authentication:
modes: ["SCRAM"]
Adjust the configuration based on your requirements, such as the number of replica set members, MongoDB version, and authentication settings.
Deploy the MongoDB replica set:
kubectl apply -f mongodb-replicaset.yaml
Step 4: Verify the Deployment Check the status of the MongoDB replica set deployment:
kubectl get mongodb -n my-mongodb
Ensure that the STATUS is Running and the READY count matches the number of replica set members.
Step 5: Connect to the Replica Set To connect to the MongoDB replica set, retrieve the connection details:
kubectl get mongodb -n my-mongodb -o jsonpath='{.items[0].status.connectionString}'
Use the obtained connection string to connect to the replica set from your application or MongoDB client.
Conclusion In this guide, you successfully deployed a MongoDB replica set using the MongoDB Kubernetes Operator. You can now leverage the power and scalability of MongoDB on Kubernetes for your applications.