postgres-deployment.yaml#

# -----------------------------------------------------------------------------
# Title:       PostgreSQL Database Deployment Manifests
# Purpose:     Defines Kubernetes resources for PostgreSQL database in MLOps platform
# Owner:       MLOps Weather Forecasting Team
# Source:      k8s/postgres-deployment.yaml
# Last-Reviewed: 2025-08-24
# Depends-On:  postgres-secrets (external Secret), storage-class (default)
# Change-Log:
#   2024-01-15: Initial creation for demo environment
#   2025-08-24: Updated comments and documentation structure
# Links:
#   - https://kubernetes.io/docs/concepts/workloads/controllers/deployment/
#   - https://www.postgresql.org/docs/15/index.html
# -----------------------------------------------------------------------------
# PostgreSQL database deployment for MLOps weather forecasting platform.
#
# WHY: PostgreSQL serves as the central data store for weather observations,
# ML training status, coordinates, and training logs. Ensures data persistence
# and reliability for the distributed system architecture.
#
# Architecture Context: Single source of truth for all weather data and ML
# training state. Critical for maintaining data consistency across FastAPI
# backend and Slurm training pipeline components.
#
# Resources defined:
# - PersistentVolumeClaim: Ensures data persistence across pod lifecycle
# - Deployment: Runs PostgreSQL 15 with proper configuration and secret injection
# - Service: Provides internal cluster networking for database connections
#
# Security Note: Database credentials are externalized via 'postgres-secrets'
# to prevent credential leakage in version control systems.
---
# SECTION: Data Persistence Layer
# WHY: Database state must survive pod restarts and rescheduling for data integrity.
# Ensures weather data, ML training status, and coordinates persist across deployments.
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: postgres-pvc  # Referenced by postgres-storage volume in deployment
spec:
  accessModes:
    - ReadWriteOnce  # Single pod access for data consistency and safety
  resources:
    requests:
      storage: 1Gi  # Sufficient for development/demo workloads (scale up for production)
---
# SECTION: Database Service Deployment
# WHY: Runs PostgreSQL 15 with optimized configuration for weather data storage.
# Ensures consistent data handling for time-series weather observations and ML state.
apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
  # Deployment manages pod lifecycle and updates for the database service
spec:
  selector:
    matchLabels:
      app: postgres  # Selector must match template labels for pod targeting
  replicas: 1  # Single replica for development/demo - scale up for production
  template:
    metadata:
      labels:
        app: postgres  # Service selector target
    spec:
      containers:
        - name: postgres
          image: postgres:15  # PostgreSQL 15 for reliable performance
          ports:
            - containerPort: 5432  # Default PostgreSQL port
          envFrom:
            - secretRef:
                name: postgres-secrets  # External secret containing DB credentials
          volumeMounts:
            - name: postgres-storage
              mountPath: /var/lib/postgresql/data  # PostgreSQL data directory
      volumes:
        - name: postgres-storage
          persistentVolumeClaim:
            claimName: postgres-pvc  # References the PVC defined above
---
# SECTION: Database Service Networking
# WHY: Provides stable network endpoint for database connectivity within cluster.
# Enables consistent service discovery for FastAPI backend and other components.
apiVersion: v1
kind: Service
metadata:
  name: postgres
  # Service name becomes DNS resolvable within the cluster namespace
spec:
  type: ClusterIP  # Internal-only access - suitable for demo/development
  selector:
    app: postgres  # Routes traffic to pods with matching labels
  ports:
    - port: 5432  # Service port (accessible within cluster)
      targetPort: 5432  # Maps to container port defined in deployment
      protocol: TCP  # Explicit TCP protocol for clarity