This commit is contained in:
2024-02-20 17:15:27 +08:00
committed by huty
parent 6706e1a633
commit 34158042ad
1529 changed files with 177765 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: todo-db-config
labels:
kiamol: ch09
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

View File

@@ -0,0 +1,10 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: todo-db-env
labels:
kiamol: ch09
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"

View File

@@ -0,0 +1,44 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: todo-db-scripts
labels:
kiamol: ch09
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

View File

@@ -0,0 +1,9 @@
apiVersion: v1
kind: Secret
metadata:
name: todo-db-secret
labels:
kiamol: ch09
type: Opaque
stringData:
POSTGRES_PASSWORD: "kiamol-2*2*"

View File

@@ -0,0 +1,14 @@
apiVersion: v1
kind: Service
metadata:
name: todo-db
labels:
kiamol: ch09
spec:
ports:
- port: 5432
targetPort: 5432
name: postgres
selector:
app: todo-db
clusterIP: None

View File

@@ -0,0 +1,99 @@
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: todo-db
labels:
kiamol: ch09
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: ch09
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 50Mi

View File

@@ -0,0 +1,103 @@
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: todo-db
labels:
kiamol: ch09
spec:
replicas: 2
selector:
matchLabels:
app: todo-db
serviceName: todo-db
updateStrategy:
type: RollingUpdate
rollingUpdate:
partition: 1 # only updates Pod 1
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.8-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.8-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: ch09
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 50Mi

View File

@@ -0,0 +1,101 @@
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: todo-db
labels:
kiamol: ch09
spec:
replicas: 2
selector:
matchLabels:
app: todo-db
serviceName: todo-db
updateStrategy:
type: RollingUpdate
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.8-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.8-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: ch09
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 50Mi

View File

@@ -0,0 +1,45 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: todo-proxy-configmap
labels:
kiamol: ch09
data:
nginx.conf: |-
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=STATIC:1m inactive=24h max_size=1g;
gzip on;
gzip_proxied any;
map $sent_http_content_type $expires {
default off;
~image/ 6M;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
location / {
proxy_pass http://todo-web;
proxy_set_header Host $host;
proxy_cache STATIC;
proxy_cache_valid 200 10s;
proxy_cache_use_stale error timeout invalid_header updating
http_500 http_502 http_503 http_504;
add_header X-Cache $upstream_cache_status;
add_header X-Host $hostname;
}
}
}

View File

@@ -0,0 +1,13 @@
apiVersion: v1
kind: Service
metadata:
name: todo-proxy
labels:
kiamol: ch09
spec:
ports:
- port: 8091
targetPort: 80
selector:
app: todo-proxy
type: LoadBalancer

View File

@@ -0,0 +1,33 @@
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: todo-proxy
labels:
kiamol: ch09
spec:
selector:
matchLabels:
app: todo-proxy
template:
metadata:
labels:
app: todo-proxy
spec:
containers:
- image: nginx:1.17-alpine
name: nginx
ports:
- containerPort: 80
name: http
volumeMounts:
- name: config
mountPath: "/etc/nginx/"
readOnly: true
- name: cache-volume
mountPath: /data/nginx/cache
volumes:
- name: config
configMap:
name: todo-proxy-configmap
- name: cache-volume
emptyDir: {}

View File

@@ -0,0 +1,38 @@
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: todo-proxy
labels:
kiamol: ch09
spec:
selector:
matchLabels:
app: todo-proxy
updateStrategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 1
minReadySeconds: 90
template:
metadata:
labels:
app: todo-proxy
spec:
containers:
- image: nginx:1.18-alpine
name: nginx
ports:
- containerPort: 80
name: http
volumeMounts:
- name: config
mountPath: "/etc/nginx/"
readOnly: true
- name: cache-volume
mountPath: /data/nginx/cache
volumes:
- name: config
configMap:
name: todo-proxy-configmap
- name: cache-volume
emptyDir: {}

View File

@@ -0,0 +1,16 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: todo-web-config
labels:
kiamol: ch09
data:
config.json: |-
{
"ConfigController": {
"Enabled" : true
},
"Database" : {
"Provider" : "Postgres"
}
}

View File

@@ -0,0 +1,15 @@
apiVersion: v1
kind: Secret
metadata:
name: todo-web-secret
labels:
kiamol: ch09
type: Opaque
stringData:
secrets.json: |-
{
"ConnectionStrings": {
"ToDoDb": "Server=todo-db-0.todo-db.default.svc.cluster.local;Database=todo;User Id=postgres;Password=kiamol-2*2*;",
"ToDoDb-ReadOnly": "Server=todo-db-1.todo-db.default.svc.cluster.local;Database=todo;User Id=postgres;Password=kiamol-2*2*;"
}
}

View File

@@ -0,0 +1,13 @@
apiVersion: v1
kind: Service
metadata:
name: todo-web
labels:
kiamol: ch09
spec:
ports:
- port: 80
targetPort: 80
selector:
app: todo-web
type: ClusterIP

View File

@@ -0,0 +1,42 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: todo-web
labels:
kiamol: ch09
spec:
selector:
matchLabels:
app: todo-web
template:
metadata:
labels:
app: todo-web
spec:
containers:
- name: web
image: kiamol/ch04-todo-list
env:
- name: ASPNETCORE_ENVIRONMENT
value: Test
volumeMounts:
- name: config
mountPath: "/app/config"
readOnly: true
- name: secret
mountPath: "/app/secrets"
readOnly: true
volumes:
- name: config
configMap:
name: todo-web-config
items:
- key: config.json
path: config.json
- name: secret
secret:
secretName: todo-web-secret
defaultMode: 0400
items:
- key: secrets.json
path: secrets.json

View File

@@ -0,0 +1,44 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: todo-web
labels:
kiamol: ch09
spec:
selector:
matchLabels:
app: todo-web
template:
metadata:
labels:
app: todo-web
spec:
containers:
- name: web
image: kiamol/ch04-todo-list
env:
- name: ASPNETCORE_ENVIRONMENT
value: Test
- name: Database__ReadOnly
value: "true"
volumeMounts:
- name: config
mountPath: "/app/config"
readOnly: true
- name: secret
mountPath: "/app/secrets"
readOnly: true
volumes:
- name: config
configMap:
name: todo-web-config
items:
- key: config.json
path: config.json
- name: secret
secret:
secretName: todo-web-secret
defaultMode: 0400
items:
- key: secrets.json
path: secrets.json