cloudnative-pg

by Arktec Quant

Helm chartv1.0.0App 0.5.0Verified publisher

Deploys and manages a CloudNativePG cluster and its associated resources. This chart fixes PVC/node coupling issues by adding PVC protection annotations to ensure PVCs survive node deletions.

About this chart

CloudNativePG Helm Chart

⚠️ Why This Chart Exists
This chart fixes two critical problems in the original CloudNativePG chart that cause data loss:

Problem 1: PVCs Deleted When Cluster is Deleted
When you delete the Helm release or Cluster CRD, Kubernetes/Helm deletes the PVCs along with the cluster, causing permanent data loss. This chart adds PVC protection annotations to prevent this.

Problem 2: PVCs Tightly Coupled to Nodes
When using node-local storage classes (like local-path or local-storage), PVCs are bound to specific nodes. If those nodes are deleted, the PVCs and all data are permanently lost, even if the cluster still exists.

✅ The Solution
This chart adds PVC protection annotations via pvcTemplate in the Cluster CRD to ensure PVCs survive both cluster deletions and node deletions. PVCs can now be safely reattached to new clusters or different nodes.

A production-grade Helm chart for CloudNativePG that addresses critical data loss issues. This chart is based on the official CloudNativePG cluster chart (version 0.5.0) with fixes to ensure PVCs survive both cluster deletions and node deletions.

Problem Statement

The original CloudNativePG chart has two critical problems that cause data loss:

Problem 1: PVCs Deleted When Cluster is Deleted

The Issue:

  • When you run helm delete or delete the Cluster CRD, Kubernetes/Helm automatically deletes all associated PVCs
  • This causes permanent data loss - you cannot recover your database
  • Even if you recreate the cluster, the data is gone forever

Why It Happens:

  • The original chart doesn't include PVC protection annotations
  • Kubernetes' default behavior is to delete PVCs when their owner (the Cluster CRD) is deleted
  • Helm follows the same pattern - deleting the release deletes all resources including PVCs

Problem 2: PVCs Tightly Coupled to Nodes

The Issue:

  • When using node-local storage classes (like local-path, local-storage, hostpath), PVCs are bound to specific nodes
  • If a node is deleted, the PVCs on that node are permanently lost
  • Even if the cluster is still running, data on deleted nodes cannot be recovered

Why It Happens:

  • Node-local storage classes create PersistentVolumes that are physically tied to specific nodes
  • When a node is deleted, the PV and all data on it are lost
  • The PVC cannot be reattached to a different node because the storage only exists on the deleted node

Solution

This chart fixes these issues by:

  1. PVC Protection Annotations: Adds pvcTemplate with protection annotations to the Cluster CRD's storage and walStorage sections
  2. Resource Policy: Prevents Helm, ArgoCD, and Flux from deleting PVCs
  3. Storage Class Flexibility: Works with any storage class, but recommends distributed storage classes

Key Changes from Original Chart

The chart adds pvcTemplate sections to the storage configuration:

storage:
  size: 8Gi
  storageClass: ""
  pvcAnnotations:
    helm.sh/resource-policy: keep
    argocd.argoproj.io/sync-options: Prune=false,Delete=false
    resources.fluxcd.io/ignore: "true"
    argocd.argoproj.io/health-check-skip: "true"

These annotations are automatically applied to all PVCs created by the Cluster CRD.

Installation

helm repo add arktec-quant https://charts.arktecquant.com
helm install my-postgres arktec-quant/cloudnative-pg

Configuration

Storage Configuration

cluster:
  storage:
    size: 20Gi
    storageClass: "standard-rwo"  # Use distributed storage class
    pvcAnnotations:
      helm.sh/resource-policy: keep
      argocd.argoproj.io/sync-options: Prune=false,Delete=false
      resources.fluxcd.io/ignore: "true"
      argocd.argoproj.io/health-check-skip: "true"
  
  walStorage:
    enabled: true
    size: 10Gi
    storageClass: "standard-rwo"
    pvcAnnotations:
      helm.sh/resource-policy: keep
      argocd.argoproj.io/sync-options: Prune=false,Delete=false
      resources.fluxcd.io/ignore: "true"
      argocd.argoproj.io/health-check-skip: "true"

Disabling PVC Protection

To disable PVC protection (not recommended):

cluster:
  storage:
    pvcAnnotations: {}  # Empty object disables protection

How It Works

The chart uses CloudNativePG's pvcTemplate feature in the Cluster CRD. When you specify pvcAnnotations in the values, the chart automatically adds a pvcTemplate section to the storage configuration:

spec:
  storage:
    size: 8Gi
    storageClass: "standard-rwo"
    pvcTemplate:
      metadata:
        annotations:
          helm.sh/resource-policy: keep
          argocd.argoproj.io/sync-options: Prune=false,Delete=false
          resources.fluxcd.io/ignore: "true"
          argocd.argoproj.io/health-check-skip: "true"

These annotations ensure that:

  • Helm won't delete PVCs when the release is deleted
  • ArgoCD won't prune or delete PVCs during sync
  • Flux won't manage PVCs
  • Health checks skip PVCs (they may be in Pending state)

Recovering from Existing PVCs

Good News: CloudNativePG automatically reuses existing PVCs when you recreate a cluster with the same name! The pod names follow the pattern <cluster-name>-<instance-number>, and CloudNativePG will automatically attach pods to existing PVCs with matching names.

How Automatic PVC Reuse Works

CloudNativePG uses a deterministic naming convention:

  • Pods: <cluster-name>-1, <cluster-name>-2, <cluster-name>-3, etc.
  • PVCs: <cluster-name>-1, <cluster-name>-2, <cluster-name>-3, etc.
  • WAL PVCs: <cluster-name>-1-wal, <cluster-name>-2-wal, etc.

When you recreate a cluster:

  1. CloudNativePG creates pods with the same names (based on cluster name + instance number)
  2. If PVCs with those exact names already exist, CloudNativePG automatically attaches them
  3. The pods will use the existing data - no initialization needed!

Automatic Recovery Process

Step 1: Delete the cluster (PVCs remain due to protection annotations):

helm delete my-postgres
# Or: kubectl delete cluster my-postgres

Step 2: Verify PVCs still exist:

kubectl get pvc -n <namespace>
# You should see: my-postgres-1, my-postgres-2, my-postgres-3, etc.

Step 3: Clear PV claimRefs (required to allow PVC rebinding):

# Get list of PVs bound to your PVCs
kubectl get pv | grep <cluster-name>

# For each PV, clear the claimRef to allow rebinding
kubectl patch pv <pv-name> -p '{"spec":{"claimRef":null}}'

Step 4: Recreate cluster with EXACT same name and instance count:

helm install my-postgres arktec-quant/cloudnative-pg \
  --set cluster.instances=3 \
  --set cluster.storage.size=8Gi \
  --set cluster.storage.storageClass=standard-rwo

Step 5: CloudNativePG automatically attaches existing PVCs:

  • Pods will be created with names: my-postgres-1, my-postgres-2, my-postgres-3
  • These pods will automatically attach to existing PVCs: my-postgres-1, my-postgres-2, my-postgres-3
  • No initialization will occur - your data will be preserved!

Critical Requirements for Automatic Reuse

✅ Same cluster name - Must be exactly the same (e.g., my-postgres)
✅ Same namespace - PVCs must be in the same namespace
✅ Same instance count - Must match the original (e.g., if you had 3 instances, recreate with 3)
✅ PV claimRef cleared - PersistentVolumes must have claimRef: null
✅ Same storage class - Should match original (though not always required)
✅ PVCs exist - PVCs must still exist (protected by our annotations)

Example: Complete Recovery

# 1. Original cluster
helm install my-postgres arktec-quant/cloudnative-pg \
  --set cluster.instances=3

# 2. Cluster gets deleted (accidentally or intentionally)
helm delete my-postgres
# PVCs remain: my-postgres-1, my-postgres-2, my-postgres-3

# 3. Clear PV claimRefs
kubectl get pv -o name | xargs -I {} kubectl patch {} -p '{"spec":{"claimRef":null}}'

# 4. Recreate with SAME name and instance count
helm install my-postgres arktec-quant/cloudnative-pg \
  --set cluster.instances=3 \
  --set cluster.storage.size=8Gi

# 5. CloudNativePG automatically reuses PVCs!
# Pods my-postgres-1, my-postgres-2, my-postgres-3 attach to existing PVCs
# Your data is preserved!

Troubleshooting PVC Reuse

If pods are not attaching to existing PVCs:

  1. Check pod names match PVC names:

    kubectl get pods
    kubectl get pvc
    # Pod names should exactly match PVC names (without -wal suffix)
    
  2. Verify PVCs are in Released/Available state:

    kubectl get pvc
    # Status should be "Released" or "Available", not "Bound"
    
  3. Check PV claimRef is cleared:

    kubectl get pv <pv-name> -o yaml | grep claimRef
    # Should show: claimRef: null or no claimRef field
    
  4. Ensure same instance count:

    # If original had 3 instances, new cluster must also have 3
    kubectl get cluster <name> -o jsonpath='{.spec.instances}'
    

Recovery Mode (Alternative)

If automatic reuse doesn't work or you prefer a safer approach, use CloudNativePG's recovery mode:

mode: recovery
recovery:
  method: backup  # or object_store, pg_basebackup
  backupName: <backup-name>

This creates a new cluster from a backup, which is safer but requires backups to be configured.

Storage Class Recommendations

Recommended (Distributed Storage):

  • standard-rwo (most cloud providers)
  • longhorn (Kubernetes-native)
  • ceph-rbd (Ceph)
  • nfs-client (NFS)
  • Any storage class that supports cross-node attachment

Avoid (Node-Local Storage):

  • local-path (binds to specific nodes)
  • local-storage (binds to specific nodes)
  • hostpath (binds to specific nodes)

Troubleshooting

PVCs Still Being Deleted

If PVCs are still being deleted:

  1. Verify pvcAnnotations are set in values.yaml
  2. Check that the Cluster CRD has pvcTemplate section: kubectl get cluster <name> -o yaml
  3. Verify annotations are on PVCs: kubectl get pvc -o yaml

PVCs Not Moving Between Nodes

If PVCs cannot move between nodes:

  1. Check storage class supports cross-node attachment
  2. Verify storage class is not node-local
  3. Ensure volume binding mode allows cross-node attachment

Cluster Not Reusing Existing PVCs

If the recreated cluster is not reusing existing PVCs:

  1. Verify cluster name matches exactly: The new cluster must have the exact same name as the original

    # Check original cluster name
    kubectl get pvc --show-labels | grep cnpg.io/cluster
    # New cluster name must match
    
  2. Check instance count matches: Must have the same number of instances

    # Count existing PVCs (excluding -wal)
    kubectl get pvc | grep -v -- "-wal" | wc -l
    # New cluster instances must match
    
  3. Verify namespace matches: PVCs must be in the same namespace

    kubectl get pvc -n <namespace>
    
  4. Clear PV claimRefs: Run for each PV:

    kubectl patch pv <pv-name> -p '{"spec":{"claimRef":null}}'
    
  5. Check PVC names match pod names:

    • Pods: <cluster-name>-1, <cluster-name>-2, etc.
    • PVCs: <cluster-name>-1, <cluster-name>-2, etc.
    • They must match exactly!
  6. Verify PVCs are in Released state:

    kubectl get pvc
    # Status should be "Released" or "Available", not "Bound"
    
  7. Check storage class matches: New cluster should use the same storage class

    kubectl get pvc <pvc-name> -o jsonpath='{.spec.storageClassName}'
    
  8. Verify PVCs exist: Make sure PVCs weren't accidentally deleted

    kubectl get pvc | grep <cluster-name>
    

Differences from Original Chart

  1. PVC Protection: Automatically adds pvcTemplate with protection annotations
  2. Default Values: Includes pvcAnnotations in default values.yaml
  3. Documentation: Focuses on PVC/node decoupling issues

License

Apache 2.0

Sources

Deploy cloudnative-pg with OpenTenant

Each tenant gets its own release of this chart, with its own namespace, secrets and domain. The builder below already has cloudnative-pg on a Hetzner server. Change the host, add apps from the marketplace, then copy the generated files. Providers and prices are samples, and nothing here is sent anywhere.

  1. Pick a serverConnect Hetzner, OVHcloud or AWS. The builder starts on Hetzner, the cheapest sample rate.
  2. Place the chartcloudnative-pg is pinned at v1.0.0 in opentenant.yaml. Drag more apps from the marketplace onto a rack.
  3. ApplySave opentenant.yaml and main.tf from the console, then run the commands below.
# opentenant.yaml, main.tf and dns.tf come from the builder below
opentenant validate opentenant.yaml
terraform init && terraform apply