Create a PostgreSQL consolidated manifest file for k8s Deployment.
2 min readJun 23, 2024
To Deploy the Postgres database in the K8S environment divided into three parts
- Create PVC manifest with existing Storage Class within Cluster
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: postgres-vol
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
storageClassName: storage-class-name
volumeMode: Filesystem
2. Create a Deployment file with an init container to remove default data to create a user-defined DB
apiVersion: apps/v1
kind: Deployment
metadata:
name: postgres-deployment
labels:
app: postgres
spec:
replicas: 1
selector:
matchLabels:
app: postgres
template:
metadata:
labels:
app: postgres
spec:
initContainers:
- name: init-postgres
image: busybox
command: ["sh", "-c", "rm -rf /var/lib/postgresql/data/*"]
volumeMounts:
- mountPath: /var/lib/postgresql/data
name: postgres-vol
containers:
- name: postgres
image: postgres
ports:
- containerPort: 5432
env:
- name: POSTGRES_DB
value: dbname
- name: POSTGRES_USER
value: dbuser
- name: POSTGRES_PASSWORD
value: dbpassword
volumeMounts:
- mountPath: /var/lib/postgresql/data
name: postgres-vol
volumes:
- name: postgres-vol
persistentVolumeClaim:
claimName: postgres-vol
3. Create a Service with to access database
apiVersion: v1
kind: Service
metadata:
name: postgres
labels:
app: postgres
spec:
ports:
- port: 5432
targetPort: 5432
selector:
app: postgres
type: ClusterIP
4. Now Consolidate all parts into one manifest file
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: postgres-vol
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
storageClassName: storage-class-name
volumeMode: Filesystem
----
apiVersion: apps/v1
kind: Deployment
metadata:
name: postgres-deployment
labels:
app: postgres
spec:
replicas: 1
selector:
matchLabels:
app: postgres
template:
metadata:
labels:
app: postgres
spec:
initContainers:
- name: init-postgres
image: busybox
command: ["sh", "-c", "rm -rf /var/lib/postgresql/data/*"]
volumeMounts:
- mountPath: /var/lib/postgresql/data
name: postgres-vol
containers:
- name: postgres
image: postgres
ports:
- containerPort: 5432
env:
- name: POSTGRES_DB
value: dbname
- name: POSTGRES_USER
value: dbuser
- name: POSTGRES_PASSWORD
value: dbpassword
volumeMounts:
- mountPath: /var/lib/postgresql/data
name: postgres-vol
volumes:
- name: postgres-vol
persistentVolumeClaim:
claimName: postgres-vol
----
apiVersion: v1
kind: Service
metadata:
name: postgres
labels:
app: postgres
spec:
ports:
- port: 5432
targetPort: 5432
selector:
app: postgres
type: ClusterI
4. If stateful kind deployment is required then apply following manifest file
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: postgres-vol
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
storageClassName: storage-class-name
volumeMode: Filesystem
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: postgres-deployment
labels:
app: postgres
spec:
replicas: 1
selector:
matchLabels:
app: postgres
serviceName: postgres
template:
metadata:
labels:
app: postgres
spec:
initContainers:
- name: init-postgres
image: busybox
command: ["sh", "-c", "rm -rf /var/lib/postgresql/data/*"]
volumeMounts:
- mountPath: /var/lib/postgresql/data
name: postgres-vol
containers:
- name: postgres
image: postgres
ports:
- containerPort: 5432
env:
- name: POSTGRES_DB
value: dbname
- name: POSTGRES_USER
value: dbuser
- name: POSTGRES_PASSWORD
value: dbpassword
volumeMounts:
- mountPath: /var/lib/postgresql/data
name: postgres-vol
volumeClaimTemplates:
- metadata:
name: postgres-vol
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 10Gi
---
apiVersion: v1
kind: Service
metadata:
name: postgres
labels:
app: postgres
spec:
ports:
- port: 5432
targetPort: 5432
selector:
app: postgres
type: ClusterIP
6. Enjoy!!!