Title

k8s使用nfs作为storageclass

Master节点安装nfs

yum -y install nfs-utils rpcbind 

编辑nfs配置

创建目录

mkdir /data

echo "/data *(rw,async,no_root_squash)" >>/etc/exports 

启动服务

systemctl enable --now nfs-server

systemctl enable --now rpcbind

验证

showmount -e 10.50.195.2  ##看是否能看到/nfs *字段;如果没有该命令yum -y install showmount

 

Node都节点安装nfs服务

yum -y install nfs-utils rpcbind

showmount -e  ##看是否能看到/nfs *字段;如果没有该命令yum -y install showmount

 

创建基于nfs的storageclass

创建nasespace

kubectl create namespace nfs

创建yaml文件

vim nfs-client-rbac.yaml

apiVersion: v1

kind: ServiceAccount

metadata:

  name: nfs-client-provisioner

  namespace: nfs

---

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

    namespace: nfs

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

  namespace: nfs

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

  namespace: nfs

subjects:

  - kind: ServiceAccount

    name: nfs-client-provisioner

    namespace: nfs

roleRef:

  kind: Role

  name: leader-locking-nfs-client-provisioner

  apiGroup: rbac.authorization.k8s.io

 

 

vim nfs-provisioner.yaml

apiVersion: apps/v1

kind: Deployment

metadata:

  name: nfs-client-provisioner

  labels:

    app: nfs-client-provisioner

  namespace: nfs #与RBAC文件中的namespace保持一致

spec:

  replicas: 1

  selector:

    matchLabels:

      app: nfs-client-provisioner

  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

          imagequay.io/external_storage/nfs-client-provisioner:latest

          volumeMounts:

            - name: nfs-client-root

              mountPath: /persistentvolumes

          env:

            - name: PROVISIONER_NAME

              value: nfs-storage  #provisioner名称,请确保该名称与 nfs-StorageClass.yaml文件中的provisioner名称保持一致

            - name: NFS_SERVER

              value: 10.50.195.2   #NFS Server IP地址

            - name: NFS_PATH

              value: /nfs    #NFS挂载卷

      volumes:                                                                                                                          

        - name: nfs-client-root

          nfs:

            server: 10.50.195.2  #NFS Server IP地址

            path: /nfs     #NFS 挂载卷

 

vim nfs-storagesclass.yaml

apiVersion: storage.k8s.io/v1

kind: StorageClass

metadata:

  name: managed-nfs-storage

provisioner: nfs-storage     #这里的名称要和provisioner配置文件中的环境变量PROVISIONER_NAME保持一致parameters:  archiveOnDelete: "false"

 

运行以上三个yaml

 

kubectl apply -f nfs-client-rbac.yaml

kubectl apply -f nfs-provisioner.yaml

kubectl apply -f nfs-storagesclass.yaml

posted @ 2024-06-18 17:47  Esurts~  阅读(21)  评论(0)    收藏  举报