RubenLaguna
RubenLaguna

Reputation: 24806

How can create a PVC/PV that backed by a pre-existing EBS volume?

Let's say that I have a pre-existing (retained) EBS volume that was created by a PVC/PV that has been deleted by mistake. This volume was created like this:

---
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: gp2-retain
parameters:
  fsType: ext4
  type: gp2
provisioner: kubernetes.io/aws-ebs
reclaimPolicy: Retain
allowVolumeExpansion: true
volumeBindingMode: Immediate
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  labels:
    app: prometheus
  name: prometheus-server
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 8Gi
  storageClassName: gp2-retain
  volumeMode: Filesystem

and was used by a pod created by a helm chart with

helm install prometheus-current stable/prometheus server.persistentVolume.existingClaim=prometheus-server

So this EBS contains some files created by that pod that I want to keep. Now, we managed to delete the PVC/PV but the EBS volume was retained due to the reclaimPolicy.

So I want to recreate the PersistingVolumeClaim and PersistentVolume in a way that points to this particular EBS volumeID aws://eu-west-1/vol-xxxxx. How can created a PVC without triggering the dynamic provisioning and create a new PV backed by a completely new EBS volume?

Upvotes: 3

Views: 2078

Answers (1)

RubenLaguna
RubenLaguna

Reputation: 24806

You can "adopt" an existing EBS-volume into a new PVC/PV the key points are:

  • Create a PersistentVolume with a .metadata.name of your choosing (like vol-imported-prometheus-server and a .spec.awsElasticBlockStore.volumeID equal to aws://region/vol-xxxx
    • If you specify the volumeID Kubernetes will not try to allocate a new EBS volume
  • Create a PersistentVolumeClaim with spec.volumeName equal to the name of the PV in the previous step
    • If you specify the volumeName Kubernetes will bound the PVC to the existing PV instead of trying to dynamically provision a new PV based on the StorageClass

Like this example:

---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: vol-imported-prometheus-server
spec:
  accessModes:
  - ReadWriteOnce
  awsElasticBlockStore:
    fsType: ext4
    volumeID: aws://eu-west-1c/vol-xxxxx
  capacity:
    storage: 8Gi
  persistentVolumeReclaimPolicy: Retain
  storageClassName: gp2-retain
  volumeMode: Filesystem
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  labels:
    app: prometheus
  name: imported-prometheus-server
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 8Gi
  storageClassName: gp2-retain
  volumeMode: Filesystem
  volumeName: vol-imported-prometheus-server

If you kubectl apply -f thatfile.yaml you will end up with the desired PVC -> PV -> existing EBS volume.

kubectl get pv
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                                   STORAGECLASS   REASON   AGE
vol-imported-prometheus-server             8Gi        RWO            Retain           Bound    prometheus/imported-prometheus-server   gp2-retain              15m

kubectl get pvc
NAME                         STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
imported-prometheus-server   Bound    vol-imported-prometheus-server             8Gi        RWO            gp2-retain     16m

And then you can use that PVC name in helm like so:

helm install prometheus-current stable/prometheus server.persistentVolume.existingClaim=imported-prometheus-server 

where imported-prometheus-server is the name of the PVC you just created.

Upvotes: 4

Related Questions