feat: store leads in PostgreSQL instead of SQLite

- server.js uses pg (pinned 8.23.1), reads DATABASE_URL, creates the
  leads table on start, fails fast if Postgres is unreachable
- tests run against a real Postgres in a throwaway schema
- CI runs the test image against a throwaway Postgres on DinD
- k8s: Postgres 18 StatefulSet (Longhorn, 2Gi), init container wait,
  password secret, network policy app-to-postgres; app no longer needs
  a volume or Recreate
- README: DBeaver via port-forward, CSV export via docs/export-leads.sql

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfmHGGiYUjNcvJoyzdf
This commit is contained in:
ngqileandClaude Opus 5.5 committed 2026-12-01 11:55:14 +08:00
1 parent 5bb2a7343d
commit f78bd635a2
15 files changed
+508 -166

No files matched your search

+84
View File
@@ -0,0 +1,84 @@
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: ins-landing-page-postgres
namespace: ins-landing-page
spec:
storageClassName: longhorn
# ReadWriteOnce, not ReadWriteMany: Longhorn serves RWX over NFS, which a database should not
# write through. One replica mounts it.
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 2Gi
---
apiVersion: v1
kind: Service
metadata:
name: postgres
namespace: ins-landing-page
spec:
clusterIP: None
selector:
app: postgres
ports:
- name: postgres
port: 5432
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: postgres
namespace: ins-landing-page
spec:
serviceName: postgres
replicas: 1
selector:
matchLabels:
app: postgres
template:
metadata:
labels:
app: postgres
spec:
terminationGracePeriodSeconds: 60
containers:
- name: postgres
image: postgres:18
env:
- name: POSTGRES_USER
value: ins
- name: POSTGRES_DB
value: ins
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: ins-landing-page-db
key: POSTGRES_PASSWORD
ports:
- name: postgres
containerPort: 5432
readinessProbe:
exec:
command: ["pg_isready", "-U", "ins", "-d", "ins"]
periodSeconds: 5
livenessProbe:
exec:
command: ["pg_isready", "-U", "ins", "-d", "ins"]
periodSeconds: 20
failureThreshold: 6
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
memory: 1Gi
volumeMounts:
# postgres:18 keeps its data under /var/lib/postgresql/<major>/docker.
- name: data
mountPath: /var/lib/postgresql
volumes:
- name: data
persistentVolumeClaim:
claimName: ins-landing-page-postgres
+24 -27
View File
@@ -1,18 +1,3 @@
# Leads live in a SQLite file on this volume. ReadWriteOnce + 1 replica + Recreate: SQLite has a
# single writer, so two pods must never hold the file at once (not even during a rollout).
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: ins-landing-page-data
namespace: ins-landing-page
spec:
storageClassName: longhorn
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
---
apiVersion: v1
kind: Service
metadata:
@@ -32,8 +17,6 @@ metadata:
namespace: ins-landing-page
spec:
replicas: 1
strategy:
type: Recreate
selector:
matchLabels:
app: ins-landing-page
@@ -49,7 +32,23 @@ spec:
runAsNonRoot: true
runAsUser: 1000 # the image's `node` user
runAsGroup: 1000
fsGroup: 1000 # so the app can write to the Longhorn volume
# Start the app only once Postgres answers, instead of crash-looping into a growing back-off
# after a first install or a node restart. Same image as the StatefulSet, so nothing extra is pulled.
initContainers:
- name: wait-for-postgres
image: postgres:18
command: ["sh", "-c", "until pg_isready -h postgres -p 5432 -U ins -d ins; do sleep 2; done"]
resources:
requests:
cpu: 10m
memory: 16Mi
limits:
memory: 64Mi
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop: ["ALL"]
containers:
- name: ins-landing-page
# One moving tag per profile (CI); the rollout restarts the pod to pull it.
@@ -58,14 +57,16 @@ spec:
env:
- name: PORT
value: "3000"
- name: DB_PATH
value: /data/leads.db
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: ins-landing-page-db
key: POSTGRES_PASSWORD
- name: DATABASE_URL
value: postgres://ins:$(POSTGRES_PASSWORD)@postgres:5432/ins
ports:
- name: http
containerPort: 3000
volumeMounts:
- name: data
mountPath: /data
readinessProbe:
httpGet:
path: /
@@ -89,7 +90,3 @@ spec:
readOnlyRootFilesystem: true
capabilities:
drop: ["ALL"]
volumes:
- name: data
persistentVolumeClaim:
claimName: ins-landing-page-data
+21
View File
@@ -35,6 +35,27 @@ spec:
- protocol: TCP
port: 3000
---
# Postgres: app pods only (kubectl exec and port-forward are not pod network traffic and are unaffected).
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: postgres-from-app
namespace: ins-landing-page
spec:
podSelector:
matchLabels:
app: postgres
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
app: ins-landing-page
ports:
- protocol: TCP
port: 5432
---
# cert-manager's HTTP-01 solver pod: Traefik must reach it, or the certificate cannot be issued
# or renewed.
apiVersion: networking.k8s.io/v1
+12
View File
@@ -0,0 +1,12 @@
# EXAMPLE ONLY - never commit real values. Create the real Secret on the cluster:
# microk8s kubectl -n ins-landing-page create secret generic ins-landing-page-db \
# --from-literal=POSTGRES_PASSWORD="$(openssl rand -hex 24)"
# The password is embedded in a postgres:// URL, so use URL-safe characters (hex is).
apiVersion: v1
kind: Secret
metadata:
name: ins-landing-page-db
namespace: ins-landing-page
type: Opaque
stringData:
POSTGRES_PASSWORD: change-me