Deleting Kubernetes cluster should not delete persistance disk

8/30/2019

I have created a kubernetes cluster using terraform with persistance disk (pd-ssd). I have also created storage class and persistance volume claim as well.

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: pvc-claim
      labels:
        app: elasticsearch
    spec:
      storageClassName: ssd
      accessModes:
        - ReadWriteOnce
      resources:
        requests:
          storage: 30G
    ---
    kind: StorageClass
    apiVersion: storage.k8s.io/v1
    metadata:
      name: ssd
    provisioner: kubernetes.io/gce-pd
    parameters:
      type: pd-ssd
    reclaimPolicy: Retain
---
apiVersion: v1
kind: Service
metadata:
  name: elasticsearch
  labels:
    name: elasticsearch
spec:
  type: NodePort
  ports:
    - name: elasticsearch-port1
      port: 9200
      protocol: TCP
      targetPort: 9200
    - name: elasticsearch-port2
      port: 9300
      protocol: TCP
      targetPort: 9300
  selector:
    app: elasticsearch
    tier: elasticsearch
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: elasticsearch-application
  labels:
    app: elasticsearch
spec:
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: elasticsearch
        tier: elasticsearch
    spec:
      hostname: elasticsearch
      containers:
        - image: gcr.io/xxxxxxxxxxxx/elasticsearch:7.3.1 
          name: elasticsearch
          ports:
            - containerPort: 9200
              name: elasticport1
            - containerPort: 9300
              name: elasticport2
          env:
            - name: discovery.type
              value: single-node
          volumeMounts:
          - mountPath: /app/elasticsearch/gcp/
            name: elasticsearch-pv-volume
      volumes:
          - name: elasticsearch-pv-volume
            persistentVolumeClaim:
              claimName: pvc-claim

The pvc-claim and storage classes bound perfeclty and I have set the reclamin policy as retain. so the persistance disk should not be deleted when the kubernetes cluster is deleted. But the cluster and other data's deletes with cluster

pvc_bounded_successfully

My scenerio is I need a persitance disk and when the cluster is deleted also my data's should not be deleted. The disk should remain as it is. Is there any fesible solution to my scenerio.

-- klee
google-cloud-platform
kubernetes
kubernetes-deployment

0 Answers