Kubernetes部署nfs-provisioner 实现PV 动态供给(StorageClass)

发布时间:2024年01月23日

nfs搭建可点击
在这里插入图片描述
nfs-pv动态供给流程:
1、预先创建StorageClass,用于定义动态供给的行为。
2、用户创建使用StorageClass的持久化存储声明(PVC)。
3、PVC被提交后,系统会检查是否有足够的存储资源来满足PVC的要求。如果有,系统会自动创建一个PV,并将其与PVC绑定。
4、用户可以创建一个使用PVC的Pod。
5、Pod中的应用可以通过PVC进行数据的持久化。
6、PVC使用PV进行数据的最终持久化处理

一、部署nfs-orivisioer

nfs-provisioner项目地址:
https://github.com/kubernetes-sigs/nfs-subdir-external-provisioner/tree/master/deploy
分别下载class.yaml,deployment.yaml,rabc.yaml. 也可以直接从下面复制粘贴
class.yaml 创建存储类

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: nfs-client   #存储类名字
provisioner: k8s-sigs.io/nfs-subdir-external-provisioner # or choose another name, must match deployment's env PROVISIONER_NAME'
parameters:
  archiveOnDelete: "false"  #pvc删除后,pv不删除(nfs共享目录不删除),设置true会删除

deployment.yaml 部署nfs-client-provisioner
这里除了nfs配置信息还改了镜像地址,因为默认的镜像下载地址在国外。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nfs-client-provisioner
  labels:
    app: nfs-client-provisioner
  # replace with namespace where provisioner is deployed
  namespace: default
spec:
  replicas: 1
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app: nfs-client-provisioner
  template:
    metadata:
      labels:
        app: nfs-client-provisioner
    spec:
      serviceAccountName: nfs-client-provisioner
      containers:
        - name: nfs-client-provisioner
          image: zhaoguanghui6/nfs-client-provisioner:v4.0.1 
          volumeMounts:
            - name: nfs-client-root
              mountPath: /persistentvolumes
          env:
            - name: PROVISIONER_NAME
              value: k8s-sigs.io/nfs-subdir-external-provisioner
            - name: NFS_SERVER
              value: 10.3.243.101 #nfs服务器IP,改成自己的
            - name: NFS_PATH
              value: /ifs/kubernetes  #nfs服务器共享目录
      volumes:
        - name: nfs-client-root
          nfs:
            server: 10.3.243.101  #nfs服务器IP
            path: /ifs/kubernetes   #nfs服务器共享目录

rabc.yaml 授权(连接k8s-api)

apiVersion: v1
kind: ServiceAccount
metadata:
  name: nfs-client-provisioner
  # replace with namespace where provisioner is deployed
  namespace: default
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: nfs-client-provisioner-runner
rules:
  - apiGroups: [""]
    resources: ["nodes"]
    verbs: ["get", "list", "watch"]
  - apiGroups: [""]
    resources: ["persistentvolumes"]
    verbs: ["get", "list", "watch", "create", "delete"]
  - apiGroups: [""]
    resources: ["persistentvolumeclaims"]
    verbs: ["get", "list", "watch", "update"]
  - apiGroups: ["storage.k8s.io"]
    resources: ["storageclasses"]
    verbs: ["get", "list", "watch"]
  - apiGroups: [""]
    resources: ["events"]
    verbs: ["create", "update", "patch"]
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: run-nfs-client-provisioner
subjects:
  - kind: ServiceAccount
    name: nfs-client-provisioner
    # replace with namespace where provisioner is deployed
    namespace: default
roleRef:
  kind: ClusterRole
  name: nfs-client-provisioner-runner
  apiGroup: rbac.authorization.k8s.io
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: leader-locking-nfs-client-provisioner
  # replace with namespace where provisioner is deployed
  namespace: default
rules:
  - apiGroups: [""]
    resources: ["endpoints"]
    verbs: ["get", "list", "watch", "create", "update", "patch"]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: leader-locking-nfs-client-provisioner
  # replace with namespace where provisioner is deployed
  namespace: default
subjects:
  - kind: ServiceAccount
    name: nfs-client-provisioner
    # replace with namespace where provisioner is deployed
    namespace: default
roleRef:
  kind: Role
  name: leader-locking-nfs-client-provisioner
  apiGroup: rbac.authorization.k8s.io
kubectl apply -f class.yaml
kubectl apply -f deployment.yaml
kubectl apply -f rabc.yaml

二 、创建pvc测试

kubectl apply -f pvc.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nfs-pvc2  
spec: 
  storageClassName: "nfs-client"  #存储类的名字,只需要加这一个参数
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 5Gi
kubectl get pvc,pv
#可以看到pv已经被自动创建
文章来源:https://blog.csdn.net/weixin_48711696/article/details/135740873
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。