Postgres is a relational database that uses SQL to perform queries. It provides features that safely allow users to persist and scale data workloads. It can handle workloads ranging from small single-machine applications to large internet-facing applications with many concurrent users.
Stateful workloads have been shown to run successfully on Kubernetes. Postgres on Kubernetes is a popular choice, especially for organizations that have standardized on Kubernetes as the control plane for their applications. In fact, a recent survey cited Postgres as one of the top-3 technologies running on Docker!
For the sake of simplicity, we will be using a hostPath volume but you can choose any StorageClass you have deployed in the cluster.
To get started, create your free Platform9 Kubernetes account by entering your email below, then select the ‘Deploy Now’ option. Once you have created and verified your account, follow the steps outlined below.
NOTE: This section is for those who want to try PostGres for testing/Stage/Dev environment and deploys PostGres as a deployment object and not as a Statefulset. The storageClass assumed in this section is hostPath which stores the data on the node’s local disk. If the node crashes, the postgres data on the node will not be recoverable.
$ git clone https://github.com/KoolKubernetes/db.git
xxxxxxxxxx
$ kubectl create secret generic postgres-secret --from-literal=POSTGRES_DB='postgresdb' \
--from-literal=POSTGRES_USER='' --from-literal=POSTGRES_PASSWORD=''
For eg.
xxxxxxxxxx
$ kubectl create secret generic postgres-secret --from-literal=POSTGRES_DB='postgresdb' \
--from-literal=POSTGRES_USER='' --from-literal=POSTGRES_PASSWORD=''
$ kubectl apply -f deploy.yaml
persistentvolume/postgres-pv-volume created
persistentvolumeclaim/postgres-pv-claim created
deployment.apps/postgres created
service/postgres configured
$ kubectl get pods -l app=postgres
(NOTE: You can change the serviceType to loadbalancer or any other type as per your application needs. NodePort type has been choosen here only for simplicity sake.)
$ sudo psql -h localhost -U --password -p 31070
If you are following the example mentioned above,
$ sudo psql -h localhost -U postgresadmin --password -p 31070 postgresdb
You will be asked for a password, enter the DBpassword that was specified during Secret creation in Step 2.
xxxxxxxxxx
Password for user postgresadmin:
psql (9.5.21, server 12.3 (Debian 12.3-1.pgdg100+1))
WARNING: psql major version 9.5, server major version 12.
Some psql features might not work.
Type "help" for help.
postgresdb=#
Run the following command to cleanup the created Kubernetes objects –
$ kubectl delete -f deploy.yaml
All the checks in the above command should complete successfully, before proceeding.
NOTE: This section is for those who want to deploy Postgres in an HA setup where loss of one PostGres pod doesn’t result in a data loss. Please note that you need a StorageClass that provides Shared Storage, Replication and redundancy. This guide is using Rook and we’ll be using StatefulSet instead of a deployment object in the earlier section.
xxxxxxxxxx
helm repo add bitnami https://charts.bitnami.com/bitnami
helm install postgresql-ha bitnami/postgresql-ha
(NOTE: you can pass additional values referred here if needed)
xxxxxxxxxx
kubectl get pods
NAME READY STATUS RESTARTS AGE
my-release-postgresql-ha-pgpool-768898c659-xlrqn 1/1 Running 0 156m
my-release-postgresql-ha-postgresql-0 1/1 Running 1 147m
my-release-postgresql-ha-postgresql-1 1/1 Running 0 155m
[2020-08-20 08:16:02] [NOTICE] starting monitoring of node "my-release-postgresql-ha-postgresql-2" (ID: 1001)
On the standby pod, you can observe the logs as mentioned below
xxxxxxxxxx
[2020-08-20 08:14:46] [NOTICE] monitoring cluster primary "my-release-postgresql-ha-postgresql-1" (ID: 1000)
[2020-08-20 08:14:52] [NOTICE] new standby "my-release-postgresql-ha-postgresql-2" (ID: 1001) has connected
helm delete postgresql-ha