kubernetes笔记-PV&PVC

在kube-node1上新建本地目录/mnt/data作为PV,并在目录中新建文件index.hml

[root@kube-node1 ~]#mkdir /mnt/data

[root@kube-node1 ~]#echo 'Hello from Kubernetes storage' > /mnt/data/index.html


新建pv.yaml定义pv  task-pv-volume,其中容量为10Gi,访问模式为单个node上读写挂载,storageClassName为manual


kind: PersistentVolume

apiVersion: v1

metadata:

  name: task-pv-volume

  labels:

    type: local

spec:

  storageClassName: manual   

  capacity:

    storage: 10Gi

  accessModes:

    - ReadWriteOnce          

  hostPath:

path: "/mnt/data"


创建pv

[root@kube-node1 ~]# kubectl apply -f pv.yaml

persistentvolume/task-pv-volume created


查看pv,可以看到yaml中定义的参数及reclaim策略为Retain

[root@kube-node1 ~]# kubectl get pv

NAME             CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE

task-pv-volume      10Gi       RWO            Retain           Available           manual                  118s


新建pvc.yaml定义pvc task-pv-claim,读写模式、storageClassName与PV一致,requests小于PV

kind: PersistentVolumeClaim

apiVersion: v1

metadata:

  name: task-pv-claim

spec:

  storageClassName: manual

  accessModes:

    - ReadWriteOnce

  resources:

    requests:

      storage: 3Gi


创建PVC资源

[root@kube-node1 ~]# kubectl apply -f pvc.yaml

persistentvolumeclaim/task-pv-claim created


查看PVC,可以看到PVC的容量为实际匹配到的PV的容量。PVC会匹配大于等于“pvc请求资源”中并最接近请求资源的那个PV

[root@kube-node1 ~]# kubectl get pvc

NAME            STATUS   VOLUME           CAPACITY   ACCESS MODES   STORAGECLASS   AGE

task-pv-claim   Bound    task-pv-volume   10Gi       RWO            manual         35s


新建pod定义文件pv-pvc-pod.yaml挂载PVC

kind: Pod

apiVersion: v1

metadata:

  name: task-pv-pod

spec:

  volumes:

    - name: task-pv-storage

      persistentVolumeClaim:

       claimName: task-pv-claim

  containers:

    - name: task-pv-container

      image: nginx

      ports:

        - containerPort: 80

          name: "http-server"

      volumeMounts:

        - mountPath: "/usr/share/nginx/html"

          name: task-pv-storage


创建Pod task-pv-pod

[root@kube-node1 ~]# kubectl apply -f pv-pvc-pod.yaml  

pod/task-pv-pod created


查看Pod task-pv-pod

[root@kube-node1 ~]# kubectl get pods task-pv-pod

NAME          READY   STATUS    RESTARTS   AGE

task-pv-pod   1/1     Running   0          24s


进入Pod查看PV挂载目录,并没有发现文件index.html

[root@kube-node1 ~]# kubectl exec -it task-pv-pod -- /bin/bash

root@task-pv-pod:/# cd /usr/share/nginx/html

root@task-pv-pod:/usr/share/nginx/html# ls


检查发现pod调度到了kube-node2,kube-node2上没有创建相关目录和文件

[root@kube-node1 ~]# kubectl get pods -o wide

NAME                      READY   STATUS    RESTARTS   AGE     IP             NODE         NOMINATED NODE

task-pv-pod               1/1     Running   0          2m24s   10.244.1.108   kube-node2   


修改pv-pvc-pod.yaml,选择在kube-node1上运行Pod,删除原有pod并重新启动Pod               

kind: Pod

apiVersion: v1

metadata:

  name: task-pv-pod

spec:

  nodeSelector:

    kubernetes.io/hostname: kube-node1

  volumes:

    - name: task-pv-storage

      persistentVolumeClaim:

       claimName: task-pv-claim

  containers:

    - name: task-pv-container

      image: nginx

      ports:

        - containerPort: 80

          name: "http-server"

      volumeMounts:

        - mountPath: "/usr/share/nginx/html"

          name: task-pv-storage


重新进入pod,可以看到index.html已经存在

[root@kube-node1 ~]# kubectl exec -it task-pv-pod -- /bin/sh   

#

# cd /usr/share/nginx/html

# ls

index.html

# cat index.html      

Hello from Kubernetes storage

#cd ..

# ls -l

total 0

drwxr-xr-x 2 root root 24 Apr  1 03:10 html


[if !supportLists]Ø [endif]存储对象的保护

如果删除正在使用的PVC,不会立即删除PVC,直到没有任何Pod使用PVC;如果删除已经绑定PVC的PV,不会立即删除PVC,直到没有任何PVC使用PV。

删除pvc task-pv-claim,可以发现删除无法完成。如果此时按Ctrl+C将无法终止,删除命令已经提交,当保护不再生效时会删除。


查看pvc和pv都可以看到 Finalizers:[kubernetes.io/pvc-protection]


kubernetes笔记-PV&PVC_第1张图片
kubernetes笔记-PV&PVC_第2张图片


由于PVC没有正常删除,删除pv同样无法完成


查看pv状态,可以发现状态变为Terminating


kubernetes笔记-PV&PVC_第3张图片

当pod删除后,pv和pvc都可以完成删除。


[if !supportLists]Ø [endif]PV的回收

Retained: PVC删除后,变为released,不能再次被claim,需要手动回收:删除PV及相关文件和存储上的对象


kubernetes笔记-PV&PVC_第4张图片

Recycled:

Deleted:需要与云存储配合


[if !supportLists]Ø [endif]访问模式

ReadWriteOnce – 单节点读写挂载

ReadOnlyMany – 多节点只读挂载

ReadWriteMany – 多节点读写挂载

注意:一个Volume一次只能使用一个访问模式装载.


[if !supportLists]Ø [endif]PV的状态

Available –空闲的,还没有被使用

Bound – volume已经绑定到claim

Released – claim已经删除,但是资源还没有回收

Failed –自动回收失败

你可能感兴趣的:(kubernetes笔记-PV&PVC)