How to recreate a Kubernetes persistentVolume?

2/6/2021

I have a persistent volume.

I want to force Kubernetes to recreate it, as the contents is corrupted. Alternatively, if there's a way to fix that, it would be a solution.

I have checked that the persistent volume is working as expected using:

kubectl describe pv -n 

And my pod was previously using it. However, my pod is now failing due to a corrupted file within the persistent volume.

I would like to recreate the persistent volume.

If I delete the persistent volume, will Kubernetes create a new one, or will I have to manually create a new one to attach?

-- fuzzi
amazon-ebs
amazon-eks
kubernetes

2 Answers

2/7/2021

You should be able to check if the volume is in usable state by accessing it from the host on which the volume is present. Simply try creating and reading a file in it to check.

You could also do a fsck on the block device to check if the Filesystem can be fixed. For example: # fsck /dev/sda3 If it is corrupted for good, the only way would be to recover from backup if available. Or else the data is gone, and you'd need to create a new volume.

Volume creation in Kubernetes can be done manually. When you use options such as hostPath, awsElasticBlockStore, etc., under volumes section of the pod definition, volume creation is static. In this case the volume which must be already present is assigned to the pod - Kubernetes will not create new volume for the pod.

If you want volumes to be created dynamically, then you must use Persistent Volume Claim under volume section of the pod definition, combined with Storage Classes. Storage Classes allow to use provisioners such as AWSElasticBlockStore, AzureFile, AzureDisk, CephFS, GlusterFS, etc., which provision volume on demand.

-- Parthiban Sekar
Source: StackOverflow

2/6/2021

If you delete a persistent volume then kubernetes will not create a new one for you, you have to manually create a new one. Basically it is the simple answer of your question.

But there are basically three options when you are done with your pv, you can delete the PVC object then depending on the PV reclaim policy you will have three options: Delete, Retain, Recycle. Now it depends on what policy is set in your pv reclaim policy.

As kubernetes official docs stated:

When a user is done with their volume, they can delete the PVC objects from the API that allows reclamation of the resource. The reclaim policy for a PersistentVolume tells the cluster what to do with the volume after it has been released of its claim. Currently, volumes can either be Retained, Recycled, or Deleted.

for more you can look at the persistent volume docs of kubernetes.

-- Sahadat Hossain
Source: StackOverflow