Database Layer (CloudNativePG and DragonflyDB)

Relevant source files

The database layer provides the stateful foundation for the home-ops cluster, primarily utilizing the CloudNativePG (CNPG) operator for PostgreSQL and the DragonflyDB operator for Redis-compatible caching. This layer is designed for high availability, automated backups, and Point-In-Time Recovery (PITR) capabilities.

CloudNativePG (PostgreSQL)

The cluster manages PostgreSQL through the CloudNativePG operator, which handles the lifecycle of the database including provisioning, master election, and replication kubernetes/apps/database/cloudnative-pg/app/helmrelease.yaml19-45

Cluster Configuration and Recovery

The primary database cluster, named postgres, is configured as a single-instance setup for resource efficiency while maintaining enterprise-grade backup features kubernetes/apps/database/cloudnative-pg/cluster/cluster.yaml4-11 It uses local-hostpath storage for high performance kubernetes/apps/database/cloudnative-pg/cluster/cluster.yaml19

A critical feature is the Point-In-Time Recovery (PITR) and migration support. The configuration includes a bootstrap section that allows the cluster to recover from a previous cluster (e.g., postgres-v11) by targeting a specific timestamp kubernetes/apps/database/cloudnative-pg/cluster/cluster.yaml35-39

Backup and WAL Archiving

Data durability is ensured through a multi-tier backup strategy:

  1. WAL Archiving (Garage S3): Write-Ahead Logs (WAL) are continuously archived to a Garage S3 bucket (s3://postgresql/) using bzip2 compression kubernetes/apps/database/cloudnative-pg/cluster/cluster.yaml42-49
  2. Object Store Backups: Full backups are stored in the same S3 destination with a 30-day retention policy kubernetes/apps/database/cloudnative-pg/cluster/cluster.yaml40-41
  3. Local pg_dump Backups: A secondary cronjob (managed via app-template) performs daily pg_dump operations for specific databases like sure and authentikkubernetes/apps/database/cloudnative-pg/backup/helmrelease.yaml23-55 These are stored on an NFS mount at /tank/Backup/Databasekubernetes/apps/database/cloudnative-pg/backup/helmrelease.yaml61-64

Database Data Flow and Backup Topology

The following diagram illustrates the relationship between the PostgreSQL cluster, the S3 storage for WAL/PITR, and the NFS storage for logical dumps.

PostgreSQL Backup and Recovery Architecture

[Flowchart Diagram]

Sources: kubernetes/apps/database/cloudnative-pg/cluster/cluster.yaml40-65kubernetes/apps/database/cloudnative-pg/backup/helmrelease.yaml23-64

DragonflyDB (Redis-Compatible)

DragonflyDB serves as a high-performance, Redis-compatible multi-threaded data store. It is managed by the dragonfly-operatorkubernetes/apps/database/dragonfly-operator/app/helmrelease.yaml17-20

The operator is deployed via an OCI repository kubernetes/apps/database/dragonfly-operator/app/helmrelease.yaml1-13 It includes built-in monitoring integration, deploying a Grafana dashboard into the database folder kubernetes/apps/database/dragonfly-operator/app/helmrelease.yaml36-38 DragonflyDB is primarily utilized by services like Authentik for session caching and high-speed state management.

Sources: kubernetes/apps/database/dragonfly-operator/app/helmrelease.yaml1-39kubernetes/apps/security/kustomization.yaml10

Mosquitto MQTT Broker

The mosquitto broker is deployed in the database namespace to support the Home Automation stack (e.g., Home Assistant, Frigate) kubernetes/apps/database/kustomization.yaml11 It provides the messaging backbone for IoT device communication across different VLANs kubernetes/apps/home-automation/kustomization.yaml11-12

Sources: kubernetes/apps/database/kustomization.yaml11kubernetes/apps/home-automation/kustomization.yaml11-12

Monitoring and Observability

The database layer is deeply integrated with the observability stack:

ComponentMonitoring ImplementationSource
PostgreSQLpodMonitorEnabled in operator and custom PodMonitor relabeling for cnpg_cluster labels.kubernetes/apps/database/cloudnative-pg/cluster/cluster.yaml27-34
DragonflyDBgrafanaDashboard enabled in operator values.kubernetes/apps/database/dragonfly-operator/app/helmrelease.yaml36-38
AlertingPrometheusRule resources define alerts for database health.kubernetes/apps/database/cloudnative-pg/cluster/prometheusrule.yaml

Code Entity Mapping

This diagram bridges the natural language concepts of the database layer to the specific Kubernetes resources and files defined in the repository.

Database Layer Entity Map

[Flowchart Diagram]

Sources: kubernetes/apps/database/cloudnative-pg/cluster/cluster.yaml1-5kubernetes/apps/database/cloudnative-pg/app/helmrelease.yaml15-19kubernetes/apps/database/dragonfly-operator/app/helmrelease.yaml15-19kubernetes/apps/database/cloudnative-pg/backup/helmrelease.yaml1-5