How to Rebind a StatefulSet to an Existing PVC in Azure Kubernetes Service (AKS)

 

  • You deleted the StatefulSet, PVC, PV, and StorageClass, but the Azure File Share still has the data because of the reclaimPolicy: Retain.
  • A new PVC was automatically created when you redeployed the StatefulSet, but you want the StatefulSet to use the old PVC (pvc-84031045-6eaa-4680-8c4f-ee32528b17eb) with the retained data instead.

Solution Steps

  1. Delete the newly created PVC: First, delete the newly created PVC (pvc-4f8c6892-d973-4213-9325-7ed9ee128772), as you want the StatefulSet to reuse the existing one. This can be done with:

    bash
    kubectl delete pvc pvc-4f8c6892-d973-4213-9325-7ed9ee128772
  2. Retain the Existing PV: You need to manually reclaim the existing PV (Persistent Volume) that was retained (pvc-84031045-6eaa-4680-8c4f-ee32528b17eb) and bind it to a new PVC. Since the PV is in the Retain state, you'll need to manually associate it with the PVC.

    Here's how to reclaim and rebind it to your StatefulSet:

    • Identify the PV: First, get the list of the Persistent Volumes (PV) and check if the old PV is in the Released state.

      bash

      kubectl get pv

      The output should list the existing PV with the old PVC name (in Released status):

      bash

      NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE pvc-84031045-6eaa-4680-8c4f-ee32528b17eb 20Gi RWX Retain Released default/pvc-84031045-6eaa-4680-8c4f-ee32528b17eb azurefile-csi-custom 10d
    • Edit the PV: Edit the PV (pvc-84031045-6eaa-4680-8c4f-ee32528b17eb) and remove the existing claim reference (this is necessary to bind it to a new PVC):

      bash

      kubectl edit pv pvc-84031045-6eaa-4680-8c4f-ee32528b17eb

      In the PV YAML, you will see a reference to the old PVC under the spec.claimRef section. Delete the entire claimRef section to unbind the PV from the old PVC.

      yaml

      spec: claimRef: apiVersion: v1 kind: PersistentVolumeClaim name: pvc-84031045-6eaa-4680-8c4f-ee32528b17eb namespace: default uid: 84031045-6eaa-4680-8c4f-ee32528b17eb

      Remove this block and save the changes.

  3. Create a New PVC: Now that the PV is unbound, create a new PVC that will bind to this existing PV. Create a YAML file for the new PVC, ensuring that the size, storage class, and access mode match the old PV. Here's an example of how the PVC should look:

    yaml

    apiVersion: v1 kind: PersistentVolumeClaim metadata: name: mssql-data-existing spec: accessModes: - ReadWriteMany # This should match the old PV's access mode storageClassName: azurefile-csi-custom # This should match the old PV's storage class resources: requests: storage: 20Gi # This should match the old PV's size

    Apply the PVC:

    bash

    kubectl apply -f new-pvc.yaml

    After the PVC is created, Kubernetes should automatically bind this new PVC to the existing PV (pvc-84031045-6eaa-4680-8c4f-ee32528b17eb) because it matches the size, storage class, and access mode.

    You can verify that the new PVC is bound to the existing PV:

    bash

    kubectl get pvc

    You should see the new PVC (mssql-data-existing) in the Bound state.

  4. Update StatefulSet to Use the Existing PVC: Now that the PVC is bound to the existing PV, update your StatefulSet to reference the existing PVC. In your StatefulSet YAML, replace the volumeClaimTemplates section with a direct reference to the existing PVC:

    yaml

    volumeMounts: - name: mssql-data mountPath: /var/opt/mssql volumes: - name: mssql-data persistentVolumeClaim: claimName: mssql-data-existing # The newly bound PVC

    Apply the updated StatefulSet:

    bash

    kubectl apply -f statefulset.yaml
  5. Verify the Pod: After deploying the updated StatefulSet, verify that the pod is using the existing PVC:

    bash

    kubectl get pods

    You can also describe the pod to ensure that the volume is mounted correctly:

    bash

    kubectl describe pod <pod-name>

    Make sure that the pod is mounting the existing PVC (mssql-data-existing) at /var/opt/mssql.

No comments:

Post a Comment