安装local-path-provisioner基于HostPath动态制备PV

一、背景

更改 PV 的回收策略

  • 示例的前提是动态配置PV

  • 在 Kubeadm 安装的 Kubernetes 集群环境中,动态供应 PersistentVolumes 需要先安装 Container Storage Interface (CSI) 驱动程序。

二、安装local-path-provisioner

1、地址

GitHub地址

git clone git@github.com:rancher/local-path-provisioner.git

image-20240812150230619

2、更改 local-path-provisioner 使用的默认存储路径

sed -i 's|/opt/local-path-provisioner|/mnt/data/local-path-provisioner|' /root/local-path-provisioner/deploy/local-path-storage.yaml

image-20240812151055433

3、创建文件并提权

mkdir -p /mnt/data/local-path-provisioner
chmod 777 /mnt/data/local-path-provisioner

image-20240812151302807

4、创建 NameSpace

kubectl create ns local-path-storage

image-20240812151706932

5、应用 local-path-storage

kubectl apply -f local-path-storage.yaml

image-20240812151824277

6、验证相关资源状态

kubectl get pods -n local-path-storage

kubectl get sc

image-20240812152324590

三、设置 local-path 为default SC

kubectl patch storageclass local-path -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'

kubectl patch storageclass local-path -p '{"metadata": {"annotations":{"storageclass.beta.kubernetes.io/is-default-class":"true"}}}'

image-20240812152611957

四、使用 StorageClass 动态制备 PV

1、创建PVC

更改 PV 的回收策略 示例中是三个,这里也创建三个,对应修改资源清单里的 metadata.name

cat << EOF | kubectl apply -f -
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: dynamic-pvc1
spec:
  storageClassName: local-path
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
EOF

2、创建 Pod

对应修改metadata.namespec.volumes.persistentVolumeClaim.claimName

同样的也是创建三个 Pod

cat << EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
  name: task-pv-pod1
spec:
  volumes:
    - name: task-pv-storage
      persistentVolumeClaim:
        claimName: dynamic-pvc1
  containers:
    - name: task-pv-container
      image: nginx:latest
      ports:
        - containerPort: 80
          name: "http-server"
      volumeMounts:
        - mountPath: "/usr/share/nginx/html"
          name: task-pv-storage
EOF

3、查看 PV

可以看到 pv 已经被正常动态创建起来了

kubectl get pv

image-20240812154630598

五、卸载 local-path

kubectl delete -f local-path-storage.yaml

image-20240812155343786

posted @ 2024-08-12 16:00  misakivv  阅读(21)  评论(0编辑  收藏  举报