StorageClass简介 StorageClass 为管理员提供了描述存储 “类” 的方法。 不同的类型可能会映射到不同的服务质量等级或备份策略,或是由集群管理员制定的任意策略。 Kubernetes 本身并不清楚各种类代表的什么。这个类的概念在其他存储系统中有时被称为 “配置文件”。
参考文档:
安装配置StorageClass 要使用 StorageClass,我们就得安装对应的自动配置程序,比如存储后端使用的是 nfs-server,那么我们就需要使用到一个 nfs-client 的自动配置程序,我们也叫它 Provisioner,这个程序使用我们已经配置好的 nfs 服务器,来自动创建持久卷,也就是自动帮我们创建 PV。
参考文档:
安装nfs-server 本文中,我们安装一个单节点的nfs-server,所在主机IP为192.168.56.101。
参考文档:
1、关闭防火墙
1 2 sudo systemctl stop firewalld.service sudo systemctl disable firewalld.service
2、安装nfs(包括nfs-server和nfs-client)
1 sudo yum install -y nfs-utils rpcbind
3、创建共享目录&设置共享目录权限
1 2 sudo mkdir -p /data/nfs/ sudo chmod -R 777 /data/nfs
4、配置共享目录sudo vim /etc/exports
添加内容:
1 /data /nfs/ *(insecure ,rw ,sync ,no_root_squash )
5、启动nfs-server
1 2 3 4 5 6 7 8 9 sudo systemctl enable rpcbind sudo systemctl enable nfs-server sudo systemctl start rpcbind sudo systemctl start nfs-server sudo systemctl status rpcbind sudo systemctl status nfs-server sudo exportfs -r
6、检查nfs状态
1 2 sudo exportfs sudo rpcinfo -p | grep nfs
7、查看目录挂载权限
1 sudo cat /var/lib/nfs/etab
安装nfs-client 在需要挂载nfs的主机上安装nfs-client。 对于要使用nfs作为存储系统的k8s集群来说,集群中所有节点都需要安装nfs-client。
1、关闭防火墙
1 2 sudo systemctl stop firewalld.service sudo systemctl disable firewalld.service
2、安装nfs(包括nfs-server和nfs-client)
1 sudo yum install -y nfs-utils rpcbind
3、启动nfs-client
1 2 3 4 5 6 sudo systemctl enable rpcbind sudo systemctl enable nfs sudo systemctl start rpcbind sudo systemctl start nfs sudo systemctl status rpcbind sudo systemctl status nfs
4、检查101是否有nfs共享目录
1 sudo showmount -e 192.168.56.101
5、挂载nfs共享目录到本机
1 2 sudo mkdir /data/mtnfs sudo mount -t nfs 192.168.56.101:/data/nfs /data/mtnfs
6、测试读写文件 102机器读写文件
1 2 sudo echo "test" > /data/mtnfs/test.txt sudo cat /data/mtnfs/test.txt
101机器读文件
1 sudo cat /data/nfs/test.txt
安装nfs-client-provisioner 1、编写sa spec,nfs-client-sa.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 apiVersion: v1 kind: ServiceAccount metadata: name: nfs-client-provisioner --- 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: ["create" , "delete" , "get" , "list" , "watch" , "patch" , "update" ] --- kind: ClusterRoleBinding apiVersion: rbac.authorization.k8s.io/v1 metadata: name: run-nfs-client-provisioner subjects: - kind: ServiceAccount name: nfs-client-provisioner namespace: default roleRef: kind: ClusterRole name: nfs-client-provisioner-runner apiGroup: rbac.authorization.k8s.io
2、编写nfs-client-provisioner的deployment spec,nfs-client.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 kind: Deployment apiVersion: apps/v1 metadata: name: nfs-client-provisioner 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: registry.cn-beijing.aliyuncs.com/mydlq/nfs-subdir-external-provisioner:v4.0.0 volumeMounts: - name: nfs-client-root mountPath: /persistentvolumes env: - name: PROVISIONER_NAME value: fuseim.pri/ifs - name: NFS_SERVER value: 192.168 .56 .101 - name: NFS_PATH value: /data/nfs volumes: - name: nfs-client-root nfs: server: 192.168 .56 .101 path: /data/nfs
3、编写sc spec,nfs-client-sc.yaml
1 2 3 4 5 6 7 apiVersion: storage.k8s.io/v1 kind: StorageClass metadata: name: nfs-storage annotations: "storageclass.kubernetes.io/is-default-class": "true" provisioner: fuseim.pri/ifs
这里的fuseim.pri/ifs
是deployment中定义的 PROVISIONER_NAME
变量
4、创建资源对象
1 2 3 kubectl apply -f nfs-client-sa.yaml kubectl apply -f nfs-client.yaml kubectl apply -f nfs-client-sc.yaml
5、查看pod状态
1 kubectl describe pods nfs-client-provisioner-7975f9b954-7fw5l
报错:
1 2 3 4 Output: mount: wrong fs type, bad option, bad superblock on 192.168.56.101:/data/nfs missing codepage or helper program, or other error (for several filesystems (e.g. nfs, cifs) you might need a /sbin/mount.<type> helper program)
这是因为nfs-client-provider依赖宿主机上的/sbin/mount.<type>
,所以所有的节点都需要安装nfs和rpcbind。详情参考nfs挂载报错误wrong fs type, bad option, bad superblock
设置默认storageclass 如果没有设置默认的storageclass,可以参考改变默认 StorageClass ,设置nfs-storage
为默认storageclass
1 kubectl patch storageclass nfs-storage -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'
启用selflink 1.20.x之后的k8s版本,selflink已经弃用了。 而quay.io/external_storage/nfs-client-provisioner:latest
的实现基于selflink,因此在使用时会报错。
解决办法:apiserver添加启动参数--feature-gates=RemoveSelfLink=false
。 详情参考文档《K8S中安装KubeSphere》
如果是k8s1.24.x以上的版本,是不支持修改启动参数RemoveSelfLink的,否则api-server无法启动。 解决办法:替换nfs-client-provisioner的镜像版本为新版本(上文中已经替换)。
使用storageclass 参考文档配置 Pod 以使用 PersistentVolume 作为存储
创建 PersistentVolumeClaim 1、定义pvc.yaml
1 2 3 4 5 6 7 8 9 10 11 apiVersion: v1 kind: PersistentVolumeClaim metadata: name: busybox-pv-claim spec: storageClassName: nfs-storage accessModes: - ReadWriteOnce resources: requests: storage: 3Gi
2、创建pvc
1 2 kubectl apply -f pvc.yaml kubectl get pvc
创建Pod 1、定义pod.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 apiVersion: v1 kind: Pod metadata: creationTimestamp: null labels: run: busybox name: busybox spec: containers: - command: - sleep - "3000" image: busybox:1.25 name: busybox resources: {} volumeMounts: - mountPath: "/data" name: busybox-pv-storage volumes: - name: busybox-pv-storage persistentVolumeClaim: claimName: busybox-pv-claim dnsPolicy: ClusterFirst restartPolicy: Never status: {}
2、创建pod
1 2 3 kubectl apply -f pod.yaml kubectl get pvc kubectl get pods
3、验证挂载
1 2 3 kubectl exec -it busybox /bin/sh ls /datatouch /data/test.txt
然后,登录到nfs-server的主机192.168.56.101,查看写入结果。
volumeClaimTemplates 对于statefulset,需要使用volumeClaimTemplates来声明使用storageclass,详情参考StatefulSet
安装配置多个StorageClass 如果k8s集群中的存储全部使用同一个nfs-server,那么这个nfs-server的网络压力会很大。 为了分担单个nfs-server的压力,我们可以在多个主机上安装配置nfs-server,然后在k8s中软件部署时规划好哪些软件使用哪个nfs-server。
假设现在我们在192.168.56.102主机上也安装好了nfs-server,下面我们在k8s集群中添加它的配置。
1、再创建一个nfs-client-provisioner,sa不用变
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 kind: Deployment apiVersion: apps/v1 metadata: name: nfs-client-provisioner-102 spec: replicas: 1 strategy: type: Recreate selector: matchLabels: app: nfs-client-provisioner-102 template: metadata: labels: app: nfs-client-provisioner-102 spec: serviceAccountName: nfs-client-provisioner containers: - name: nfs-client-provisioner image: quay.io/external_storage/nfs-client-provisioner:latest volumeMounts: - name: nfs-client-root mountPath: /persistentvolumes env: - name: PROVISIONER_NAME value: fuseim.pri/ifs-102 - name: NFS_SERVER value: 192.168 .56 .102 - name: NFS_PATH value: /data/nfs volumes: - name: nfs-client-root nfs: server: 192.168 .56 .102 path: /data/nfs
2、再创建一个storageclass
1 2 3 4 5 apiVersion: storage.k8s.io/v1 kind: StorageClass metadata: name: nfs-storage-102 provisioner: fuseim.pri/ifs-102
3、使用192.168.56.102存储数据 当我们使用nfs-storage-102
这个storageclass时,就能够存储数据到 192.168.56.102 这台主机上了。
nfs高可用 上文中,我们搭建的nfs-server都是单节点的,如果节点挂掉,会造成很多依赖它的软件不可用。如果节点数据丢失,更是一场灾难。因此,生产环境最好搭建nfs高可用环境。
参考文档: