Kubernetes(k8s)创建NFS动态存储供应

2018-08-23 4343点热度 0人点赞 0条评论

参考链接

云栖社区博文

NFS动态存储供应

官方文档链接

Kubernetes NFS-Client Provisioner

创建NFS服务

参考链接:centos7安装NFS服务

下载NFS-Client Provisioner文件

NFS-Client Provisioner所在目录:external-storage/nfs-client,操作步骤参考此目录下的README.md

git clone https://github.com/kubernetes-incubator/external-storage.git

设置认证

如果集群启用了RBAC,如果namespace不是default,则需要编辑deploy/auth/clusterrolebinding.yaml,这里保持默认。

$ kubectl create -f deploy/auth/serviceaccount.yaml -f deploy/auth/clusterrole.yaml -f deploy/auth/clusterrolebinding.yaml
serviceaccount "nfs-client-provisioner" created
clusterrole "nfs-client-provisioner-runner" created
clusterrolebinding "run-nfs-client-provisioner" created

deploy/auth/serviceaccount.yaml

apiVersion: v1
kind: ServiceAccount
metadata:
  name: nfs-client-provisioner

deploy/auth/clusterrole.yaml

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"]
  - apiGroups: [""]
    resources: ["endpoints"]
    verbs: ["get", "list", "watch", "create", "update", "patch"]

deploy/auth/clusterrolebinding.yaml

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

配置NFS-Client provisioner

deploy/deployment.yaml

编辑deploy/deployment.yaml,修改NFS配置为自己的配置。

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: <YOUR NFS SERVER HOSTNAME>
            - name: NFS_PATH
              value: /var/nfs
      volumes:
        - name: nfs-client-root
          nfs:
            server: <YOUR NFS SERVER HOSTNAME>
            path: /var/nfs

这里需要注意,需要下载镜像quay.io/external_storage/nfs-client-provisioner:latest,如果无法下载,则服务无法运行,因此想办法下载这个镜像到自己的镜像仓库,修改成自己的镜像地址。
PROVISIONER_NAME默认为fuseim.pri/ifs,可以修改,但是以下的storage class也要一起修改。

deploy/class.yaml

如果没有修改过deployment.yaml中的PROVISIONER_NAME,此处可以不做任何修改。需要注意归档策略的选择。

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: managed-nfs-storage
provisioner: fuseim.pri/ifs # or choose another name, must match deployment's env PROVISIONER_NAME'
parameters:
  archiveOnDelete: "false" # When set to "false" your PVs will not be archived
                           # by the provisioner upon deletion of the PVC.

测试是否成功

kubectl create -f deploy/test-claim.yaml -f deploy/test-pod.yaml

test-claim.yaml

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: test-claim
  annotations:
    volume.beta.kubernetes.io/storage-class: "managed-nfs-storage"
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Mi

test-pod.yaml

这里需要下载gcr.io/google_containers/busybox:1.24镜像。

kind: Pod
apiVersion: v1
metadata:
  name: test-pod
spec:
  containers:
  - name: test-pod
    image: gcr.io/google_containers/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

创建成功之后,会在nfs目录下有个claim的目录,目录下会有一个SUCCESS文件,说明已经成功。

定义自己的PersistentVolumeClaims(pvc)

定义自己的pvc的时候要注意storage-class要与deploy/class.yaml中的保持一致:

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: test-claim
  annotations:
    volume.beta.kubernetes.io/storage-class: "managed-nfs-storage"
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Mi

王显锋

激情工作,快乐生活!

文章评论