利用阿里云文件存储nas动态提供Kubernetes后端存储卷

GitHub地址:https://github.com/Idiomroot/k8s-storageClass.git

一、安装nfs服务端

cat> deployment.yaml <

[root@idiom-k8s storageClass]# kubectl  create -f deployment.yaml 

二、rbac授权

cat > rbac.yaml  << EOF
kind: ServiceAccount
apiVersion: v1
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: ["create", "update", "patch"]
---
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
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: leader-locking-nfs-client-provisioner
rules:
  - apiGroups: [""]
    resources: ["endpoints"]
    verbs: ["get", "list", "watch", "create", "update", "patch"]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: leader-locking-nfs-client-provisioner
subjects:
  - kind: ServiceAccount
    name: nfs-client-provisioner
    # replace with namespace where provisioner is deployed
    namespace: default
roleRef:
  kind: Role
  name: leader-locking-nfs-client-provisioner
  apiGroup: rbac.authorization.k8s.io
EOF

[root@idiom-k8s storageClass]# kubectl  create -f rbac.yaml 

三、创建存储类

cat > class.yaml <

[root@idiom-k8s storageClass]#  kubectl  create -f  class.yaml

[root@idiom-k8s storageClass]#  kubectl  get sc
NAME                     PROVISIONER      AGE
pointsmart-nfs-storage   fuseim.pri/ifs   34d

设置pointsmart-nfs-storage sc为后端默认存储类

[root@idiom-k8s storageClass]#kubectl patch storageclass pointsmart-nfs-storage -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'

[root@idiom-k8s storageClass]#   kubectl  get sc
NAME                               PROVISIONER      AGE
pointsmart-nfs-storage (default)   fuseim.pri/ifs   34d

四、创建PVC文件

cat > test-claim.yaml <

[root@idiom-k8 storageClass]# kubectl  create -f  test-claim.yaml
persistentvolumeclaim/test-claim created
[root@idiom-k8s storageClass]# kubectl  get pv | grep test
pvc-2d682f68-bca4-11e9-8520-fa163ea24731   2Gi        RWX            Delete           Bound    default/test-claim                    pointsmart-nfs-storage            1s

启动一个pod测试在test-claim的PV里touch一个SUCCESS文件

cat >  test-pod.yaml <

[root@idiom-k8s storageClass]# kubectl  create -f test-pod.yaml 
pod/test-pod created
[root@idiom-k8s storageClass]# df -Th
Filesystem                                       Type      Size  Used Avail Use% Mounted on
/dev/vda1                                        ext4       40G  7.7G   30G  21% /
devtmpfs                                         devtmpfs  980M     0  980M   0% /dev
tmpfs                                            tmpfs     991M     0  991M   0% /dev/shm
tmpfs                                            tmpfs     991M  116M  875M  12% /run
tmpfs                                            tmpfs     991M     0  991M   0% /sys/fs/cgroup
xxxxxxxxxxxxxxxxxx.nas.aliyuncs.com:/      5.0T  4.8G  5.0T   1% /nfs
tmpfs                                            tmpfs     199M     0  199M   0% /run/user/0
[root@idiom-k8s storageClass]# kubectl get pod|grep test
test-pod                                 0/1     Completed   0          44s
[root@idiom-k8s storageClass]# ls /nfs/default-test-claim-pvc-2d682f68-bca4-11e9-8520-fa163ea24731/
SUCCESS

由此可见,部署正常,并且可以动态分配NFS的共享卷

注意:

不通过存储类,直接创建PV、PVC

cat > nfs.yaml <

 

你可能感兴趣的:(kubernet)