-
-
Save pegasuskim/8cca81481628537cccc9c23a013ebd31 to your computer and use it in GitHub Desktop.
Running postgres in kubernetes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# postgres cluster in kubernetes with max 8 replicas | |
# | |
# the master is always on ${host}-0 | |
# | |
# | |
# postgres master/slave configuration | |
# | |
apiVersion: v1 | |
kind: ConfigMap | |
metadata: | |
name: master-slave-config | |
data: | |
master-slave-config.sh: |- | |
HOST=`hostname -s` | |
ORD=${HOST##*-} | |
HOST_TEMPLATE=${HOST%-*} | |
case $ORD in | |
0) | |
echo "host replication all all md5" >> /var/lib/postgresql/data/pg_hba.conf | |
echo "archive_mode = on" >> /etc/postgresql/postgresql.conf | |
echo "archive_mode = on" >> /etc/postgresql/postgresql.conf | |
echo "archive_command = '/bin/true'" >> /etc/postgresql/postgresql.conf | |
echo "archive_timeout = 0" >> /etc/postgresql/postgresql.conf | |
echo "max_wal_senders = 8" >> /etc/postgresql/postgresql.conf | |
echo "wal_keep_segments = 32" >> /etc/postgresql/postgresql.conf | |
echo "wal_level = hot_standby" >> /etc/postgresql/postgresql.conf | |
;; | |
*) | |
# stop initial server to copy data | |
pg_ctl -D /var/lib/postgresql/data/ -m fast -w stop | |
rm -rf /var/lib/postgresql/data/* | |
# add service name for DNS resolution | |
PGPASSWORD=k8s-postgres-ha pg_basebackup -h ${HOST_TEMPLATE}-0.postgresql-service -w -U replicator -p 5432 -D /var/lib/postgresql/data -Fp -Xs -P -R | |
# start server to keep container's screep happy | |
pg_ctl -D /var/lib/postgresql/data/ -w start | |
;; | |
esac | |
create-replication-role.sql: |- | |
CREATE USER replicator WITH REPLICATION ENCRYPTED PASSWORD 'k8s-postgres-ha'; | |
--- | |
# | |
# postgres service | |
# | |
apiVersion: v1 | |
kind: Service | |
metadata: | |
name: postgresql-service | |
spec: | |
selector: | |
app: postgresql | |
ports: | |
- name: postgresql | |
port: 5432 | |
targetPort: 5432 | |
--- | |
# | |
# postgres containers | |
# | |
apiVersion: apps/v1 | |
kind: StatefulSet | |
metadata: | |
name: postgresql | |
spec: | |
selector: | |
matchLabels: | |
app: postgresql | |
replicas: 2 | |
serviceName: postgresql-service | |
template: | |
metadata: | |
labels: | |
app: postgresql | |
spec: | |
containers: | |
- name: pg-db | |
image: postgres:12 | |
ports: | |
- name: postgresql | |
containerPort: 5432 | |
readinessProbe: | |
tcpSocket: | |
port: 5432 | |
initialDelaySeconds: 10 | |
timeoutSeconds: 5 | |
livenessProbe: | |
tcpSocket: | |
port: 5432 | |
initialDelaySeconds: 10 | |
timeoutSeconds: 5 | |
volumeMounts: | |
- name: datadir | |
mountPath: /var/lib/postgresql/data | |
volumeMounts: | |
- name: init-scripts | |
mountPath: /docker-entrypoint-initdb.d | |
env: | |
- name: POSTGRES_PASSWORD | |
value: let-me-in | |
volumes: | |
- name: init-scripts | |
configMap: | |
name: master-slave-config | |
volumeClaimTemplates: | |
- metadata: | |
name: datadir | |
spec: | |
accessModes: [ "ReadWriteOnce" ] | |
resources: | |
requests: | |
storage: 10Gi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment