🐘
Kubernetes

PostgreSQL on Kubernetes

PostgreSQL with PVC — persistent database in the cluster

📋Configuration Files

postgres-pvc.yml

yaml
1apiVersion: v1
2kind: PersistentVolumeClaim
3metadata:
4 name: postgres-pvc
5spec:
6 accessModes:
7 - ReadWriteOnce
8 resources:
9 requests:
10 storage: 5Gi

postgres-deployment.yml

yaml
1apiVersion: apps/v1
2kind: Deployment
3metadata:
4 name: postgres
5spec:
6 replicas: 1
7 selector:
8 matchLabels:
9 app: postgres
10 template:
11 metadata:
12 labels:
13 app: postgres
14 spec:
15 containers:
16 - name: postgres
17 image: postgres:16-alpine
18 ports:
19 - containerPort: 5432
20 env:
21 - name: POSTGRES_USER
22 value: app
23 - name: POSTGRES_PASSWORD
24 valueFrom:
25 secretKeyRef:
26 name: postgres-secret
27 key: password
28 - name: POSTGRES_DB
29 value: appdb
30 volumeMounts:
31 - name: data
32 mountPath: /var/lib/postgresql/data
33 volumes:
34 - name: data
35 persistentVolumeClaim:
36 claimName: postgres-pvc

postgres-secret.yml

yaml
1apiVersion: v1
2kind: Secret
3metadata:
4 name: postgres-secret
5type: Opaque
6stringData:
7 password: change-me-in-production
📄

Step 01

Apply PostgreSQL

(01)Deploy to cluster

Linux
1kubectl apply -f .
2kubectl get all
3kubectl get pods -w

(02)Remove

Linux
1kubectl delete -f .