kubernetes

kubernetes学习笔记-PV/PVC

一、概念

PV即PersistentVolume持久化存储,由管理员定义的一块存储空间,抽象化底层存储细节,这是它与volume最大的区别,pv是集群级别的资源,生命周期独立于Pod,支持静态创建和动态创建两种方式
PVC即PersistentVolumeClaim持久化存储申明,作为pv资源的使用者,可以指定存储容量大小和访问模式
StorageClass,创建PV,通过在PVC中指定StorageClass动态创建PV

PV创建相关的关键字段

  • capacity 存储大小
  • storageClassName指定静态StorageClass的名称
  • accessModes 访问模式,指定node的挂载方式,有ReadWriteOnce、ReadOnlyMany、ReadWriteMany
  • persistentVolumeReclaimPolicy 回收模式 ,Retain保留、Recycle回收、DELETE删除

但是这里有一点需要说明,并不是所有的存储plugin都支持accessModes的三种模式
03k8s12

二、PV与PVC使用

定义PV存储需要经过以下几步

  • 管理员创建PV
  • 用户创建PVC
  • 用户创建使用PVC的Pod
  • Pod使用PVC存储数据
  • PVC使用PV存储数据

三、PV创建

cat pv-nfs-storage.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-nfs-storage
  labels:
    storage: nfs
  annotations:
    kubernetes.io.description: pv-storage
spec:
  storageClassName: nfs # 指定存储类StorageClass名称
  capacity:             # 只当存储容量大小
    storage: 10Gi
  accessModes:          # 访问模式为单个节点读写模式
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  nfs:
    server: 172.19.159.7
    path: /mnt/data

创建PersistentVolumes
kubectl apply -f pv-nfs-storage.yaml
查看PersistentVolumes
kubectl get persistentvolumes

NAME             CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
pv-nfs-storage   10Gi       RWO            Retain           Available           nfs  

上面输出的结果
status处于Avaiable状态,这表示PV刚创建且处于未绑定状态,Bound表示与PVC绑定,Released表示PVC删除PV未释放,Failed表示异常
kubectl describe persistentvolume pv-nfs-storage

Name:            pv-nfs-storage
Labels:          storage=nfs
Annotations:     kubectl.kubernetes.io/last-applied-configuration:
                   {"apiVersion":"v1","kind":"PersistentVolume","metadata":{"annotations":{"kubernetes.io.description":"pv-storage"},"labels":{"storage":"nfs...
                 kubernetes.io.description: pv-storage
Finalizers:      [kubernetes.io/pv-protection]
StorageClass:    nfs
Status:          Available
Claim:           
Reclaim Policy:  Retain
Access Modes:    RWO
VolumeMode:      Filesystem
Capacity:        10Gi
Node Affinity:   <none>
Message:         
Source:
    Type:      NFS (an NFS mount that lasts the lifetime of a pod)
    Server:    172.19.159.7
    Path:      /mnt/data
    ReadOnly:  false
Events:        <none>

四、PVC引用PV

定义PVC,通过selector和PV实现关联,指定到相同的StorageClass
cat pvc-nfs-storage.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-nfs-storage
  labels:
    storage: pvc  
  annotations:
    kubernetes.io/description: "PersistentVolumeClaim for PV"
spec:
  accessModes:
    - ReadWriteOnce
  volumeMode: Filesystem
  storageClassName: nfs  # 指定与PV一样的存储类
  resources:         # 资源控制
    requests:
      storage: 1Gi
    limits:
      storage: 10Gi
  selector:           # 标签选择器
    matchLabels:
      storage: nfs    # 创建PV时给其的标签

创建PersistentVolumeClaim
kubectl apply -f pvc-nfs-storage.yaml
kubectl get persistentvolumeclaims查看PVC列表

NAME              STATUS   VOLUME           CAPACITY   ACCESS MODES   STORAGECLASS   AGE
pvc-nfs-storage   Bound    pv-nfs-storage   10Gi       RWO            nfs            25m

从输出来看PVC与PV绑定了
kubectl describe persistentvolumeclaims pvc-nfs-storage 查看PVC详情

Name:          pvc-nfs-storage
Namespace:     default
StorageClass:  nfs
Status:        Bound
Volume:        pv-nfs-storage
Labels:        storage=pvc
Annotations:   kubectl.kubernetes.io/last-applied-configuration:
                 {"apiVersion":"v1","kind":"PersistentVolumeClaim","metadata":{"annotations":{"kubernetes.io/description":"PersistentVolumeClaim for PV"},"...
               kubernetes.io/description: PersistentVolumeClaim for PV
               pv.kubernetes.io/bind-completed: yes
               pv.kubernetes.io/bound-by-controller: yes
Finalizers:    [kubernetes.io/pvc-protection]
Capacity:      10Gi
Access Modes:  RWO
VolumeMode:    Filesystem
Events:        <none>
Mounted By:    <none>

kubectl get persistentvolumes再次查看PV

NAME             CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                     STORAGECLASS   REASON   AGE
pv-nfs-storage   10Gi       RWO            Retain           Bound    default/pvc-nfs-storage   nfs                     3h40m

从输出看出PV已经与default命名空间下的PVC pvc-nfs-storage相关联

五、Pod使用PVC

前面使用本地存储emptyDir、hostPath和nfs的时候是直接使用Pod来调用,现在将Pod以template的形式封装在Deployments控制器中
定义一个Deployments,通过deployment.spec.template.spec应用Pod在volumes中调用PVC存储,volumeMounts将存储挂载到指定目录
cat pvc-nfs-deployments.yaml

apiVersion: apps/v1
kind: Deployment
metadata:                  # Deployment元数据
  name: pvc-nfs-deployment 
  labels:                  
     app: pvc-nfs-deployment
spec:
  replicas: 1              # Deployment副本控制数
  selector:                # 标签选择器
    matchLabels:
      app: pvc-nfs-deployment  
  template:                # 通过定义模板引用Pod,template中的信息与Pod定义的一致,包含metadata,spec信息
    metadata:
      labels:
        app: pvc-nfs-deployment
    spec:
      containers:
      - name: nginx-web
        image: nginx:latest
        imagePullPolicy: IfNotPresent
        ports:
        - name: nginx-http-80
          protocol: TCP
          containerPort: 80
        volumeMounts:                # 调用存储
        - name: pvc-nfs-storage      # 存储名称
          mountPath: /usr/share/nginx/html   # 存储挂载到此目录
      volumes:                       # 定义存储驱动
      - name: pvc-nfs-storage         # 存储名称
        persistentVolumeClaim:        # 存储驱动类型
          claimName: pvc-nfs-storage   # 抽象化的存储空间申明PVC,它会调用PV来存储数据,需提前创建PV与PVC

创建Deployments
kubectl apply -f pvc-nfs-deployments.yaml
查看Deployments列表
kubectl get deployments

pvc-nfs-deployment   1/1     1            1           92m

查看Deployments中的Pod
kubectl get pods -l app=pvc-nfs-deployment -o wide

NAME                                  READY   STATUS    RESTARTS   AGE    IP            NODE     NOMINATED NODE   READINESS GATES
pvc-nfs-deployment-549b69499b-h2842   1/1     Running   0          125m   10.244.2.64   node-3   <none>           <none>

进入容器查看挂载信息
kubectl exec -it pvc-nfs-deployment-549b69499b-h2842 bash
df -h

Filesystem              Size  Used Avail Use% Mounted on
overlay                  50G  7.3G   40G  16% /
tmpfs                    64M     0   64M   0% /dev
tmpfs                   1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/vda1                50G  7.3G   40G  16% /etc/hosts
shm                      64M     0   64M   0% /dev/shm
172.19.159.7:/mnt/data   50G  9.6G   38G  21% /usr/share/nginx/html
tmpfs                   1.9G   12K  1.9G   1% /run/secrets/kubernetes.io/serviceaccount
tmpfs                   1.9G     0  1.9G   0% /proc/scsi
tmpfs                   1.9G     0  1.9G   0% /sys/firmware

测试
echo "test page 3" > /usr/share/nginx/html/index.html
这里报了一个错,无权限创建index.html,但是当前用户时root,理论上权限是够的,只能退出容器,直接在存储目录创建index.html
echo "test page 3" > /mnt/data/index.html
查看pod的ip
kubectl get pods pvc-nfs-deployment-549b69499b-h2842 -o wide

NAME                                  READY   STATUS    RESTARTS   AGE    IP            NODE     NOMINATED NODE   READINESS GATES
pvc-nfs-deployment-549b69499b-h2842   1/1     Running   0          177m   10.244.2.64   node-3   <none>           <none>

curl 10.244.2.64
输出test page 3

支付宝扫码打赏 微信打赏

若你觉得我的文章对你有帮助,欢迎点击上方按钮对我打赏

扫描二维码,分享此文章

linuxwt's Picture
linuxwt

我叫王腾,来自武汉,2016年毕业后在上海做了一年helpdesk,自学了linux后回武汉从事系统运维的工作,从2017年开始写博客记录自己的学习工作,现在正在进行数据迁移到此博客,目前就职于中国移动设计院有限公司,个人的座右铭是:逃脱舒适区才能在闲暇的时候惬意的玩耍。

武汉光谷 https://linuxwt.com

Subscribe to 今晚打老虎

Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!

Comments