How to change a Kubernetes hostpath-provisioner mount path?

一曲冷凌霜 提交于 2020-12-02 10:33:08

问题


With the storage add-on for MicroK8s, Persistent Volume Claims are by default given storage under /var/snap/microk8s/common/default-storage on the host system. How can that be changed?

Viewing the declaration for the hostpath-provisioner pod, shows that there is an environment setting called PV_DIR pointing to /var/snap/microk8s/common/default-storage - seems like what I'd like to change, but how can that be done?

Not sure if I'm asking a MicroK8s specific question or if this is something that applies to Kubernetes in general?

$ microk8s.kubectl describe -n kube-system pod/hostpath-provisioner-7b9cb5cdb4-q5jh9

Name:         hostpath-provisioner-7b9cb5cdb4-q5jh9
Namespace:    kube-system
Priority:     0
Node:         ...
Start Time:   ...
Labels:       k8s-app=hostpath-provisioner
              pod-template-hash=7b9cb5cdb4
Annotations:  <none>
Status:       Running
IP:           ...
IPs:
  IP:           ...
Controlled By:  ReplicaSet/hostpath-provisioner-7b9cb5cdb4
Containers:
  hostpath-provisioner:
    Container ID:   containerd://0b74a5aa06bfed0a66dbbead6306a0bc0fd7e46ec312befb3d97da32ff50968a
    Image:          cdkbot/hostpath-provisioner-amd64:1.0.0
    Image ID:       docker.io/cdkbot/hostpath-provisioner-amd64@sha256:339f78eabc68ffb1656d584e41f121cb4d2b667565428c8dde836caf5b8a0228
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      ...
    Last State:     Terminated
      Reason:       Unknown
      Exit Code:    255
      Started:      ...
      Finished:     ...
    Ready:          True
    Restart Count:  3
    Environment:
      NODE_NAME:   (v1:spec.nodeName)
      PV_DIR:     /var/snap/microk8s/common/default-storage
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from microk8s-hostpath-token-nsxbp (ro)
      /var/snap/microk8s/common/default-storage from pv-volume (rw)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  pv-volume:
    Type:          HostPath (bare host directory volume)
    Path:          /var/snap/microk8s/common/default-storage
    HostPathType:  
  microk8s-hostpath-token-nsxbp:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  microk8s-hostpath-token-nsxbp
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:          <none>

回答1:


HostPath

If You want to add your own path to your persistentVolume You can use spec.hostPath.path value

example yamls

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: base                 
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: Immediate
apiVersion: v1
kind: PersistentVolume
metadata:
  name: task-pv-volume
  labels:
    type: local
spec:
  storageClassName: base
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data"

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: task-pv-claim
spec:
  storageClassName: base
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 3Gi

kindly reminder

Depending on the installation method, your Kubernetes cluster may be deployed with an existing StorageClass that is marked as default. This default StorageClass is then used to dynamically provision storage for PersistentVolumeClaims that do not require any specific storage class. See PersistentVolumeClaim documentation for details.

You can check your storageclass by using

kubectl get storageclass

If there is no <your-class-name>(default) that means You need to make your own default storage class.

Mark a StorageClass as default:

kubectl patch storageclass <your-class-name> -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'

After You make defualt storageClass You can use those yamls to create pv and pvc

apiVersion: v1
kind: PersistentVolume
metadata:
  name: task-pv-volume3
  labels:
    type: local
spec:
  storageClassName: ""
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data2"

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: task-pv-claim3
spec:
  storageClassName: ""
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 3Gi

one pv for each pvc

Based on kubernetes documentation

Once bound, PersistentVolumeClaim binds are exclusive, regardless of how they were bound. A PVC to PV binding is a one-to-one mapping.



来源:https://stackoverflow.com/questions/58615019/how-to-change-a-kubernetes-hostpath-provisioner-mount-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!