Hands-On Kubernetes: Deploy MongoDB and Mongo-Express on Digital Ocean with Helm

Today, we’ll walk through the process of setting up a Kubernetes cluster on Digital Ocean, installing Helm, and deploying a MongoDB instance with a UI using Mongo-Express. This guide will help you understand the fundamental steps and commands required to get your application up and running.

Step 1: Create a Kubernetes Cluster on Digital Ocean

  1. Create a Kubernetes Cluster: Log in to your Digital Ocean account and create a Kubernetes cluster. Choose the desired specifications and node pool for your application.

  2. Download Configuration Files: Once your cluster is created, download the kubeconfig file to your local machine. This file is essential for connecting to your cluster using kubectl.

  3. Set Permissions for the Config File and Export KUBECONFIG Environment Variable: :

     chmod 400 your-kubeconfig-file.yaml
     export KUBECONFIG=your-kubeconfig-file.yaml
    

Step 2: Install Helm

  1. Install Helm Using Snap:

     sudo snap install helm
    
  2. Add Bitnami Repository:

     helm repo add bitnami https://charts.bitnami.com/bitnami
    
  3. Search for MongoDB Chart:

     helm search repo bitnami/mongodb
    

    Step 3: Configure MongoDB with Persistent Storage

    1. Create a Configuration File for MongoDB (helm-mongodb.yaml):

       architecture: replicaset
       replicaCount: 3
       persistence:
         storageClass: "do-block-storage"  // I am using digital ocean you can use any cloud provider storage class
       auth:
         rootPassword: rootPassword
      
      1. Install MongoDB Using Helm:

         helm install mongodb --values helm-mongodb.yaml bitnami/mongodb
        
      2. Verify Installation:

         kubectl get all
        
      3. Retrieve MongoDB Secret:

         kubectl get secret mongodb -o yaml
        

        Step 4: Deploy Mongo-Express UI

        1. Create a Deployment and Service YAML for Mongo-Express (mongo-express.yaml):

           apiVersion: apps/v1
           kind: Deployment
           metadata:
             name: mongo-express
             labels:
               app: mongo-express
           spec:
             replicas: 1
             selector:
               matchLabels:
                 app: mongo-express
             template:
               metadata:
                 labels:
                   app: mongo-express
               spec:
                 containers:
                 - name: mongo-express
                   image: mongo-express
                   ports:
                   - containerPort: 8081
                   env:
                   - name: ME_CONFIG_MONGODB_ADMINUSERNAME
                     value: root
                   - name: ME_CONFIG_MONGODB_SERVER
                     value: mongodb-0.mongodb-headless
                   - name: ME_CONFIG_MONGODB_ADMINPASSWORD
                     valueFrom:
                       secretKeyRef:
                         name: mongodb
                         key: mongodb-root-password
           ---
           apiVersion: v1
           kind: Service
           metadata:
             name: mongo-express-service
           spec:
             selector:
               app: mongo-express
             ports:
               - protocol: TCP
                 port: 8081
                 targetPort: 8081
          
        2. Apply the YAML File:

           kubectl apply -f mongo-express.yaml
          

          Step 5: Install NGINX Ingress Controller

          1. Install NGINX Ingress Controller:

             helm install nginx-ingress ingress-nginx/ingress-nginx --set controller.publishedService.enabled=true
            
          2. Check the Created Services: You will notice a loadbalancer running with external ip Its created by ingress controller

             kubectl get svc
            

            Step 6: Configure Ingress for Mongo-Express

            1. Create an Ingress YAML (ingress.yaml):

               apiVersion: networking.k8s.io/v1
               kind: Ingress
               metadata:
                   annotations:
                     nginx.ingress.kubernetes.io/rewrite-target: /
                   name: mongo-express
               spec:
                 ingressClassName: nginx
                 rules:
                 - host: your-domain.com
                   http:
                     paths:
                       - path: /
                         pathType: Prefix
                         backend:
                           service:
                             name: mongo-express-service
                             port:
                               number: 8081
              
            2. Apply the Ingress Configuration:

               kubectl apply -f ingress.yaml
              
              1. Update DNS Settings:
  • On Digital Ocean: Go to Networking and add a domain with the IP address of your Load Balancer.

  • Advanced DNS Tab: Add the Load Balancer IP address in an A record.

    Now go to your-domain.com in browser mongo-express is running you can test persistent volume by creating datatbase and then using below command to delete pods and up running again

    
       kubectl scale --replicas=0 statefulset/mongodb
      // start again
       kubectl scale --replicas=3 statefulset/mongodb
    
      /* you can even uninstall helm and mongodb 
      but still in digital ocean you will see 
      volumes unattched */
    

Conclusion

By following these steps, you've successfully deployed a MongoDB instance with persistent storage and Mongo-Express UI on a Kubernetes cluster hosted on Digital Ocean. You also configured ingress using NGINX to access the Mongo-Express UI via your domain name. This hands-on learning session demonstrates the power and flexibility of Kubernetes and Helm for managing complex applications. Happy deploying!