How to back up a MongoDB database deployed to a Kubernetes environment

Eduardo Patrocinio
2 min readMay 23, 2018

--

In this tutorial, we will show how to back up a MongoDB database, deployed to a Kubernetes cluster. I will use IBM Cloud Private as the target Kubernetes environment, but the solution should know in any other environment

Deploy a MongoDB database

First, we will deploy a MongoDB.

I am using you have kubectl and helm properly configured to talk to your Kubernetes cluster.

Run the following command to deploy a MongoDB database to your Kubernetes cluster:

helm install -n mongodb stable/mongodb --tls \
--set mongodbRootPassword=password

You will see a message stating that the MongoDB helm chart has been deployed

Load some data in MongoDB

Now that MongoDB is running, let’s load some data.

Run the following command to connect to the database:

kubectl run mongodb-mongodb-client --rm --tty -i --image bitnami/mongodb \
--command -- mongo --host mongodb-mongodb -p password

You will see the CLI prompt. Run the following commands to load the data:

db.myCollection.insertOne ( { key1: "value1" });
db.myCollection.insertOne ( { key2: "value2" });

Now, run the following command to retrieve the values:

db.myCollection.find()

Keep the command open, as we will use in the next steps

Back up MongoDB

Next, we will back up MongoDB, using mongodump.

First, we need to create a Persistent Volume Claim, by running the following command:

kubectl apply -f https://raw.githubusercontent.com/patrocinio/mongodb/master/deploy/mongodump_pvc.yaml

Then, run the following command to dump the MongoDB database:

kubectl apply -f https://raw.githubusercontent.com/patrocinio/mongodb/master/deploy/mongodump_job.yaml

This Kubernetes job will dump the MongoDB databases into the persistent volume created above.

Simulate data loss in MongoDB

Now, we are going to simulate some data loss in MongoDB, by deleting the data inserted above.

Inside the MongoDB CLI Pod, run the following command to delete one key:

db.myCollection.deleteOne ({ key1: "value1" });

If you run the following command:

db.myCollection.find()

you will see there is a single document in the collection.

Restore the MongoDB database

Now, let’s restore the MongoDB database, by running the following command:

kubectl apply -f https://raw.githubusercontent.com/patrocinio/mongodb/master/deploy/mongorestore_job.yaml

Validate the data is restored

Finally, let’s check to see if the data has been restored.

Back in the MongoDB CLI Pod, run the following command:

db.myCollection.find()

you will see that both documents are present.

Conclusion

In this article, I showed how to use mongodump and mongorestore to back up and restore a MongoDB database, using a PersistentVolume to store the data.

The solution is very simple and can be extended to schedule the backup or store the backup in different directories.

Bring your plan to the IBM Garage.
Are you ready to learn more about working with the IBM Garage? We’re here to help. Contact us today to schedule time to speak with a Garage expert about your next big idea. Learn about our IBM Garage Method, the design, development and startup communities we work in, and the deep expertise and capabilities we bring to the table.

Schedule a no-charge visit with the IBM Garage.

--

--