How to Deploy MongoDB on Google Kubernetes Engine (GKE)
Published: 2024-07-01 14:50:48

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
```

**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:
```

**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
```

**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
```

**Apply the StatefulSet configuration:**

```
apiVersion: v1
kind: Service
metadata:
  name: mongo
spec:
  ports:
  - port: 27017
  clusterIP: None
  selector:
    app: mongo
```

**Apply the service configuration:**

```
kubectl apply -f mongodb-service.yaml
```

**Step 4: Creating and Applying Secrets**

**Create the secrets**

Encode your username and password in Base64:

```
echo -n 'admin' | base64
echo -n 'yourpassword' | base64
```

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
```

Apply the secret configuration:
```
kubectl apply -f mongodb-secret.yaml
```

**Create the MongoDB root user**
Access the MongoDB pod:

```
kubectl exec -it mongo-0 -- bash
```

**Open the MongoDB shell:**

```
mongo
```

Create the root user:

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

Exit the MongoDB shell and pod:

```
exit
exit
```

**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
```

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!