How to use existing PVC in statefulSet definition in Kubernetes?
Asked Answered
C

2

6

I have applied the following pvc yaml.

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: ebs-claim
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: ebs-sc
  resources:
    requests:
      storage: 4Gi


I now want my statefulset to use the PVC I have created. Instead, it is creating new PVC of a different storageclass.

apiVersion: v1
kind: statefulset
    metadata:
      name: example
    spec:
        # Name for the service object created by the operator
      serviceName: mongodb-service 
      selector: {}
        # Specifies a size for the data volume different from the default 10Gi
      volumeClaimTemplates:
        - metadata:
            name: ebs-claim
      template:
        spec:
          nodeSelector:
            eks.amazonaws.com/nodegroup: managed-ng-private-1

How can I get my statefulset to use existing PVCs instead of creating new ones?

Chicalote answered 17/7, 2021 at 21:22 Comment(1)
Is the PV, which was previously created, in state Released?Schoolmistress
M
5

Specify it like normal in the volumes section of the pod spec template. But you won't get the special behavior of creating a new PVC for each replica since that requires creating new ones.

Multivalent answered 17/7, 2021 at 21:39 Comment(0)
T
0

The stateful set definition is missing the volume. Give the below yaml a try and check.

apiVersion: v1
kind: statefulset
metadata:
    name: example
spec:
    # Name for the service object created by the operator
  serviceName: mongodb-service 
  selector: {}
    # Specifies a size for the data volume different from the default 10Gi
  volumes:
    - name: ebs-vol
      persistentVolumeClaim:
      claimName: PersistentVolumeClaim
  volumeClaimTemplates:
    - metadata:
        name: ebs-claim
  template:
    spec:
      nodeSelector:
        eks.amazonaws.com/nodegroup: managed-ng-private-1
Tiger answered 18/7, 2021 at 11:9 Comment(1)
Nope, this will create a new PVC named ebs-claim-0.Multivalent

© 2022 - 2024 — McMap. All rights reserved.