Volume for MongoDB
As you know, our data is only persisted in our MongoDB Pod and will be lost if the Pod is e.g. going to be rescheduled. We also know that a Deployment is maybe not the right Workload type for a database (e.g. a StatefulSet could be a better match), but we will continue with our Deployment Workload. Before starting to modify our Deployment again, we will add an small application that allows us to visualize the content of our MongoDB easily.
1. MongoExpress Deployment
In this chapter we will deploy a GUI client to the mongoDB, called Mongo Express. It will help us track the guestbook entries and give you some more practice in deploying applications in Kubernetes as well as connecting Pods within your cluster.
First, check out the image of Mongo Express on Docker Hub. Read up on the configuration of the image. Think about, which variables we need to properly connect the client to our existent mongoDB. Also take note of the port, the mongo-express image is listening on.
Now, inspect the Services and Secrets in our cluster to locate all information you will need to connect the mongo-express to mongo-deploy.
Define:
ME_CONFIG_MONGODB_ADMINUSERNAME
ME_CONFIG_MONGODB_ADMINPASSWORD
ME_CONFIG_MONGODB_SERVER
HINT 1
ME_CONFIG_MONGODB_ADMINUSERNAME: [secretKeyRef: admin username from *mongo-secret*]
ME_CONFIG_MONGODB_ADMINPASSWORD: [secretKeyRef: admin password from *mongo-secret*]
ME_CONFIG_MONGODB_SERVER: [environment variable: mongo service name]
containerPort: 8081
When you located all necessary information, create the blueprint for the mongo-express Deployment as follows:
name: mongo-express
labels: app = mongo-express
replicas: 1
image: mongo-express:1.0.0-alpha
containerPort: 8081
env: [variables pointing to the mongo service and secret]
Solution
- Take 8 - Persistence Mongo/1-express-deploy.yaml and 8 - Persistence Mongo/2-express-svc.yaml as a reference
2. Adding a PVC
As you could see, the guestbook entries still weren’t persisted. When the MongoDB pod dies, all data written into the container is lost.
Therefore we will now bind a Persistent Volume to our Deployment. The messages will be written on this volume of a storage provider. As our cluster is hosted on AKS, the PVs will be provisioned by Microsoft Azure within the AKS cluster. Take a look at the Microsoft Documentation for storage options.
We choose to automatically provision PVs. As soon as a Pod gets created it claims storage for the application it’s hosting. That’s handled by a Persistent Volume Claim that is bound to the Pod. The PVC will connect to the defined Storage Class, which then provides a Volume dynamically.
As a first step, inspect all Storage Classes, that are already available in your cluster.
HINT
$ kubectl get storageclasses
or
$ k get sc
We choose mongo-fs, that was prepared by your trainer. Inspect this StorageClass by using kubectl describe.
Now, create the Persistent Volume Claim that will connect to the mongo-fs Storage Class.
name: entries-pvc
accessModes: ReadWriteOnce
storageClassName: mongo-fs
storage: 100Mi
HINT
- Take the blueprint from the Kubernetes Documentation and edit the specs
- Also check the Microsoft Documentation for dynamic Azure Files storage
- Take 8 - Persistence Mongo/3-entries-pvc.yaml as a reference.
3. Adding the volume to the MongoDB Deployment
Finally, configure the Deployment of our MongoDB to make use of the Persistent Volume Claim. That way a Persistent Volume dynamically gets provisioned and bound to the Persistent Volume Claim. Our application can now make use of the external storage from AKS.
So, let us add the volumes and volumeMounts sections to the Deployment spec at the right positions:
volumes:
- name: entries-storage
persistentVolumeClaim:
claimName: entries-pvc
volumeMounts:
- name: entries-storage
mountPath: /data/db
HINT
1. You may edit the existing deployment file and apply the changes.
2. See the structure of the Pod definition in this example
Solution
$ kubectl -n mongo apply -f 8 - Persistence Mongo/4-mongo-deploy.yaml
Important
If you encounter authentication errors of the client applications, try to remove all deployments (including MongoDB, guestbook, mongo-express) and the PVC. Then deploy the MongoDB first and the client applications (guestbook, mongo-express) secondly.