Introduction
PostgreSQL, often called Postgres, is an open-source, object-relational database management system (DBMS). It is known for its robustness, scalability, and ability to handle complex queries and transactions.
Kubernetes is a free and open source container management tool, we can deploy and manage containerized applications across a cluster of nodes. It provides features such as automated rollouts and rollbacks, self-healing, load balancing, and scaling, all of which make it easier to manage and scale containerized applications.
In this post, We will show you how to deploy postgres database on Kubernetes cluster.
Step 1: Creating Configmap
We need to create a configmap for postgres database that container varibale’s value like postgres username, password and database.
To create configmap.
nano postgres-configmap.yaml
Paste the following configuration.
apiVersion: v1
kind: ConfigMap
metadata:
name: postgres-config
labels:
app: postgres
data:
POSTGRES_DB: postgres
POSTGRES_USER: admin
POSTGRES_PASSWORD: admin
We need to update relace the postgres’ username, password and database in real env and save and exit from the nano editor.
Step 2: Deploy Configmap
We need to deplot the configmap by using following command that will create a configmap.
To deploy the postgres’s configmap.
kubectl create -f postgres-configmap.yaml
To check configmap.
kubectl get configmap
Step 3: Creating Persistent Storage Volume and Persistent Volume Claim
To create storage configuration using YAML file
nano postgres-storage.yaml
Paste the following storage configuration.
kind: PersistentVolume
apiVersion: v1
metadata:
name: postgres-pv-volume
labels:
type: local
app: postgres
spec:
storageClassName: manual
capacity:
storage: 10Gi
accessModes:
- ReadWriteMany
hostPath:
path: "/mnt/postgres_data"
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: postgres-pv-claim
labels:
app: postgres
spec:
storageClassName: manual
accessModes:
- ReadWriteMany
resources:
requests:
storage: 10Gi
We can change the host machine mount path and storage capity in real env and save and edit from nano editor.
To deploy the volume.
kubectl create -f postgres-storage.yaml
To verify volume and we need to also verify the PVC is connected to the PV.
kubectl get pvc
Step 4: Creating Postgres Deployment
We are good to create postgres deployment and we can configure configmap and volume configuration.
To create postgres deployment yaml file.
nano postgres-deployment.yaml
Paste the following configuration.
apiVersion: apps/v1
kind: Deployment
metadata:
name: postgres
spec:
replicas: 1
selector:
matchLabels:
app: postgres
template:
metadata:
labels:
app: postgres
spec:
containers:
- name: postgres
image: postgres:12
imagePullPolicy: "IfNotPresent"
ports:
- containerPort: 5432
envFrom:
- configMapRef:
name: postgres-config
volumeMounts:
- mountPath: /var/lib/postgresql/data
name: postgredb
volumes:
- name: postgredb
persistentVolumeClaim:
claimName: postgres-pv-claim
We can replace the postgres relica number, postgres version and others things save and edit from the nano editor.
To deploy the postgres.
kubectl create -f postgres-deployment.yaml
To verify the deployment.
kubectl get deployment
To validate postgres pod.
kubectl get pods
Step 5: Creating Postgres Service
Right now postgres pod is deploy and it should be running state but can not access the postgres from the network so that’s why we need to create service to make accessable in the network.
to create postgres service yaml configuration.
nano postgres-service.yaml
Paste the following configuration.
apiVersion: v1
kind: Service
metadata:
name: postgres-service
labels:
app: postgres
spec:
type: NodePort
ports:
- port: 5432
selector:
app: postgres
We need to replace the network type NodePort to LoadBalancer or ClusterIP as per your need, Leave on NodePort that will work with k8s node’s IP address, Save and edit from the nano editor.
To deploy postgres service.
kubectl create -f postgres-service.yaml
To validate postgres service.
kubectl get svc
To get Postgress Shell terminal.
kubectl exec -it [pod-name] -- psql -h localhost -U admin --password -p [port] postgres
Conclusion
We have successfully deployed the PostgreSQL database on the K8S cluster. If you are still facing problems, feel free to leave a commit.