如何更改 Kubernetes hostpath-provisioner 挂载路径?

How to change a Kubernetes hostpath-provisioner mount path?

使用 MicroK8s 的存储附加组件,持久卷声明默认在主机系统的 /var/snap/microk8s/common/default-storage 下提供存储。如何改变?

查看 hostpath-provisioner pod 的声明,显示有一个名为 PV_DIR 的环境设置指向 /var/snap/microk8s/common/default-storage - 似乎是我想要更改的内容,但如何更改可以吗?

不确定我问的是 MicroK8s 的具体问题,还是一般适用于 Kubernetes 的问题?

$ 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>

主机路径

如果您想将自己的路径添加到您的持久卷中,您可以使用 spec.hostPath.path

示例 yaml

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

温馨提醒

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.

您可以使用

检查您的存储空间class
kubectl get storageclass

如果没有<your-class-name>(default)表示您需要创建自己的默认存储class。

将 StorageClass 标记为默认值:

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

在你创建 defualt 之后 storageClass 你可以使用这些 yaml 来创建 pv 和 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

每个 pvc 一个 pv

基于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.