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.
Requirements
- kubectl
- mongoshell
Steps
1) Clone this repository
git clone https://github.com/harkiratsm/DigitalOcean-Kubernetes-Challenge.git
cd DigitalOcean-Kubernetes-Challenge
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
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
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
Now deploy those yaml file
Use the Following command
kubectl create -f .
Now check it using the following command
kubectl get all
2) Initilize the replicaset i.e we are gonna connect mongo-0 mongo-1 mongo-2
kubectl exec -it mongo-0 -- mongosh
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")
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
2) Now apply config.yaml
kubectl create -f config.yaml
3) Now lets expose the pods
kubectl expose pod mongo-0 --port 27017 --target-port 27017 --type LoadBalancer
Now expose the other pods also .
4) Now you access it by using the following command
mongosh mongodb://192.168.59.51
Like you can also do this
mongosh mongodb://{ExternalIp1,ExternalIp2,...}
To add new Member to the replicaset for that We are going to scale the statefulset
kubectl scale sts mongo --replicas 4
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
2) Next Step
kubectl exec -it <pod_name> -- bin/bash
To know the pod_name type in the command
 kubectl get pods
3) Now we are inside the pod type in the following command to connect .
mongosh mongodb://mongo-0.mongo:27017
Like wise you can also do
mongosh mongodb://mongo-0.mongo:27017,mongo-1.mongo:27017,....  
To know more about it you can checkout here
Connect with me ❤️ -
Twitter
 
Top comments (0)