How to reuse an existing persistent volume claims

PhiloJunkie picture PhiloJunkie · Nov 5, 2017 · Viewed 7.6k times · Source

I have deleted my elasticsearch cluster, but now after I've deployed a new cluster I need to access the old data that was stored on 3 Persistent Volumes PV described bellow:

NAME                       STATUS    VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
storage-es-data-0          Bound     pvc-19429b0b-ba42-11e7-979d-42010a840ff7   12Gi       RWO            standard       10d
storage-es-data-1          Bound     pvc-36505962-ba42-11e7-979d-42010a840ff7   12Gi       RWO            standard       10d
storage-es-data-2          Bound     pvc-422da328-ba42-11e7-979d-42010a840ff7   12Gi       RWO            standard       10d

This is the description of the old PV claims:

NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS    CLAIM                              STORAGECLASS   REASON    AGE
pvc-19429b0b-ba42-11e7-979d-42010a840ff7   12Gi       RWO            Delete           Bound     default/storage-es-data-0          standard                 10d
pvc-36505962-ba42-11e7-979d-42010a840ff7   12Gi       RWO            Delete           Bound     default/storage-es-data-1          standard                 10d
pvc-422da328-ba42-11e7-979d-42010a840ff7   12Gi       RWO            Delete           Bound     default/storage-es-data-2          standard                 10d

My new deployment is described as follow:

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: es-data
  labels:
    component: elasticsearch
    role: data
spec:
  replicas: 1
  template:
    metadata:
      labels:
        component: elasticsearch
        role: data
    spec:
      initContainers:
      - name: init-sysctl
        image: busybox
        imagePullPolicy: IfNotPresent
        command: ["sysctl", "-w", "vm.max_map_count=262144"]
        securityContext:
          privileged: true
      containers:
      - name: es-data
        image: docker.elastic.co/elasticsearch/elasticsearch:5.6.3
        imagePullPolicy: Always
        ports:
        - containerPort: 9300
          name: transport
          protocol: TCP
        volumeMounts:
        - name: storage
          mountPath: /data
      volumes:
      - name: storage
        persistentVolumeClaim:
          claimName: storage-es-data-0

After connecting my pod with a Loadblance service, I didn't find any documents. Am I missing something? And how can I use the three PV in the same POD.

Answer

nickgryg picture nickgryg · Nov 6, 2017

Your deployment yaml file is correct. You should be able to find files from pvc-19429b0b-ba42-11e7-979d-42010a840ff7 volume inside /data folder in your pod.

In order to use three PV in the same POD just add them to your deployment yaml:

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: es-data
  labels:
    component: elasticsearch
    role: data
spec:
  replicas: 1
  template:
    metadata:
      labels:
        component: elasticsearch
        role: data
    spec:
      initContainers:
      - name: init-sysctl
        image: busybox
        imagePullPolicy: IfNotPresent
        command: ["sysctl", "-w", "vm.max_map_count=262144"]
        securityContext:
          privileged: true
      containers:
      - name: es-data
        image: docker.elastic.co/elasticsearch/elasticsearch:5.6.3
        imagePullPolicy: Always
        ports:
        - containerPort: 9300
          name: transport
          protocol: TCP
        volumeMounts:
        - name: storage-0
          mountPath: /data0
        - name: storage-1
          mountPath: /data1
        - name: storage-2
          mountPath: /data2
      volumes:
      - name: storage-0
        persistentVolumeClaim:
          claimName: storage-es-data-0
      - name: storage-1
        persistentVolumeClaim:
          claimName: storage-es-data-1
      - name: storage-2
        persistentVolumeClaim:
          claimName: storage-es-data-2