DEV Community

Cover image for How to Deploy MongoDB on Google Kubernetes Engine (GKE)
Rodrigo Burgos
Rodrigo Burgos

Posted on

How to Deploy MongoDB on Google Kubernetes Engine (GKE)

In this tutorial, we will walk through the steps to deploy MongoDB on Google Kubernetes Engine (GKE). This setup assumes you already have an existing Google Cloud Platform (GCP) project.

Step 1: Creating the Subnet
Enable the necessary APIs
First, enable the necessary APIs to use Kubernetes Engine:

gcloud services enable container.googleapis.com

Create a VPC network
Create a VPC subnetwork for your GKE cluster:

gcloud compute networks subnets create mongo-subnet \
    --network default \
    --region us-central1 \
    --range 10.0.0.0/20
Enter fullscreen mode Exit fullscreen mode

Step 2: Creating a GKE Cluster with the Subnet

Create a GKE cluster
Next, create a GKE cluster with the subnet you just created:

Step 2: Creating a GKE Cluster with the Subnet
Create a GKE cluster
Next, create a GKE cluster with the subnet you just created:
Enter fullscreen mode Exit fullscreen mode

Get the credentials for the cluster
Fetch the credentials for your newly created cluster:

gcloud container clusters get-credentials mongo-cluster --zone us-central1-a
Enter fullscreen mode Exit fullscreen mode

Step 3: Deploying MongoDB

Apply the MongoDB StatefulSet configuration

Create a file named mongodb-deployment.yaml and add the following content:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mongo
spec:
  serviceName: "mongo"
  replicas: 3
  selector:
    matchLabels:
      app: mongo
  template:
    metadata:
      labels:
        app: mongo
    spec:
      containers:
      - name: mongo
        image: mongo:4.2
        ports:
        - containerPort: 27017
          name: mongo
        env:
        - name: MONGO_INITDB_ROOT_USERNAME
          valueFrom:
            secretKeyRef:
              name: mongo-secret
              key: MONGO_INITDB_ROOT_USERNAME
        - name: MONGO_INITDB_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mongo-secret
              key: MONGO_INITDB_ROOT_PASSWORD
        volumeMounts:
        - name: mongo-persistent-storage
          mountPath: /data/db
  volumeClaimTemplates:
  - metadata:
      name: mongo-persistent-storage
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 5Gi
Enter fullscreen mode Exit fullscreen mode

Apply the StatefulSet configuration:

apiVersion: v1
kind: Service
metadata:
  name: mongo
spec:
  ports:
  - port: 27017
  clusterIP: None
  selector:
    app: mongo
Enter fullscreen mode Exit fullscreen mode

Apply the service configuration:

kubectl apply -f mongodb-service.yaml
Enter fullscreen mode Exit fullscreen mode

Step 4: Creating and Applying Secrets

Create the secrets

Encode your username and password in Base64:

echo -n 'admin' | base64
echo -n 'yourpassword' | base64
Enter fullscreen mode Exit fullscreen mode

Create a file named mongodb-secret.yaml and add the following content:

apiVersion: v1
kind: Secret
metadata:
  name: mongo-secret
type: Opaque
data:
  MONGO_INITDB_ROOT_USERNAME: YWRtaW4=
  MONGO_INITDB_ROOT_PASSWORD: eW91cnBhc3N3b3Jk
Enter fullscreen mode Exit fullscreen mode

Apply the secret configuration:

kubectl apply -f mongodb-secret.yaml
Enter fullscreen mode Exit fullscreen mode

Create the MongoDB root user
Access the MongoDB pod:

kubectl exec -it mongo-0 -- bash
Enter fullscreen mode Exit fullscreen mode

Open the MongoDB shell:

mongo
Enter fullscreen mode Exit fullscreen mode

Create the root user:

use admin
db.createUser({
  user: "admin",
  pwd: "yourpassword",
  roles: [ { role: "root", db: "admin" } ]
})
Enter fullscreen mode Exit fullscreen mode

Exit the MongoDB shell and pod:

exit
exit
Enter fullscreen mode Exit fullscreen mode

Step 5: Verifying the Deployment

Check the status of the pods
Verify that the pods are running:

kubectl get pods

Check the status of the services

Verify that the services are running:

kubectl get svc

Step 6: Accessing MongoDB

Port forward to access MongoDB locally

kubectl port-forward svc/mongo 27017:27017

Connect to MongoDB using the mongo shell

kubectl exec -it mongo-0 -- mongo --username admin --password yourpassword --authenticationDatabase admin --host localhost --port 27017
Enter fullscreen mode Exit fullscreen mode

Conclusion
You have successfully deployed MongoDB on Google Kubernetes Engine (GKE). You can now manage your MongoDB instance and integrate it with your applications.

Feel free to share your experiences or ask any questions in the comments below!

Top comments (1)

Collapse
 
raajaryan profile image
Deepak Kumar

Hello everyone,

I hope you're all doing well. I recently launched an open-source project called the Ultimate JavaScript Project, and I'd love your support. Please check it out and give it a star on GitHub: Ultimate JavaScript Project. Your support would mean a lot to me and greatly help in the project's growth.

Thank you!