Skip to content

Instantly share code, notes, and snippets.

@mtorrisi
Last active February 14, 2025 16:02
Show Gist options
  • Select an option

  • Save mtorrisi/c09df0ba0e18ae9c67bc43e1d70e3360 to your computer and use it in GitHub Desktop.

Select an option

Save mtorrisi/c09df0ba0e18ae9c67bc43e1d70e3360 to your computer and use it in GitHub Desktop.
Code snippets used in technical report
...
db:
image: timescale/timescaledb-ha:pg14.2-ts2.6.0-oss-latest
...
command: |
postgres
-c wal_level=replica
-c hot_standby=on
-c max_wal_senders=10
-c max_replication_slots=10
-c hot_standby_feedback=on
...
volumes:
- "./docker/timescale/pg_hba.conf:/home/postgres/pgdata/data/pg_hba.conf"
- "db_data:/home/postgres/pgdata/data"
...
# TYPE DATABASE USER ADDRESS METHOD
...
# Allows the user "replicator" from the allowed_host to connect to the primary as a replication standby
host replication all <allowed_host> md5
# replica-config.yaml
---
apiVersion: v1
kind: ConfigMap
metadata:
...
data:
REPL_SLOT: "<replication_slot_name>"
PRIMARY_HOST: "<primary_database>"
PGUSER: "<replicator_user>"
DB_USER: "<db_user>" # Used by TSDSystem app
DB_NAME: "<db_password>" # Used by TSDSystem app
init_script.sh: |
mkdir -p /home/postgres/pgdata/data
if [ -d "/home/postgres/pgdata/data" ] && [ -z "$(ls -A /home/postgres/pgdata/data)" ]; then
until pg_basebackup --pgdata=/home/postgres/pgdata/data -R --slot=${REPL_SLOT} --host=${PRIMARY_HOST} --port=5432 --progress
do
echo 'Waiting for primary to connect...'
sleep 1s
done
fi
echo 'Backup done, starting replica...'
chown -R postgres:postgres /home/postgres/pgdata/data
chmod 0700 /home/postgres/pgdata/data
# timescale-replicator-secret.yaml
---
apiVersion: v1
kind: Secret
metadata:
...
data:
postgres_password: <Base64.encode(postgres_password)>
pgpassword: <Base64.encode(pgpassword)>
# timescale-replicator-ss.yaml
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
...
containers:
- name: timescaledb-replica
image: timescale/timescaledb-ha:pg14.2-ts2.6.0-oss-latest
imagePullPolicy: IfNotPresent
env:
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: tsd-replicator-secret
key: postgres_password
- name: PGPASSWORD
valueFrom:
secretKeyRef:
name: tsd-replicator-secret
key: pgpassword
- name: PGUSER
valueFrom:
configMapKeyRef:
name: tsd-replica-data
key: PGUSER
envFrom:
- configMapRef:
name: tsd-replica-data
livenessProbe:
exec:
command:
[
"/bin/bash",
"-c",
"psql -U ${DB_USER} -d ${DB_NAME} -c 'SELECT 1;'",
]
initialDelaySeconds: 45
periodSeconds: 15
readinessProbe:
exec:
command:
[
"/bin/bash",
"-c",
"psql -U ${DB_USER} -d ${DB_NAME} -c 'SELECT 1;'",
]
initialDelaySeconds: 30
timeoutSeconds: 2
ports:
- containerPort: 5432
volumeMounts:
- name: db-data
mountPath: /home/postgres/pgdata
initContainers:
- name: init-timescaledb-replica
image: timescale/timescaledb-ha:pg14.2-ts2.6.0-oss-latest
env:
- name: REPL_SLOT
valueFrom:
configMapKeyRef:
name: tsd-replica-data
key: REPL_SLOT
- name: PRIMARY_HOST
valueFrom:
configMapKeyRef:
name: tsd-replica-data
key: PRIMARY_HOST
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: tsd-replicator-secret
key: postgres_password
- name: PGPASSWORD
valueFrom:
secretKeyRef:
name: tsd-replicator-secret
key: pgpassword
- name: PGUSER
value: replicator
command:
- "/bin/sh"
- "/home/postgres/init_script.sh"
securityContext:
runAsUser: 0
privileged: true
volumeMounts:
- name: db-data
mountPath: /home/postgres/pgdata
- name: tsd-replica-data
mountPath: /home/postgres/init_script.sh
subPath: init_script.sh
volumes:
- name: tsd-replica-data
configMap:
name: tsd-replica-data
volumeClaimTemplates:
- metadata:
name: db-data
spec:
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 50Gi

Comments are disabled for this gist.