<p>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?</p>

<p>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.</p>

<p>Here is the YAML file:</p>

<pre><code>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
</code></pre>

<p>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 <code>StorageClass</code>.  For details on how to define and use it, please refer to </p>

<p><a href="https://kubernetes.io/docs/concepts/storage/storage-classes/" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/storage/storage-classes/</a></p>

<p>and </p>

<p><a href="https://kubernetes.io/docs/concepts/storage/persistent-volumes/" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/storage/persistent-volumes/</a></p>
