How to create PV exclusively for a pod

5/10/2019

I want to dynamically create a PV and PVC for a pod where it can store some data which will be generated by the application running inside a container. I am making sure that only one container is running on each pod. How can I use StatefulSets to dynamically create PVs instead of admin creating it manually?

I used volumeClaimTemplate in StatefulSet. In minikube, It created both PV and PVC for each pod. But when I tried it in Kubernetes cluster, somehow it doesn't create PV. If I create PV manually, then a bound happens between PV and PVC. But I want it to happen dynamically.

Here is the YAML file:

apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: StatefulSet
metadata:
  name: app-deployment
spec:
  selector:
    matchLabels:
      app: myapp
  serviceName: "app-service"
  replicas: 1 # tells deployment to run 2 pods matching the template
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
      - name: myapp
        image: myrepository:5000/docker/images/app:v0
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 8082
        volumeMounts:
        - name: jms
          mountPath: /opt/APP/DATA/jms-data
  volumeClaimTemplates:
  - metadata:
      name: jms
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: standard
      resources:
        requests:
          storage: 256Mi
---

kind: Service
apiVersion: v1
metadata:
  name: app-service
spec:
  selector:
    app: myapp
  ports:
  - protocol: TCP
    port: 8082
    targetPort: 8082
  type: NodePort
-- Srinath Samudrala
kubernetes

2 Answers

5/10/2019

Just as Prateek Jain mentioned you need a provisioner and a storageclass. It does work with minikube and will work on services like GKE, EKS, AKS both of these are available by default. You can check that by kubectl get storageclass and then kubectl describe stroageclass name.

I assume that in your Kubernetes cluster this was missing (it does for kubeadm for example).

Here again you can use already linked by P Ekambaram local-PVs. You will need a An external static provisioner:

provided here to help simplify local storage management once the local volumes are configured. Note that the local storage provisioner is different from most provisioners and does not support dynamic provisioning. Instead, it requires that administrators preconfigure the local volumes on each node and if volumes are supposed to be.

You can read more about dynamic provisioning here.

Solution to your problem is dependent on the volumes your want to use, the provisioner or to use managed Kubernetes engine (GKE, AKS, EKS).

-- aurelius
Source: StackOverflow

5/10/2019

If I understand your issue correctly then, you want a mechanism via which admin wont have to manually create PVs for pods. K8S has mechanism of creating storage classes for PVs (one time effort). Once the storage class is defined by admin and marked as default then there is no need for anyone to create PVs as they will be created on the fly by K8S using that StorageClass. For details on how to define and use it, please refer to

https://kubernetes.io/docs/concepts/storage/storage-classes/

and

https://kubernetes.io/docs/concepts/storage/persistent-volumes/

-- Prateek Jain
Source: StackOverflow