How to use claims as Volumes

5/11/2021

Can anybody please tell me how to use the claim as volumes in kubernetes?

Does a vol needs to be created?

Documentation does not give much information about it: https://kubernetes.io/docs/concepts/storage/persistent-volumes/#claims-as-volumes

-- user9356263
kubernetes

2 Answers

5/12/2021

You need to create a persistent volume claim which would help you to retain the data even if the pod gets deleted, the volume data is preserved on your server at a particular location, location where u want to preserve your data can be given in deployment.yaml. With the help of persistent volume claim when you recreate new pod, the data will be intact i.e it will the fetch the data from your server(location where u want to intake your data)

Example For Mysql database with persistent volume claim on Kubernetes

PVC.yaml

---
  apiVersion: "v1"
  kind: "PersistentVolumeClaim"
  metadata: 
    name: "mysqldb-pvc-development"
    namespace: "development"
    labels: 
      app: "mysqldb-development"
  spec: 
    accessModes: 
      - ReadWriteOnce
    resources: 
      requests: 
        storage: 5Gi
    storageClassName: gp2

deployment.yaml

---
  apiVersion: "apps/v1"
  kind: "Deployment"
  metadata: 
    name: "mysqldb-development"
    namespace: "development"
  spec: 
    selector: 
      matchLabels: 
        app: "mysqldb-development"
    replicas: 1
    strategy: 
      type: "RollingUpdate"
      rollingUpdate: 
        maxSurge: 1
        maxUnavailable: 1
    minReadySeconds: 5
    template: 
      metadata: 
        labels: 
          app: "mysqldb-development"
          tier: "mysql"
      spec: 
        containers: 
          - 
            name: "mysqldb-development"
            image: "mysql_image_name"
            imagePullPolicy: "Always"
            env: 
              - 
                name: "MYSQL_ROOT_PASSWORD"
                value: "mysql_password"
            ports: 
              - 
                containerPort: 3306
                name: "mysql"
            volumeMounts:
                 - 
                   name: "mysql-persistent-storage"
                   mountPath: "/var/lib/mysql"
        volumes:
            - 
              name: "mysql-persistent-storage"
              persistentVolumeClaim:
                 claimName: "mysqldb-pvc-development"
        imagePullSecrets: 
          - 
            name: "mysqldb"

Note:- The ClaimName in deployment.yaml file and Name for pvc.yaml file should be same.

-- SVD
Source: StackOverflow

5/12/2021

A PersistentVolumeClaim allows to bind to an existing PersistentVolume. A PersistentVolume is a representation of a "real" storage device.

You have the detailed lookup algorithm in the following page, section Matching and binding: https://github.com/kubernetes/community/design-proposals/storage/persistent-storage.md

Since it is not very practical to declare each PersistentVolume manually there is an option to use a StorageClass that allows to create a PersistentVolume dynamically. You can either set the StorageClass in the PersistentVolumeClaim or define a default StorageClass for your cluster.

So when a Pod uses a PersistentVolumeClaim as volume. First a matching PersistentVolume will be searched. If no matching PV can be found and a StorageClass is defined in the claim (or a default StorageClass exists) then a volume will be dynamically created.

-- Arnaud Develay
Source: StackOverflow