feat(aula-17): adicionar Cloud Native PostgreSQL (CNPG)
- Cluster PostgreSQL 3 instâncias com anti-affinity por hostname (hard) e por zona (soft) para spread entre datacenters Hetzner - Backup automático via Barman Cloud para Hetzner Object Storage (S3) - ScheduledBackup diário às 03:00 - App demo que valida conexão com o banco - setup.sh: instala operator, cria cluster, deploya demo - cleanup.sh: remoção limpa (backups S3 preservados)
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: pg-demo
|
||||
namespace: cnpg
|
||||
spec:
|
||||
replicas: 1
|
||||
selector:
|
||||
matchLabels:
|
||||
app: pg-demo
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: pg-demo
|
||||
spec:
|
||||
containers:
|
||||
- name: pg-demo
|
||||
image: postgres:17
|
||||
env:
|
||||
- name: PGHOST
|
||||
value: shared-postgres-rw
|
||||
- name: PGPORT
|
||||
value: "5432"
|
||||
- name: PGUSER
|
||||
value: app
|
||||
- name: PGDATABASE
|
||||
value: app
|
||||
- name: PGPASSWORD
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: shared-postgres-superuser
|
||||
key: password
|
||||
command:
|
||||
- /bin/bash
|
||||
- -c
|
||||
- |
|
||||
echo "=== CNPG Demo App ==="
|
||||
echo "Connecting to $PGHOST:$PGPORT/$PGDATABASE as $PGUSER..."
|
||||
|
||||
until pg_isready -h "$PGHOST" -p "$PGPORT" -U app -q; do
|
||||
echo "Waiting for PostgreSQL..."
|
||||
sleep 2
|
||||
done
|
||||
|
||||
echo "Connected! Creating test table..."
|
||||
psql -c "
|
||||
CREATE TABLE IF NOT EXISTS visits (
|
||||
id SERIAL PRIMARY KEY,
|
||||
visited_at TIMESTAMP DEFAULT NOW(),
|
||||
source TEXT DEFAULT 'pg-demo'
|
||||
);
|
||||
"
|
||||
|
||||
echo "Inserting visit..."
|
||||
psql -c "INSERT INTO visits (source) VALUES ('pg-demo');"
|
||||
|
||||
echo ""
|
||||
echo "Recent visits:"
|
||||
psql -c "SELECT * FROM visits ORDER BY id DESC LIMIT 5;"
|
||||
|
||||
echo ""
|
||||
echo "=== Keeping alive. Press Ctrl+C to stop. ==="
|
||||
echo "Connection is working! Check with: kubectl logs -f deployment/pg-demo -n cnpg"
|
||||
sleep infinity
|
||||
@@ -0,0 +1,12 @@
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: pg-demo
|
||||
namespace: cnpg
|
||||
spec:
|
||||
selector:
|
||||
app: pg-demo
|
||||
ports:
|
||||
- port: 80
|
||||
targetPort: 80
|
||||
type: ClusterIP
|
||||
Reference in New Issue
Block a user