Welcome!

Deploy Mongo on Portworx volumes using Helm

Difficulty:Intermediate

Estimated Time:10 minutes

Portworx is the cloud native storage company that enterprises depend on to reduce the cost and complexity of rapidly deploying containerized applications across multiple clouds and on-prem environments. With Portworx, you can manage any database or stateful service on any infrastructure using any container scheduler. You get a single data management layer for all of your stateful services, no matter where they run.

A popular Kubernetes storage and persistent storage for Docker solution, Portworx is a clustered block storage solution and provides a Cloud-Native layer from which containerized stateful applications programmatically consume block, file and object storage services directly through the scheduler.

In this tutorial, you will learn how to deploy MongoDB to Kubernetes using Helm and Portworx:

Step: Deploy Helm to our Kubernetes cluster

In this step we will show you how to deploy Helm to your Kubernetes cluster. Helm helps you manage Kubernetes applications — Helm Charts helps you define, install, and upgrade even the most complex Kubernetes application. Charts are easy to create, version, share, and publish — so start using Helm and stop the copy-and-paste madness.

Step: Deploy MongoDB using Helm with the Portworx Storage Class

Once Helm is deployed we will use it to deploy the MongoDB chart. In order to provide for a highly available MongoDB instance, we will configure the chart to use a Portworx volume that we will create to ensure 3 replicas of the data. Portworx will perform synchronous replication of the volume so that the MongoDB data is well protected. Once MongoDB is deployed we will use the MongoDB client to create some data.

This diagram illustrates the configuration:

Step: Perform a failover test

In this step we will simulate a node failure to show how Kubernetes can reschedule your MongoDB pod to run on another node in your cluster. Portworx is able to ensure that the rescheduled pod connects to it data from anywhere in the cluster but goes a step further by ensuring that the pod will be scheduled on a node which has a local copy of the data so that you get the best level of performance. This advanced orchestration of stateful workloads is handled by the STorage ORchestrator for Kubernetes (STORK).

This diagram illustrates the failover scenario:

Step: Expand the volume used by MongoDB

In this step we will show how Portworx volumes can be dynamically expanded with zero downtime. Portworx are thinly provisioned and the size of the PVC is used as a maximum size. Once you reach this upper limit on the size of your MongoDB database you can easily perform this operation to add more capacity to your volume.

This diagram illustrates the expansion of your volume:

Step: Take a snapshot and restore it

In this final step we will show how snapshots can be used with MongoDB volumes. Snapshots are efficient point-in-time copies of volumes that can be either read-write or read-only. Each snapshot is a volume in its own right and can be used freely by applications. They are implemented using a copy-on-write technique, so that they only use space in places where they differ from their parent volume.

This diagram illustrates the snapshot of your volume:

Other things you should know

To learn more about how why running Mongo on Portworx is a great idea take a look at the following links:

Steps

Deploy Mongo on Portworx volumes using Helm

Wait for Kubernetes & Portworx to be ready

Step: Wait for Kubernetes to be ready

Click the below section which waits for all Kubernetes nodes to be ready.

watch kubectl get nodes

When all 4 nodes show status Running then hit clear to ctrl-c and clear the screen.

Step: Wait for Portworx to be ready

Watch the Portworx pods and wait for them to be ready on all the nodes. This can take a few minutes since it involves pulling multiple docker images. You will see 'No resources found' until all images are pulled.

watch kubectl get pods -n kube-system -l name=portworx -o wide

When all the pods show STATUS Running and READY 1/1 then hit clear to ctrl-c and clear the screen.

Now that we have the Portworx cluster up, let's proceed to the next step!

Deploy Helm

In this step, we will deploy Helm

Step: Create Service Account

Warning: this is not how you should secure Helm, take a look at this for more information.

It'll take a few seconds and then Helm will report on the configurations it created. You will see that Helm creates a kubernetes secret, a service, a deployment, and a pod. To learn more about this Helm chart check it out on github!

That's it! Isn't helm great? We think it is.

Step: Wait for Mongo to start

Run the below command and wait for the Mongo pod to be in ready state.

watch kubectl get pods -o wide

When the Mongo pod is in STATE Running and READY 1/1 then hit clear to ctrl-c and clear the screen. Be patient, if it's staying in Pending state for a while it's because it has to fetch the docker image.

HA Failover

In this step, we will simulate failure by cordoning the node where mongoQL is running and then deleting the mongoQL pod. The pod will then be resheduled by the STorage ORchestrator for Kubernetes (STORK) to make sure it lands on one of the nodes that has of replica of the data.

Step: Simulate a node failure to force mongoQL to restart

You should make sure you exited the MongoDB client shell in the previous step and that you see the master$ prompt before you cordon the node where MongoDB is running to simulate a node failure or network partition:

As you can see the volume is now expanded and our MongoDB database didn't require restarting.

kubectl get pods

Take Snapshot

In this step, we will take a snapshot, simulate destroy our database, and recover from the snapshot

Step: Take snapshot using kubectl

First, take a look at px-snap.yaml cat px-snap.yaml. Notice that we create pre and post snapshot rules to ensure the snapshot is consistent. The pre-snapshot rule forces the mongod to flush all pending write operations to disk and locks the entire mongod instance to prevent additional writes until the user releases the lock with a corresponding db.fsyncUnlock() command. The post-snapshot rule releases this lock. Let's use kubectl to take the snapshot:

kubectl create -f px-snap.yaml

You can see the snapshots using the following command:

kubectl get volumesnapshot,volumesnapshotdatas

Now we're going to go ahead and do something stupid because it's Katacoda and we're here to learn.
Run the client and in a shell.

Step: Restore the snapshot and see your data is still there

Snapshots are just like volumes so we can go ahead and use it to start a new instance of MongoDB. Note here that we're leaving the old instance to carry on with it's version of the volume and we're creating a brand new instance of MongoDB with the snapshot data!

First, lets create a new pvc, cat px-snap-pvc.yaml, from the snapshot:

kubectl create -f px-snap-pvc.yaml

Now let's deploy a second MongoDB that uses this new PVC and make sure it came up.

Replica Sets

MongoDB can run in a single node configuration as we showed and in a clustered configuration using replica sets (not to be confused with the Kubernetes Stateful Sets). A replica set is a group of MongoDB instances that maintain the same data set. A replica set contains several data bearing nodes and optionally one arbiter node. Of the data bearing nodes, one and only one member is deemed the primary node, while the other nodes are deemed secondary nodes. This is documented here.

Step: Deploying a MongoDB Replica Set with Kubernetes Stateful Set

Portworx supports Stateful Sets which allow you to deploy a MongoDB replica set. With stateful sets the PVC are dynamically created based on a provided storage class. Try the following command to launch a 3 node MongoDB replica set backed by Portworx volumes using the helm chart for MongoDB replicasets:

Debugging Scenarios

Help

Katacoda offerings an Interactive Learning Environment for Developers. This course uses a command line and a pre-configured sandboxed environment for you to use. Below are useful commands when working with the environment.

cd <directory>

Change directory

ls

List directory

echo 'contents' > <file>

Write contents to a file

cat <file>

Output contents of file

Vim

In the case of certain exercises you will be required to edit files or text. The best approach is with Vim. Vim has two different modes, one for entering commands (Command Mode) and the other for entering text (Insert Mode). You need to switch between these two modes based on what you want to do. The basic commands are: