Cloud Native PostgreSQL: An Operator to run full featured PostgreSQL in Kubernetes

Introduction
CloudNativePG is the Kubernetes operator that covers the full lifecycle of a highly available PostgreSQL database cluster with a primary/standby architecture, using native streaming replication.
Installation
To install the CloudNativePG operator, you can use the helm chart:
Configuration
The CloudNativePG operator can be configured using the following parameters:
Deploy a cluster
First of all, set the login/password of the superuser. Let's define a secret for this:
As you can see, the password and username are base64 encoded. You can use the following command to encode them:
And then set the cluster configuration with MetalLB service:
Then, deploy the cluster and the secret:
You can validate the deployment pods and services this way:
As you can see, multiple services:
pg-cluster-r: Points to any PostgreSQL instance in the cluster (read).pg-cluster-roPoints to the replicas, where available (read-only).pg-cluster-rwPoints to the primary instance of the cluster (read/write).pg-cluster-rw-lbis the load balancer service we've set earlier
CLI
The CloudNativePG operator provides a CLI tool to manage your PostgreSQL clusters!!! This is a must have tool to manage your clusters. It allows you to manage its lifecycle, backup, restore, etc.
To install the CLI, you can use the following command:
Then, you can check your cluster status for example:
Backups
With CloudNativePG, backups are managed by a third party tool called Barman Cloud Plugin. We'll see here how to perform backups with PITR (Point In Time Recovery) on an Object Storage and we'll take Garage as an example.
Installation is simple:
Then we'll start to configure it. So first, we'll add a configuration explaining how backups will be made with S3:
Now the secret containing the credentials:
Now the configuration is correct. We need to apply it to an existing cluster in the spec section of the cluster configuration for WAL archiving:
And finally, setup the backup scheduling:
Now apply the configuration:
You should see the cluster WAL coming into the S3 bucket.
Restore
If all your nodes are in a bad shape and you need to restore the cluster. Here is the procedure:
-
We'll start by deleting the existing cluster (if we want to keep the same name):
-
Then, let's update our cluster configuration by adding the recovery section:
-
Finally, we'll apply the configuration:
Note
Once the cluster is restored, you need to update the cluster configuration to remove or comment the bootstrap section and the externalClusters section.