Deploying PostgreSQL Admin with PostgreSQL on Amazon EKS
Tags: Postgresql, Kubernetes, Kubernetes Cluster, Amazon Eks, Amazon

So, Let's go for the integration part:

First, we have to do download eksctl command and then we have to set it in the environmental variable and also we need to configure the AWS CLI with IAM credentials. After that, we have to download and configure kubectl command in CLI

Now , we are ready to create a cluster, for that we have to run this file:

-> Create file Cluster.yml

apiVersion: eksctl.io/v1alpha5

kind: ClusterConfig metadata:

name: my-cluster

region: ap-south-1z nodeGroups:

- name: ng1

desiredCapacity: 3

instanceType: t2.micro

ssh:

publicKeyName: Mainkey

In this cluster.yml, we have created one node group with three instances. This files automatically creates the cluster for us. The command that we have to run in client is as follows:

cmd> eksctl create cluster -f cluster.yml

The output in the terminal is as follows:

After the cluster launches, you can check it through AWS portal

After that you have to config kubectl command by this snippet of code which is as follows:

cmd> aws eks update-kubeconfig --name my-cluster

After that, you have to create one different namespace so that it is easier for you to differentiate between their cluster. The command for that is as follows:

cmd> kubectl create ns mypsns

Create a new EFS for integration with EKS. For that, you have to create EFS system with same cluster VPC and also you have to use subnets that are of sharedNodeGroups.

After that you have to create these files which are as follows:

For connection with EFS , you have to create these files which are as follows, In this demonstration, we use EFS instead of EBS , you can also use EBS.

Create my-efs-provisioner.yml file

kind: Deployment

apiVersion: apps/v1

metadata:

name: efs-provisioner

spec:

selector:

matchLabels:

app: efs-provisioner

replicas: 1

strategy:

type: Recreate

template:

metadata:

labels:

app: efs-provisioner

spec:

containers:

- name: efs-provisioner

image: quay.io/external_storage/efs-provisioner:v0.1.0

env:

- name: FILE_SYSTEM_ID

value: fs-5729a386 #your efs id

- name: AWS_REGION

value: ap-south-1

- name: PROVISIONER_NAME

value: myefs/aws-efs

volumeMounts:

- name: pv-volume

mountPath: /persistentvolumes

volumes:

- name: pv-volume

nfs:

server: fs-5729a386.efs.ap-south-1.amazonaws.com

path: /

2. Create my-rbac.yml, it is used for ClusterRolebinding Creation:

apiVersion: rbac.authorization.k8s.io/v1beta1

kind: ClusterRoleBinding

metadata:

name: nfs-role-binding

subjects:

- kind: ServiceAccount

name: default

namespace: mypsns

roleRef:

kind: ClusterRole

name: cluster-admin

apiGroup: rbac.authorization.k8s.io

3. Create a my-storage.yml file, it is used for persistent volume creation and creation of storage class in a cluster.

kind: StorageClass

apiVersion: storage.k8s.io/v1

metadata:

name: aws-efs

provisioner: myefs/aws-efs

---

kind: PersistentVolumeClaim

apiVersion: v1

metadata:

name: efs-postgres

annotations:

volume.beta.kubernetes.io/storage-class: "aws-efs"

spec:

accessModes:

- ReadWriteMany

resources:

requests:

storage: 10Gi

4. Now, create the deployment file of PostgreSQL, deploy-postgres.yml

apiVersion: v1

kind: Service

metadata:

name: pgadmin-postgres

labels:

app: pgadmin

spec:

ports:

- port: 5432

nodePort: 30432

type: NodePort

selector:

app: pgadmin

tier: postgres ---

apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2

kind: Deployment

metadata:

name: pgadmin-postgres

labels:

app: pgadmin

spec:

selector:

matchLabels:

app: pgadmin

tier: postgres

strategy:

type: Recreate

template:

metadata:

labels:

app: pgadmin

tier: postgres

spec:

containers:

- image: postgres:latest

name: postgres

env:

- name: POSTGRES_DB

value: postgresdb

- name: POSTGRES_USER

value: postgres

- name: POSTGRES_PASSWORD

valueFrom:

secretKeyRef:

name: postgres-pwd

key: mypwd

ports:

- containerPort: 5432

name: postgres

volumeMounts:

- name: postgres-persistent-storage

mountPath: /var/lib/postgresql/data

volumes:

- name: postgres-persistent-storage

persistentVolumeClaim:

claimName: efs-postgres

After this file, we are ready to create a Postgres admin deployment file, deploy-pgadmin.yml

apiVersion: v1

kind: Service

metadata:

name: pgadmin

labels:

app: pgadmin

spec:

ports:

- port: 80

selector:

app: pgadmin

tier: frontend

type: LoadBalancer

---

apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2

kind: Deployment

metadata:

name: pgadmin

labels:

app: pgadmin

spec:

selector:

matchLabels:

app: pgadmin

tier: frontend

strategy:

type: Recreate

template:

metadata:

labels:

app: pgadmin

tier: frontend

spec:

containers:

- image: dpage/pgadmin4

name: pgadmin

env:

- name: PGADMIN_DEFAULT_EMAIL

value: "admin"

- name: PGADMIN_PORT

value: "80"

- name: PGADMIN_DEFAULT_PASSWORD

valueFrom:

secretKeyRef:

name: postgres-pwd

key: pgadmin-pass

ports:

- containerPort: 80

name: pgadminport

Now, we have to execute these files, for that we can use kustomization.yml file, which contains the sequence of execution of file and we have any easy process.

So the kustomization.yml file code is as follows:

apiVersion: kustomize.config.k8s.io/v1beta1

kind: Kustomization secretGenerator:

- name: postgres-pwd

literals:

- mypwd=redhat

- pgadmin-pass=redhat

resources:

- my-efs-provisioner.yml

- my-rbac.yml

- my-storage.yml

- deploy-postgres.yml

- deploy-pgadmin.yml

After the creation of these files , you can run these commands in command prompt of same terminal.

cmd> kubectl create -k . -n mypsns

Here ,-n is used for namespace.To check the pods are ready or not , we can use this command:

kubectl get all -n mypsns

You can check you cli , if all the pods are ready ,then copy the path services on browser and you can see the window , which is shown below:

After that, you can see the output in the browser using ELB DNS

The project link is as follows:

https://github.com/Harsh-Gupta9897/Amazon_eks_postgresql.git