PostgreSQL Database Operators
There are a number of PostgreSQL database Kubernetes operators available—including Crunchy PostgreSQL for Kubernetes, Zalando PostgreSQL Operator, and Percona Distribution for PostgreSQL on Kubernetes to name just a few—however, for this blog post, I will be taking the popular EnterpriseDB (EDB) PostgreSQL Operator out for a spin.
If you want to try looking at using other database operators, visit OperatorHub.io, which provides a registry of Kubernetes operators.
Before we start, let’s take a quick look at my Kubernetes environment:
![Kubectl](/wp-content/uploads/2023/09/Kubectl.jpeg)
Install Cloud Native PostgreSQL
To begin, let’s install the latest Cloud Native PostgreSQL operator:
![Kubectl-1](/wp-content/uploads/2023/09/Kubectl-1.jpeg)
Next, let’s confirm the Operator installation:
![Kubectl-2](/wp-content/uploads/2023/09/Kubectl-2.jpeg)
Create Portworx Storage Class
Use a text editor to create a file, in the example below I have set the replication factor to 2, io profile for database workloads, and asked Portworx to support volume expansions.
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: px-postgresql-sc
provisioner: kubernetes.io/portworx-volume
parameters:
repl: "2"
io_profile: "db_remote"
allowVolumeExpansion: true
Next, create a Kubernetes storage class; you can use kubectl apply, for example.
![Kubectl-3](/wp-content/uploads/2023/09/Kubectl-3.jpeg)
Deploy PostgreSQL Cluster
Create a file named “px-postgresql-cluster.yaml”:
![Kubectl-4](/wp-content/uploads/2023/09/Kubectl-4.jpeg)
Next, use kubectl to create a 3-node PostgreSQL cluster:
kubectl apply -f px-postgresql-cluster.yaml
Then, check the PostgreSQL pod status:
![Kubectl-5](/wp-content/uploads/2023/09/Kubectl-5.jpeg)
Next, check the Portworx PersistentVolumeClaims:
![Kubectl-6](/wp-content/uploads/2023/09/Kubectl-6.jpeg)
Finally, check the PostgreSQL cluster status:
![Kubectl-10](/wp-content/uploads/2023/09/Kubectl-10.jpeg)
If you want additional details, you can obtain them with kubectl get cluster -o json.
Next, we want to get the cluster name:
![Kubectl-7](/wp-content/uploads/2023/09/Kubectl-7.jpeg)
Then, we want to get the cluster description:
![Kubectl-8](/wp-content/uploads/2023/09/Kubectl-8.jpeg)
The next step is to get the cluster current primary:
![Kubectl-9](/wp-content/uploads/2023/09/Kubectl-9.jpeg)
And finally, we want to get the cluster status:
![Kubectl-11](/wp-content/uploads/2023/09/Kubectl-11.jpeg)
Cloud Native PostgreSQL Plugin
EnterpriseDB provides a plugin for kubectl to manage a PostgreSQL cluster in Kubernetes. We can install this with:
![Kubectl-12](/wp-content/uploads/2023/09/Kubectl-12.jpeg)
The cnp extension provides some additional features to kubectl:
![Kubectl-13](/wp-content/uploads/2023/09/Kubectl-13.jpeg)
Checking the Cloud Native PostgreSQL Status
CNP shows we are running two replicas; if the primary fails for any reason, the cluster will failover to one of them.
![Kubectl-14](/wp-content/uploads/2023/09/Kubectl-14.jpeg)
Summary
In this post, I have shared how we can deploy a PostgreSQL database on Kubernetes using the EDB PostgreSQL operator and provide additional resilience using Portworx persistent storage.