postgresql deployment to OpehSHift cluster example - unix1998/technical_notes GitHub Wiki

Certainly! Deploying PostgreSQL as a StatefulSet in OpenShift involves several steps, including creating necessary YAML files and configuring the environment. Here's a step-by-step guide:

Step 1: Create a Persistent Volume and Persistent Volume Claim

First, create the persistent storage that PostgreSQL will use.

persistent-volume.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-postgresql
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data/postgresql"

persistent-volume-claim.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-postgresql
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi

Apply these configurations:

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

Step 2: Create a ConfigMap for PostgreSQL Configuration

configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: postgresql-config
data:
  POSTGRES_DB: mydatabase
  POSTGRES_USER: myuser
  POSTGRES_PASSWORD: mypassword

Apply the ConfigMap:

oc apply -f configmap.yaml

Step 3: Create the StatefulSet

statefulset.yaml

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: postgresql
spec:
  serviceName: "postgresql"
  replicas: 1
  selector:
    matchLabels:
      app: postgresql
  template:
    metadata:
      labels:
        app: postgresql
    spec:
      containers:
      - name: postgresql
        image: postgres:13
        ports:
        - containerPort: 5432
          name: postgres
        envFrom:
        - configMapRef:
            name: postgresql-config
        volumeMounts:
        - name: postgresql-storage
          mountPath: /var/lib/postgresql/data
  volumeClaimTemplates:
  - metadata:
      name: postgresql-storage
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 10Gi

Apply the StatefulSet:

oc apply -f statefulset.yaml

Step 4: Create a Headless Service for StatefulSet

headless-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: postgresql
spec:
  ports:
  - port: 5432
    name: postgres
  clusterIP: None
  selector:
    app: postgresql

Apply the headless service:

oc apply -f headless-service.yaml

Step 5: Create a Service to Expose PostgreSQL Outside the OpenShift Cluster

service-expose.yaml

apiVersion: v1
kind: Service
metadata:
  name: postgresql-external
spec:
  type: LoadBalancer
  ports:
  - port: 5432
    targetPort: 5432
    protocol: TCP
    name: postgres
  selector:
    app: postgresql

Apply the service:

oc apply -f service-expose.yaml

Summary of YAML Files to Create

  1. Persistent Volume: persistent-volume.yaml
  2. Persistent Volume Claim: persistent-volume-claim.yaml
  3. ConfigMap: configmap.yaml
  4. StatefulSet: statefulset.yaml
  5. Headless Service: headless-service.yaml
  6. External Service: service-expose.yaml

Applying the YAML Files

Execute the following commands to apply all configurations:

oc apply -f persistent-volume.yaml
oc apply -f persistent-volume-claim.yaml
oc apply -f configmap.yaml
oc apply -f statefulset.yaml
oc apply -f headless-service.yaml
oc apply -f service-expose.yaml

This will set up a PostgreSQL database in a StatefulSet within OpenShift and expose it to the outside world.