k8s ------Storage volumes (PV, PVC)

Table of contents

1: Why do you need storage volumes?

2: emptyDir storage volume 

Three: hostPath storage volume 

Four: nfs shared storage volume 

 Five: PVC and PV

1. Introduction to PVC and PV

2. The interaction between PV and PVC follows the life cycle

3. 4 states of PV

4. The specific process of a PV from creation to destruction

Six: statically create pv and pvc resources and use them by pod

1. Configure nfs storage

2. Define PV

3. Define PVC

4. Test access

7. StorageClass + nfs-client-provisioner builds dynamically created pv

1. Create a shared directory

2. Create Service Account

 3. Use Deployment to create NFS Provisioner

 4. Create StorageClass

 5. Create PVC and Pod tests


1: Why do you need storage volumes?

The life cycle of files on the container disk is short-lived, which causes some problems when running important applications in the container. First, when a container crashes, the kubelet will restart it, but the files in the container will be lost - the container is restarted in a clean state (the original state of the image). Secondly, when multiple containers are running simultaneously in a Pod, files usually need to be shared between these containers. The Volume abstraction in Kubernetes solves these problems very well. Containers in the Pod share the Volume through the Pause container.

2: emptyDir storage volume 

When a Pod is assigned to a node, the emptyDir volume is first created and exists as long as the Pod is running on that node. As the name of the volume states, it is initially empty. Containers in a Pod can read and write the same files in the emptyDir volume, although the volume can be mounted to the same or different paths in each container. When a Pod is removed from a node for any reason, the data in emptyDir is permanently deleted.

emptyDir can share directory data between containers in a Pod, but the emptyDir volume cannot persist data and will be deleted when the Pod life cycle ends.

mkdir /opt/volumes
cd /opt/volumes

vim pod-emptydir.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: pod-emptydir
  namespace: default
  labels:
    app: myapp
    tier: frontend
spec:
  containers:
  - name: myapp
    image: ikubernetes/myapp:v1
    imagePullPolicy: IfNotPresent
    ports:
    - name: http
      containerPort: 80
    #定义容器挂载内容
    volumeMounts:
    #使用的存储卷名称,如果跟下面volume字段name值相同,则表示使用volume的这个存储卷
    - name: html
      #挂载至容器中哪个目录
      mountPath: /usr/share/nginx/html/
  - name: busybox
    image: busybox:latest
    imagePullPolicy: IfNotPresent
    volumeMounts:
    - name: html
      #在容器内定义挂载存储名称和挂载路径
      mountPath: /data/
    command: ['/bin/sh','-c','while true;do echo $(date) >> /data/index.html;sleep 2;done']
  #定义存储卷
  volumes:
  #定义存储卷名称  
  - name: html
    #定义存储卷类型
    emptyDir: {}
    
    
kubectl apply -f pod-emptydir.yaml

kubectl get pods -o wide
NAME           READY   STATUS    RESTARTS   AGE   IP            NODE     NOMINATED NODE   READINESS GATES
pod-emptydir   2/2     Running   0          36s   10.244.2.19   node02   <none>           <none>

//在上面定义了2个容器,其中一个容器是输入日期到index.html中,然后验证访问nginx的html是否可以获取日期。以验证两个容器之间挂载的emptyDir实现共享。
curl 10.244.2.19
Thu May 27 18:17:11 UTC 2021
Thu May 27 18:17:13 UTC 2021
Thu May 27 18:17:15 UTC 2021
Thu May 27 18:17:17 UTC 2021
Thu May 27 18:17:19 UTC 2021
Thu May 27 18:17:21 UTC 2021
Thu May 27 18:17:23 UTC 2021


Three: hostPath storage volume 

The hostPath volume mounts files or directories in the node's file system to the cluster.
hostPath can achieve persistent storage, but it will also cause data loss when the node node fails.

Mount the directories/files on the Node node into the container to achieve persistent data storage. However, the storage space will be limited by the single machine of the Node node. If the Node node fails, the data will be lost, and the Pod cannot share data across nodes.

//在 node01 节点上创建挂载目录
mkdir -p /data/pod/volume1
echo 'node01.kgc.com' > /data/pod/volume1/index.html

//在 node02 节点上创建挂载目录
mkdir -p /data/pod/volume1
echo 'node02.kgc.com' > /data/pod/volume1/index.html

//创建 Pod 资源
vim pod-hostpath.yaml
apiVersion: v1
kind: Pod
metadata:
  name: pod-hostpath
  namespace: default
spec:
  containers:
  - name: myapp
    image: ikubernetes/myapp:v1
    #定义容器挂载内容
    volumeMounts:
    #使用的存储卷名称,如果跟下面volume字段name值相同,则表示使用volume的这个存储卷
    - name: html
      #挂载至容器中哪个目录
      mountPath: /usr/share/nginx/html
      #读写挂载方式,默认为读写模式false
      readOnly: false
  #volumes字段定义了paues容器关联的宿主机或分布式文件系统存储卷
  volumes:
    #存储卷名称
    - name: html
      #路径,为宿主机存储路径
      hostPath:
        #在宿主机上目录的路径
        path: /data/pod/volume1
        #定义类型,这表示如果宿主机没有此目录则会自动创建
        type: DirectoryOrCreate


kubectl apply -f pod-hostpath.yaml

//访问测试
kubectl get pods -o wide
NAME           READY   STATUS    RESTARTS   AGE   IP            NODE     NOMINATED NODE   READINESS GATES
pod-hostpath   2/2     Running   0          37s   10.244.2.35   node02   <none>           <none>

curl 10.244.2.35
node02.kgc.com

//删除pod,再重建,验证是否依旧可以访问原来的内容
kubectl delete -f pod-hostpath.yaml  
kubectl apply -f pod-hostpath.yaml 

kubectl get pods -o wide
NAME           READY   STATUS    RESTARTS   AGE   IP            NODE     NOMINATED NODE   READINESS GATES
pod-hostpath   2/2     Running   0          36s   10.244.2.37   node02   <none>           <none>

curl  10.244.2.37 
node02.kgc.com

 

Four: nfs shared storage volume 


//在stor01节点上安装nfs,并配置nfs服务
mkdir /data/volumes -p
chmod 777 /data/volumes

vim /etc/exports
/data/volumes 192.168.80.0/24(rw,no_root_squash)

systemctl start rpcbind
systemctl start nfs

showmount -e
Export list for stor01:
/data/volumes 192.168.80.0/24


//master节点操作
vim pod-nfs-vol.yaml
apiVersion: v1
kind: Pod
metadata:
  name: pod-vol-nfs
  namespace: default
spec:
  containers:
  - name: myapp
    image: ikubernetes/myapp:v1
    volumeMounts:
    - name: html
      mountPath: /usr/share/nginx/html
  volumes:
    - name: html
      nfs:
        path: /data/volumes
        server: stor01


kubectl apply -f pod-nfs-vol.yaml

kubectl get pods -o wide
NAME                     READY     STATUS    RESTARTS   AGE       IP            NODE
pod-vol-nfs              1/1       Running   0          21s       10.244.2.38   node02


//在nfs服务器上创建index.html
cd /data/volumes
vim index.html
<h1> nfs stor01</h1>

//master节点操作
curl 10.244.2.38
<h1> nfs stor01</h1>

kubectl delete -f pod-nfs-vol.yaml   #删除nfs相关pod,再重新创建,可以得到数据的持久化存储

kubectl apply -f pod-nfs-vol.yaml

 

 Five: PVC and PV

1. Introduction to PVC and PV

The full name of PV is Persistent Volume, which is a persistent storage volume . It is used to describe or define a storage volume, which is usually defined by operation and maintenance engineers.

The full name of PVC is Persistent Volume Claim, which is a request for persistent storage . It is used to describe what kind of PV storage you want to use or what conditions you want to meet.

The usage logic of PVC: Define a storage volume in the Pod (the storage volume type is PVC), specify the size directly when defining, the PVC must establish a relationship with the corresponding PV, the PVC will apply to the PV according to the configuration definition, and the PV is Created from storage space. PV and PVC are storage resources abstracted by Kubernetes.

A PV can be used by one or more PODs. PV is a dedicated storage resource in a k8s cluster and is a resource object that logically divides the storage device space. Storage resources must provide storage space for storage resources to use and cannot appear out of thin air. What really provides storage space is the storage device, such as the directory mounted on the hard disk, the directory shared by nfs, ceph distributed storage, etc.
As a K8S cluster administrator, we can create a PV in the K8S cluster, and then divide the storage space from the storage device to the PV.
Then which PV my POD wants to reference, I must first define a PVC to describe what kind of PV I want to use. What conditions are met for PV storage, such as how much storage space, whether it is dedicated, one-to-one, or one-to-many? POD will find qualified PVs based on PVC to bind, and finally mount them for POD to use.
The PV and PVC modes introduced above require operation and maintenance personnel to create PVs first, and then developers define PVCs for one-to-one bonding. However, if there are thousands of PVC requests, thousands of PVs need to be created. Maintenance costs are very high for operation and maintenance personnel. Kubernetes provides a mechanism to automatically create PVs called StorageClass, which is used to create PV templates.

Creating a StorageClass requires defining the attributes of the PV, such as storage type, size, etc.; in addition, creating such a PV requires the use of storage plug-ins, such as Ceph, etc. With these two pieces of information, Kubernetes can find the corresponding StorageClass based on the PVC submitted by the user. Then Kubernetes will call the storage plug-in declared by the StorageClass to automatically create the required PV and bind it.

PV is a resource in the cluster. PVC is a request for these resources and an index check of the resources. 

2. The interaction between PV and PVC follows the life cycle

The interaction between PV and PVC follows this life cycle:
Provisioning ---> Binding ---> Using ---> Releasing ---> Recycling

●Provisioning , that is, the creation of PV, you can directly create PV (static method), or you can use StorageClass to dynamically create
●Binding , assign PV to PVC
●Using , Pod uses the Volume through PVC, and can control StorageProtection through admission (1.9 and previous versions are PVCProtection) Prevent deletion of PVC in use
●Releasing , Pod releases the Volume and deletes PVC
●Reclaiming , recycles PV, you can keep the PV for next use, or you can delete it directly from cloud storage

3. 4 states of PV

●Available : Indicates the available status and has not been bound to any PVC
●Bound : Indicates that the PV has been bound to the PVC
●Released : Indicates that the PVC has been deleted, but the resource has not yet been clustered Recycling
●Failed : Indicates that the automatic recycling of the PV failed

4. The specific process of a PV from creation to destruction

1. After a PV is created, its status will change to Available, waiting to be bound by the PVC.
2. Once bound by PVC, the status of PV will change to Bound, and it can be used by Pods with corresponding PVC defined.
3. After the Pod is used, the PV will be released, and the status of the PV will change to Released.
4. The PV that becomes Released will be recycled according to the defined recycling strategy. There are three recycling strategies, Retain, Delete and Recycle. Retain means to retain the scene. The K8S cluster does nothing and waits for the user to manually process the data in the PV. After the processing is completed, the PV is manually deleted. Delete policy, K8S will automatically delete the PV and the data in it. In Recycle mode, K8S will delete the data in the PV, and then change the status of the PV to Available, which can then be bound and used by new PVCs.

kubectl explain pv    #查看pv的定义方式
FIELDS:
    apiVersion: v1
    kind: PersistentVolume
    metadata:    #由于 PV 是集群级别的资源,即 PV 可以跨 namespace 使用,所以 PV 的 metadata 中不用配置 namespace
      name: 
    spec
    
kubectl explain pv.spec    #查看pv定义的规格
spec:
  nfs:(定义存储类型)
    path:(定义挂载卷路径)
    server:(定义服务器名称)
  accessModes:(定义访问模型,有以下三种访问模型,以列表的方式存在,也就是说可以定义多个访问模式)
    - ReadWriteOnce          #(RWO)卷可以被一个节点以读写方式挂载。 ReadWriteOnce 访问模式也允许运行在同一节点上的多个 Pod 访问卷。
    - ReadOnlyMany           #(ROX)卷可以被多个节点以只读方式挂载。
    - ReadWriteMany          #(RWX)卷可以被多个节点以读写方式挂载。
#nfs 支持全部三种;iSCSI 不支持 ReadWriteMany(iSCSI 就是在 IP 网络上运行 SCSI 协议的一种网络存储技术);HostPath 不支持 ReadOnlyMany 和 ReadWriteMany。
  capacity:(定义存储能力,一般用于设置存储空间)
    storage: 2Gi (指定大小)
  storageClassName: (自定义存储类名称,此配置用于绑定具有相同类别的PVC和PV)
  persistentVolumeReclaimPolicy: Retain    #回收策略(Retain/Delete/Recycle)
#Retain(保留):当用户删除与之绑定的PVC时候,这个PV被标记为released(PVC与PV解绑但还没有执行回收策略)且之前的数据依然保存在该PV上,但是该PV不可用,需要手动来处理这些数据并删除该PV。
#Delete(删除):删除与PV相连的后端存储资源。对于动态配置的PV来说,默认回收策略为Delete。表示当用户删除对应的PVC时,动态配置的volume将被自动删除。(只有 AWS EBS, GCE PD, Azure Disk 和 Cinder 支持)
#Recycle(回收):如果用户删除PVC,则删除卷上的数据,卷不会删除。(只有 NFS 和 HostPath 支持)

kubectl explain pvc   #查看PVC的定义方式
KIND:     PersistentVolumeClaim
VERSION:  v1
FIELDS:
   apiVersion    <string>
   kind    <string>  
   metadata    <Object>
   spec    <Object>

#PV和PVC中的spec关键字段要匹配,比如存储(storage)大小、访问模式(accessModes)、存储类名称(storageClassName)
kubectl explain pvc.spec
spec:
  accessModes: (定义访问模式,必须是PV的访问模式的子集)
  resources:
    requests:
      storage: (定义申请资源的大小)
  storageClassName: (定义存储类名称,此配置用于绑定具有相同类别的PVC和PV)

Six: statically create pv and pvc resources and use them by pod


1. Configure nfs storage

mkdir v{1,2,3,4,5}

vim /etc/exports
/data/volumes/v1 192.168.231.0/24(rw,no_root_squash)
/data/volumes/v2 192.168.231.0/24(rw,no_root_squash)
/data/volumes/v3 192.168.231.0/24(rw,no_root_squash)
/data/volumes/v4 192.168.231.0/24(rw,no_root_squash)
/data/volumes/v5 192.168.231.0/24(rw,no_root_squash)

exportfs -arv

showmount -e


2. Define PV

//这里定义5个PV,并且定义挂载的路径以及访问模式,还有PV划分的大小。
vim pv-demo.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv001
  labels:
    name: pv001
spec:
  nfs:
    path: /data/volumes/v1
    server: stor01
  accessModes: ["ReadWriteMany","ReadWriteOnce"]
  capacity:
    storage: 1Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv002
  labels:
    name: pv002
spec:
  nfs:
    path: /data/volumes/v2
    server: stor01
  accessModes: ["ReadWriteOnce"]
  capacity:
    storage: 2Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv003
  labels:
    name: pv003
spec:
  nfs:
    path: /data/volumes/v3
    server: stor01
  accessModes: ["ReadWriteMany","ReadWriteOnce"]
  capacity:
    storage: 2Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv004
  labels:
    name: pv004
spec:
  nfs:
    path: /data/volumes/v4
    server: stor01
  accessModes: ["ReadWriteMany","ReadWriteOnce"]
  capacity:
    storage: 4Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv005
  labels:
    name: pv005
spec:
  nfs:
    path: /data/volumes/v5
    server: stor01
  accessModes: ["ReadWriteMany","ReadWriteOnce"]
  capacity:
    storage: 5Gi

 


kubectl apply -f pv-demo.yaml

kubectl get pv
NAME      CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM     STORAGECLASS   REASON    AGE
pv001     1Gi        RWO,RWX        Retain           Available                                      7s
pv002     2Gi        RWO            Retain           Available                                      7s
pv003     2Gi        RWO,RWX        Retain           Available                                      7s
pv004     4Gi        RWO,RWX        Retain           Available                                      7s
pv005     5Gi        RWO,RWX        Retain           Available                                       7s


3. Define PVC

//这里定义了pvc的访问模式为多路读写,该访问模式必须在前面pv定义的访问模式之中。定义PVC申请的大小为2Gi,此时PVC会自动去匹配多路读写且大小为2Gi的PV,匹配成功获取PVC的状态即为Bound
vim pod-vol-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mypvc
  namespace: default
spec:
  accessModes: ["ReadWriteMany"]
  resources:
    requests:
      storage: 2Gi
---
apiVersion: v1
kind: Pod
metadata:
  name: pod-vol-pvc
  namespace: default
spec:
  containers:
  - name: myapp
    image: ikubernetes/myapp:v1
    volumeMounts:
    - name: html
      mountPath: /usr/share/nginx/html
  volumes:
    - name: html
      persistentVolumeClaim:
        claimName: mypvc

 


kubectl apply -f pod-vol-pvc.yaml

kubectl get pv
NAME      CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM           STORAGECLASS   REASON    AGE
pv001     1Gi        RWO,RWX        Retain           Available                                            19m
pv002     2Gi        RWO            Retain           Available                                            19m
pv003     2Gi        RWO,RWX        Retain           Bound       default/mypvc                            19m
pv004     4Gi        RWO,RWX        Retain           Available                                            19m
pv005     5Gi        RWO,RWX        Retain           Available                                            19m

kubectl get pvc
NAME      STATUS    VOLUME    CAPACITY   ACCESS MODES   STORAGECLASS   AGE
mypvc     Bound     pv003     2Gi        RWO,RWX                       22s

4. Test access

//在存储服务器上创建index.html,并写入数据,通过访问Pod进行查看,可以获取到相应的页面。
cd /data/volumes/v3/
echo "welcome to use pv3" > index.html

kubectl get pods -o wide
pod-vol-pvc             1/1       Running   0          3m        10.244.2.39   k8s-node02

curl  10.244.2.39
welcome to use pv3

7. StorageClass + nfs-client-provisioner builds dynamically created pv

Understanding StorageClass + nfs-client-provisioner

The dynamic PV creation supported by Kubernetes itself does not include NFS, so you need to use an external storage volume plug-in to allocate PV. For details, see: https://kubernetes.io/zh/docs/concepts/storage/storage-classes/

The volume plug-in is called Provisioner (storage allocator), and NFS uses nfs-client. This external volume plug-in will automatically create a PV using the configured NFS server.
Provisioner: used to specify the type of Volume plug-in, including built-in plug-ins (such as kubernetes.io/aws-ebs) and external plug-ins (such as ceph.com/cephfs provided by external-storage). 1. Install nfs on the stor01 node and configure the nfs service

1. Create a shared directory

mkdir /opt/k8s
chmod 777 /opt/k8s/

vim /etc/exports
/opt/k8s 192.168.231.0/24(rw,no_root_squash,sync)

systemctl restart nfs

2. Create Service Account

Used to manage the permissions of NFS Provisioner running in k8s cluster, and set nfs-client rules for PV, PVC, StorageClass, etc.

vim nfs-client-rbac.yaml
#创建 Service Account 账户,用来管理 NFS Provisioner 在 k8s 集群中运行的权限
apiVersion: v1
kind: ServiceAccount
metadata:
  name: nfs-client-provisioner
---
#创建集群角色
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: nfs-client-provisioner-clusterrole
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: ["list", "watch", "create", "update", "patch"]
  - apiGroups: [""]
    resources: ["endpoints"]
    verbs: ["create", "delete", "get", "list", "watch", "patch", "update"]
---
#集群角色绑定
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: nfs-client-provisioner-clusterrolebinding
subjects:
- kind: ServiceAccount
  name: nfs-client-provisioner
  namespace: default
roleRef:
  kind: ClusterRole
  name: nfs-client-provisioner-clusterrole
  apiGroup: rbac.authorization.k8s.io

 


kubectl apply -f nfs-client-rbac.yaml

 

 

 

 

 3. Use Deployment to create NFS Provisioner

NFS Provisioner (ie nfs-client) has two functions: one is to create a mount point (volume) in the NFS shared directory, and the other is to associate the PV with the NFS mount point.

 

#由于 1.20 版本启用了 selfLink,所以 k8s 1.20+ 版本通过 nfs provisioner 动态生成pv会报错,解决方法如下:
vim /etc/kubernetes/manifests/kube-apiserver.yaml
spec:
  containers:
  - command:
    - kube-apiserver
    - --feature-gates=RemoveSelfLink=false       #添加这一行
    - --advertise-address=192.168.80.20
......

kubectl apply -f /etc/kubernetes/manifests/kube-apiserver.yaml
kubectl delete pods kube-apiserver -n kube-system 
kubectl get pods -n kube-system | grep apiserver

#创建 NFS Provisioner
vim nfs-client-provisioner.yaml
kind: Deployment
apiVersion: apps/v1
metadata:
  name: nfs-client-provisioner
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nfs-client-provisioner
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: nfs-client-provisioner
    spec:
      serviceAccountName: nfs-client-provisioner         #指定Service Account账户
      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: nfs-storage       #配置provisioner的Name,确保该名称与StorageClass资源中的provisioner名称保持一致
            - name: NFS_SERVER
              value: stor01           #配置绑定的nfs服务器
            - name: NFS_PATH
              value: /opt/k8s          #配置绑定的nfs服务器目录
      volumes:              #申明nfs数据卷
        - name: nfs-client-root
          nfs:
            server: stor01
            path: /opt/k8s
    
    
kubectl apply -f nfs-client-provisioner.yaml 

 

kubectl get pod
NAME                                   READY   STATUS    RESTARTS   AGE
nfs-client-provisioner-cd6ff67-sp8qd   1/1     Running   0          14s

 

 4. Create StorageClass

Responsible for establishing PVC and calling NFS provisioner to perform scheduled work, and associate PV with PVC

vim nfs-client-storageclass.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: nfs-client-storageclass
provisioner: nfs-storage     #这里的名称要和provisioner配置文件中的环境变量PROVISIONER_NAME保持一致
parameters:
  archiveOnDelete: "false"   #false表示在删除PVC时不会对数据目录进行打包存档,即删除数据;为ture时就会自动对数据目录进行打包存档,存档文件以archived开头
  
  
kubectl apply -f nfs-client-storageclass.yaml

kubectl get storageclass
NAME                      PROVISIONER   RECLAIMPOLICY   VOLUMEBINDINGMODE   ALLOWVOLUMEEXPANSION   AGE
nfs-client-storageclass   nfs-storage   Delete          Immediate           false                  43s

 5. Create PVC and Pod tests

vim test-pvc-pod.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: test-nfs-pvc
  #annotations: volume.beta.kubernetes.io/storage-class: "nfs-client-storageclass"     #另一种SC配置方式
spec:
  accessModes:
    - ReadWriteMany
  storageClassName: nfs-client-storageclass    #关联StorageClass对象
  resources:
    requests:
      storage: 1Gi
---
apiVersion: v1
kind: Pod
metadata:
  name: test-storageclass-pod
spec:
  containers:
  - name: busybox
    image: busybox:latest
    imagePullPolicy: IfNotPresent
    command:
    - "/bin/sh"
    - "-c"
    args:
    - "sleep 3600"
    volumeMounts:
    - name: nfs-pvc
      mountPath: /mnt
  restartPolicy: Never
  volumes:
  - name: nfs-pvc
    persistentVolumeClaim:
      claimName: test-nfs-pvc      #与PVC名称保持一致
      
      
kubectl apply -f test-pvc-pod.yaml

//PVC 通过 StorageClass 自动申请到空间
kubectl get pvc
NAME            STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS              AGE
test-nfs-pvc   Bound    pvc-11670f39-782d-41b8-a842-eabe1859a456   1Gi        RWX            nfs-client-storageclass   2s

//查看 NFS 服务器上是否生成对应的目录,自动创建的 PV 会以 ${namespace}-${pvcName}-${pvName} 的目录格式放到 NFS 服务器上
ls /opt/k8s/
default-test-nfs-pvc-pvc-11670f39-782d-41b8-a842-eabe1859a456

//进入 Pod 在挂载目录 /mnt 下写一个文件,然后查看 NFS 服务器上是否存在该文件
kubectl exec -it test-storageclass-pod sh
/ # cd /mnt/
/mnt # echo 'this is test file' > test.txt

//发现 NFS 服务器上存在,说明验证成功
cat /opt/k8s/test.txt

cp pvc.yaml pvc1.yaml
vim pvc1.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mypvc02-nfs
spec:
  accessModes:
    - ReadOnlyMany
  resources:
    requests:
      storage: 2Gi
  storageClassName: nfs-client-storageclass

 

 

 

Guess you like

Origin blog.csdn.net/A1100886/article/details/132231454