imirsh

导航

利用NFS动态提供Kubernetes后端存储卷

利用 NFS Server 给 Kubernetes 作为持久存储的后端,并且动态提供PV。前提条件是有已经安装好的 NFS 服务器,并且 NFS 服务器与 Kubernetes 的 Slave 节点都能网络连通。

准备 NFS Server

nfs-client-provisioner 是一个Kubernetes的简易NFS的外部provisioner,本身不提供NFS,需要现有的NFS服务器提供存储

  • PV以 ${namespace}-${pvcName}-${pvName} 的命名格式提供(在NFS服务器上)
~]# yum -y install nfs-utils 
~]# mkdir -pv /ifs/kubernetes/
~]# vim /etc/exports
/ifs/kubernetes 192.168.124.0/24(rw,no_root_squash)
~]# for i in start enable;do systemctl $i nfs;done

安装部署

deployment.yaml 需要修改的地方只有NFS服务器所在的IP地址(192.168.124.250),以及NFS服务器共享的路径(/ifs/kubernetes),两处都需要修改为你实际的NFS服务器和共享目录

nfs-client]# wget https://raw.githubusercontent.com/kubernetes-incubator/external-storage/master/nfs-client/deploy/deployment.yaml
nfs-client]# deployment.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nfs-client-provisioner
  labels:
    app: nfs-client-provisioner
  # replace with namespace where provisioner is deployed
  namespace: default
spec:
  replicas: 1
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app: nfs-client-provisioner
  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: 192.168.124.250 # 修改成自己的 IP
            - name: NFS_PATH
              value: /ifs/kubernetes
      volumes:
        - name: nfs-client-root
          nfs:
            server: 192.168.124.250 # 修改成自己的 IP
            path: /ifs/kubernetes

class.yaml 修改provisioner的名字,需要与上面的deployment的PROVISIONER_NAME名字一致,如果没有变动可以不修改。

nfs-client]# wget https://raw.githubusercontent.com/kubernetes-incubator/external-storage/master/nfs-client/deploy/class.yaml

rbac.yaml RBAC授权,保持默认即可

nfs-client]# wget https://raw.githubusercontent.com/kubernetes-incubator/external-storage/master/nfs-client/deploy/rbac.yaml

使用 nfs-client]# kubectl apply -f . 部署以上资源清单文件

测试验证

  • 创建 PVC
test-pod]# kubectl  apply -f 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
  • 创建 POD
test-pod]# vim test-pod.yaml
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
  • 检查共享存储是否有文件生成
~]# ls /ifs/kubernetes/default-test-claim-pvc-3b5014c6-223c-49f8-9836-edf69e9ecd59/
SUCCESS
  • 删除 POD
test-pod]# kubectl  delete -f test-pod.yaml 
  • 删除 PVC
test-pod]# kubectl  delete -f test-claim.yaml 

posted on 2020-07-14 08:33  imirsh  阅读(248)  评论(0编辑  收藏  举报