Search code examples
kuberneteskubernetes-statefulsetsidecar

How to sync a folder in kubernetes statefulset


We are trying to create a solution where we want to replicate changes made to a folder inside any of the pods of the Statefulset. Any file changes inside that folder on any POD should also reflect in other pods. Is there a sidecar solution for this requirement? Because we know that Statefulset will create separate PVs for each POD and there won't be any common mount across the pods of the Statefulset.


Solution

  • You can try using the NFS or file system like EFS using that you will be able to implement the ReadWritemany.

    For ref Azure File.

    apiVersion: apps/v1beta1
    kind: StatefulSet
    metadata:
      name: statefulset-azurefile
      labels:
        k8s-app: nginx
        version: v1
    spec:
      serviceName: statefulset-azurefile
      replicas: 1
      template:
        metadata:
          labels:
            k8s-app: nginx
            version: v1
        spec:
          containers:
          - name: statefulset-azurefile
            image: nginx
            volumeMounts:
            - name: persistent-storage
              mountPath: /mnt/azurefile
      volumeClaimTemplates:
      - metadata:
          name: persistent-storage
          annotations:
            volume.beta.kubernetes.io/storage-class: azurefile
        spec:
          accessModes: [ "ReadWriteMany" ]
          resources:
            requests:
              storage: 5Gi
    

    Demo : https://github.com/andyzhangx/demo/tree/master/linux/statefulset

    If volumeClaimTemplates not work as expected use the persistentVolumeClaim

    Article to read about ReadWriteMany access Mode :https://learn.microsoft.com/en-us/azure/aks/azure-files-volume

    If you are on other cloud providers like GCP, AWS, Oracle(OCI) provides different file services.

    • GCP- Filestore
    • AWS- EFS
    • OCI- Filestorage

    OCI article if you want to explore : https://enabling-cloud.github.io/oci-learning/manual/StaticPersistentVolumeOnOCI.html