-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathBaseConfigMapTemplate.yaml
More file actions
255 lines (207 loc) · 10.8 KB
/
Copy pathBaseConfigMapTemplate.yaml
File metadata and controls
255 lines (207 loc) · 10.8 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
apiVersion: v1
kind: ConfigMap
metadata:
name: base-kubegres-config
namespace: default
# This is the base configuration for all Kubegres resources. It is automatically created by the Kubernetes operator.
# It is created once for each namespace and it applies to all Kubegres resources on that namespace.
#
# Please do not remove any config and script in this file because they are referenced in the resources configured by the operator.
#
# If you would like to override shared configs by all Kubegres resources, this is the right place to edit.
#
# If you would like to override a configuration for a specific Kubegres resource only, create your custom configMap
# and there you can add any of the following data keys which will override the base one:
# - postgres.conf
# - primary_init_script.sh
# - backup_database.sh
# - pg_hba.conf
# In your Kubegres resource file, set the name of your custom configMap in 'spec.customConfig'.
#
# Note that, in your custom configMap, you cannot override these data keys:
# - primary_create_replication_role.sh
# - copy_primary_data_to_replica.sh
# - promote_replica_to_primary.sh
# We highly recommend that you do not modify these 3 data keys as it could break the operator.
data:
# This is the standard Postgres config applying to Primary and Replica servers.
# You can edit this config as it suits your requirement.
#
# Be careful with the value you set for 'listen_addresses' because you could break the operator.
#
# By default, PostgreSql set the property 'wal_level' to 'replica'. You can modify the value of 'wal_level' in here.
# Kubegres was tested with "wal_level = replica" and "wal_level = logical".
#
# If you edit the config in this file, your changes will apply to all Kubegres resources.
# Alternatively, you can override it for your Kubegres resource. To do so, create a custom configMap and in your
# Kubegres resource file set its name in 'spec.customConfig'. In your ConfigFile, copy the contents of this script
# and edit it as its suits your requirement.
#
postgres.conf: |
# Replication configs
listen_addresses = '*'
max_wal_senders = 10
max_connections = 100
shared_buffers = 128MB
# Logging
#log_destination = 'stderr,csvlog'
#logging_collector = on
#log_directory = 'pg_log'
#log_filename= 'postgresql-%Y-%m-%d_%H%M%S.log'
# This bash script is run once, the 1st time a Primary PostgreSql container is created. It is run in the Primary container only.
#
# You can edit this script as it suits your requirement. For example, you could add SQL statements to create specific
# database(s), user(s), grant accesses, etc...
# See the commented examples in the script below.
#
# This script will be located in the container folder "/docker-entrypoint-initdb.d"
# That folder is referenced in the Postgres Docker page: https://hub.docker.com/_/postgres
#
# If you edit the script in this file, your changes will apply to all Kubegres resources.
# Alternatively, you can override it for your Kubegres resource. To do so, create a custom configMap and in your
# Kubegres resource file set its name in 'spec.customConfig'. In your ConfigFile, copy the contents of this script
# and edit it as its suits your requirement.
#
primary_init_script.sh: |
#!/bin/bash
set -e
# This script assumes that the env-var $POSTGRES_MYAPP_PASSWORD contains the password of the custom user to create.
# You can add any env-var in your Kubegres resource config YAML.
#dt=$(date '+%d/%m/%Y %H:%M:%S');
#echo "$dt - Running init script the 1st time Primary PostgreSql container is created...";
#customDatabaseName="my_app"
#customUserName="my_username"
#echo "$dt - Running: psql -v ON_ERROR_STOP=1 --username $POSTGRES_USER --dbname $POSTGRES_DB ...";
#psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
#CREATE USER $customUserName WITH PASSWORD '$POSTGRES_MYAPP_PASSWORD';
#CREATE DATABASE $customDatabaseName;
#\connect $customDatabaseName;
#GRANT ALL ON SCHEMA public TO $customUserName;
#EOSQL
#echo "$dt - Init script is completed";
# If you enabled back-up for your Kubegres resource, this bash script backs-up a Postgres database into a given destination-volume.
# It is triggered to run regularly by a Kubernetes Cronjob.
#
# It runs in a Replica container in order to not impact the performance of Primary. If there is no Replica then it runs in a Primary container.
#
# You can edit this script as it suits your requirement.
#
# If you edit the script in this file, your changes will apply to all Kubegres resources.
# Alternatively, you can override it for your Kubegres resource. To do so, create a custom configMap and in your
# Kubegres resource file set its name in 'spec.customConfig'. In your ConfigFile, copy the contents of this script
# and edit it as its suits your requirement.
#
backup_database.sh: |
#!/bin/bash
set -e
dt=$(date '+%d/%m/%Y %H:%M:%S');
fileDt=$(date '+%d_%m_%Y_%H_%M_%S');
backUpFileName="$KUBEGRES_RESOURCE_NAME-backup-$fileDt.gz"
backUpFilePath="$BACKUP_DESTINATION_FOLDER/$backUpFileName"
echo "$dt - Starting DB backup of Kubegres resource $KUBEGRES_RESOURCE_NAME into file: $backUpFilePath";
echo "$dt - Running: pg_dumpall -h $BACKUP_SOURCE_DB_HOST_NAME -U postgres -c | gzip > $backUpFilePath"
pg_dumpall -h $BACKUP_SOURCE_DB_HOST_NAME -U postgres -c | gzip > $backUpFilePath
if [ $? -ne 0 ]; then
rm $backUpFilePath
echo "Unable to execute a BackUp. Please check DB connection settings"
exit 1
fi
echo "$dt - DB backup completed for Kubegres resource $KUBEGRES_RESOURCE_NAME into file: $backUpFilePath";
# This is the standard Postgres host-based authentication (hba) config applying to Primary and Replica servers.
# https://www.postgresql.org/docs/current/auth-pg-hba-conf.html
#
# You can edit this config as it suits your requirement.
# However, please be careful if you change the rules below for 'replication' database & role
# as it could break PostgreSql's ability to replicate.
#
# If you edit the config in this file, your changes will apply to all Kubegres resources.
# Alternatively, you can override it for your Kubegres resource. To do so, create a custom configMap and in your
# Kubegres resource file set its name in 'spec.customConfig'. In your ConfigFile, copy the contents of this script
# and edit it as its suits your requirement.
#
pg_hba.conf: |
# TYPE DATABASE USER ADDRESS METHOD
# Replication connections by a user with the replication privilege
host replication replication all md5
# As long as it is authenticated, all connections allowed except from "0.0.0.0/0"
local all all md5
host all all all md5
host all all 0.0.0.0/0 reject
# -----------------------------------------------------------------------------------------------
# From here, you cannot override the configs below in a custom ConfigMap
# ------------
# This script creates a replication role in charge of replicating data from Primary to Replica PostgreSql instances.
# It is executed once, the 1st time a Primary PostgreSql container is created.
# It is run in Primary container.
#
# If you modify this script, there is a risk of breaking the operator.
#
# This script will be located in the folder "/docker-entrypoint-initdb.d"
# That folder is referenced in the Postgres Docker page: https://hub.docker.com/_/postgres
#
primary_create_replication_role.sh: |
#!/bin/bash
set -e
dt=$(date '+%d/%m/%Y %H:%M:%S');
echo "$dt - Creating replication role...";
echo "$dt - Running: psql -v ON_ERROR_STOP=1 --username $POSTGRES_USER --dbname $POSTGRES_DB ... CREATE ROLE replication WITH REPLICATION PASSWORD ... GRANT EXECUTE ON FUNCTION pg_promote TO replication;";
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
CREATE ROLE replication WITH REPLICATION PASSWORD '$POSTGRES_REPLICATION_PASSWORD' LOGIN;
GRANT EXECUTE ON FUNCTION pg_promote TO replication;
EOSQL
echo "$dt - Replication role created";
# This script replicates data from the Primary PostgreSql to the Replica database.
# It is executed once, the 1st time a Replica PostgreSql container is created.
# It is run in Replica containers.
#
# If you modify this script, there is a risk of breaking the operator.
#
# This script will be located in the folder "/tmp"
#
copy_primary_data_to_replica.sh: |
#!/bin/bash
set -e
dt=$(date '+%d/%m/%Y %H:%M:%S');
echo "$dt - Attempting to copy Primary DB to Replica DB...";
if [ -z "$(ls -A $PGDATA)" ]; then
echo "$dt - Copying Primary DB to Replica DB folder: $PGDATA";
echo "$dt - Running: pg_basebackup -R -h $PRIMARY_HOST_NAME -D $PGDATA -P -U replication;";
pg_basebackup -R -h $PRIMARY_HOST_NAME -D $PGDATA -P -U replication;
if [ $UID == 0 ]
then
chown -R postgres:postgres $PGDATA;
fi
echo "$dt - Copy completed";
else
echo "$dt - Skipping copy from Primary DB because Replica DB already exists";
fi
# This script promotes a Replica to a Primary by creating a trigger-file signaling PostgreSql to start the promotion process.
# It is executed once, when a Replica is set to become a Primary.
# It is run in a selected Replica container by the operator.
#
# If you modify this script, there is a risk of breaking the operator.
#
# This script will be located in the folder "/tmp"
promote_replica_to_primary.sh: |
#!/bin/bash
set -e
dt=$(date '+%d/%m/%Y %H:%M:%S');
echo "$dt - Attempting to promote a Replica PostgreSql to Primary..." > /usr/share/promotion.log
standbyFilePath="$PGDATA/standby.signal"
if [ ! -f "$standbyFilePath" ]; then
echo "$dt - Skipping as this PostgreSql is already a Primary since the file '$standbyFilePath' does not exist." >> /usr/share/promotion.log
else
counter=1
until pg_isready -U postgres || [ $counter -gt 10 ]
do
echo "$dt - Attempt $counter - Postgres is not ready yet. Waiting..." > /usr/share/promotion.log
((counter++))
sleep 5
done
if pg_isready -U postgres; then
echo "$dt - Running: su -c '/usr/lib/postgresql/$PG_MAJOR/bin/pg_ctl promote -D $PGDATA' postgres" >> /usr/share/promotion.log
su -c '/usr/lib/postgresql/$PG_MAJOR/bin/pg_ctl promote -D $PGDATA' postgres >> /usr/share/promotion.log
else
echo "$dt - Postgres is still not ready. We tried $counter times. Stopping attempts." > /usr/share/promotion.log
fi
fi