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!
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.
hostPath
volume only works on a single node and doesn’t persist if the pod is rescheduled to a different node.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
do-block-storage
storage class is available in your cluster. It is automatically created in DigitalOcean Kubernetes clusters.This has several benefits:
hostPath
.Let me know if you run into any issues!
- Bobby
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.