Blog

Comprehensive Guide to Deploying Zalando Postgres Operator and PostgreSQL Cluster on OKD/OpenShift

06.01.2025
Reading time: 3 mins.
Last Updated: 15.01.2025

Table of Contents

Deploying a PostgreSQL cluster on OKD/OpenShift using the Zalando Postgres Operator is a powerful and scalable solution. This guide provides step-by-step instructions on deploying the operator, configuring storage using StorageClass or NFS, and accessing the PostgreSQL database efficiently.

Before proceeding, ensure you have:

  • An OKD/OpenShift cluster with the CLI (oc) installed and configured
  • Helm installed on your local system
  • An NFS server (if using NFS for storage)
git clone https://github.com/zalando/postgres-operator.git
helm repo add postgres-operator-charts 
https://opensource.zalando.com/postgres-operator/charts/postgres-operator
  • Create a dedicated namespace:
oc create namespace zalando-operator
  • Install the operator using Helm:
helm install postgres-operator postgres-operator-charts/postgres-

operator -n zalando-operator

OpenShift enforces security constraints via SCC. Grant the anyuid SCC to the default service account:

oc adm policy add-scc-to-user anyuid -z default -n zalando-operator

Check that the operator pods are running:

oc get pods -l app=postgres-operator -n zalando-operator

Apply the minimal manifest provided in the Zalando repository:

oc apply -f manifests/minimal-postgres-manifest.yaml

Step 1: Create a Persistent Volume (PV)

Save the following PV definition to zalando-persistent-volume.yaml:

kind: PersistentVolume
metadata:
name: database-storage
annotations:
kubernetes.io/pv-protection: "true"
spec:
capacity:
storage: 100Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
volumeMode: Filesystem
nfs:
path: /VM/NFS/postgresql
server: 10.124.0.4

Apply the PV configuration:

oc apply -f zalando-persistent-volume.yaml

Step 2: Create a Persistent Volume Claim (PVC)

Save the following PVC definition to zalando-persistent-volume-claim.yaml:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pgdata-acid-cluster-avitech-0
namespace: zalando-cluster
labels:
application: spilo
cluster-name: acid-cluster-avitech
team: acid
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 100Gi
volumeName: database-storage
volumeMode: Filesystem
storageClassName: ""

Apply the PVC configuration:

oc apply -f zalando-persistent-volume-claim.yaml

Step 3: Grant SCC for the PostgreSQL Namespace

Grant the privileged SCC to the default service account in the zalando-cluster namespace:

oc adm policy add-scc-to-user privileged -z default -n zalando-cluster

Save the following Role definition to zalando-role.yaml:

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: zalando-role
  namespace: zalando-cluster
rules:
- apiGroups: [""]
  resources:
    - endpoints
    - endpoints/restricted
  verbs:
    - get
    - list
    - watch
    - create
    - update
    - delete

Apply the Role:

oc apply -f zalando-role.yaml

Save the following RoleBinding definition to zalando-rolebinding.yaml:

apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: zalando-rolebinding
  namespace: zalando-cluster
subjects:
  - kind: ServiceAccount
    name: postgres-pod
    namespace: zalando-cluster
roleRef:
  kind: Role
  name: zalando-role
  apiGroup: rbac.authorization.k8s.io

Apply the RoleBinding:

oc apply -f zalando-rolebinding.yaml

Check that your PostgreSQL cluster is running:

oc get pods -l application=spilo -n zalando-cluster

1.Retrieve the PostgreSQL credentials:

export PGPASSWORD=$(oc get secret postgres.acid-minimal-cluster.credentials.postgresql.acid.zalan.do -o 'jsonpath={.data.password}' | base64 -d)
export PGSSLMODE=require

2.Retrieve the cluster's service URL:
export HOST_PORT=$(oc get svc acid-minimal-cluster -o jsonpath='{.spec.clusterIP}:{.spec.ports[0].port}')
export PGHOST=$(echo $HOST_PORT | cut -d: -f 1)
export PGPORT=$(echo $HOST_PORT | cut -d: -f 2)
Connect using psql:
psql -U postgres

Access the PostgreSQL pod directly:

oc exec -it acid-cluster-avitech-0 -n zalando-cluster -- /bin/sh

Start psql inside the pod:

psql

Change the password if needed:

ALTER USER postgres WITH PASSWORD 'ExampleStrongPassword'

To connect to the Zalando PostgreSQL cluster using JDBC, ensure your connection string is correctly configured. Below are the appropriate formats:

jdbc:postgresql://acid-cluster-avitech.zalando-cluster.svc.cluster.local:5432/postgres?postgres&ssl=true&sslmode=require
jdbc:postgresql://<svc-name>.<namespace>.svc.cluster.local:5432/postgres?postgres&ssl=true&sslmode=require

Key Points:

  • Replace with the PostgreSQL service name
  • Replace with the Kubernetes namespace where the cluster is deployed
  • Ensure SSL is enabled (ssl=true) and the sslmode is set to require for secure connections.

Deploying a PostgreSQL cluster on OKD/OpenShift using the Zalando Postgres Operator is straightforward and highly scalable. By following this guide, you can seamlessly configure your database cluster with either a StorageClass or NFS, ensuring reliable storage and robust performance.

For further information, refer to the Zalando Postgres Operator Documentation.

Explore ITGix SRE services and learn more about our expertise.

Leave a Reply

Your email address will not be published. Required fields are marked *

More Posts

Learn effective strategies for Amazon S3 migration and explore advanced tools like Storage Lens, Athena, and QuickSight to gain actionable insights into your big data usage. Perfect for organizations optimizing...
Reading
AWS re:Invent 2024 showcased groundbreaking updates to cloud technology, addressing the evolving needs of modern organizations. With innovations designed to boost performance, enhance security, and simplify scalability, these advancements promise...
Reading
Get In Touch
ITGix provides you with expert consultancy and tailored DevOps services to accelerate your business growth.
Newsletter for
Tech Experts
Join 12,000+ business leaders and engineers who receive blogs, e-Books, and case studies on emerging technology.