Files
hapi/docs/adr/0003-kubernetes-infrastructure.md
T
2026-11-18 15:33:26 -05:00

4.3 KiB

ADR-003: Kubernetes Infrastructure for PostgreSQL

Date: 2026-11-18
Status: Proposed
Author: Bobby
Related Issues: #2 (Kubernetes StatefulSet for Production PostgreSQL)

Context

We need a production-grade PostgreSQL deployment on Kubernetes that:

  1. Provides persistent storage for database state
  2. Enables learnable infrastructure with stateful workload patterns
  3. Integrates with existing Linode Terraform infrastructure
  4. Supports both staging and production environments

Decision

We will deploy PostgreSQL using a Kubernetes StatefulSet with the following components:

Components

Component File Purpose
StatefulSet k8s/statefulset.yaml Manages PostgreSQL pod with stable identity and persistent storage
PVC k8s/pvc.yaml Provides 50Gi of block storage for PostgreSQL data
Service k8s/service.yaml Exposes PostgreSQL within the cluster
Kustomize k8s/kustomization.yaml Enables environment-specific customization

Design Choices

1. StatefulSet over Deployment

Why StatefulSet:

  • Stable network identity (postgres-0, postgres-1, etc.)
  • Ordered, graceful deployment and scaling
  • Persistent storage per pod (via VolumeClaimTemplates)
  • Better for stateful workloads like databases

2. Linode Block Storage

Why Linode Block Storage:

  • Integrated with LKE clusters
  • Supports ReadWriteOnce for single-pod access
  • Performance suitable for production workloads
  • Already available in our Linode account

3. PostgreSQL 16.3

Why PostgreSQL 16.3:

  • Latest stable major version
  • Good balance of features and stability
  • Well-supported by Kubernetes ecosystem
  • Compatible with our existing database schema

4. Kustomize for Customization

Why Kustomize:

  • Native Kubernetes tool (no additional dependencies)
  • Simple overlay-based customization
  • Works well with environment-specific configurations
  • Easy to version control and review

Consequences

Positive

  • Learnable Infrastructure: StatefulSet provides clear patterns for stateful workloads
  • Persistent Storage: Block storage ensures data survives pod restarts
  • Environment Support: Kustomize enables staging/production separation
  • Extensibility: Easy to add read replicas, monitoring, etc.

Negative

  • Complexity: More components than managed PostgreSQL services
  • Maintenance: We manage backups, updates, and scaling
  • Resource Overhead: Kubernetes overhead for cluster management

Alternatives Considered

1. Linode LKE Managed PostgreSQL

Pros:

  • Fully managed, automatic backups
  • Less operational overhead

Cons:

  • Less "learnable" infrastructure
  • Higher cost
  • Less control over configuration

2. PostgreSQL on Linode VM

Pros:

  • Full control over configuration
  • No Kubernetes overhead

Cons:

  • Manual scaling and high availability
  • More complex backup strategy
  • Less integrated with existing infrastructure

3. External Database Service (AWS RDS, etc.)

Pros:

  • Fully managed
  • Enterprise-grade features

Cons:

  • Vendor lock-in
  • Additional cost
  • Less integrated with Linode infrastructure

Implementation

Files Created

infra/k8s/
├── kustomization.yaml    # Kustomize configuration
├── Makefile              # Convenient deployment commands
├── pvc.yaml              # PersistentVolumeClaim
├── README.md             # Documentation
├── service.yaml          # Service definition
├── statefulset.yaml      # StatefulSet definition
└── terraform.tfvars.example  # Example Terraform variables

Deployment Steps

# Set environment
export ENVIRONMENT=staging

# Apply configuration
kubectl apply -k .

# Verify deployment
kubectl get statefulset,service,pvc -l app=postgres

Next Steps

  1. Deploy to staging cluster and verify functionality
  2. Configure backup to Object Storage
  3. Set up monitoring with Prometheus
  4. Create Terraform module for Kubernetes infrastructure
  5. Deploy to production and validate

References