preface

NFS is usually used to create a k8S cluster or persistent storage. For easy management, nfS-client-provisinoner is usually installed to dynamically create storage. The following describes its installation and use.

The environment

The host name ip role
mldong01 192.168.0.245 master
mldong02 192.168.0.54 node01
mldong03 192.168.0.22 node02

Three hosts are HUAWEI ECS, CentOS Linux release 7.6.1810 (Core)

Preparations before Installation

The NFS client is installed on all nodes

yum install -y nfs-utils
Copy the code

Select an NFS server

Perform the following configuration (select mlDONG01 node here) :

  1. Creating a root directory

    mkdir -p /mnt/nfs
    Copy the code
  2. The editor

    Vim/etc/exports/MNT/NFS / 192.168.0.0/24 (rw, sync and fsid = 0)Copy the code
    • A host with the same network number as 192.168.0.0/24 can mount the/MNT/NFS/directory on the NFS server to its own file system
    • Rw stands for read-write
    • Sync: Indicates synchronous write
    • Fsid =0 wraps/MNT into a root directory
  3. Starting the NFS Service

    systemctl start rpcbind.service
    systemctl start nfs-server.service
    Copy the code
  4. Set boot to start automatically

    systemctl enable rpcbind.service
    systemctl enable nfs-server.service
    Copy the code
  5. Taking Effect using the Configuration

    exportfs -r
    Copy the code
  6. View the mounting status

    exportfs
    Copy the code

Validate on another node

Select MLdong02 here

Mkdir -p/MNT/NFS mount -t NFS 192.168.0.245:/ MNT/NFS/MNT/NFSCopy the code

View the mounting status

df -h | grep /mnt/nfs
Copy the code

You can add a file to the master and see if it is available on node

Unmount (as needed)

umount /mnt/nfs
Copy the code

NFS – provisioner

  1. Add the Helm repository

    helm repo add azure http://mirror.azure.cn/kubernetes/charts/
    Copy the code
  2. Local warehouse search

    helm search repo nfs-client-provisioner
    Copy the code
  3. Start the installation

    Helm Install nfs-storage Azure /nfs-client-provisioner \ --set nfs.server=192.168.0.245 \ --set nfs.path=/ MNT/NFS /k8s \ --set storageClass.name=nfs-storage \ --set storageClass.defaultClass=trueCopy the code

    Description:

    • Nfs. server: indicates the NFS service address
    • Path: indicates the NFS root directory
    • Storageclass. name: indicates the storageClass name
    • StorageClass. DefaultClass: set to the default storage class
  4. Viewing the Installation

    kubectl get sc
    Copy the code

Use the sample

  1. Create a PVC

    #configuration
    cat <<EOF >  pvc-test.yaml
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: pvc-test
    spec:
      storageClassName: "nfs-storage"
      accessModes:
        - ReadWriteMany
      resources:
        requests:
          storage: 10Mi
    EOF
    Copy the code
  2. Execute issue command

    kubectl apply -f pvc-test.yaml
    Copy the code
  3. Check the PVC

    #Check the list
    kubectl get pvc
    #Check the details
    
    Copy the code

    A Pending state is an exception and a Bound state is a success.

    If the message “Permission denied” is displayed, you need to grant permission to the NFS directory 777

    chmod 777 -R /mnt/nfs/k8s
    Copy the code

  4. Viewing an NFS Directory

    A dynamic persistent directory is generated

    The naming rule is as follows: namespace + PVC name + PVC label + random character string

  5. uninstall

    kubectl delete -f pvc-test.yaml
    Copy the code
  6. After uninstallation, the directory will still exist, but with archived drops

    Of course, this is related to the PVC configuration strategy, which will not be expanded here for the time being, but will simply be used first.

  7. Pods use sample

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx
      namespace: mldong-test
    spec:
      selector:
        matchLabels:
          app: nginx
      replicas: 1
      template:
        metadata:
          labels:
            app: nginx
        spec:
          containers:
            - name: nginx
              image: nginx:latest
              imagePullPolicy: IfNotPresent
              ports:
                - containerPort: 80
                  name: port
                  protocol: TCP
              volumeMounts:
                - name: nginx-pvc Volumes. name
                  mountPath: "/usr/share/nginx/html" The directory corresponding to the container
          volumes:
            - name: nginx-pvc Volumemounts.name = volumemounts.name = volumemounts.name
              persistentVolumeClaim: 
                claimName: nginx-pvc # create the PVC name
    Copy the code

summary

This article has only briefly covered the installation of nfS-client-provisioner, but basically with nfs-client-provisioner installed, you are ready to define PVC using the dynamic storage class. Then hang the PVC on the container. Here, I will not talk about more specific cases. Later, when it comes to persistence, I will expand in detail.

Related articles

K8s Intermediate – Installing a K8S cluster using RKE

K8s Intermediate -Helm installation and Introduction

K8s Intermediate – Cert-Manager +Let’s Encrypt Automatic certificate issuing