Now let’s deploy the mongoAuth service to a Kubernetes cluster.

Here, it does not matter if your cluster is on AWS, Azure, GCP or whatever Cloud provider. We consider that, the cluster is deployed and that you have a `client.conf` configuration file to contact the cluster with kubectl` .

Okay, so let’s create our Kubernetes object configurations. Create k8s ` folder in your db folder. You will need the following objects to deploy mongo:

PersistentVolume

PersistentVolumeClaim

ClusterIP

Deployment

Let’s start with the PersistentVolume configuration in a `k8s/pois-mongo-pv-volume.yaml`file:

apiVersion: v1

kind: PersistentVolume

metadata:

name: pois-mongo-pv-volume

namespace: pois

labels:

type: local

spec:

storageClassName: manual

capacity:

storage: 10Gi

accessModes:

- ReadWriteOnce

hostPath:

path: "/mnt/data"

Now, the PersistentVolumeClaim in a `k8s/pois-mongo-pv-claim.yaml`file:

apiVersion: v1

kind: PersistentVolumeClaim

metadata:

name: pois-mongo-pv-claim

namespace: pois

spec:

storageClassName: manual

accessModes:

- ReadWriteOnce

resources:

requests:

storage: 10Gi

Then the ClusterIP Service in a `k8s/pois-mongo-cluster-ip-service.yaml`file:

apiVersion: v1

kind: Service

metadata:

namespace: pois

name: pois-mongo-cluster-ip-service

spec:

ports:

# service port in cluster

- port: 27017

# port to contact inside container

targetPort: 27017

selector:

app: pois-mongo

type: ClusterIP

Finally, the Deployment in a `k8s/pois-mongo-deployment.yaml`file:

apiVersion: apps/v1

kind: Deployment

metadata:

name: pois-mongo-deployment

namespace: pois

spec:

selector:

matchLabels:

app: pois-mongo

strategy:

type: Recreate

template:

metadata:

labels:

app: pois-mongo

spec:

containers:

# by default, the image is pulled from docker hub

- image: <DOCKER_ID>/mongoAuth

name: mongo

ports:

- containerPort: 27017

volumeMounts:

- name: mongo-persistent-storage

mountPath: /data/db

volumes:

- name: mongo-persistent-storage

persistentVolumeClaim:

claimName: pois-mongo-pv-claim

So basically, we first create a Kubernetes persistent volume. This Kubernetes object will instruct our Cloud provider to a create a volume. Most likely, this volume will be the one used by your EC2 instance (on AWS)containing your master (aka control plane) node or any volume known to your Kubernetes cluster.

The Kubernetes Deployment containing one MongoDB Pod (wrapping the mongo container) is bound to a persistent volume claim (named `pois-mongo-pv-claim`). This persistent volume claim will look for available persistent volumes in the cluster that meet its requirements. It will find the persistent volume named `pois-mongo-pv-volume` which meets the memory size and access modes requirements.

Finally, to deploy all that in the cluster on AWS, use the following:

kubectl --kubeconfig=<PATH_TO>/client.conf apply -f k8s/