Add persistent volume in Kubernetes StatefulSet on Minikube Add persistent volume in Kubernetes StatefulSet on Minikube kubernetes kubernetes

Add persistent volume in Kubernetes StatefulSet on Minikube


If you use volumeClaimTemplates in StatefulSet k8s will do dynamic provisioning & create one PVC and corresponding PV for each pod, so each one of them gets their own storage.

What you want is to create one PV & one PVC and use it in all replicas of Statefulset.

Below is example on Kubernetes 1.10 how you can do it, where /var/www/html will be shared by all three Pods, just change /directory/on/host to some local directory on your machine. Also I ran this example on minikube v0.26.0

Ofcourse below is just an example to illustrate the idea, but in a real example processes in Pod should be aware of syncronizing access to shared storage.


kind: StorageClassapiVersion: storage.k8s.io/v1metadata:  name: local-storageprovisioner: kubernetes.io/no-provisionervolumeBindingMode: WaitForFirstConsumer---apiVersion: v1kind: PersistentVolumemetadata:  name: example-pvspec:  capacity:    storage: 100Gi  # volumeMode field requires BlockVolume Alpha feature gate to be enabled.  volumeMode: Filesystem  accessModes:    - ReadWriteOnce  persistentVolumeReclaimPolicy: Delete  storageClassName: local-storage  local:    path: /directory/on/host  nodeAffinity:    required:      nodeSelectorTerms:        - matchExpressions:            - key: kubernetes.io/hostname              operator: In              values:                - minikube ---kind: PersistentVolumeClaimapiVersion: v1metadata:  name: example-local-claimspec:  accessModes:    - ReadWriteOnce  resources:    requests:      storage: 5Gi  storageClassName: local-storage---apiVersion: "apps/v1beta1"kind: StatefulSetmetadata:  name: nginxspec:  serviceName: nginx  replicas: 3  template:    metadata:      labels:        app: nginx    spec:      containers:        - name: nginx-container           image: "nginx:1.12.2"          imagePullPolicy: "IfNotPresent"          volumeMounts:             - name: localvolume              mountPath: /var/www/html      volumes:        - name: localvolume          persistentVolumeClaim:            claimName: example-local-claim