K8s 部署 StorageClass(基于NFS)
1 创建部署的yaml文件
apiVersion: v1 kind: ServiceAccount metadata: name: nfs-client-provisioner namespace: kube-system --- kind: ClusterRole apiVersion: rbac.authorization.k8s.io/v1 metadata: name: nfs-client-provisioner-runner rules: - 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: ["list", "watch", "create", "update", "patch"] - apiGroups: [""] resources: ["endpoints"] verbs: ["get", "list", "watch", "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: kube-system roleRef: kind: ClusterRole name: nfs-client-provisioner-runner apiGroup: rbac.authorization.k8s.io --- kind: Deployment apiVersion: apps/v1 metadata: name: nfs-provisioner-01 namespace: kube-system spec: replicas: 1 strategy: type: Recreate selector: matchLabels: app: nfs-provisioner-01 template: metadata: labels: app: nfs-provisioner-01 spec: serviceAccountName: nfs-client-provisioner containers: - name: nfs-client-provisioner image: registry.cn-hangzhou.aliyuncs.com/navy-wang/nfs-subdir-external-provisioner:v4.0.2 imagePullPolicy: IfNotPresent volumeMounts: - name: nfs-client-root mountPath: /persistentvolumes env: - name: PROVISIONER_NAME value: nfs-provisioner-01 # 此处供应者名字供storageclass调用 - name: NFS_SERVER value: 192.168.0.4 # 填入NFS的地址 - name: NFS_PATH value: /volume1/nfs-hdd-k8s # 填入NFS挂载的目录 volumes: - name: nfs-client-root nfs: server: 192.168.0.4 # 填入NFS的地址 path: /volume1/nfs-hdd-k8s # 填入NFS挂载的目录 ---
apiVersion: storage.k8s.io/v1 kind: StorageClass metadata: name: sc-nfs-hdd provisioner: nfs-provisioner-01 # Supported policies: Delete、 Retain , default is Delete reclaimPolicy: Retain
2 部署yaml
kc apply -f /root/yaml/storageclass/storageclass-nfs-hdd.yaml # kc是我设置的kubectl的别名
3 查看pod是否启动成功
4 创建pvc
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: pvc1 namespace: dev spec: storageClassName: sc-nfs-hdd accessModes: - ReadWriteMany resources: requests: storage: 10Mi
5 查看pvc、pv是否创建成功
6 NFS服务器上查看pv是否挂载成功
7 部署Pod验证
7.1 部署Pod
apiVersion: v1 kind: Pod metadata: name: nginx-pvc namespace: dev spec: containers: - name: nginx image: nginx volumeMounts: - name: html-path mountPath: /usr/share/nginx/html volumes: - name: html-path persistentVolumeClaim: claimName: pvc1
7.2 进入Pod并写入index.html文件
7.3 查看NFS服务器
没问题,部署StorageClass成功