新增learn-kubernetes(https://github.com/yyong-brs/learn-kubernetes)相关文件
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
apiVersion: batch/v1beta1
|
||||
kind: CronJob
|
||||
metadata:
|
||||
name: todo-db-backup
|
||||
labels:
|
||||
kiamol: ch08
|
||||
spec:
|
||||
schedule: "*/2 * * * *" # see https://crontab.guru
|
||||
concurrencyPolicy: Forbid
|
||||
jobTemplate:
|
||||
spec:
|
||||
template:
|
||||
spec:
|
||||
restartPolicy: Never
|
||||
containers:
|
||||
- name: backup
|
||||
image: postgres:11.6-alpine
|
||||
command: ['sh', '-c', 'pg_dump -h $POSTGRES_SECONDARY_FQDN -U postgres -F tar -f "/backup/$(date +%y%m%d-%H%M).tar" todo']
|
||||
envFrom:
|
||||
- configMapRef:
|
||||
name: todo-db-env
|
||||
env:
|
||||
- name: PGPASSWORD
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
key: POSTGRES_PASSWORD
|
||||
name: todo-db-secret
|
||||
volumeMounts:
|
||||
- name: backup
|
||||
mountPath: "/backup"
|
||||
volumes:
|
||||
- name: backup
|
||||
persistentVolumeClaim:
|
||||
claimName: todo-db-backup-pvc
|
||||
@@ -0,0 +1,12 @@
|
||||
apiVersion: v1
|
||||
kind: PersistentVolumeClaim
|
||||
metadata:
|
||||
name: todo-db-backup-pvc
|
||||
labels:
|
||||
kiamol: ch08
|
||||
spec:
|
||||
accessModes:
|
||||
- ReadWriteOnce
|
||||
resources:
|
||||
requests:
|
||||
storage: 100Mi
|
||||
@@ -0,0 +1,35 @@
|
||||
apiVersion: batch/v1beta1
|
||||
kind: CronJob
|
||||
metadata:
|
||||
name: todo-db-backup
|
||||
labels:
|
||||
kiamol: ch08
|
||||
spec:
|
||||
schedule: "*/2 * * * *" # see https://crontab.guru
|
||||
concurrencyPolicy: Forbid
|
||||
suspend: true
|
||||
jobTemplate:
|
||||
spec:
|
||||
template:
|
||||
spec:
|
||||
restartPolicy: Never
|
||||
containers:
|
||||
- name: backup
|
||||
image: postgres:11.6-alpine
|
||||
command: ['sh', '-c', 'pg_dump -h $POSTGRES_SECONDARY_FQDN -U postgres -F tar -f "/backup/$(date +%y%m%d-%H%M).tar" todo']
|
||||
envFrom:
|
||||
- configMapRef:
|
||||
name: todo-db-env
|
||||
env:
|
||||
- name: PGPASSWORD
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
key: POSTGRES_PASSWORD
|
||||
name: todo-db-secret
|
||||
volumeMounts:
|
||||
- name: backup
|
||||
mountPath: "/backup"
|
||||
volumes:
|
||||
- name: backup
|
||||
persistentVolumeClaim:
|
||||
claimName: todo-db-backup-pvc
|
||||
@@ -0,0 +1,51 @@
|
||||
apiVersion: v1
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
name: todo-db-config
|
||||
labels:
|
||||
kiamol: ch08
|
||||
data:
|
||||
primary.conf: |-
|
||||
listen_addresses = '*'
|
||||
max_connections = 100
|
||||
shared_buffers = 128MB
|
||||
dynamic_shared_memory_type = posix
|
||||
log_timezone = 'UTC'
|
||||
datestyle = 'iso, mdy'
|
||||
timezone = 'UTC'
|
||||
lc_messages = 'en_US.utf8'
|
||||
lc_monetary = 'en_US.utf8'
|
||||
lc_numeric = 'en_US.utf8'
|
||||
lc_time = 'en_US.utf8'
|
||||
default_text_search_config = 'pg_catalog.english'
|
||||
wal_level = hot_standby
|
||||
max_wal_senders = 5
|
||||
wal_keep_segments = 32
|
||||
standby.conf: |-
|
||||
listen_addresses = '*'
|
||||
max_connections = 100
|
||||
shared_buffers = 128MB
|
||||
dynamic_shared_memory_type = posix
|
||||
log_timezone = 'UTC'
|
||||
datestyle = 'iso, mdy'
|
||||
timezone = 'UTC'
|
||||
lc_messages = 'en_US.utf8'
|
||||
lc_monetary = 'en_US.utf8'
|
||||
lc_numeric = 'en_US.utf8'
|
||||
lc_time = 'en_US.utf8'
|
||||
default_text_search_config = 'pg_catalog.english'
|
||||
hot_standby = on
|
||||
pg_hba.conf: |-
|
||||
# "local" is for Unix domain socket connections only
|
||||
local all all trust
|
||||
# IPv4 local connections:
|
||||
host all all 127.0.0.1/32 trust
|
||||
# IPv6 local connections:
|
||||
host all all ::1/128 trust
|
||||
# Allow replication connections from localhost, by a user with the
|
||||
# replication privilege.
|
||||
local replication all trust
|
||||
host replication all 127.0.0.1/32 trust
|
||||
host replication all ::1/128 trust
|
||||
host replication replication all md5
|
||||
host all all all md5
|
||||
@@ -0,0 +1,10 @@
|
||||
apiVersion: v1
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
name: todo-db-env
|
||||
labels:
|
||||
kiamol: ch08
|
||||
data:
|
||||
POSTGRES_PRIMARY_NAME: "todo-db-0"
|
||||
POSTGRES_PRIMARY_FQDN: "todo-db-0.todo-db.default.svc.cluster.local"
|
||||
POSTGRES_SECONDARY_FQDN: "todo-db-1.todo-db.default.svc.cluster.local"
|
||||
@@ -0,0 +1,44 @@
|
||||
apiVersion: v1
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
name: todo-db-scripts
|
||||
labels:
|
||||
kiamol: ch08
|
||||
data:
|
||||
wait-service.sh: |-
|
||||
#!/bin/sh
|
||||
if [ "$HOSTNAME" == "$POSTGRES_PRIMARY_NAME" ]; then
|
||||
echo '** Postgres primary **'
|
||||
else
|
||||
echo '** Postgres standby - waiting on DNS for primary **'
|
||||
until nslookup ${POSTGRES_PRIMARY_FQDN}; do echo Waiting for ${POSTGRES_PRIMARY_FQDN}; sleep 1; done
|
||||
fi
|
||||
initialize-replication.sh: |-
|
||||
#!/bin/bash
|
||||
if [ "$HOSTNAME" == "$POSTGRES_PRIMARY_NAME" ]; then
|
||||
echo '** Postgres primary - creating replication user script **'
|
||||
cp /scripts/create-replica-user.sh /docker-entrypoint-initdb.d/create-replica-user.sh
|
||||
ls -l /docker-entrypoint-initdb.d
|
||||
else
|
||||
echo '** Postgres standby - waiting on primary **'
|
||||
until pg_isready -h "$POSTGRES_PRIMARY_FQDN"; do echo Waiting for db to be ready; sleep 1; done
|
||||
fi
|
||||
create-replica-user.sh: |-
|
||||
#!/bin/bash
|
||||
set -e
|
||||
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
|
||||
CREATE ROLE replication WITH REPLICATION PASSWORD '$PGPASSWORD' LOGIN
|
||||
EOSQL
|
||||
startup.sh: |-
|
||||
#!/bin/sh
|
||||
if [ "$HOSTNAME" == "$POSTGRES_PRIMARY_NAME" ]; then
|
||||
echo '** Postgres primary **'
|
||||
/docker-entrypoint.sh postgres -c config_file=/conf/primary.conf -c hba_file=/conf/pg_hba.conf
|
||||
else
|
||||
echo '** Postgres standby - initializing replication**'
|
||||
if [ -z "$(ls -A ${PGDATA})" ]; then
|
||||
pg_basebackup -R -h "$POSTGRES_PRIMARY_FQDN" -D "$PGDATA" -P -U replication
|
||||
chown -R postgres:postgres $PGDATA
|
||||
fi
|
||||
/docker-entrypoint.sh postgres -c config_file=/conf/standby.conf
|
||||
fi
|
||||
@@ -0,0 +1,86 @@
|
||||
apiVersion: apps/v1
|
||||
kind: StatefulSet
|
||||
metadata:
|
||||
name: todo-db
|
||||
labels:
|
||||
kiamol: ch08
|
||||
spec:
|
||||
selector:
|
||||
matchLabels:
|
||||
app: todo-db
|
||||
serviceName: todo-db
|
||||
replicas: 2
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: todo-db
|
||||
spec:
|
||||
initContainers:
|
||||
- name: wait-service
|
||||
image: kiamol/ch03-sleep
|
||||
envFrom:
|
||||
- configMapRef:
|
||||
name: todo-db-env
|
||||
command: ['/scripts/wait-service.sh']
|
||||
volumeMounts:
|
||||
- name: scripts
|
||||
mountPath: "/scripts"
|
||||
- name: initialize-replication
|
||||
image: postgres:11.6-alpine
|
||||
envFrom:
|
||||
- configMapRef:
|
||||
name: todo-db-env
|
||||
env:
|
||||
- name: PGPASSWORD # used as replication password
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
key: POSTGRES_PASSWORD
|
||||
name: todo-db-secret
|
||||
command: ['/scripts/initialize-replication.sh']
|
||||
volumeMounts:
|
||||
- name: scripts
|
||||
mountPath: "/scripts"
|
||||
- name: initdb
|
||||
mountPath: /docker-entrypoint-initdb.d
|
||||
containers:
|
||||
- name: db
|
||||
image: postgres:11.6-alpine
|
||||
command: ["/scripts/startup.sh"]
|
||||
envFrom:
|
||||
- configMapRef:
|
||||
name: todo-db-env
|
||||
env:
|
||||
- name: POSTGRES_PASSWORD_FILE
|
||||
value: /secrets/postgres_password
|
||||
- name: PGPASSWORD # used as replication password
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
key: POSTGRES_PASSWORD
|
||||
name: todo-db-secret
|
||||
volumeMounts:
|
||||
- name: secret
|
||||
mountPath: "/secrets"
|
||||
- name: scripts
|
||||
mountPath: "/scripts"
|
||||
- name: config
|
||||
mountPath: "/conf"
|
||||
- name: initdb
|
||||
mountPath: /docker-entrypoint-initdb.d
|
||||
volumes:
|
||||
- name: secret
|
||||
secret:
|
||||
secretName: todo-db-secret
|
||||
defaultMode: 0400
|
||||
items:
|
||||
- key: POSTGRES_PASSWORD
|
||||
path: postgres_password
|
||||
- name: scripts
|
||||
configMap:
|
||||
name: todo-db-scripts
|
||||
defaultMode: 0555
|
||||
- name: config
|
||||
configMap:
|
||||
name: todo-db-config
|
||||
defaultMode: 0444
|
||||
- name: initdb
|
||||
emptyDir: {}
|
||||
@@ -0,0 +1,99 @@
|
||||
apiVersion: apps/v1
|
||||
kind: StatefulSet
|
||||
metadata:
|
||||
name: todo-db
|
||||
labels:
|
||||
kiamol: ch08
|
||||
spec:
|
||||
selector:
|
||||
matchLabels:
|
||||
app: todo-db
|
||||
serviceName: todo-db
|
||||
replicas: 2
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: todo-db
|
||||
spec:
|
||||
initContainers:
|
||||
- name: wait-service
|
||||
image: kiamol/ch03-sleep
|
||||
envFrom:
|
||||
- configMapRef:
|
||||
name: todo-db-env
|
||||
command: ['/scripts/wait-service.sh']
|
||||
volumeMounts:
|
||||
- name: scripts
|
||||
mountPath: "/scripts"
|
||||
- name: initialize-replication
|
||||
image: postgres:11.6-alpine
|
||||
envFrom:
|
||||
- configMapRef:
|
||||
name: todo-db-env
|
||||
env:
|
||||
- name: PGPASSWORD # used as replication password
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
key: POSTGRES_PASSWORD
|
||||
name: todo-db-secret
|
||||
command: ['/scripts/initialize-replication.sh']
|
||||
volumeMounts:
|
||||
- name: scripts
|
||||
mountPath: "/scripts"
|
||||
- name: initdb
|
||||
mountPath: /docker-entrypoint-initdb.d
|
||||
containers:
|
||||
- name: db
|
||||
image: postgres:11.6-alpine
|
||||
command: ["/scripts/startup.sh"]
|
||||
envFrom:
|
||||
- configMapRef:
|
||||
name: todo-db-env
|
||||
env:
|
||||
- name: POSTGRES_PASSWORD_FILE
|
||||
value: /secrets/postgres_password
|
||||
- name: PGPASSWORD # used as replication password
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
key: POSTGRES_PASSWORD
|
||||
name: todo-db-secret
|
||||
volumeMounts:
|
||||
- name: secret
|
||||
mountPath: "/secrets"
|
||||
- name: scripts
|
||||
mountPath: "/scripts"
|
||||
- name: config
|
||||
mountPath: "/conf"
|
||||
- name: initdb
|
||||
mountPath: /docker-entrypoint-initdb.d
|
||||
- name: data
|
||||
mountPath: /var/lib/postgresql/data
|
||||
volumes:
|
||||
- name: secret
|
||||
secret:
|
||||
secretName: todo-db-secret
|
||||
defaultMode: 0400
|
||||
items:
|
||||
- key: POSTGRES_PASSWORD
|
||||
path: postgres_password
|
||||
- name: scripts
|
||||
configMap:
|
||||
name: todo-db-scripts
|
||||
defaultMode: 0555
|
||||
- name: config
|
||||
configMap:
|
||||
name: todo-db-config
|
||||
defaultMode: 0444
|
||||
- name: initdb
|
||||
emptyDir: {}
|
||||
volumeClaimTemplates:
|
||||
- metadata:
|
||||
name: data
|
||||
labels:
|
||||
kiamol: ch08
|
||||
spec:
|
||||
accessModes:
|
||||
- ReadWriteOnce
|
||||
resources:
|
||||
requests:
|
||||
storage: 50Mi
|
||||
@@ -0,0 +1,9 @@
|
||||
apiVersion: v1
|
||||
kind: Secret
|
||||
metadata:
|
||||
name: todo-db-secret
|
||||
labels:
|
||||
kiamol: ch08
|
||||
type: Opaque
|
||||
stringData:
|
||||
POSTGRES_PASSWORD: "kiamol-2*2*"
|
||||
@@ -0,0 +1,14 @@
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: todo-db
|
||||
labels:
|
||||
kiamol: ch08
|
||||
spec:
|
||||
ports:
|
||||
- port: 5432
|
||||
targetPort: 5432
|
||||
name: postgres
|
||||
selector:
|
||||
app: todo-db
|
||||
clusterIP: None
|
||||
@@ -0,0 +1,34 @@
|
||||
apiVersion: apps/v1
|
||||
kind: StatefulSet
|
||||
metadata:
|
||||
name: todo-db
|
||||
labels:
|
||||
kiamol: ch08
|
||||
spec:
|
||||
selector:
|
||||
matchLabels:
|
||||
app: todo-db
|
||||
serviceName: todo-db
|
||||
replicas: 2
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: todo-db
|
||||
spec:
|
||||
containers:
|
||||
- name: db
|
||||
image: postgres:11.6-alpine
|
||||
env:
|
||||
- name: POSTGRES_PASSWORD_FILE
|
||||
value: /secrets/postgres_password
|
||||
volumeMounts:
|
||||
- name: secret
|
||||
mountPath: "/secrets"
|
||||
volumes:
|
||||
- name: secret
|
||||
secret:
|
||||
secretName: todo-db-secret
|
||||
defaultMode: 0400
|
||||
items:
|
||||
- key: POSTGRES_PASSWORD
|
||||
path: postgres_password
|
||||
Reference in New Issue
Block a user