k8s学习 volume之静态 pv、pvc

对于学习点滴的记录, 会慢慢的丰富示例以及详细内容.

PersistentVolume(PV)是集群中由管理员配置的一段网络存储。 它是集群中的资源,就像节点是集群资源一样。 PV是容量插件,如Volumes,但其生命周期独立于使用PV的任何单个pod。 此API对象捕获存储实现的详细信息,包括NFS,iSCSI或特定于云提供程序的存储系统。

PersistentVolumeClaim(PVC)是由用户进行存储的请求。 它类似于pod。 Pod消耗节点资源,PVC消耗PV资源。Pod可以请求特定级别的资源(CPU和内存)。声明可以请求特定的大小和访问模式(例如,可以一次读/写或多次只读)。

如果是手动创建的 pv 以及 pvc 然后提供给 pod 使用, 这种方式属于静态 pv、pvc.

创建 PV 的文件

root@master:/data/volume# cat pv.yaml 
apiVersion: v1
kind: PersistentVolume
metadata: 
  name: pv1
spec:
  nfs:
    path: /mnt/pv1
    server: 172.16.212.134
  capacity:
    storage: 1Gi
  accessModes:
  - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain

---
apiVersion: v1
kind: PersistentVolume
metadata: 
  name: pv2
spec:
  nfs:
    path: /mnt/pv2
    server: 172.16.212.134
  capacity:
    storage: 2Gi
  accessModes:
  - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain

---
apiVersion: v1
kind: PersistentVolume
metadata: 
  name: pv3
spec:
  nfs:
    path: /mnt/pv3
    server: 172.16.212.134
  capacity:
    storage: 3Gi
  accessModes:
  - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain

创建 PVC 的文件

root@master:/data/volume# cat pvc.yaml 
apiVersion: v1
kind: PersistentVolumeClaim
metadata: 
  name: pvc1
  namespace: default
spec:
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 1Gi

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata: 
  name: pvc2
  namespace: default
spec:
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 1Gi

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata: 
  name: pvc3
  namespace: default
spec:
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 2Gi

创建 pod

真正的在 pod 使用已经创建好的 pvc

root@master:/data/volume# cat pod-pvc.yaml 
apiVersion: apps/v1
kind: Deployment
metadata: 
  name: pod-pvc1
  namespace: default
  labels:
    controller: deploy
spec:
  replicas: 1
  selector: 
    matchLabels:
      app: volume-pod-pvc
  template:
    metadata:
      labels: 
        app: volume-pod-pvc
    spec:
      containers:
      - name: busybox
        image: busybox:1.30
        command: ["/bin/sh", "-c", "while true;do echo pod1 >> /root/out.text; sleep 10; done;"]
        volumeMounts:
        - name: logs-volume
          mountPath: /root/
      volumes:
      - name: logs-volume
        persistentVolumeClaim:
          claimName: pvc1
          readOnly: false
---
apiVersion: apps/v1
kind: Deployment
metadata: 
  name: pod-pvc2
  namespace: default
  labels:
    controller: deploy
spec:
  replicas: 1
  selector: 
    matchLabels:
      app: volume-pod-pvc
  template:
    metadata:
      labels: 
        app: volume-pod-pvc
    spec:
      containers:
      - name: busybox
        image: busybox:1.30
        command: ["/bin/sh", "-c", "while true;do echo pod2 >> /root/out.text; sleep 10; done;"]
        volumeMounts:
        - name: logs-volume
          mountPath: /root/
      volumes:
      - name: logs-volume
        persistentVolumeClaim:
          claimName: pvc2
          readOnly: false
---
apiVersion: apps/v1
kind: Deployment
metadata: 
  name: pod-pvc3
  namespace: default
  labels:
    controller: deploy
spec:
  replicas: 1
  selector: 
    matchLabels:
      app: volume-pod-pvc
  template:
    metadata:
      labels: 
        app: volume-pod-pvc
    spec:
      containers:
      - name: busybox
        image: busybox:1.30
        command: ["/bin/sh", "-c", "while true;do echo pod3 >> /root/out.text; sleep 10; done;"]
        volumeMounts:
        - name: logs-volume
          mountPath: /root/
      volumes:
      - name: logs-volume
        persistentVolumeClaim:
          claimName: pvc3
          readOnly: false

查看 pv

root@master:/data/volume# kubectl get pv
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                         STORAGECLASS          REASON   AGE
pv1                                        1Gi        RWX            Retain           Bound    default/pvc1                                                 44h
pv2                                        2Gi        RWX            Retain           Bound    default/pvc2                                                 44h
pv3                                        3Gi        RWX            Retain           Bound    default/pvc3                                                 44h

查看 pvc

如果 pvc 申请的资源不够的话, PVC 会处于 pending 状态.

NAME   STATUS   VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   AGE
pvc1   Bound    pv1      1Gi        RWX                           44h
pvc2   Bound    pv2      2Gi        RWX                           44h
pvc3   Bound    pv3      3Gi        RWX                           44h

//参考: https://blog.csdn.net/zenglingmin8/article/details/121737953

你可能感兴趣的:(k8s学习 volume之静态 pv、pvc)