The Ops Community

Cover image for Deploying a NoSQL Database on K8's cluster
Harkirat Singh
Harkirat Singh

Posted on

Deploying a NoSQL Database on K8's cluster

MongoDB Kubernetes Cluster

Kubernetes is the industry-leading container orchestration platform. You can use any distribution of Kubernetes to manage the full lifecycle of your MongoDB clusters, wherever you choose to run them, from on-premises infrastructure to the public cloud.

mongodb

Requirements

  • kubectl
  • mongoshell

Steps

1) Clone this repository

git clone https://github.com/harkiratsm/DigitalOcean-Kubernetes-Challenge.git
Enter fullscreen mode Exit fullscreen mode
cd DigitalOcean-Kubernetes-Challenge
Enter fullscreen mode Exit fullscreen mode

Take a Look At headless-service.yaml

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

Take a look at mongodb-statefulset.yaml

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mongo
spec:
  selector:
    matchLabels:
      app: mongo
  serviceName: "mongo"
  replicas: 3
  template:
    metadata:
      labels:
        app: mongo
    spec:
      terminationGracePeriodSeconds: 10
      containers:
      - name: mongo
        image: mongo
        command: 
        - mongod 
        - "--bind_ip_all"
        - "--replSet"
        - rs0
        ports:
        - containerPort: 27017
        volumeMounts:
        - name: mongo-volume
          mountPath: /data/db
  volumeClaimTemplates:
  - metadata:
      name: mongo-volume
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 1Gi
Enter fullscreen mode Exit fullscreen mode

Take a look at config.yaml , to know more about visit here

apiVersion: v1
kind: ConfigMap
metadata:
  namespace: metallb-system
  name: config
data:
  config: |
    address-pools:
    - name: default
      protocol: layer2
      addresses:
      - 192.168.59.50-192.168.59.250
Enter fullscreen mode Exit fullscreen mode

Now deploy those yaml file

Use the Following command

kubectl create -f .
Enter fullscreen mode Exit fullscreen mode

Now check it using the following command

kubectl get all
Enter fullscreen mode Exit fullscreen mode

2) Initilize the replicaset i.e we are gonna connect mongo-0 mongo-1 mongo-2

kubectl exec -it mongo-0 -- mongosh
Enter fullscreen mode Exit fullscreen mode
rs.initiate()
var cfg = rs.conf()
cfg.members[0].host="mongo-0.mongo:27017"
rs.reconfig(cfg)
rs.status()
rs.add("mongo-1.mongo:27017")
rs.add("mongo-2.mongo:27017")
Enter fullscreen mode Exit fullscreen mode

Accessing the mongodb replica set

They are two way with which we can access Within a Cluster and Outside the cluster

Let discuss about Outside the cluster

For that we have to expose it as LoadBalancer for it we using MetalLB

1) Lets first install MetalLB

kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.11.0/manifests/namespace.yaml
kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.11.0/manifests/metallb.yaml
Enter fullscreen mode Exit fullscreen mode

2) Now apply config.yaml

kubectl create -f config.yaml
Enter fullscreen mode Exit fullscreen mode

3) Now lets expose the pods

kubectl expose pod mongo-0 --port 27017 --target-port 27017 --type LoadBalancer
Enter fullscreen mode Exit fullscreen mode

services

Now expose the other pods also .

4) Now you access it by using the following command

mongosh mongodb://192.168.59.51
Enter fullscreen mode Exit fullscreen mode

Like you can also do this

mongosh mongodb://{ExternalIp1,ExternalIp2,...}
Enter fullscreen mode Exit fullscreen mode

To add new Member to the replicaset for that We are going to scale the statefulset

kubectl scale sts mongo --replicas 4
Enter fullscreen mode Exit fullscreen mode

sts

Now Lets talk on accesing Replica set within a Cluster -

1) Lets first create a mongodb deployment use the following command

kubectl create deployment mongo --image=mongo
Enter fullscreen mode Exit fullscreen mode

image

2) Next Step

kubectl exec -it <pod_name> -- bin/bash
Enter fullscreen mode Exit fullscreen mode

To know the pod_name type in the command

kubectl get pods

pods

3) Now we are inside the pod type in the following command to connect .

mongosh mongodb://mongo-0.mongo:27017
Enter fullscreen mode Exit fullscreen mode

Like wise you can also do

mongosh mongodb://mongo-0.mongo:27017,mongo-1.mongo:27017,....  
Enter fullscreen mode Exit fullscreen mode

To know more about it you can checkout here

Connect with me ❤️ -

Twitter

Discussion (0)