Score:2

kubernetes - assign individual storage to each replica in statefulset

in flag

I am running a statefulset in my cluster and am trying to assign each of the replicas (3 in total) to assign their own PVC. Unfortunately, I can only provide 1 name as claimName. How do I make sure each starting pod grabs it's own pvc? The pods I am using are currently called test and so are the pvcs. So when I start up the containers I have a pod called test-0, test-1 and test-2. My PCVs are called claim-0, claim-1 and claim-2. What options do I have to tell pod test-0 to grab the claim-0 pvc, pod-1 to grab claim-1, etc? Thanks for your input.

Score:4
pt flag

If each pod in your StatefulSet requires its down PV, you should be creating your StatefulSet using a volumeClaimTemplates section, as shown in the documentation:

For each VolumeClaimTemplate entry defined in a StatefulSet, each Pod receives one PersistentVolumeClaim. In the nginx example above, each Pod receives a single PersistentVolume with a StorageClass of my-storage-class and 1 Gib of provisioned storage. If no StorageClass is specified, then the default StorageClass will be used. When a Pod is (re)scheduled onto a node, its volumeMounts mount the PersistentVolumes associated with its PersistentVolume Claims. Note that, the PersistentVolumes associated with the Pods' PersistentVolume Claims are not deleted when the Pods, or StatefulSet are deleted. This must be done manually.

So if you create a StatefulSet like this:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: example
spec:
  replicas: 3
  template:
    spec:
      containers:
        - name: whoami
          image: docker.io/containous/whoami:latest
          ports:
            - name: http
              containerPort: 80
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes:
          - ReadWriteOnce
        resources:
          requests:
            storage: 1Gi

Then you'll end up with three pods:

$ kubectl get pods
NAME                   READY   STATUS    RESTARTS   AGE
example-0              1/1     Running   0          78s
example-1              1/1     Running   0          73s
example-2              1/1     Running   0          68s

And three PVCs:

$ kubectl get pvc
NAME             STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
data-example-0   Bound    pvc-b31c564d-65fb-43ef-af5d-64faeaae6897   1Gi        RWO            standard       53s
data-example-1   Bound    pvc-28f83158-d10d-450b-a723-954fc113eb92   1Gi        RWO            standard       48s
data-example-2   Bound    pvc-55ae7f94-7ea9-4690-9e27-01e8a09cc3da   1Gi        RWO            standard       43s

Each one linked to a specific pod.

realShadow avatar
in flag
that is what I was looking for. I knew there was a way I just couldn't figure that out. I might come back and ask a few more questions but for now I'll get at at and try that out.
realShadow avatar
in flag
it freaking works. nice. I did it the way you described and it works like a charm. I was looking for that damn functionality for 2 days. I have it working now. THANKS man.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.