In this post we will review the steps to deploy a PostgreSQL cluster on a kubernetes cluster.
PostgreSQL is a veteran DBMS existing for ~30 years(!!). As such it is less a kubernetes player and does not automatically adjust the cluster like other technologies such as NATS and ClickHouse. Hence to maintain the cluster we require first to deploy an operator. There are several operators available, and in this post we use the CloudNativePG.
Cloud Native PG Operator
To deploy the CloudNativePG we can use the simple command:
kubectl apply -f https://raw.githubusercontent.com/cloudnative-pg/cloudnative-pg/release-1.29/releases/cnpg-1.29.0.yaml
However, in case of need to include this as part of an umbrella helm chart, we should download the file and split it to multiple templates. The CRDs should be included in a different folder, for example:
my-umbrella-helm/charts/cnpg/crds
The CRDs folder is a special helm folder enabling deployment of the CRDs before any additional templates are rendered.
Other template can be deployed in the standard templates folder, for example:
my-umbrella-helm/charts/cnpg/templates
We would usually update the templates in this folder to match our own helm deployment standards such as entities names, images location, labels, enable/disable flags. Notice that CloudNativePG requires the label:
app.kubernetes.io/name: cloudnative-pgSo we must include this label when customizing the labels.
The PostgreSQL cluster
Once the operator is up and running, all we need to do is to render a cluster CRD, for example:
apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
name: postgresql
spec:
instances: 3
imageName: my-repo/my-postgresql/dev:18.3
imagePullPolicy: Always
storage:
size: 10Gi
postgresql:
parameters:
max_connections: "200"
bootstrap:
initdb:
database: my-db
owner: admin
secret:
name: postgresql-secret
We usually update the image name to be downloaded from our own repo. Notice the CloudNativePG blocks usage of "latest" version for the PostgreSQL cluster, so even on the local kind deployment we must use a specific version.
In the cluster resource we specify several important settings: the default database name, the default owner role of the database, and a secret with the password for that user.