Kubernetes Tutorial: How to Create Local Snapshots of PostgreSQL Persistent Volume Claims on GKE

This tutorial is a walk-through of how to create local snapshots of PostgreSQL persistent volume claims on Google Kubernetes Engine (GKE) by Certified Kubernetes Administrator (CKA) and Application Developer (CKAD) Janakiram MSV.

TRANSCRIPT:

Janakiram MSV: Hi. In this demo, I want to show you how to take a snapshot of a running PostgreSQL database running in GKE backed by Portworx. A local snapshot is a point-in-time backup object stored within the local cluster of Portworx. So let’s take a look at the environment. We have exactly one pod that’s currently running PostgreSQL database and it’s already populated with some sample data. So let’s grab the pod name, do an exec to get into the shell, and then check out the sample data because we want to make sure that this is going to be intact when we do a backup and restore.

So we’re going to access the psql shell and then look at the tables. We have a bunch of tables. We’d also check the count of the rows from one of the tables called accounts. So this has about five million rows. So let’s exec the psql shell and the pod and start the snapshot process. So as I mentioned, the local snapshot is going to be deciding within the local storage cluster of Portworx. So creating this is pretty straightforward. First, we need to identify what is the PVC that we are going to use for taking the snapshot. So in our case, we have one PVC that is associated with the Postgres pod that we have seen earlier.

Now, we are going to create a local snapshot that uses this as the source. So how does this natural definition look like? Well, a snapshot is not very different from other artifacts or other objects of Kubernetes. So we are going to mention the kind as volume snapshot and define the persistent volume claim which is already existing and backing our production database, which is called the “px-postgres-pvc”. And this was the PVC that we have just seen with kubectl get pvc command. So, we are pointing the snapshot to this PVC. And from there, we are going to create a snapshot. So, let’s go head and create a local snapshot.

So this is going to now result in a volume snapshot so we can check it out by running kubectl get volume snapshot and this shows that the PVC is now created as a snapshot. We can also check out the volume snapshot data that is going to show us the volume snapshot that’s created from the previous PVC that we have seen. So now that we have the snapshot, it’s time for us to use it to create a new instance of Postgres. So we can create a new PVC but before that I want to point you to something. So when we deploy Portworx within Kubernetes, we get to see a storage class called “stork-snapshot-sc” and this is the key player behind the scenes that is helping us create snapshots and restoring them.

So, this storage class is very essential for us to perform storage ops related to backup and restore. So with the snapshot in place, let’s understand how to restore it. So we’re going to create a new PVC and this is again, not very different, except that we’re going to create an annotation that says we are going to use the existing snapshot created in the previous step. So that’s why we are pointing this to PX Postgres snapshot. And we’re also mentioning the storage class name as the “stork-snapshot-sc”. And everything else remains the same.

Of course, we are now creating a new PVC with 2 GB of size. So let’s go ahead and create the new PVC, which is coming from the snapshot. So now when we do “kubectl get pvc”, we notice that there is a clone of the previous PVC that was already created. So this can be used to create a brand new pod, which is going to bring the data from the snapshot. So, let’s move to the final step of creating a pod from the new PVC, which is restored from this snapshot. So here, everything remains the same. This pod is not very different except that we are pointing it to the claim, which is the “px-postgres-snap-clone”, which is actually coming from our PVC mentioned here.

So it’s pretty straightforward where we are pointing a pod to the PVC restored from the snapshot. In fact, the pod doesn’t even know that it is actually talking to a PVC, which is a clone and not the exact same PVC that was backing the original pod. So that’s the final step. Now, we are creating a pod which is going to contain exactly the same data because we are restoring from the snapshot. So, let’s go ahead and check out the pods. Now, you notice that there is a new Postgres pod, which is based on the snapshot and which is a clone. So let’s grab the name of this pod, do an exec. So there we go, we have then exactly the same set of tables and we can also do a count to make sure that it is intact. So we have 5 million rows, perfect. So let’s quit Postgres and get out of the pod.

So that was a quick walk-through of creating a snapshot and then creating a PVC from that snapshot and finally backing up a pod from that PVC created from the snapshot. Pretty straightforward. Thanks for watching.

Avatar

Contributor | Certified Kubernetes Administrator (CKA) and Developer (CKAD)

Share Share on Facebook Tweet about this on Twitter Share on LinkedIn



Back to Blog