Mirth on Kubernetes
Part: II
Hello there, tech enthusiasts and integration engineers! 👋🏼
In the previous article, we have learned what to install and how to install. And finally, we can begin to our journey through the world of possibilities.
In the Part II, I will help you to create stateful PostgreSQL cluster on Kubernetes using Minikube and just a little bit of Lens Desktop.
PostgeSQL Setup
In order to run the PostgreSQL up on Kubernetes, we need to write a couple basic configurations using YAML.
Creating Configurations
In total, we are going to be needed 4 YAML files. They are;
- postgres-configmap.yaml
- postgres-pv.yaml
- postgres-stateful.yaml
- postgres-service.yaml
Creating postgres-configmap.yaml
Firstly, we are going to create a secret file to store our precious username and password information for PostgreSQL superuser.
So, let’s create a file named postgres-configmap.yaml .
apiVersion: v1
kind: ConfigMap
metadata:
name: postgres-secret
labels:
app: postgres
data:
POSTGRES_DB: <db_name>
POSTGRES_USER: <username>
POSTGRES_PASSWORD: <password>
Parameters & Why?
Side Note: The only required environment for PostgreSQL is POSTGRES_PASSWORD. So you are free to not configure POSTGRES_DB and POSTGRES_USER variables but then you need to know their default values. You can check the official documentation for more information.
Cont’d Creating postgres-configmap.yaml
Once it’s done, run the command below to add this configmap to Kubernetes.
kubectl apply -f postgres-configmap.yaml
Creating postgres-pv.yaml
Now we are going to create the volume allocations for our PostgreSQL DB storage by defining PersistentVolume (PV) and PersistentVolumeClaim (PVC).
What are PV and PVC?
In a nutshell according to the official documentation;
PVs are resources in the cluster. PVCs are requests for those resources and also act as claim checks to the resource.
So in real-life, PV is what system administrators define for the allocation from the host machine and PVC is what developers try to allocate and also promise to use their application as minimum.
Cont’d Creating postgres-pv.yaml
Now, we know what PV and PVC are and we are ready to configure these for our PostgreSQL DB storage.
Let’s create a file named postgres-pv.yaml .
apiVersion: v1
kind: PersistentVolume
metadata:
name: postgres-db-volume
labels:
type: local
app: postgres
spec:
storageClassName: manual
capacity:
storage: 3Gi
accessModes:
- ReadWriteMany
hostPath:
path: /data/postgresql
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: postgres-volume-claim
labels:
app: postgres
spec:
storageClassName: manual
accessModes:
- ReadWriteMany
resources:
requests:
storage: 3Gi
Parameters & Why
Persistent Volume
Persistent Volume Claim
P.S.: I did not add explanation for the same parameters over and over again, just added the differentiated ones to keep it as short and yet explanatory as possible.
Side Note: When you tried to create the storage claims with the configurations above and get an error that says storageclass.storage.k8s.io “manual” not found then you can change manual to standard as a workaround but that’s not a 100% ideal way to fix it. For more information, please see the discussion in GitHub and read the official documentation.
Cont’d Creating postgres-pv.yaml
Once it’s done, run the command below to add this configmap to Kubernetes.
kubectl apply -f postgres-pv.yaml
Creating postgres-service.yaml
Alright, we have created our application and it’s running right now. But there is one more thing, we need to be able to access it.
In order to do that, we are going to create a Kubernetes service to allow communications between our pods and also between pods and our host machine. Basically, we are going to define what we will expose to the host machine.
So let’s create a file named postgres-service.yaml .
apiVersion: v1
kind: Service
metadata:
name: postgres-svc
labels:
app: postgres
spec:
type: NodePort
ports:
- port: 5432
selector:
app: postgres
Parameters & Why
Cont’d Creating postgres-service.yaml
Once it’s done, run the command below to add this configmap to Kubernetes.
kubectl apply -f postgres-service.yaml
Creating postgres-stateful.yaml
This is the heart of our setup. We are going to define what image we will use, how much replicas we will have, which port we will use, bind the configurations what we have created before… Center of our configurations.
Without further ado, let’s create a file named postgres-stateful.yaml .
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: postgres
spec:
serviceName: postgres-svc
replicas: 2
selector:
matchLabels:
app: postgres
template:
metadata:
labels:
app: postgres
spec:
containers:
- name: postgres
image: "postgres:latest"
imagePullPolicy: IfNotPresent
resources:
limits:
memory: "2048Mi"
cpu: "1000m"
ports:
- containerPort: 5432
envFrom:
- configMapRef:
name: postgres-secret
volumeMounts:
- mountPath: /var/lib/postgresql/data
name: postgresdata
volumes:
- name: postgresdata
persistentVolumeClaim:
claimName: postgres-volume-claim
Parameters & Why
Cont’d Creating postgres-configuration.yaml
Once it’s done, run the command below to add this configmap to Kubernetes.
kubectl apply -f postgres-stateful.yaml
Now, our PostgreSQL DB is running on Kubernetes and it can communicate with other pods. But how are we going to communicate with it?
Enabling Port Forwarding
Using Lens (Faster Way)
If Lens is installed on your host machine, then just open Lens and then follow the screenshots down below.
If you followed the guide till this point, thank you for your time, you should have been set the PostgreSQL up successfully. So our next step is our final destination which is to create our configuration YAML files for Mirth Connect.