Script icon
Docker icon
If icon
SlackIncomingWebhook icon
Log icon
Return icon
Schedule icon

CloudNative-PG WAL Archive Status and Backup Stall Sentinel

Audits CloudNative-PG PostgreSQL operator metrics to detect WAL archiving failures, streaming replication lag, and backup execution stalls.

Categories
CloudInfrastructure

Automates continuous health and data durability surveillance for CloudNative-PG Kubernetes PostgreSQL database clusters.

How it works

  1. A schedule trigger executes the CloudNative-PG health audit workflow every 15 minutes.
  2. The Python task queries the CloudNative-PG instance Prometheus metrics endpoint on port 9187.
  3. The script extracts WAL archiving status flags, archive failure event counts, streaming replication lag, and backup health.
  4. An evaluation gate determines whether WAL segment upload stalls, replication lag spikes, or backup failures exceed limits.
  5. When database durability degradation is detected, a high-priority Slack incident alert is dispatched to Database SREs.
  6. When PostgreSQL streams write-ahead logs cleanly to S3/MinIO and replicas stay synchronized, nominal telemetry is logged.
  7. A structured JSON audit manifest is emitted for database disaster recovery compliance and durability governance archives.

Architecture diagram

graph TD
    Trigger[Schedule Trigger: 15m] --> ScanMetrics[Python Script: Scrape CloudNative-PG Metrics]
    ScanMetrics --> EvalBreach{WAL Status == 0 OR Failures > 2 OR Lag > 60s?}
    EvalBreach -- Yes --> SlackAlert[Slack: High-Urgency DBA Alert]
    EvalBreach -- No --> LogNominal[Log: Nominal Postgres State]
    SlackAlert --> EmitManifest[Debug Return: Database Audit Manifest]
    LogNominal --> EmitManifest

Architecture components

  • Trigger: Automated schedule polling at 15-minute recurring intervals.
  • Script Task: Scrapes CloudNative-PG Prometheus metrics endpoints using Docker container runners.
  • Decision Gate: Evaluates whether WAL archiving drops to failing state or replication lag breaches bounds.
  • Notification Task: Dispatches structured Slack alerts with cluster name and database replication metrics.
  • Audit Manifest: Emits JSON execution payloads containing PostgreSQL durability health and timestamps.

Key capabilities

  • Continuous surveillance of CloudNative-PG Cluster CRDs, barman-cloud WAL archiving, and physical streaming replicas.
  • Early warning detection for S3 object storage authentication drops that cause local WAL disk space exhaustion.
  • Automated tracking of replica lag spikes that threaten point-in-time recovery (PITR) Recovery Point Objectives (RPO).
  • Zero host dependency execution via isolated containerized Python runners.

What you get

  • Real-time visibility into Kubernetes-native PostgreSQL database durability and backup execution integrity.
  • Proactive alerts preventing cluster failover data loss and storage disk volume saturation.
  • Standardized audit manifests containing exact replication lag measurements, archive statuses, and timestamps.
  • Zero-overhead deployment compatible with CloudNative-PG Barman object store backups and volume snapshots.

Who it's for

  • Database Administrators (DBAs) managing mission-critical enterprise PostgreSQL workloads on Kubernetes.
  • Site Reliability Engineers safeguarding transaction durability and recovery point objectives (RPO).
  • Platform engineers designing resilient cloud-native stateful storage architectures.
  • Compliance officers auditing continuous backup verification and database disaster recovery readiness.

Why orchestrate this with Kestra

  • Serverless Docker Isolation: Executes telemetry scraping in ephemeral Docker environments without persistent workers.
  • Centralized Secrets Governance: Protects sensitive Slack webhooks using encrypted namespace secrets.
  • Declarative YAML Workflows: Manages database durability surveillance as versioned code in Git repositories.
  • Comprehensive Audit History: Preserves historical database durability metrics in searchable Kestra execution logs.

Use cases

  • S3 Archive Destination Outage: Alerts when IAM permission changes block Write-Ahead Log upload to cloud storage.
  • Standby Replica Dropout Alarm: Detects network partitions between primary database pods and read-only standbys.
  • Scheduled Nightly Backup Stall: Pinpoints when base backup snapshot scripts hang or hit volume timeout limits.

Inputs

Name Type Default Description
cnpg_metrics_url STRING http://cluster-pg-metrics.database.svc.cluster.local:9187 Internal HTTP URL exposing Prometheus metrics for CloudNative-PG.
cluster_name STRING production-orders-db Identifier of the target CloudNative-PG PostgreSQL cluster.
max_replication_lag_seconds INT 60 Maximum allowable physical streaming replication lag in seconds.
max_wal_archive_failures_threshold INT 2 Maximum allowable Write-Ahead Log archiving failures.
alert_on_backup_failure BOOL true Toggle whether backup execution failure triggers an incident alert.

Expected outputs

  • {{ outputs.scan_cnpg_telemetry.vars.wal_archiving_status }}: Status of Write-Ahead Log archiving (1 for active, 0 for failing).
  • {{ outputs.scan_cnpg_telemetry.vars.wal_archive_failures_total }}: Total count of WAL archive transfer failures.
  • {{ outputs.scan_cnpg_telemetry.vars.replication_lag_seconds }}: Maximum observed physical streaming replication lag.
  • {{ outputs.scan_cnpg_telemetry.vars.is_alerting }}: Boolean flag indicating if database durability SLA is breached.
  • {{ outputs.scan_cnpg_telemetry.vars.audit_timestamp }}: ISO-8601 UTC timestamp of the CloudNative-PG audit run.

Prerequisites

  • CloudNative-PG operator deployed on Kubernetes with Prometheus metrics enabled on port 9187.
  • Network connectivity from the Kestra worker to the PostgreSQL metrics service endpoint.
  • Configured Slack incoming webhook URL with permissions to post in database alert channels.

Secrets

  • SLACK_WEBHOOK_URL: Secure webhook URL for routing database alerts to the DBA Slack channel.

Quick start

  1. Verify that CloudNative-PG exposes Prometheus metrics on HTTP port 9187.
  2. Add the SLACK_WEBHOOK_URL secret within your Kestra namespace settings.
  3. Specify your PostgreSQL cluster identifier in cluster_name.
  4. Run a manual flow execution via the Kestra UI to validate metric ingestion.
  5. Enable the periodic_cnpg_audit schedule trigger to establish automated monitoring.

Common pitfalls and troubleshooting

  • WAL Disk Full Panic: When WAL archiving fails, PostgreSQL retains WAL files locally until disk fills, halting writes.
  • S3 Bucket Endpoint DNS: Ensure Postgres pods can resolve external S3 or MinIO bucket endpoints from inside the cluster.
  • Replication Slot Invalidation: Replicas that fall too far behind may drop their replication slot, requiring resync.
  • Barman Cloud Credentials: Verify that the Kubernetes Secret containing AWS credentials or MinIO keys is mounted.

How to extend

  • Automated Backup Trigger: Trigger a manual on-demand backup via Kestra if the last scheduled run is older than 24 hours.
  • PagerDuty Escalation: Escalate to on-call DBAs immediately if WAL archiving remains failed for more than 15 minutes.
  • Multi-Instance Fleet Audit: Expand script parameters to monitor all PostgreSQL clusters across enterprise namespaces.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.