Skip to content

Commit bac661e

Browse files
committed
add kustomize restore example
1 parent 1243d8d commit bac661e

16 files changed

+331
-0
lines changed

kustomize/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Kustomize example for use in restore.

kustomize/base/kustomization.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: kustomize.config.k8s.io/v1beta1
2+
kind: Kustomization
3+
4+
resources:
5+
- pgbackrest-config.yaml
6+
- pgbackrest-scripts.yaml
7+
- pgbackrest-files.yaml
8+
- postgres-scripts.yaml
9+
- postgres-config.yaml
10+
- statefulset.yaml
11+
- pvc.yaml
12+
- service.yaml

kustomize/base/pgbackrest-config.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
name: pgbackrest-config
5+
data:
6+
# Stanza options
7+
PGBACKREST_PG1_PATH: /var/lib/postgresql/data/pgdata
8+
# Global options
9+
PGBACKREST_LOG_LEVEL_CONSOLE: info
10+
PGBACKREST_LOG_LEVEL_FILE: info
11+
PGBACKREST_PROCESS_MAX: "4"
12+
PGBACKREST_ARCHIVE_ASYNC: "y"
13+
# Repo options
14+
PGBACKREST_REPO1_BUNDLE: "y"
15+
PGBACKREST_REPO1_PATH: /repo
16+
PGBACKREST_REPO1_HOST_CA_FILE: /certs/ca.crt
17+
PGBACKREST_REPO1_HOST_CERT_FILE: /certs/tls.crt
18+
PGBACKREST_REPO1_HOST_KEY_FILE: /certs/tls.key
19+
PGBACKREST_REPO1_HOST_TYPE: tls
20+
# TLS server options
21+
PGBACKREST_TLS_SERVER_ADDRESS: "*"
22+
PGBACKREST_TLS_SERVER_CA_FILE: /certs/ca.crt
23+
PGBACKREST_TLS_SERVER_CERT_FILE: /certs/tls.crt
24+
PGBACKREST_TLS_SERVER_KEY_FILE: /certs/tls.key

kustomize/base/pgbackrest-files.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
name: pgbackrest-files
5+
data:
6+
postgresql.conf: |
7+
listen_addresses = '*'
8+
max_connections = 100
9+
shared_buffers = 128MB
10+
dynamic_shared_memory_type = posix
11+
max_wal_size = 1GB
12+
min_wal_size = 80MB
13+
log_timezone = 'Etc/UTC'
14+
datestyle = 'iso, mdy'
15+
timezone = 'Etc/UTC'
16+
lc_messages = 'en_US.utf8'
17+
lc_monetary = 'en_US.utf8'
18+
lc_numeric = 'en_US.utf8'
19+
lc_time = 'en_US.utf8'
20+
default_text_search_config = 'pg_catalog.english'
21+
pg_hba.conf: |
22+
local all all trust
23+
host all all 127.0.0.1/32 trust
24+
host all all ::1/128 trust
25+
local replication all trust
26+
host replication all 127.0.0.1/32 trust
27+
host replication all ::1/128 trust
28+
host all all all scram-sha-256
29+
pg_ident.conf:
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
name: pgbackrest-scripts
5+
data:
6+
restore.sh: |
7+
#!/bin/bash
8+
set -e
9+
10+
# look specifically for PG_VERSION, as it is expected in the DB dir
11+
if [ -s "$PGDATA/PG_VERSION" ]; then
12+
echo "The data directory is not empty. Skipping restore."
13+
exit 0
14+
fi
15+
16+
# PGBACKREST_STANZA and PGBACKREST_TARGET must be set
17+
if [ -z "${PGBACKREST_STANZA}" ] || [ -z "${PGBACKREST_TARGET}" ]; then
18+
echo "PGBACKREST_STANZA and PGBACKREST_TARGET aren't set. Skipping restore."
19+
exit 0
20+
fi
21+
22+
pgbackrest restore --stanza="${PGBACKREST_STANZA}" --type=time --target="${PGBACKREST_TARGET}" --target-action=promote --archive-mode=off --log-level-file=info
23+
24+
# copy postgresql.conf, pg_ident.conf and pg_hba.conf to PGDATA from /defaults if they are missing from PGDATA
25+
if [ ! -f "${PGDATA}/postgresql.conf" ]; then
26+
cp /defaults/postgresql.conf "${PGDATA}/postgresql.conf"
27+
fi
28+
29+
if [ ! -f "${PGDATA}/pg_hba.conf" ]; then
30+
cp /defaults/pg_hba.conf "${PGDATA}/pg_hba.conf"
31+
fi
32+
33+
if [ ! -f "${PGDATA}/pg_ident.conf" ]; then
34+
cp /defaults/pg_ident.conf "${PGDATA}/pg_ident.conf"
35+
fi

kustomize/base/postgres-config.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
name: postgres-config
5+
data:
6+
PGDATA: /var/lib/postgresql/data/pgdata

kustomize/base/postgres-scripts.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
name: postgres-scripts
5+
data:
6+
init-database.sh: |
7+
#!/bin/bash
8+
set -eu
9+
10+
# exit if POSTGRES_DB_USER nor POSTGRES_DB_PASSWORD are set
11+
if [ -z "${POSTGRES_DB_USER}" ] || [ -z "${POSTGRES_DB_PASSWORD}" ]; then
12+
echo "POSTGRES_DB_USER and POSTGRES_DB_PASSWORD must be set"
13+
exit 1
14+
fi
15+
16+
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "${POSTGRES_DB:-POSTGRES_USER}" <<-EOSQL
17+
CREATE USER $POSTGRES_DB_USER WITH PASSWORD '$POSTGRES_DB_PASSWORD';
18+
CREATE DATABASE $POSTGRES_DB_NAME OWNER $POSTGRES_DB_USER;
19+
EOSQL

kustomize/base/pvc.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apiVersion: v1
2+
kind: PersistentVolumeClaim
3+
metadata:
4+
name: postgres-pvc
5+
labels:
6+
app.kubernetes.io/name: postgres
7+
app.kubernetes.io/instance: postgres
8+
spec:
9+
accessModes:
10+
- ReadWriteOnce
11+
resources:
12+
requests:
13+
storage: 10Gi

kustomize/base/service.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
name: postgres
5+
labels:
6+
app.kubernetes.io/name: postgres
7+
app.kubernetes.io/instance: postgres
8+
spec:
9+
type: ClusterIP
10+
ports:
11+
- name: postgres
12+
port: 5432
13+
targetPort: postgres
14+
selector:
15+
app.kubernetes.io/name: postgres
16+
app.kubernetes.io/instance: postgres

kustomize/base/statefulset.yaml

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
apiVersion: apps/v1
2+
kind: StatefulSet
3+
metadata:
4+
name: postgres
5+
labels:
6+
app.kubernetes.io/name: postgres
7+
app.kubernetes.io/instance: postgres
8+
spec:
9+
replicas: 1
10+
selector:
11+
matchLabels:
12+
app.kubernetes.io/name: postgres
13+
app.kubernetes.io/instance: postgres
14+
template:
15+
metadata:
16+
labels:
17+
app.kubernetes.io/name: postgres
18+
app.kubernetes.io/instance: postgres
19+
spec:
20+
securityContext:
21+
runAsNonRoot: true
22+
runAsUser: 999
23+
runAsGroup: 999
24+
fsGroup: 999
25+
terminationGracePeriodSeconds: 60
26+
initContainers:
27+
- name: import-base
28+
image: ghcr.io/codestation/pgbackrest:2.54.2-postgres17
29+
command:
30+
- bash
31+
- -c
32+
- /scripts/restore.sh
33+
envFrom:
34+
- configMapRef:
35+
name: pgbackrest-config
36+
- configMapRef:
37+
name: postgres-config
38+
volumeMounts:
39+
- name: pgdata
40+
mountPath: /var/lib/postgresql/data
41+
- name: pgbackrest-scripts
42+
mountPath: /scripts
43+
- name: pgbackrest-files
44+
mountPath: /defaults
45+
- name: pgbackrest-certs
46+
mountPath: /certs
47+
containers:
48+
- name: postgres
49+
image: ghcr.io/codestation/postgres:17
50+
command: ["docker-entrypoint.sh"]
51+
args: ["postgres"]
52+
envFrom:
53+
- configMapRef:
54+
name: postgres-config
55+
- configMapRef:
56+
name: pgbackrest-config
57+
- secretRef:
58+
name: postgres-secret
59+
optional: true
60+
env:
61+
- name: POSTGRES_USER
62+
valueFrom:
63+
secretKeyRef:
64+
name: postgres-root-credentials
65+
key: username
66+
- name: POSTGRES_PASSWORD
67+
valueFrom:
68+
secretKeyRef:
69+
name: postgres-root-credentials
70+
key: password
71+
ports:
72+
- containerPort: 5432
73+
name: postgres
74+
lifecycle:
75+
preStop:
76+
exec:
77+
command:
78+
- sh
79+
- -c
80+
- exec pg_ctl stop -D /var/lib/postgresql/data -m fast -w -t 60
81+
volumeMounts:
82+
- name: pgdata
83+
mountPath: /var/lib/postgresql/data
84+
- name: shm
85+
mountPath: /dev/shm
86+
- name: pgbackrest-spooler
87+
mountPath: /var/spool/pgbackrest
88+
- name: postgres-scripts
89+
mountPath: /docker-entrypoint-initdb.d
90+
- name: pgbackrest-certs
91+
mountPath: /certs
92+
volumes:
93+
- name: pgdata
94+
persistentVolumeClaim:
95+
claimName: postgres-pvc
96+
- name: shm
97+
emptyDir:
98+
medium: Memory
99+
- name: postgres-scripts
100+
configMap:
101+
name: postgres-scripts
102+
defaultMode: 0755
103+
- name: pgbackrest-spooler
104+
emptyDir: {}
105+
- name: pgbackrest-scripts
106+
configMap:
107+
name: pgbackrest-scripts
108+
defaultMode: 0755
109+
- name: pgbackrest-files
110+
configMap:
111+
name: pgbackrest-files
112+
defaultMode: 0600
113+
- name: pgbackrest-certs
114+
projected:
115+
defaultMode: 0600
116+
sources:
117+
- secret:
118+
name: pgbackrest-certs
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
apiVersion: kustomize.config.k8s.io/v1beta1
2+
kind: Kustomization
3+
4+
namespace: db-restore
5+
6+
resources:
7+
- ../../base
8+
- pgbackrest-certs.yaml
9+
- namespace.yaml
10+
- secret.yaml
11+
12+
configMapGenerator:
13+
- name: pgbackrest-config
14+
behavior: merge
15+
envs:
16+
- pgbackrest-config.env
17+
- name: postgres-config
18+
behavior: merge
19+
envs:
20+
- postgres-config.env
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
apiVersion: v1
2+
kind: Namespace
3+
metadata:
4+
name: db-restore
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
apiVersion: v1
2+
kind: Secret
3+
metadata:
4+
name: pgbackrest-certs
5+
type: kubernetes.io/tls
6+
stringData:
7+
ca.crt: |
8+
-----BEGIN CERTIFICATE-----
9+
...
10+
-----END CERTIFICATE-----
11+
tls.crt: |
12+
-----BEGIN CERTIFICATE-----
13+
...
14+
-----END CERTIFICATE-----
15+
tls.key: |
16+
-----BEGIN PRIVATE KEY-----
17+
...
18+
-----END PRIVATE KEY-----
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
PGBACKREST_STANZA=my_pgbackrest_stanza
2+
PGBACKREST_TARGET=2025-04-01 12:34:00-05:00
3+
PGBACKREST_REPO1_HOST=pgbackrest.example.com
4+
PGBACKREST_TLS_SERVER_AUTH=pgbackrest.example.com=*
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
POSTGRES_DB_NAME=my_database_name
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
apiVersion: v1
2+
kind: Secret
3+
metadata:
4+
name: postgres-root-credentials
5+
labels:
6+
app.kubernetes.io/name: postgres
7+
app.kubernetes.io/instance: postgres
8+
type: kubernetes.io/basic-auth
9+
stringData:
10+
username: postgres
11+
password: my_secret_password

0 commit comments

Comments
 (0)