Report this

What is the reason for this report?

Deploying Postgresql in Kubernetes Cluster PersistentVolume problem

Posted on December 31, 2024

Hello, I have a problem with postgresql deployed in Kubernetes cluster. In my Persistent Volume YAML file I have this

hostPath:
    path: /MyWindows/Directory/data/postgresql

The problem is that this Directory is always empty and the DB is not persistent.

Can you help me please? Thank you Roberto



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hello Roberto!

Usually, when working with Managed Kubernetes instances, it’s recommended to avoid using hostPath volumes because they are tied to specific nodes and not suitable for distributed, production-grade environments. Instead, you should use Persistent Volume Claims backed by DigitalOcean Volumes.

  1. The hostPath volume only works on a single node and doesn’t persist if the pod is rescheduled to a different node.
  2. In managed Kubernetes clusters, it’s better to use storage that is independent of the node lifecycle, like DigitalOcean Block Storage. Otherwise, you risk losing data if the pod is rescheduled to a different node or if the node is replaced.

DigitalOcean integrates with Kubernetes to provide dynamic volume provisioning:

Define a PVC that uses DigitalOcean Block Storage as the backing store.

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

This PVC will dynamically provision a block storage volume of 10Gi in size.

Modify your PostgreSQL deployment to use the PVC for data persistence.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgresql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgresql
  template:
    metadata:
      labels:
        app: postgresql
    spec:
      containers:
      - name: postgresql
        image: postgres:latest
        volumeMounts:
        - mountPath: "/var/lib/postgresql/data"
          name: postgresql-storage
        env:
        - name: POSTGRES_DB
          value: "your_database"
        - name: POSTGRES_USER
          value: "your_user"
        - name: POSTGRES_PASSWORD
          value: "your_password"
      volumes:
      - name: postgresql-storage
        persistentVolumeClaim:
          claimName: postgresql-pvc

Deploy the PVC and PostgreSQL resources:

kubectl apply -f pvc.yaml
kubectl apply -f postgresql-deployment.yaml
  1. Make sure the do-block-storage storage class is available in your cluster. It is automatically created in DigitalOcean Kubernetes clusters.
  2. For detailed instructions on adding volumes, check the official DigitalOcean documentation: Adding Volumes to DigitalOcean Kubernetes.

This has several benefits:

  • Data persists even if the pod is rescheduled or the node fails.
  • It adheres to Kubernetes best practices for cloud-native storage.
  • You avoid the limitations and risks of hostPath.

Let me know if you run into any issues!

- Bobby

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.