Kubernetes: What is the best practice for create dynamic local volume to auto assign PVs for PVCs?

ghz 1years ago ⋅ 7453 views

Question

Recently, I installed the k8s via kubeadm init on my CentOS7 clusters. Unfortunately, I suffered a lots of problems. The biggest problem is "How to create a dynamic local storage/volume for automatically assign PVs for PVCs?"

Q: Why local storage not cloud storage service?

A: This is a limitation of new lab of my university.

Problem description:

The k8s(v1.18.0) did not create a default storage class for me after the installing process successfully. I not sure about that whether the situation normally. However, when I install my first application, [JupyterHub](https://zero-to-jupyterhub.readthedocs.io/en/latest/setup- jupyterhub/setup-jupyterhub.html), on k8s, I got error from it because it could found a suitable PV to fit a PVC hub-db-dir. And then I attempt to manually a local storage class and a PV such below:

local-storage.yaml

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: local-storage
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer

hub-db-pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: hub-db-pv
  namespace: jhub
spec:
  capacity:
    storage: 1Gi
  accessModes:
  - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: local-storage
  local:
    path: /home/k8s_data_dir/jhub_dir
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - node1
          - node2
          - node3
          - node4

The JupyterHub work after I apply these yaml files. I thought that will success to use JupyterHub. But I so naive. I enter the main page of JupyterHub then failure to running, because the JupyterHub create a new PVC for me but it could not bound a suitable PV. I guess that the error cause by my local storage class which unable to auto create a PV to fit a PVC. Then I try to use an external provisioner [sig-storage-lib-external- provisioner](https://github.com/kubernetes-sigs/sig-storage-lib-external- provisioner) but it can not fix my problem of local storage. (I according to the tutorial of sig-storage-lib-external-provisioner to do.)

So, my questions are:

  1. What is the best practice to fix the problem of local storage?
  2. If you have a compromise method to do the same thing, please help me!

Finally, I honest to thank you for your help!


Answer

High level steps to achieve dynamic PV creation using local-storage StorageClass

From the docs [here](https://github.com/kubernetes-sigs/sig-storage-local- static-provisioner/blob/master/docs/operations.md) on how to manage local volumes in your Kubernetes cluster.

  1. Create a directory for provisioner discovering
  2. Prepare and set up local volumes in discovery directory

Next from the docs [here](https://github.com/kubernetes-sigs/sig-storage- local-static-provisioner/blob/master/docs/getting-started.md#step-2-creating- a-storageclass-19)

  1. Create a StorageClass
  2. Create local persistent volumes