Two problems in the same database namespace, found one after the other because fixing the first led me to the second. All of it runs on a BMAX Mini PC* with the MikroTik RB5009* handling the network between the cluster and everything else.
The CNPG (CloudNativePG) operator had restarted 310 times in 21 days. Meanwhile, its PodMonitor, the thing that was supposed to scrape PostgreSQL metrics into Prometheus, had never collected a single data point. Both issues were live and silent, and both traced back to a mismatch between what the operator assumed about the cluster and what the cluster actually required.
View the complete homelab infrastructure source on GitHub
The PodMonitor That Wasn’t Scraping
CNPG has a built-in enablePodMonitor: true field in its Cluster CRD. Set it to true and the operator generates a PodMonitor, which Prometheus should then pick up. In theory.
In practice, the operator-generated PodMonitor only carries the cnpg.io/cluster label. My kube-prometheus-stack Prometheus instance has a podMonitorSelector that requires release: kube-prometheus-stack on every PodMonitor it watches. Without that label, Prometheus ignores the object entirely.
The PodMonitor showed as Synced and Healthy in ArgoCD. CNPG reported it as created. But kubectl get podmonitor -n database showed the operator-generated one had no release label, and kubectl get prometheus -o jsonpath='{.spec.podMonitorSelector}' confirmed the selector required it. The metrics endpoint on :9187 was never scraped.
No CNPG CRD field adds metadata labels to the auto-generated PodMonitor. podMonitorRelabelings only rewrites samples after they’re scraped; it can’t make Prometheus notice the object in the first place. The fix was to disable the operator’s own PodMonitor and replace it with a git-managed one:
# kubernetes/system/postgres/cluster.yml
monitoring:
enablePodMonitor: false
# kubernetes/system/postgres/postgres-monitoring.yml
apiVersion: monitoring.coreos.com/v1
kind: PodMonitor
metadata:
name: postgres-authelia-metrics
namespace: database
labels:
cnpg.io/cluster: postgres-authelia
release: kube-prometheus-stack
spec:
namespaceSelector:
matchNames:
- database
selector:
matchLabels:
cnpg.io/cluster: postgres-authelia
podMetricsEndpoints:
- port: metrics
It carries both labels, so this is the one Prometheus selects, and the operator’s own version stays disabled. The name must not match the Cluster’s name: with enablePodMonitor: false, the CNPG reconciler deletes any PodMonitor named after the Cluster (postgres-authelia) on every reconcile, whoever created it, which left this Application in a sync loop until the object was renamed. Every other monitoring resource in this cluster follows the same rule: if Prometheus needs to see it, manage the label yourself instead of relying on the operator to guess what your selector requires.
The new file also had to be added to the ArgoCD Application’s directory.include pattern. The postgres Application only syncs explicitly listed YAML files, not everything in the directory, so without this the PodMonitor exists in git but is never applied:
include: '{cluster,external-secret,scheduled-backup,backup-config,network-policies,postgres-monitoring}.yml'
310 Restarts: Leader-Election Timeout
The PodMonitor investigation is what led me to the operator restart count. While checking the monitoring gap, I noticed kubectl get pods -n cnpg-system showed the operator had restarted hundreds of times.
Every restart’s previous-container log ended the same way:
context deadline exceeded
The failing call was a Lease PUT: the leader-election renewal. The CNPG operator uses controller-runtime’s standard leader-election mechanism: a single replica holds a Lease object, and when it can’t renew that Lease within the deadline, controller-runtime terminates the process so another instance can take over. With a single replica, there’s no other instance. The process dies, Kubernetes restarts it, it acquires leadership again, and the cycle repeats.
The defaults are --leader-lease-duration=15s and --leader-renew-deadline=10s. On a host with documented I/O fragility (the same one that caused the etcd cascading failure I wrote about earlier), a brief apiserver latency blip pushes the renewal past the 10-second deadline. Controller-runtime kills the process. Kubernetes restarts it. The operator is healthy for hours, then one blip kills it again.
The fix was the same approach I already applied to etcd’s timeouts in Ansible: scale the defaults up, here by 4x for the lease and 5x for the renew deadline.
# kubernetes/system/cloudnative-pg/application.yml
spec:
source:
helm:
values: |
additionalArgs:
- --leader-lease-duration=60
- --leader-renew-deadline=50
I confirmed both flags exist via the live v1.25.0 binary’s --help output before applying. Same discipline as with every Terraform resource: check what the running version supports instead of trusting documentation that might be version-skewed.
After applying, restarts dropped to zero. A 50-second renewal deadline is generous enough to ride out the I/O spikes this host reliably produces.
The Pattern
Both issues come from defaults that didn’t match this cluster.
The PodMonitor setup assumed the operator would add the right labels. The operator assumed Prometheus would select any PodMonitor with the cluster label. Neither was true, and the failure was silent: the PodMonitor existed, looked healthy, and did nothing.
The leader election assumed a 10-second renewal deadline was enough. On a host that regularly produces I/O latency spikes, it wasn’t. The operator died and restarted, invisibly unless you checked the restart count.
Both fixes replace a default with explicit configuration: a git-managed PodMonitor with the right labels, and leader-election timeouts tuned to this hardware’s failure mode.
One scoping note: the leader-election tuning applies to self-hosted control planes. The renewal is a call to the API server, and here the API server shares the same hardware as the failing disk. On a managed control plane like AKS, EKS, or GKE, the API server lives off the node, so local disk I/O can’t delay the renewal. Raising the renew deadline there buys nothing. It only adds up to 50 seconds before another instance can take over during a real leader-election failure. Keep the default deadlines, or apply this fix only where the control plane runs on the hardware you’re tuning for.