NFS 动态卷供应

用 nfs 创建动态卷供应

1、在NFS(192.168.2.3)服务器创建目录/home/k8s-nfs, 并共享这个目录。

[root@nfs-server ~]# cat /etc/exports
/home/k8s-nfs *(rw,sync,no_root_squash)

[root@nfs-server ~]# exportfs -avr
exporting *:/home/k8s-nfs

2、在master节点安装 git 客户端工具

[root@master ~]# yum install git -y

3、克隆项目并进入到目录。

[root@master ~]#  git clone https://github.com/kubernetes-incubator/external-storage.git
[root@master ~]#  cd external-storage-master/nfs-client/deploy/
[root@master deploy]# ls 
class.yaml  deployment.yaml  rbac.yaml  test-claim.yaml  test-pod.yaml

4、部署 rbac 权限

[root@master deploy]# cat rbac.yaml 
apiVersion: v1
kind: ServiceAccount
metadata:
  name: nfs-client-provisioner
  # replace with namespace where provisioner is deployed
  namespace: default
---
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
    # replace with namespace where provisioner is deployed
    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
  # replace with namespace where provisioner is deployed
  namespace: default
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
  # replace with namespace where provisioner is deployed
  namespace: default
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

[root@master deploy]# kubectl apply -f rbac.yaml

5、部署 nfs 分配器
修改deployment.yaml文件:

[root@master deploy]# cat deployment.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nfs-client-provisioner
  labels:
    app: nfs-client-provisioner
  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
          imagePullPolicy: IfNotPresent
          volumeMounts:
            - name: nfs-client-root
              mountPath: /persistentvolumes
          env:
            - name: PROVISIONER_NAME
              value: fuseim.pri/ifs
            - name: NFS_SERVER
              value: 192.168.2.3            # 修改为自己nfs服务器地址
            - name: NFS_PATH
              value: /home/k8s-nfs      # 修改为自己的NFS共享目录
      volumes:
        - name: nfs-client-root
          nfs:
            server: 192.168.2.3    # 修改为自己的NFS服务器地址
            path: /home/k8s-nfs   # 修改为自己的NFS共享目录

创建 nfs 分配器:

[root@master deploy]# kubectl apply -f deployment.yaml 
deployment.apps/nfs-client-provisioner created

查看POD

[root@master deploy]# kubectl get deployment
NAME                     READY   UP-TO-DATE   AVAILABLE   AGE
nfs-client-provisioner   1/1     1            1           5s
[root@master deploy]# kubectl get pod
NAME                                     READY   STATUS    RESTARTS   AGE
nfs-client-provisioner-d7b876856-dgvd5   1/1     Running   0          11s

6、部署 storageClass:
修改class.yaml 文件

[root@master deploy]# cat class.yaml 
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: nfs-storage
  storageclass.kubernetes.io/is-default-class: "true"   # 设置默认storageclass
provisioner: fuseim.pri/ifs # or choose another name, must match deployment's env PROVISIONER_NAME'
parameters:
  archiveOnDelete: "false"

创建 storageClass

[root@master deploy]# kubectl apply -f class.yaml 
storageclass.storage.k8s.io/nfs-storage created

查看storageClass:

# kubectl get sc
NAME                    PROVISIONER      RECLAIMPOLICY   VOLUMEBINDINGMODE   ALLOWVOLUMEEXPANSION   AGE
nfs-storage (default)   fuseim.pri/ifs   Delete          Immediate           false                  29m

7、创建 pvc
创建pvc.yaml 文件

[root@master deploy]# cat pvc.yaml 
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: nfs-pvc
  annotations:
    volume.beta.kubernetes.io/storage-class: "nfs-storage"
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Gi

创建pvc

[root@master deploy]# kubectl apply -f pvc.yaml 
persistentvolumeclaim/nfs-pvc created

查看pvc

[root@master deploy]# kubectl get pvc
NAME         STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS          AGE
nfs-pvc      Bound    pvc-9ed5add2-41f3-42b1-b9b8-e44e133dc547   1Gi        RWX            nfs-storage           3s

8、创建一个nginx pod,并挂载pvc
创建pod.yaml文件

[root@master deploy]# cat pod.yanl 
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-test
        mountPath: "/mnt"
  restartPolicy: "Never"
  volumes:
    - name: nfs-pvc-test
      persistentVolumeClaim:
        claimName: nfs-pvc

创建pod

[root@master deploy]# kubectl apply -f pvc-pod.yanl 
pod/test-pod created

查看pod

[root@master deploy]# kubectl get pods
NAME                                     READY   STATUS      RESTARTS   AGE
nfs-client-provisioner-d7b876856-dgvd5   1/1     Running     0          12m
test-pod                                 0/1     Completed   0          12s

8、验证
在nfs服务器查看,在/home/k8s-nfs/目录会自动创建一个default-nfs-pvc-pvc-9ed5add2-41f3-42b1-b9b8-e44e133dc547动态卷供pod使用。

[root@nfs-server ~]# ls  /home/k8s-nfs/default-nfs-pvc-pvc-9ed5add2-41f3-42b1-b9b8-e44e133dc547/
SUCCESS

9、扩展
创建pvc时一次处于Pending状态

# kubectl describe pvc nfs-pvc
Name:          nfs-pvc
Namespace:     default
StorageClass:  nfs-storage
Status:        Pending
Volume:        
Labels:        <none>
Annotations:   volume.beta.kubernetes.io/storage-class: nfs-storage
               volume.beta.kubernetes.io/storage-provisioner: fuseim.pri/ifs
Finalizers:    [kubernetes.io/pvc-protection]
Capacity:      
Access Modes:  
VolumeMode:    Filesystem
Used By:       <none>
Events:
  Type    Reason                Age                 From                         Message
  ----    ------                ----                ----                         -------
  Normal  ExternalProvisioning  11s (x9 over 114s)  persistentvolume-controller  waiting for a volume to be created, either by external provisioner "fuseim.pri/ifs" or manually created by system administrator

注意:如果是v1.20版本以上apiserver默认禁止使用selfLink,需要手动配置- --feature-gates=RemoveSelfLink=false 开启。
修改kube-apiserver.yaml 文件:

# cat /etc/kubernetes/manifests/kube-apiserver.yaml 
apiVersion: v1
kind: Pod
metadata:
  annotations:
    kubeadm.kubernetes.io/kube-apiserver.advertise-address.endpoint: 192.168.2.10:6443
  creationTimestamp: null
  labels:
    component: kube-apiserver
    tier: control-plane
  name: kube-apiserver
  namespace: kube-system
spec:
  containers:

  - command:
    - kube-apiserver
    - --advertise-address=192.168.2.10
    - --allow-privileged=true
    - --authorization-mode=Node,RBAC
    - --feature-gates=RemoveSelfLink=false   # 添加这条
    ... ...

然后,重启kubelet服务:

 systemctl restart kubelet
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐