When you perform a Helm upgrade with Kubernetes rolling updates, PVCs (Persistent Volume Claims) and their underlying PV (Persistent Volume) content don't automatically update because they're designed to persist data across pod restarts and upgrades. This is actually a feature, not a bug - you typically don't want to lose your data during upgrades.
Here are the common scenarios and solutions:
Understanding the Issue
PVCs are mounted to pods, not to deployments/statefulsets. When pods are recreated during a rolling update:
- The new pods mount the same PVC
- The data remains unchanged from the previous pod
- Any initialization scripts or data population that happened during initial deployment won't re-run
Solutions Based on Your Use Case
1. If You Need to Update Configuration Files in the Volume
Use an init container or sidecar pattern to update files:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
template:
spec:
initContainers:
- name: config-updater
image: busybox
command: ['sh', '-c']
args:
- |
# Copy or update configuration files
cp /config-new/* /config-persistent/
# Or use sed/awk to modify existing files
volumeMounts:
- name: config-volume
mountPath: /config-persistent
- name: config-new
mountPath: /config-new
containers:
- name: main-app
image: my-app:latest
volumeMounts:
- name: config-volume
mountPath: /app/config
volumes:
- name: config-volume
persistentVolumeClaim:
claimName: my-pvc
- name: config-new
configMap:
name: my-config
2. If You Need to Completely Replace the Data
You'll need to delete and recreate the PVC (which means data loss):
# Scale down the deployment
kubectl scale deployment my-app --replicas=0
# Delete the PVC (this will delete the data)
kubectl delete pvc my-pvc
# Run helm upgrade (which will recreate the PVC)
helm upgrade my-release my-chart
# The deployment will scale back up automatically
3. For StatefulSets with Multiple PVCs
StatefulSets are trickier because each pod has its own PVC:
# Delete the StatefulSet but keep the pods
kubectl delete statefulset my-statefulset --cascade=orphan
# Delete the PVCs if you need fresh data
kubectl delete pvc data-my-statefulset-0 data-my-statefulset-1 # etc.
# Run helm upgrade
helm upgrade my-release my-chart
4. Using Jobs to Update PVC Content
Create a Kubernetes Job to update the PVC content:
apiVersion: batch/v1
kind: Job
metadata:
name: pvc-updater
spec:
template:
spec:
restartPolicy: Never
containers:
- name: updater
image: your-updater-image
command: ["/bin/sh"]
args:
- -c
- |
# Your update logic here
# e.g., download new data, run migrations, etc.
volumeMounts:
- name: data-volume
mountPath: /data
volumes:
- name: data-volume
persistentVolumeClaim:
claimName: my-pvc
5. Helm Hooks for Data Migration
Use Helm hooks to run data migration before the main upgrade:
apiVersion: batch/v1
kind: Job
metadata:
name: {{ .Release.Name }}-data-migration
annotations:
"helm.sh/hook": pre-upgrade
"helm.sh/hook-weight": "-5"
"helm.sh/hook-delete-policy": before-hook-creation
spec:
template:
spec:
restartPolicy: Never
containers:
- name: migrate
image: migration-image
command: ["./migrate.sh"]
volumeMounts:
- name: data
mountPath: /data
volumes:
- name: data
persistentVolumeClaim:
claimName: {{ .Values.pvcName }}
Best Practices
-
Separate Application Data from Configuration
- Use ConfigMaps/Secrets for configuration that changes
- Use PVCs for user data that should persist
-
Version Your Data Schema
- Include version information in your persistent data
- Run migration scripts when versions don't match
-
Use ReadWriteMany (RWX) Access Mode When Possible
- Allows multiple pods to write simultaneously
- Useful during rolling updates
-
Consider Using Operators
- For complex stateful applications, operators can handle data migrations automatically
Debugging Tips
Check if the PVC is being used by the new pods:
kubectl describe pod <new-pod-name>
kubectl get pvc
kubectl describe pvc <pvc-name>
Check events for issues:
kubectl get events --sort-by=.metadata.creationTimestamp
If you need more specific help, please share:
- Your Helm chart structure (especially the PVC and Deployment/StatefulSet definitions)
- What type of data you're trying to update
- Whether you need to preserve existing data or can recreate it