w3resource

Deploying PostgreSQL on Kubernetes


Running PostgreSQL on Kubernetes: Deployment Guide and Key Benefits

Deploying PostgreSQL on Kubernetes offers scalable, resilient database management. By containerizing PostgreSQL, Kubernetes ensures high availability, automated recovery, and easy scaling. This guide provides a complete walkthrough for deploying PostgreSQL on Kubernetes with an emphasis on persistent storage, configurations, and best practices.

Why Run PostgreSQL on Kubernetes?

Running PostgreSQL on Kubernetes combines the power of Kubernetes’ orchestration and PostgreSQL’s robust database capabilities. This setup is ideal for microservices architectures, dynamic scaling, and automated failover, all of which are vital in cloud-native applications.

Step 1: Prepare the Kubernetes Cluster

Ensure your Kubernetes cluster is running and accessible. You can use managed Kubernetes services like Google Kubernetes Engine (GKE), Amazon EKS, or Azure AKS.

Step 2: Create a Persistent Volume and Claim

Persistent storage is critical for PostgreSQL so that data persists even if a pod restarts.

Define Persistent Volume (PV) and Persistent Volume Claim (PVC):

Create a YAML file named postgres-pv.yaml:

Code:

# Persistent Volume
apiVersion: v1
kind: PersistentVolume
metadata:
  name: postgres-pv
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data/postgres" # Customize as per your environment
---
# Persistent Volume Claim
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: postgres-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi

Apply the PV and PVC configuration:

kubectl apply -f postgres-pv.yaml

Step 3: Create PostgreSQL Deployment and Service

The Deployment defines the pod specification, while the Service exposes PostgreSQL.

Define Deployment and Service

Create postgres-deployment.yaml:

# PostgreSQL Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
      - name: postgres
        image: postgres:latest
        ports:
        - containerPort: 5432
        env:
        - name: POSTGRES_USER
          value: "user"
        - name: POSTGRES_PASSWORD
          value: "password"
        - name: POSTGRES_DB
          value: "mydatabase"
        volumeMounts:
        - mountPath: "/var/lib/postgresql/data"
          name: postgres-storage
      volumes:
      - name: postgres-storage
        persistentVolumeClaim:
          claimName: postgres-pvc
---
# PostgreSQL Service
apiVersion: v1
kind: Service
metadata:
  name: postgres-service
spec:
  type: ClusterIP
  ports:
    - port: 5432
      targetPort: 5432
  selector:
    app: postgres

Apply the Deployment and Service:

kubectl apply -f postgres-deployment.yaml

Step 4: Access PostgreSQL in Kubernetes

To connect to PostgreSQL from within the Kubernetes cluster, use postgres-service as the hostname.

kubectl exec -it <pod_name> -- psql -U user -d mydatabase -h postgres-service

Additional Tips:

  • Helm Charts: For simplified deployment, consider using Helm charts specifically designed for PostgreSQL, which often include automated configuration of persistent volumes, replicas, and monitoring.
  • Scaling: Use Kubernetes scaling commands to adjust PostgreSQL pod replicas, ensuring high availability.
  • Monitoring and Alerts: Use tools like Prometheus and Grafana for real-time monitoring of PostgreSQL metrics within Kubernetes.

All PostgreSQL Questions, Answers, and Code Snippets Collection.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/PostgreSQL/snippets/postgresql-kubernetes-deployment.php