Professional Documents
Culture Documents
Today we will do hands-on project to deploy a postgreSQL master -slave replication using
microservice cluster system kubernetes. The main focus of this hands-on project is to deploy
PostgreSQL Master-slave replication deployment on kubernetes.
1
Postgres Configmap will declare the DB name which has to be created on the spin of PODs.
YAML Configuration -
apiVersion: v1
kind: ConfigMap
metadata:
name: postgres-configmap
data:
POSTGRES_DB: trumpet-node-data
TIMESCALEDB_TELEMETRY: "off"
Apply Configuration -
# Create Credentials ConfigMap (cm)
kubectl apply -f postgres-configmap.yaml
kubectl get cm -o wide
Postgres Secrets will declare the secrets ( Credentials ) for the Database that will be use.
YAML Configuration -
apiVersion: v1
kind: Secret
metadata:
name: postgres-secrets
type: Opaque
data:
POSTGRES_PASSWORD: cG9zdGdyZXM=
stringData:
POSTGRES_USER: postgres
Apply Configuration -
# Create Credentials ConfigMap (cm)
kubectl apply -f postgres-secret.yaml
kubectl get secret -o wide
Postgres Volume will be use to create all require volume and mapping the volume.
YAML Configuration -
Apply Configuration -
# Create Volume(pv) and vVolume Claim (pvc)
kubectl apply -f postgres-volume.yaml
kubectl get pv -o wide
kubectl get pvc -o wide
In this section we will focus on configure PostgreSQL Replication. To understand this you
have to understand the process of database replication configuration process. We have to
work with pg_hba.conf, postgresql.conf and configuration may very as per requirements.
Configuration postgresql.conf
listen_addresses = '*'
max_connections = 100
shared_buffers = 128MB
dynamic_shared_memory_type = posix
max_wal_size = 1GB
min_wal_size = 80MB
log_timezone = 'Etc/UTC'
datestyle = 'iso, mdy'
timezone = 'Etc/UTC'
lc_messages = 'en_US.utf8'
lc_monetary = 'en_US.utf8'
lc_numeric = 'en_US.utf8'
lc_time = 'en_US.utf8'
default_text_search_config = 'pg_catalog.english'
#-----------------------------------------------------------------------------
# CUSTOMIZED OPTIONS
#-----------------------------------------------------------------------------
# Add settings for extensions here
wal_level = replica
max_wal_senders = 2
max_replication_slots = 2
synchronous_commit = off
Apply Configuration -
# Create Master ConfigMap (cm)
kubectl create cm postgres-master-configmap --from-file=config
kubectl get cm -o wide
YAML Configuration -
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: postgres-master
labels:
component: postgres-master
spec:
selector:
matchLabels:
component: postgres-master
serviceName: postgres-master
template:
metadata:
labels:
component: postgres-master
spec:
containers:
- name: postgres
image: postgres:11
command:
[
"sh",
"-c",
"docker-entrypoint.sh -c config_file=/var/config/postgresql.conf
-c hba_file=/var/config/pg_hba.conf",
]
# Readiness and Liveness Probe
readinessProbe:
exec:
command: [ "psql", "-Upostgres", "-dpostgres", "-c", "SELECT 1" ]
initialDelaySeconds: 10
timeoutSeconds: 10
livenessProbe:
exec:
command: [ "psql", "-Upostgres", "-dpostgres", "-c", "SELECT 1" ]
initialDelaySeconds: 30
timeoutSeconds: 10
ports:
- containerPort: 5432
envFrom:
- configMapRef:
7
Apply Configuration -
# Create Master StatefulSet (sts)
kubectl apply -f postgres-master-sts.yaml
kubectl get sts -o wide
sleep 10
kubectl get pods -o wide
YAML Configuration -
apiVersion: v1
kind: Service
metadata:
name: postgres-master
spec:
selector:
component: postgres-master
type: NodePort
ports:
- name: postgres-master-port
port: 5432
targetPort: 5432
nodePort: 30032
protocol: TCP
Apply Configuration -
# Create Master Service (svc)
kubectl apply -f postgres-master-svc.yaml
kubectl get svc -o wide
After Deploying Postgres Master StatefulSet and Postgres Master Service, use the following
commands to create 'repuser' for replication configuration.
#Create Replica User
kubectl exec -it postgres-master-0 -- bash
su - postgres
psql
SET password_encryption = 'scram-sha-256';
CREATE ROLE repuser WITH REPLICATION PASSWORD 'postgres' LOGIN;
SELECT * FROM pg_create_physical_replication_slot('replica_1_slot');
YAML Configuration -
apiVersion: batch/v1
kind: Job
metadata:
name: sync-master-data
spec:
template:
spec:
restartPolicy: OnFailure
containers:
- name: sync-master-data
image: postgres:11
command:
[
"sh",
"-c",
'PGPASSWORD="postgres" pg_basebackup -h postgres-master -D
/var/lib/slave-postgresql/data -U repuser -vP',
]
volumeMounts:
- mountPath: /var/lib/slave-postgresql/data
name: postgres-slave-data
volumes:
- name: postgres-slave-data
persistentVolumeClaim:
claimName: postgres-slave-data-vol
In this section we will focus on configure PostgreSQL Replication. To understand this you
have to understand the process of database replication configuration process. We have to
work with postgresql.conf, recovery.conf and configuration may very as per
requirements.
Configuration postgresql.conf
listen_addresses = '*'
max_connections = 100
shared_buffers = 128MB
dynamic_shared_memory_type = posix
max_wal_size = 1GB
min_wal_size = 80MB
log_timezone = 'Etc/UTC'
datestyle = 'iso, mdy'
timezone = 'Etc/UTC'
lc_messages = 'en_US.utf8'
lc_monetary = 'en_US.utf8'
lc_numeric = 'en_US.utf8'
lc_time = 'en_US.utf8'
default_text_search_config = 'pg_catalog.english'
#-----------------------------------------------------------------------------
# CUSTOMIZED OPTIONS
#-----------------------------------------------------------------------------
# Add settings for extensions here
hot_standby = on
wal_level = replica
max_wal_senders = 2
max_replication_slots = 2
synchronous_commit = off
Configuration recovery.conf
standby_mode = on
primary_conninfo = 'host=postgres-master port=5432 user=repuser
password=postgres application_name=r1'
primary_slot_name = 'replica_1_slot'
trigger_file = '/var/lib/postgresql/data/change_to_master'
10
YAML Configuration -
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: postgres-slave
labels:
component: postgres-slave
spec:
selector:
matchLabels:
component: postgres-slave
serviceName: postgres-slave
template:
metadata:
labels:
component: postgres-slave
spec:
initContainers:
- name: busybox
image: busybox
command:
- sh
- -c
- "cp /var/config/postgresql.conf
/var/lib/postgresql/data/postgresql.conf && cp /var/config/recovery.conf
/var/lib/postgresql/data/recovery.conf"
volumeMounts:
- mountPath: /var/lib/postgresql/data
name: postgres-slave-data
- mountPath: /var/config/postgresql.conf
subPath: postgresql.conf
name: postgres-slave-configmap
- mountPath: /var/config/recovery.conf
11
Apply Configuration -
# Create Slave StatefulSet (sts)
kubectl apply -f postgres-slave-sts.yaml
kubectl get sts -o wide
sleep 10
kubectl get pods -o wide
12
CREATE TABLE test2 (id int not null, val text not null);
INSERT INTO test2 VALUES (1, 'foo1');
INSERT INTO test2 VALUES (2, 'bar2');
INSERT INTO test2 VALUES (3, 'zoo3');
Extra Help : Below commd lines will help you to expose the kubernetes Dashboad using
proxy
#Dashboard - Run this only if you want to expose the dashboard to outside of
the machine.
minikube dashboard &
kubectl proxy --address='0.0.0.0' --disable-filter=true &
http://127.0.0.1:35283/api/v1/namespaces/kubernetes-dashboard/services/http:kub
ernetes-dashboard:/proxy/
http://192.168.0.102:8001/api/v1/namespaces/kubernetes-dashboard/services/http:
kubernetes-dashboard:/proxy/
13
14