Tuesday 2 April 2019


How to take PostgreSQL dump from Kubernetes cluster on Google cloud Platform








Almost all the web application has the database. When we deploy our application in Cloud (in this case Google Cloud Platform) it’s quite possible that we put the database on a different POD other than the application POD.

In this section we are going to show, how can we take the Postgres DB dump which is deployed on the Kubernetes cluster.

Assumptions: The postgres pod is already deployed in the Kubernetes cluster.

Steps 

1. Open the browser and go to the  "https://console.cloud.google.com/" 
2. On the GCP menu click on the "Activate Cloud Shell" 

3. Find out the name of the running POD using the command 
yogeshsonawane@cloudshell:~ (Project-1)$ kubectl get pods | grep <Name of deployment or Pod>
e.g. kubectl get pods | grep "mytestApp"

4. Switch to the raw terminal mode i.e. Open a shell in the running Kubernetes POD

        kubectl exec -it <POD Name> -c <Container Name> -- /bin/bash
        kubectl exec -it mytestApp-7c4fb7594c-qd94j   -c datastore -- /bin/bash

In this command "mytestApp-7c4fb7594c-qd94j" is the POD name found from the command in step#3. "DataStore" is the container name.

5. take the pgdump
        /usr/bin/pg_dump --dbname mcmdb -u myappb  > myappdb.sql

6. Open another shell. by clicking on the "Add Cloud Shell Session  button " (+) button displayed on the 
7. copy the dump file from pg datastore cluster to outside. i.e. to the current shell.
8. take the pgdump using the following command
        kubectl cp mytestApp-7c4fb7594c-qd94j:/root/ myappdb.sql  -c datastore .

You can also copy this dump file to the Google Storage bucket using the following command.

gsutil cp myappdb.sql gs://<bucket_name>/<folder_inside_bucket>/

9. Download the dump using the option available in the active shell session.