Add persistent volume in Kubernetes StatefulSet on Minikube

dimzak picture dimzak · Apr 9, 2018 · Viewed 9.2k times · Source

I'm new to Kubernetes and I'm trying to add a PVC in my StatefulSet on Minikube. PV and PVC are shown here:

NAME            CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS    CLAIM                      STORAGECLASS   REASON    AGE
neo4j-backups   5Gi        RWO            Retain           Bound     default/backups-claim      manual                   1h

NAME            STATUS    VOLUME          CAPACITY   ACCESS MODES   STORAGECLASS   AGE
backups-claim   Bound     neo4j-backups   5Gi        RWO            manual         51m

Basically I want all pods of the StatefulSet to see the contents of that volume as backup files are stored there.

StatefulSet used can be found here

Minikube version: minikube version: v0.25.2
Kubernetes version: GitVersion:"v1.9.4"

Answer

bits picture bits · Apr 11, 2018

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: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: local-storage
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: example-pv
spec:
  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: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: example-local-claim
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
  storageClassName: local-storage
---
apiVersion: "apps/v1beta1"
kind: StatefulSet
metadata:
  name: nginx
spec:
  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