k8sK8K8s

K8S-StorageClass

2021-06-26  本文已影响0人  玄德公笔记

1.概念

2. helm安装storageclass

2.1 环境准备

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 测试

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: test-claim1
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Mi
  storageClassName: nfs-client
# 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即可

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: hello-world
provisioner: fuseim.pri/ifs
# kubectl create -f storageclass02.yaml

5. 更改nfs存储


FAQ

《【FAQ】1.21创建PV报错》


上一篇下一篇

猜你喜欢

热点阅读