-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpg-slave-statefulset.yaml
95 lines (91 loc) · 3.31 KB
/
pg-slave-statefulset.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: pg-slave
spec:
serviceName: pg-slave
replicas: 1 # Adjust replicas as needed
selector:
matchLabels:
app: postgres
role: slave
template:
metadata:
labels:
app: postgres
role: slave
spec:
containers:
- name: postgres
image: postgres:15
env:
- name: POSTGRES_USER
value: "postgres"
- name: POSTGRES_PASSWORD
value: "password"
- name: PGDATA
value: "/var/lib/postgresql/data"
- name: PG_MASTER_HOST
value: "pg-master"
- name: PG_PORT
value: "5432"
volumeMounts:
- name: pg-data
mountPath: /var/lib/postgresql/data
- name: pg-config-path
mountPath: /etc/postgresql/custom
- name: pg-scripts-config-path
mountPath: /scripts/postgresql.sh
subPath: postgresql.sh
securityContext:
runAsUser: 999 # PostgreSQL user UID
runAsGroup: 999 # PostgreSQL group GID
command:
- "bash"
- "-c"
- |
"/scripts/postgresql.sh"
echo "include '/etc/postgresql/custom/postgresql.conf'" >> /var/lib/postgresql/data/postgresql.conf
cat /etc/postgresql/custom/pg_hba.conf >> /var/lib/postgresql/data/pg_hba.conf
postgres
# Extract replica name from the pod's hostname (e.g., pg-slave-0)
# REPLICA_NAME=$(hostname | sed 's/-/_/g')
#
# rm -rf /var/lib/postgresql/data/* # Ensure the directory is empty
# until pg_isready --host=$PG_MASTER_HOST --port=$PG_PORT; do
# echo "Waiting for master to be ready..."
# sleep 2
# done
# pg_basebackup -h $PG_MASTER_HOST -D /tmp/pgdata -U postgres -v -P --wal-method=stream
# mv /tmp/pgdata/* /var/lib/postgresql/data/
# chown -R postgres:postgres /var/lib/postgresql/data
# psql -h $PG_MASTER_HOST -U postgres -c "SELECT pg_create_physical_replication_slot('${REPLICA_NAME}');"
# echo "primary_slot_name = '${REPLICA_NAME}'" | tee -a /var/lib/postgresql/data/postgresql.conf
# echo "primary_conninfo = 'host=$PG_MASTER_HOST port=$PG_PORT user=postgres password=password application_name=${REPLICA_NAME}'" | tee -a /var/lib/postgresql/data/postgresql.conf
# touch /var/lib/postgresql/data/standby.signal
# chmod 700 /var/lib/postgresql/data/
# rm -rf /tmp/pgdata
# echo "Starting PostgreSQL..."
# exec postgres # Use exec to replace the shell with the PostgreSQL process
lifecycle:
preStop:
exec:
command:
- "bash"
- "-c"
- |
REPLICA_NAME=$(hostname | sed 's/-/_/g')
echo "Deleting replication slot ${REPLICA_NAME} from master..."
psql -h $PG_MASTER_HOST -U postgres -p $PG_PORT -c "SELECT pg_drop_replication_slot('${REPLICA_NAME}');"
volumes:
- name: pg-data
persistentVolumeClaim:
claimName: pg-slave-pvc
- name: pg-config-path
configMap:
name: pg-config
defaultMode: 0755
- name: pg-scripts-config-path
configMap:
name: pg-script-config
defaultMode: 0755