Kubernetes Stateful Set: Unable to attach or mount volume for more than one replica

282 Views Asked by At

I am having a simple StatefulSet deployed on K8S:

kind: StatefulSet
apiVersion: apps/v1
metadata:
  name: sftp
  labels:
    app: sftp
spec:
  replicas: 2
  selector:
    matchLabels:
      app: sftp
  template:
    metadata:
      labels:
        app: sftp
    spec:
      volumes:
        - name: sftp-storage
          persistentVolumeClaim:
            claimName: sftp-pvc
      containers:
        - name: sftp
          image: atmoz/sftp
          ports:
            - containerPort: 22
              protocol: TCP
          envFrom:
            - secretRef:
                name: secret
          env:
            - name: SFTP_USERS
              value: >-
                $(SFTP_USERNAME):$SFTP_PASSWORD)::$(SFTP_GID):$(SFTP_ROOT_DIRECTORY)
          resources:
            limits:
              cpu: 500m
              memory: 128Mi
            requests:
              cpu: 250m
              memory: 64Mi
          volumeMounts:
            - name: sftp-storage
              mountPath: /home/user1
          imagePullPolicy: IfNotPresent
      securityContext:
        fsGroup: 10001

And my storage volume declaration is using a PVC as follows:

kind: PersistentVolumeClaim
metadata:
  name: sftp-pvc
  labels:
    app: sftp
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
  volumeName: pv-shoot--kyma--c-5f908c9-5ae6d56b-6560-4b00-9018-b17582336930
  storageClassName: files
  volumeMode: Filesystem

I thought that a setup like that should work without any issues, so that there wouldn't be any errors having 2 replicas on Stateful Set accessing one source on Persistant volume, but I am getting following messages on one of replica instances:

MountVolume.WaitForAttach failed for volume "pv-shoot--kyma--c-5f908c9-5ae6d56b-6560-4b00-9018-b17582336930" : volume attachment is being deleted

That is because the other instance is already attached to this volume.

What am I doing wrong here, can anyone help?

I think that it should be possible that multiple replica instances of the same Deployment or Stateful Set could access the same volume, or am I mistaken?

1

There are 1 best solutions below

1
On

You're doing it completely the read accessModes which you're using allows only one pod to be attached. you cannot attach not more than one pod. the correct accessModes you should be using is ReadWriteMany - the volume can be mounted as read-write by many nodes.

Create a new PersistentVolumeClaim with ReadWriteMany as accessModes.

Also since the service you're deploying is sftp. by its nature if you want to run more replicas and should have a same storage attached to meaning you need to have an underlying NAS storage which can be shared across multiple deployments.

  • you need a NAS/efs like storage attached to nodes.
  • create a appropriate storageclass.
  • create pvc with ReadWriteMany
  • attach it to your pod.