k8s基于storageclass的动态存储

AI 摘要 / TL;DR

k8s基于storageclass的动态存储 来源: https://www.ucloud.cn/yun/129784.html 作者: IT那活儿 发布日期: 发布于2023 01 11 13:20 k8s基于storageclass的动态存储 使用说明:一般在k8s中创建有状态服务时会需要创建pv+pvc静态存储来

来源: https://www.ucloud.cn/yun/129784.html 作者: IT那活儿 发布日期: 发布于2023-01-11 13:20

k8s基于storageclass的动态存储

使用说明:一般在k8s中创建有状态服务时会需要创建pv+pvc静态存储来实现存储,基于sc的动态存储减少了用户对于底层存储资源各方面细节的关注,只需要关注自己创建的pvc资源,引用存储类的对应sc资源即可,简单来说就是无需创建pv,只需创建pvc后k8s系统会自动创建合适的pv,并将pv和pvc绑定好实现动态存储。

一. 创建rbac授权

(集群中开启了rbac)

cat nfs-rbac.yamlapiVersion: v1kind: ServiceAccountmetadata:  name: nfs-client-provisioner---apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRolemetadata:  name: nfs-client-provisioner-clusterrolerules:  - 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"]---apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRoleBindingmetadata:  name: nfs-client-provisioner-clusterrolebindingsubjects:- kind: ServiceAccount  name: nfs-client-provisioner  namespace: defaultroleRef:  kind: ClusterRole  name: nfs-client-provisioner-clusterrole  apiGroup: rbac.authorization.k8s.ioKubectl apply -f nfs-rbac.yaml

二. 创建provisioner

NFS Provisioner 是一个自动配置卷程序,它使用现有的和已配置的 NFS 服务器来支持通过持久卷声明动态配置 Kubernetes 持久卷

cat nfs-deployment.yaml```apiVersion: apps/v1kind: Deploymentmetadata:  name: nfs-client-prosionerspec:  selector:    matchLabels:      app: nfs-client-prosioner  replicas: 1  strategy:    type: Recreate  template:    metadata:      labels:        app: nfs-client-prosioner    spec:      serviceAccountName: nfs-client-provisioner      containers:      - name: nfs-client-prosioner        image: registry.cn-hangzhou.aliyuncs.com/rookieops/nfs-client-provisioner:v0.1        imagePullPolicy: IfNotPresent        volumeMounts:        - name: nfs-client-root         mountPath: /data/pv        env:        - name: PROVISIONER_NAME         value: rookieops/nfs        - name: NFS_SERVER         value: 192.168.16.1             - name: NFS_PATH         value: /home/nfs #nfs的共享目录      volumes:      - name: nfs-client-root       nfs:         server: 192.168.16.1         path: /home/nfs

注意:ip 192.168.16.1为nfs的服务器地址  /home/nfs 为nfs的共享目录

注意:动态存储创建是基于集群中已经安装并能使用nfs的基础上来创建的

三. 创建storageclass

cat storageclass-naf.yaml```apiVersion: storage.k8s.io/v1kind: StorageClassmetadata:  name: nfs-client-storageclassprovisioner: rookieops/nfs```

注意:provisioner 的参数rookieops/nfs 需要和上面创建的provisioner中的PROVISIONER_NAME对应

通过kubectl get sc 查看创建的sc 资源

四. 创建pvc

cat pvc.yaml```apiVersion: v1kind: PersistentVolumeClaimmetadata:  name: test-nfs-pvc2  annotations:    volume.beta.kubernetes.io/storage-class: "nfs-client-storageclass"spec:  accessModes:    - ReadWriteOnce  resources:    requests:      storage: 300Gi```

注意:nfs-client-storageclass为上面sc定义的名字

通过kubectl get pvc 和kubectl get pv 查看新创建的绑定状态

END

更多精彩干货分享

点击下方名片关注

IT那活儿