Files
hapi/infra/k8s/k8s-resources.tf
T
Bobby 88235fc824 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.
2026-11-18 15:29:00 -05:00

225 lines
4.4 KiB
Terraform

# PostgreSQL StatefulSet for HAPI
# Provides production-ready PostgreSQL with persistent storage
resource "kubernetes_stateful_set_v1" "postgres" {
metadata {
name = "postgres-statefulset"
namespace = "postgres"
labels = {
app = "postgres"
environment = var.environment
}
}
spec {
service_name = "postgres-service"
replicas = 1
selector {
match_labels = {
app = "postgres"
environment = var.environment
}
}
template {
metadata {
labels = {
app = "postgres"
environment = var.environment
}
}
spec {
container {
name = "postgres"
image = var.postgres_image
port {
container_port = 5432
name = "postgres"
}
env {
name = "POSTGRES_USER"
value_from {
secret_key_ref {
name = "postgres-secrets"
key = "username"
}
}
}
env {
name = "POSTGRES_PASSWORD"
value_from {
secret_key_ref {
name = "postgres-secrets"
key = "password"
}
}
}
env {
name = "POSTGRES_DB"
value = var.postgres_db
}
env {
name = "PGDATA"
value = "/var/lib/postgresql/data/pgdata"
}
volume_mount {
name = "postgres-data"
mount_path = "/var/lib/postgresql/data"
}
volume_mount {
name = "postgres-config"
mount_path = "/etc/postgresql/postgresql.conf"
sub_path = "postgresql.conf"
}
resources {
requests = {
cpu = var.cpu_request
memory = var.memory_request
}
limits = {
cpu = var.cpu_limit
memory = var.memory_limit
}
}
readiness_probe {
exec {
command = [
"pg_isready",
"-U",
"postgres",
"-d",
var.postgres_db,
]
}
initial_delay_seconds = 30
period_seconds = 10
}
liveness_probe {
exec {
command = [
"pg_isready",
"-U",
"postgres",
"-d",
var.postgres_db,
]
}
initial_delay_seconds = 60
period_seconds = 10
}
security_context {
run_as_user = 999
run_as_group = 999
fs_group = 999
}
}
volume {
name = "postgres-data"
persistent_volume_claim {
claim_name = "postgres-data"
}
}
volume {
name = "postgres-config"
config_map {
name = "postgres-config"
}
}
}
}
volume_claim_template {
metadata {
name = "postgres-data"
}
spec {
access_modes = ["ReadWriteOnce"]
storage_class_name = "linode-block-storage"
resources {
requests = {
storage = var.storage_size
}
}
}
}
}
pod_management_policy = "Parallel"
}
# PostgreSQL Service
resource "kubernetes_service_v1" "postgres" {
metadata {
name = "postgres-service"
namespace = "postgres"
labels = {
app = "postgres"
environment = var.environment
}
}
spec {
selector = {
app = "postgres"
environment = var.environment
}
port {
name = "postgres"
port = 5432
target_port = 5432
}
type = "ClusterIP"
}
}
# PersistentVolumeClaim for PostgreSQL
resource "kubernetes_persistent_volume_claim_v1" "postgres" {
metadata {
name = "postgres-pvc"
namespace = "postgres"
labels = {
app = "postgres"
environment = var.environment
}
}
spec {
access_modes = ["ReadWriteOnce"]
storage_class_name = "linode-block-storage"
resources {
requests = {
storage = var.storage_size
}
}
}
}