feat: Add PostgreSQL Kubernetes deployment with Kustomize

- Create infrastructure for PostgreSQL StatefulSet in Kubernetes
- Implement environment-specific configurations (staging/production)
- Add Kustomize overlays for deterministic deployments
- Document deployment process in AGENTS.md and infra/k8s/README.md
- Include Makefile and helper scripts for easy management

New directories:
- infra/k8s/base/ - Shared resources (StatefulSet, Service, PVC, Namespace)
- infra/k8s/staging/ - Staging environment overlay
- infra/k8s/production/ - Production environment overlay

Key files:
- infra/k8s/base/kustomization.yaml
- infra/k8s/staging/kustomization.yaml
- infra/k8s/production/kustomization.yaml
- infra/k8s/README.md

Resources are prefixed by environment (staging-, production-) and deployed
to the postgres namespace.
This commit is contained in:
Bobby committed 2026-11-18 15:29:00 -05:00
1 parent 9568d4ca6e
commit 88235fc824
26 files changed
+1407 -14

No files matched your search

+17
View File
@@ -0,0 +1,17 @@
#!/bin/bash
# Wrapper script for applying kustomize with environment variable substitution
set -e
# Use ENVIRONMENT from environment or default to 'staging'
ENVIRONMENT="${ENVIRONMENT:-staging}"
export ENVIRONMENT
# Set POSTGRES_DB if not set
POSTGRES_DB="${POSTGRES_DB:-hapi_${ENVIRONMENT}}"
export POSTGRES_DB
# Run kubectl apply with environment variables substituted
envsubst '${ENVIRONMENT} ${POSTGRES_DB}' < kustomization.yaml > kustomization.subst.yaml
kubectl apply -f kustomization.subst.yaml --dry-run=client
rm kustomization.subst.yaml
+10
View File
@@ -0,0 +1,10 @@
# Base Kustomize configuration for PostgreSQL
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- namespace.yaml
- statefulset.yaml
- service.yaml
- pvc.yaml
+10
View File
@@ -0,0 +1,10 @@
# Namespace for PostgreSQL resources
apiVersion: v1
kind: Namespace
metadata:
name: postgres
labels:
app: postgres
managedBy: terraform
annotations:
description: "Namespace for PostgreSQL StatefulSet and related resources"
+19
View File
@@ -0,0 +1,19 @@
# Standalone PersistentVolumeClaim for PostgreSQL
# Note: This PVC is separate from the StatefulSet's volumeClaimTemplates.
# It is currently unused in the active deployment but can be used for
# backup, migration, or testing purposes. The primary storage is provided
# by volumeClaimTemplates in statefulset.yaml.
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: postgres-pvc
labels:
app: postgres
spec:
accessModes:
- ReadWriteOnce
storageClassName: linode-block-storage
resources:
requests:
storage: 50Gi
+17
View File
@@ -0,0 +1,17 @@
# Service for PostgreSQL StatefulSet
# Provides stable network identity and load balancing
apiVersion: v1
kind: Service
metadata:
name: postgres-service
labels:
app: postgres
spec:
type: ClusterIP
ports:
- port: 5432
targetPort: 5432
name: postgres
selector:
app: postgres
+90
View File
@@ -0,0 +1,90 @@
# PostgreSQL StatefulSet for HAPI
# Provides production-ready PostgreSQL with persistent storage
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: postgres-statefulset
labels:
app: postgres
spec:
serviceName: postgres-service
replicas: 1
selector:
matchLabels:
app: postgres
template:
metadata:
labels:
app: postgres
spec:
securityContext:
fsGroup: 999
containers:
- name: postgres
image: postgres:16.3
ports:
- containerPort: 5432
name: postgres
env:
- name: POSTGRES_USER
valueFrom:
secretKeyRef:
name: postgres-secrets
key: username
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: postgres-secrets
key: password
- name: POSTGRES_DB
value: ${POSTGRES_DB}
- name: PGDATA
value: /var/lib/postgresql/data/pgdata
volumeMounts:
- name: postgres-data
mountPath: /var/lib/postgresql/data
- name: postgres-config
mountPath: /etc/postgresql/postgresql.conf
subPath: postgresql.conf
resources:
requests:
cpu: "500m"
memory: "512Mi"
limits:
cpu: "1000m"
memory: "1Gi"
securityContext:
runAsUser: 999
runAsNonRoot: true
readinessProbe:
exec:
command:
- pg_isready
- -U
- postgres
initialDelaySeconds: 5
periodSeconds: 10
livenessProbe:
exec:
command:
- pg_isready
- -U
- postgres
initialDelaySeconds: 15
periodSeconds: 20
volumes:
- name: postgres-data
persistentVolumeClaim:
claimName: postgres-data
- name: postgres-config
configMap:
name: postgres-config
volumeClaimTemplates:
- metadata:
name: postgres-data
spec:
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 10Gi