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

一、安装nfs服务端

cat> deployment.yaml <<EOF
apiVersion: v1
kind: ServiceAccount
metadata:
  name: nfs-client-provisioner
---
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: xxxxxxxxxxxxxxxxxxxx.nas.aliyuncs.com
            - name: NFS_PATH
              value: /
      volumes:
        - name: nfs-client-root
          nfs:
            server: xxxxxxxxxxxxxxxxxxxx.nas.aliyuncs.com
            path: /
EOF

[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 <<EOF
apiVersion: storage.k8s.io/v1
#allowVolumeExpansion: true 开启允许扩容功能,但是nfs类型不支持
kind: StorageClass
metadata:
  name: pointsmart-nfs-storage
mountOptions:
- vers=4
- minorversion=0
- noresvport
provisioner: fuseim.pri/ifs
parameters:
  archiveOnDelete: "false"
EOF

[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 <<EOF
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: test-claim
  annotations:
    volume.beta.kubernetes.io/storage-class: "pointsmart-nfs-storage"
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 2Gi
EOF

[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 <<EOF
kind: Pod
apiVersion: v1
metadata:
  name: test-pod
spec:
  containers:
  - name: test-pod
    image: busybox:1.24
    command:
      - "/bin/sh"
    args:
      - "-c"
      - "touch /mnt/SUCCESS && exit 0 || exit 1"
    volumeMounts:
      - name: nfs-pvc
        mountPath: "/mnt"
  restartPolicy: "Never"
  volumes:
    - name: nfs-pvc
      persistentVolumeClaim:
        claimName: test-claim
EOF

[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 <<EOF
kind: PersistentVolume
apiVersion: v1
metadata:
  name: product-claim-api
  labels:
    pv: product-claim-api
spec:
  capacity:
    storage: 5Gi
  mountOptions:
  - vers=4
  - minorversion=0
  - noresvport
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Delete
  nfs:
    server: xxxxxxxxxxxxxxxxxxxx.nas.aliyuncs.com
    path: "/api"   ###这个文件夹要提前在nfs下面创建好
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: product-claim-api
spec:
  accessModes:
    - ReadWriteMany
  storageClassName: ""
  resources:
    requests:
      storage: 5Gi
  selector:
    matchLabels:
      pv: product-claim-api
EOF

 

Logo

开源、云原生的融合云平台

更多推荐