How to decrease the kubernetes volume size

Sam A
2 min readNov 18, 2023

1. Create a New, Smaller PVC

First, define a new PVC with the desired size. Here’s an example YAML file (new-pvc.yaml):

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: new-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi # Specify your desired size here
storageClassName: default # Adjust the storage class if needed

Create the new PVC:

kubectl apply -f new-pvc.yaml

2. Deploy a Temporary Pod with Both PVCs

Deploy a temporary pod that mounts both the old and new PVCs. This will allow you to use rsync to copy data. Here's an example (temp-pod.yaml):

apiVersion: v1
kind: Pod
metadata:
name: temp-pod
spec:
containers:
- name: temp-container
image: ubuntu
command: ["/bin/bash", "-c", "sleep infinity"]
volumeMounts:
- name: old-volume
mountPath: /old-data
- name: new-volume
mountPath: /new-data
volumes:
- name: old-volume
persistentVolumeClaim:
claimName: old-pvc # Replace with your current PVC name
- name: new-volume
persistentVolumeClaim:
claimName: new-pvc

Create the temporary pod:

kubectl apply -f temp-pod.yaml

3. Copy Data Using rsync

First, ensure the temporary pod is running:

kubectl get pods temp-pod

Then, use rsync to copy data from the old PVC to the new one:

kubectl exec temp-pod -- rsync -av /old-data/ /new-data/

4. Update Your Deployment to Use the New PVC

Modify your application’s deployment configuration to use new-pvc. This step varies based on your specific deployment. You'll need to update the PVC reference in the volumeMounts section of your deployment configuration.

5. Delete the Temporary Pod and Old PVC

Once you’ve verified that the data has been successfully copied and your application is working with the new PVC, you can delete the temporary pod:

kubectl delete pod temp-pod

And if you’re sure that you no longer need the old PVC:

kubectl delete pvc old-pvc  # Replace with your old PVC name

Important Considerations

  • Downtime: This process may cause downtime for your application. Plan accordingly.
  • Data Verification: After copying, verify the data integrity in the new PVC before switching your application to use it.
  • Backup: Always have a backup of your data before performing operations like this.
  • Testing: Test this procedure in a non-production environment first.

This guide provides a basic outline for reducing PVC size using rsync. Adapt it as needed for your specific environment and requirements.

--

--

Sam A

Senior DevOps Consultant, a tech enthusiast and cloud automation expert that helps companies improve efficiency by incorporating automation