Justin
Justin

Reputation: 45350

Kubernetes create a persistentVolume per pod in deployment

I have the following PersistentVolumeClaim and Deployment in Kubernetes:

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: my-app-storage
spec:
  accessModes:
     - ReadWriteOnce
  resources:
    requests:
      storage: 25Gi
  storageClassName: "gp2"

...
containers:
   ...
   volumeMounts:
   - name: my-app-storage
     mountPath: /var/my-app/storage
     readOnly: false
   ...
volumes:
  - name: my-app-storage
    persistentVolumeClaim:
      claimName: my-app-storage

The problem is this is only creating a single volume that is mounted into each pod. I have replicas: 3 though. How can I have a volume created per pod? I need to stay using a Deployment and can't migrate to using a StatefulSet. Is this even possible? Currently, this is scheduling all the pods on one single Kubernetes worker node because the volume is shared and only mounted to a single Kubernetes worker node.

Upvotes: 0

Views: 485

Answers (1)

Justin
Justin

Reputation: 45350

I ended up converting the Deployment to a StatefulSet and this works. Key thing is remove the PersistentVolumeClaim and update the Deployment to a StatefulSet and use the following props:

  volumeClaimTemplates:
  - metadata:
      name: storage
    spec:
      accessModes:
        - ReadWriteOnce
      resources:
        requests:
          storage: 16Gi

Upvotes: 4

Related Questions