Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Deploying PostgreSQL on Kubernetes

Kubernetes is a powerful orchestration platform for managing containerized applications at scale. This tutorial will guide you through the process of deploying PostgreSQL on Kubernetes, including setup, configuration, and common tasks.

1. Prerequisites

Before you begin, ensure that you have the following prerequisites:

  • Kubernetes cluster set up. You can use Minikube for local development or a cloud provider like GKE, EKS, or AKS.
  • kubectl installed and configured to interact with your Kubernetes cluster.
  • Basic knowledge of Kubernetes and PostgreSQL.

2. Creating a PostgreSQL Deployment

To deploy PostgreSQL on Kubernetes, you need to create a Deployment and a Service. Here is a sample deployment YAML file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
      - name: postgres
        image: postgres:latest
        env:
        - name: POSTGRES_PASSWORD
          value: mysecretpassword
        ports:
        - containerPort: 5432
            

This file defines a Deployment named postgres-deployment that runs one replica of the PostgreSQL container with the latest image and sets the PostgreSQL password to mysecretpassword.

3. Creating a PostgreSQL Service

To expose PostgreSQL outside of the Kubernetes cluster, you need to create a Service. Here is a sample service YAML file:

apiVersion: v1
kind: Service
metadata:
  name: postgres-service
spec:
  type: NodePort
  selector:
    app: postgres
  ports:
    - protocol: TCP
      port: 5432
      targetPort: 5432
            

This file defines a Service named postgres-service that exposes the PostgreSQL container on port 5432.

4. Applying the Configuration

To apply the configuration files, use the following commands:

kubectl apply -f postgres-deployment.yaml
kubectl apply -f postgres-service.yaml

These commands create the PostgreSQL Deployment and Service in your Kubernetes cluster.

5. Verifying the Deployment

To verify that the PostgreSQL deployment is running correctly, use the following command:

kubectl get pods

You should see a pod named postgres-deployment-xxxxxx with the status Running.

6. Accessing PostgreSQL

To access the PostgreSQL service from your host machine, use the following command to get the NodePort:

kubectl get service postgres-service

Look for the NodePort value in the output. You can connect to PostgreSQL using a PostgreSQL client like pgAdmin or psql with the Node's IP address and the NodePort.

7. Persisting Data with PersistentVolume

To ensure that your PostgreSQL data is not lost when the pod restarts, use a PersistentVolume (PV) and PersistentVolumeClaim (PVC). Here is a sample PV and PVC YAML file:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: postgres-pv
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: postgres-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
            

This configuration creates a PersistentVolume and a PersistentVolumeClaim with 1Gi of storage.

8. Using the PVC in the Deployment

To use the PVC in the PostgreSQL Deployment, modify the deployment YAML file to include the volume and volumeMounts:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
      - name: postgres
        image: postgres:latest
        env:
        - name: POSTGRES_PASSWORD
          value: mysecretpassword
        ports:
        - containerPort: 5432
        volumeMounts:
        - mountPath: /var/lib/postgresql/data
          name: postgres-storage
      volumes:
      - name: postgres-storage
        persistentVolumeClaim:
          claimName: postgres-pvc
            

This modification mounts the PVC to the PostgreSQL data directory.

9. Applying the Persistent Storage Configuration

To apply the PV and PVC configuration, use the following command:

kubectl apply -f postgres-pv-pvc.yaml

This command creates the PersistentVolume and PersistentVolumeClaim in your Kubernetes cluster.

10. Conclusion

Deploying PostgreSQL on Kubernetes provides a scalable and resilient environment for your database. This tutorial covered the basics of setting up and managing PostgreSQL on Kubernetes, including deployment, service creation, and persistent storage. With these skills, you can easily deploy and scale PostgreSQL in a Kubernetes cluster.