-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_pg.sh
More file actions
234 lines (204 loc) · 7.82 KB
/
run_pg.sh
File metadata and controls
234 lines (204 loc) · 7.82 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
#! /bin/bash
#
set -ex
KERNEL=`uname -s`
if [ "${KERNEL}" != "Linux" ]; then
echo "Only Linux Supported" >&2
exit 1
fi
NODE=testPG.$1
DOCKER_BRIDGE_INTERFACE=`ifconfig docker0 | grep -w "inet" | awk '{print $2}' | sed 's/[0-9].[0-9]$/0.0/'`
if [ $# == 2 ]; then
if [ -z ${ARCHIVE_DIR+x} ]; then
echo "ARCHIVE_DIR variable must exist" >&2
exit 1
fi
if [ "${ARCHIVE_DIR}" = "" ]; then
echo "Missing archive directory. ${ARCHIVE_DIR} variable must not be empty" >&2
exit 1
fi
IMG=postgres/test-0.0.1
IMG_ID=`docker images ${IMG} -q`
PG_TAG=REL_18_BETA1
if [ "${IMG_ID}" = "" ]; then
if [ ! -d "postgres-${PG_TAG}" ]; then
wget https://github.com/postgres/postgres/archive/refs/tags/${PG_TAG}.tar.gz # From https://github.com/postgres/postgres/releases/tag/REL_18_BETA1
tar -xzf ${PG_TAG}.tar.gz
fi
ID=991
USR=postgres
USR_HOME=/home/postgres
cat > Dockerfile << EOF
FROM ubuntu:latest
RUN groupadd -g ${ID} ${USR} && \
useradd -r -u ${ID} -g ${USR} ${USR}
ADD postgres-${PG_TAG} ${USR_HOME}
WORKDIR ${USR_HOME}
RUN chown -R ${USR}:${USR} ${USR_HOME}
RUN apt-get update && apt-get install -y g++ \
zlib1g-dev \
make curl \
tar gzip perl \
liblz4-dev \
libreadline-dev \
flex bison libicu-dev liburing-dev
RUN apt-get install --reinstall -y pkg-config
# Build and Install Postgres
RUN ./configure --with-liburing --enable-debug --with-lz4 && \
make -j4 && \
make all && \
make install
# Putting executables in our PATH to make things easier later
RUN echo "export PATH=/usr/local/pgsql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" >> /etc/bash.bashrc && \
chown -R ${USR}:${USR} /usr/local/pgsql
USER ${USR}
EOF
docker build -t ${IMG}:latest .
rm Dockerfile
rm -rf postgres-${PG_TAG}
fi
ND=`docker ps -a | grep ${NODE} | awk '{print $NF}'`
if [ "${ND}" = "" ]; then
# export BACKUP_DIR=/home/vagrant/.tmp/Backup && mkdir -p $BACKUP_DIR
NODE1=`docker ps -a | grep testPG.1 | awk '{print $NF}'`
# Put backup directory only on primary node
if [ "${NODE1}" = "" ]; then
if [ -z ${BACKUP_DIR+x} ]; then
echo "BACKUP_DIR variable must exist" >&2
exit 1
fi
docker run -d --name ${NODE} -p $2:5432 \
--mount type=bind,source=${ARCHIVE_DIR},target=/home/postgres/.tmp \
--mount type=bind,source=${BACKUP_DIR},target=/usr/local/pgsql/data \
--restart=on-failure ${IMG}:latest bash -c 'tail /dev/null -f'
else
docker run -d --name ${NODE} -p $2:5432 \
--mount type=bind,source=${ARCHIVE_DIR},target=/home/postgres/.tmp \
--restart=on-failure ${IMG}:latest bash -c 'tail /dev/null -f'
fi
fi
fi
ETCD=`docker ps -a | grep etcd | awk '{print $NF}'`
if [ "${ETCD}" = "" ]; then
ARCH=`dpkg --print-architecture`
docker run -d --restart=no -p 2379:2379 -p 2380:2380 --name etcd ubuntu:latest bash -c 'tail -f /dev/null' && \
docker exec etcd bash -c "apt update && apt install -y git wget && \
git clone -b v3.6.0 https://github.com/etcd-io/etcd.git && \
wget https://go.dev/dl/go1.23.9.linux-${ARCH}.tar.gz && \
rm -rf /usr/local/go && tar -C /usr/local -xzf go1.23.9.linux-${ARCH}.tar.gz && \
cd etcd && export PATH=/usr/bin:/usr/local/bin:/etcd/bin:/usr/local/go/bin && \
./scripts/build.sh"
ETCD_IP=`docker inspect --format='{{ .NetworkSettings.IPAddress }}' etcd`
docker exec etcd bash -c "/etcd/bin/etcd --listen-client-http-urls='http://${ETCD_IP}:2379' --advertise-client-urls='http://${ETCD_IP}:2379' &"
fi
ETCD_IP=`docker inspect --format='{{ .NetworkSettings.IPAddress }}' etcd`
if [ "${ETCD_IP}" = "" ]; then
docker start etcd
ETCD_IP=`docker inspect --format='{{ .NetworkSettings.IPAddress }}' etcd` && \
docker exec etcd bash -c "/etcd/bin/etcd --listen-client-http-urls='http://${ETCD_IP}:2379' --advertise-client-urls='http://${ETCD_IP}:2379' &"
fi
MY_IP=`docker inspect --format='{{ .NetworkSettings.IPAddress }}' ${NODE}`
if [ "${MY_IP}" = "" ]; then
docker start ${NODE}
MY_IP=`docker inspect --format='{{ .NetworkSettings.IPAddress }}' ${NODE}`
fi
if [ -z ${PGDATA+x} ]; then
PGDATA=/usr/local/pgsql/data
fi
cat > patroni_config.yml << EOF
scope: cluster1
name: node$1
log:
format: '%(asctime)s %(levelname)s: %(message)s'
level: INFO
max_queue_size: 1000
traceback_level: ERROR
restapi:
connect_address: ${MY_IP}:8008
listen: ${MY_IP}:8008
etcd3:
password: patroni_etcd_user
url: http://${ETCD_IP}:2379
username: patroni_etcd_user
host: ${ETCD_IP}:2379
# The bootstrap configuration. Works only when the cluster is not yet initialized.
# If the cluster is already initialized, all changes in the `bootstrap` section are ignored!
bootstrap:
# This section will be written into <dcs>:/<namespace>/<scope>/config after initializing
# new cluster and all other cluster members will use it as a `global configuration`.
# WARNING! If you want to change any of the parameters that were set up
# via `bootstrap.dcs` section, please use `patronictl edit-config`!
dcs:
loop_wait: 10
retry_timeout: 10
ttl: 30
postgresql:
parameters:
wal_level: hot_standby
hot_standby: "on"
max_connections: 100
max_worker_processes: 8
wal_keep_segments: 8
max_wal_senders: 10
max_replication_slots: 10
max_prepared_transactions: 0
max_locks_per_transaction: 64
wal_log_hints: "on"
track_commit_timestamp: "off"
archive_mode: "on"
archive_timeout: 1800s
archive_command: mkdir -p /home/postgres/.tmp && test ! -f /home/postgres/.tmp/%f && cp %p /home/postgres/.tmp/%f
recovery_conf:
restore_command: cp /home/postgres/.tmp/%f %p
postgresql:
authentication:
replication:
password: dummy
username: postgres
superuser:
channel_binding: prefer
gssencmode: prefer
password: dummy
sslmode: prefer
username: postgres
bin_dir: /usr/local/pgsql/bin
connect_address: ${MY_IP}:5432
data_dir: $PGDATA
listen: ${MY_IP}, localhost:5432
parameters:
config_file: $PGDATA/postgresql.conf
hba_file: $PGDATA/pg_hba.conf
ident_file: $PGDATA/pg_ident.conf
pg_hba:
- local all all trust
- host all all 127.0.0.1/32 trust
- host all all ::1/128 trust
- local replication all trust
- host replication all 127.0.0.1/32 trust
- host replication all ::1/128 trust
- host replication patroni_repl ${DOCKER_BRIDGE_INTERFACE}/16 md5
- host all barman ${DOCKER_BRIDGE_INTERFACE}/16 md5
- host replication streaming_barman ${DOCKER_BRIDGE_INTERFACE}/16 md5
- host all prom_pg_exporter ${DOCKER_BRIDGE_INTERFACE}/16 md5
tags:
clonefrom: true
failover_priority: 1
noloadbalance: false
nosync: false
EOF
docker exec -u root ${NODE} bash -c "apt-get install -y python3-psycopg2 patroni"
docker cp patroni_config.yml ${NODE}:/home/postgres/patroni_config.yml
docker exec \
-e PATRONI_REPLICATION_USERNAME=patroni_repl \
-e PATRONI_REPLICATION_PASSWORD=patroni_repl \
-e PATRONI_SUPERUSER_USERNAME=patroni_super \
-e PATRONI_SUPERUSER_PASSWORD=patroni_super \
-e PATRONI_REWIND_USERNAME=patroni_rewind \
-e PATRONI_REWIND_PASSWORD=patroni_rewind \
-e PATRONI_ETCD3_USERNAME=patroni_etcd_user \
-e PATRONI_ETCD3_PASSWORD=patroni_etcd_user \
${NODE} bash -c "patroni patroni_config.yml"
# nohup ./run.sh 1 5432 > /tmp/test.1 &
# nohup ./run.sh 2 5433 > /tmp/test.2 &
# nohup ./run.sh 3 5434 > /tmp/test.3 &
# psql -U patroni_super -d postgres