K8S-StorageClass

1.概念

2. helm安装storageclass

2.1 环境准备

  • NFS服务器已搭建
    ip:10.252.97.213
    使用存储目录:/data/nfs/storage

  • helm已安装

2.2 安装storageclass

[root@DoM01 ~]# git clone https://github.com/helm/charts.git
[root@DoM01 ~]# cd charts/
[root@DoM01 ~]# helm install stable/nfs-client-provisioner --set nfs.server=10.252.97.213 --set nfs.path=/data/nfs/storage 
[root@DoM01 ~]# git clone https://github.com/helm/charts.git
[root@DoM01 ~]# cd charts/stable/nfs-client-provisioner 
[root@DoM01 ~]# helm install storageclass  --set nfs.server=10.252.97.213 --set nfs.path=/data/nfs/storage  -n storageclass   ./ 
  • 查看结果
[root@DoM01 ~]# kubectl get storageclasses.storage.k8s.io
NAME                   PROVISIONER                                         AGE
nfs-client    cluster.local/storageclass-nfs-client-provisioner   1d

2.3 测试

  • 创建 test_pvc.yaml文件
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: test-claim1
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Mi
  storageClassName: nfs-client
  • 创建测试pvc
# kubectl create -f test_pvc.yaml
  • 查看结果
[root@DoM01 storageclass]# kubectl get pvc
NAME          STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
test-claim1   Bound    pvc-0d1ee112-bf86-45cc-afe0-c03c014edb43   1Mi        RWX            nfs-client     1d

2.4 设置默认storageclass

  • 设置默认值
# kubectl patch storageclass nfs-client -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'
  • 查看
[root@DoM01 ~]# kubectl get storageclasses.storage.k8s.io
NAME                   PROVISIONER                                         AGE
nfs-client (default)   cluster.local/storageclass-nfs-client-provisioner   1d
  • 删除默认值
# kubectl patch storageclass nfs-client -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"false"}}}'

3. 手动安装storageclass

说明:nfs服务器准备同上

3.1 Provisioner(供应者)

创建 nfs-client.yaml文件如下:

kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name: nfs-client-provisioner
spec:
  replicas: 1
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: nfs-client-provisioner
    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
            - name: NFS_SERVER
              value: 10.252.97.208
            - name: NFS_PATH
              value: /data/nfs/storage
      volumes:
        - name: nfs-client-root
          nfs:
            server: 10.252.97.208
            path: /data/nfs/storage
  • 权限

创建nfs-client-sa.yaml文件:

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: test-claim1
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storagapiVersion: 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

3.3 创建storageclass

创建storageclass.yaml 文件:

# cat storageclass.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: df-storage 
provisioner: fuseim.pri/ifs  #对应供应者中变量 PROVISIONER_NAME 的值

3.4 测试和设置默认值

同 "2. helm安装storageclass"

4. 创建多个storageclass

如果供应者不变,直接创建storageclass即可

  • 创建storageclass02.yaml文件:
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: hello-world
provisioner: fuseim.pri/ifs
  • 启动
# kubectl create -f storageclass02.yaml

5. 更改nfs存储

  • 方法一
    更改storageclass.yaml文件后,用 apply命令修改即可
  • 方法二
    edit直接修改供应者的deployment

FAQ

《【FAQ】1.21创建PV报错》


你可能感兴趣的:(K8S-StorageClass)