• Post author:
  • Post category:运维
  • Post comments:3评论
  • Reading time:5 mins read

关于K8S的持久化方案PV、PVC、StorageClass的介绍请参照PV、PVC、StorageClass讲解,本文不再赘述。

本文只概述如何使用StorageClass动态分配存储空间,以及StorageClass的两种简单实现方案。

local-path-provisioner实现方式

该实现方式使用服务器本地硬盘作为存储源。

首先在官方Git仓库获取Release版,并在集群内创建StorageClass:

wget https://github.com/kubernetes-sigs/sig-storage-local-static-provisioner/archive/refs/tags/v2.4.0.tar.gz
tar -zxvf v2.4.0.tar.gz
cd sig-storage-local-static-provisioner-2.4.0/
kubectl create -f deployment/kubernetes/example/default_example_storageclass.yaml

安装local-path-provisioner

kubectl create -f default_example_provisioner_generated.yaml

使用kubectl get storageclass可以查看新建的StorageClass。

阿里云NAS实现方式

该实现方式使用阿里云NAS作为后端存储方式。

首先到阿里云NAS控制台开通服务。

根据需要新建实例,本文使用综合付费最低的实例:

点击挂载使用,查看挂载点地址。

之后修改下面的yaml,server改为你的挂载点地址:

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  finalizers:
    - kubernetes.io/pvc-protection
  name: nfs-pvc-nfs
  namespace: kube-system
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 100Gi
  storageClassName: nfs-storageclass-provisioner
  volumeMode: Filesystem
  volumeName: nfs-pv-nfs

---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: eip-nfs-client-provisioner
  namespace: kube-system
  resourceVersion: '500881'

---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: eip-nfs-client-provisioner-runner
  resourceVersion: '484592'
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

---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: eip-run-nfs-client-provisioner
  resourceVersion: '484593'
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: eip-nfs-client-provisioner-runner
subjects:
  - kind: ServiceAccount
    name: eip-nfs-client-provisioner
    namespace: kube-system

---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: eip-leader-locking-nfs-client-provisioner
  namespace: kube-system
  resourceVersion: '484594'
rules:
  - apiGroups:
      - ''
    resources:
      - endpoints
    verbs:
      - get
      - list
      - watch
      - create
      - update
      - patch

---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: eip-leader-locking-nfs-client-provisioner
  namespace: kube-system
  resourceVersion: '484595'
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: eip-leader-locking-nfs-client-provisioner
subjects:
  - kind: ServiceAccount
    name: eip-nfs-client-provisioner
    namespace: kube-system

---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: eip-nfs-nfs
  name: eip-nfs-nfs
  namespace: kube-system
spec:
  replicas: 1
  selector:
    matchLabels:
      app: eip-nfs-nfs
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: eip-nfs-nfs
    spec:
      containers:
        - env:
            - name: PROVISIONER_NAME
              value: nfs-nfs
            - name: NFS_SERVER
              value: xxxxxxx-qgk18.cn-shanghai.nas.aliyuncs.com
            - name: NFS_PATH
              value: /nfs
          image: 'eipwork/nfs-subdir-external-provisioner:v4.0.2'
          name: nfs-client-provisioner
          volumeMounts:
            - mountPath: /persistentvolumes
              name: nfs-client-root
      serviceAccountName: eip-nfs-client-provisioner
      volumes:
        - name: nfs-client-root
          persistentVolumeClaim:
            claimName: nfs-pvc-nfs

---
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  annotations:
    k8s.kuboard.cn/storageType: nfs_client_provisioner
  name: nfs
mountOptions:
  - >-
    vers=4,minorversion=0,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport
parameters:
  archiveOnDelete: 'false'
provisioner: nfs-nfs
reclaimPolicy: Delete
volumeBindingMode: WaitForFirstConsumer

使用kubectl apply -f来部署后端存储实现。

测试存储类

新建一个pvc,在storageClass内填写你的存储类名称:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: myclaim
spec:
  accessModes:
    - ReadWriteOnce
  volumeMode: Filesystem
  resources:
    requests:
      storage: 8Gi
  storageClassName: nfs

使用kubectl apply -f部署后可以看到为pending状态,这是因为存储类的 volumeBindingMode 为 WaitForFirstConsumer 模式,需要有pod使用到此存储类时才会变为Bound状态,此处不再详述,之后会在K8S实战部署Mysql的时候进行介绍。

葫芦

葫芦,诞生于1992年8月11日,游戏宅,胶佬,爱好摸鱼,一个干过超市收银,工地里搬过砖,当过广告印刷狗,做过电焊铁艺的现役.Net程序员。

这篇文章有 3 个评论

发表回复