Separate one-time bootstrap, historical deployment examples and local validation. Document final hostname and historical status; exclude credentials and runtime data.
146 lines
5.3 KiB
YAML
146 lines
5.3 KiB
YAML
apiVersion: v1
|
|
kind: ConfigMap
|
|
metadata:
|
|
name: pageswiki-exhibition-init
|
|
data:
|
|
storage.py: |
|
|
import os, pathlib, shutil, stat
|
|
root = pathlib.Path('/volume/pageswiki-exhibit-v018')
|
|
def owned_directory(path, uid, gid, mode):
|
|
try:
|
|
path.mkdir(mode=mode)
|
|
except FileExistsError:
|
|
s = path.lstat()
|
|
if not stat.S_ISDIR(s.st_mode) or (s.st_uid, s.st_gid) != (uid, gid):
|
|
raise RuntimeError('unexpected existing directory ownership: ' + str(path))
|
|
else:
|
|
os.chown(path, uid, gid)
|
|
owned_directory(root, 0, 0, 0o755)
|
|
owned_directory(root / 'wiki', 10001, 10001, 0o700)
|
|
owned_directory(root / 'wiki' / 'documents', 10001, 10001, 0o700)
|
|
owned_directory(root / 'postgres', 70, 70, 0o700)
|
|
os.chown('/pgsocket', 70, 70)
|
|
for name in ('server.crt', 'server.key'):
|
|
shutil.copyfile('/cert-source/' + name, '/tls/' + name)
|
|
os.chmod('/tls/' + name, 0o600)
|
|
os.chown('/tls/' + name, 70, 70)
|
|
print('Dedicated directories and PostgreSQL TLS files ready; existing ownership unchanged.')
|
|
10-app.sh: |
|
|
set -eu
|
|
psql -v ON_ERROR_STOP=1 --username postgres --dbname postgres <<SQL
|
|
CREATE ROLE pageswiki LOGIN PASSWORD '${APP_DB_PASSWORD}' NOSUPERUSER NOCREATEDB NOCREATEROLE;
|
|
CREATE DATABASE pageswiki OWNER pageswiki;
|
|
SQL
|
|
---
|
|
apiVersion: apps/v1
|
|
kind: Deployment
|
|
metadata:
|
|
name: pageswiki-postgres
|
|
spec:
|
|
replicas: 1
|
|
strategy: {type: Recreate}
|
|
selector:
|
|
matchLabels: {app: pageswiki-postgres}
|
|
template:
|
|
metadata:
|
|
labels: {app: pageswiki-postgres}
|
|
spec:
|
|
automountServiceAccountToken: false
|
|
terminationGracePeriodSeconds: 90
|
|
securityContext:
|
|
seccompProfile: {type: RuntimeDefault}
|
|
initContainers:
|
|
- name: initialize-storage
|
|
image: python@sha256:9ef1a599f863231c3d91e941389bfdc4a2d6070557243d54df1a4e57015b960e
|
|
command: [python3, /scripts/storage.py]
|
|
securityContext:
|
|
runAsUser: 0
|
|
runAsGroup: 0
|
|
allowPrivilegeEscalation: false
|
|
readOnlyRootFilesystem: true
|
|
capabilities: {drop: [ALL], add: [CHOWN, DAC_OVERRIDE]}
|
|
resources:
|
|
requests: {cpu: 50m, memory: 32Mi}
|
|
limits: {cpu: 250m, memory: 128Mi}
|
|
volumeMounts:
|
|
- {name: data, mountPath: /volume}
|
|
- {name: scripts, mountPath: /scripts, readOnly: true}
|
|
- {name: certificates, mountPath: /cert-source, readOnly: true}
|
|
- {name: tls, mountPath: /tls}
|
|
- {name: socket, mountPath: /pgsocket}
|
|
containers:
|
|
- name: postgres
|
|
image: postgres@sha256:005c5c37db96c1e6652a79ea31f9886402fba81ac95dc2db897739121dd7f8f2
|
|
args:
|
|
- postgres
|
|
- -c
|
|
- ssl=on
|
|
- -c
|
|
- ssl_cert_file=/tls/server.crt
|
|
- -c
|
|
- ssl_key_file=/tls/server.key
|
|
- -c
|
|
- shared_buffers=128MB
|
|
- -c
|
|
- max_connections=40
|
|
env:
|
|
- {name: PGDATA, value: /var/lib/postgresql/data/pgdata}
|
|
- name: POSTGRES_PASSWORD
|
|
valueFrom: {secretKeyRef: {name: pageswiki-exhibition-db, key: admin-password}}
|
|
- name: APP_DB_PASSWORD
|
|
valueFrom: {secretKeyRef: {name: pageswiki-exhibition-db, key: app-password}}
|
|
- {name: POSTGRES_INITDB_ARGS, value: '--auth-host=scram-sha-256'}
|
|
ports: [{name: postgres, containerPort: 5432}]
|
|
securityContext:
|
|
runAsNonRoot: true
|
|
runAsUser: 70
|
|
runAsGroup: 70
|
|
allowPrivilegeEscalation: false
|
|
capabilities: {drop: [ALL]}
|
|
resources:
|
|
requests: {cpu: 250m, memory: 256Mi}
|
|
limits: {cpu: '1', memory: 1Gi}
|
|
startupProbe:
|
|
exec: {command: [pg_isready, -U, postgres, -d, postgres]}
|
|
periodSeconds: 5
|
|
failureThreshold: 60
|
|
readinessProbe:
|
|
exec: {command: [pg_isready, -U, postgres, -d, postgres]}
|
|
periodSeconds: 5
|
|
volumeMounts:
|
|
- {name: data, mountPath: /var/lib/postgresql/data, subPath: pageswiki-exhibit-v018/postgres}
|
|
- {name: scripts, mountPath: /docker-entrypoint-initdb.d/10-app.sh, subPath: 10-app.sh, readOnly: true}
|
|
- {name: tls, mountPath: /tls, readOnly: true}
|
|
- {name: socket, mountPath: /var/run/postgresql}
|
|
volumes:
|
|
- name: data
|
|
persistentVolumeClaim: {claimName: pageswiki-data}
|
|
- name: scripts
|
|
configMap: {name: pageswiki-exhibition-init}
|
|
- name: certificates
|
|
secret: {secretName: pageswiki-exhibition-tls, defaultMode: 0400}
|
|
- name: tls
|
|
emptyDir: {}
|
|
- name: socket
|
|
emptyDir: {}
|
|
---
|
|
apiVersion: v1
|
|
kind: Service
|
|
metadata: {name: pageswiki-postgres}
|
|
spec:
|
|
selector: {app: pageswiki-postgres}
|
|
ports: [{name: postgres, port: 5432, targetPort: postgres}]
|
|
---
|
|
apiVersion: networking.k8s.io/v1
|
|
kind: NetworkPolicy
|
|
metadata: {name: pageswiki-postgres}
|
|
spec:
|
|
podSelector:
|
|
matchLabels: {app: pageswiki-postgres}
|
|
policyTypes: [Ingress]
|
|
ingress:
|
|
- from:
|
|
- podSelector:
|
|
matchLabels: {pageswiki-db-client: 'true'}
|
|
ports: [{protocol: TCP, port: 5432}]
|