美文网首页云原生
41-静态卷挂载-PV-PVC

41-静态卷挂载-PV-PVC

作者: 紫荆秋雪_文 | 来源:发表于2022-02-24 18:07 被阅读0次

    一、PV(PersistentVolume)

    卷对象

    • 创建pv对象
    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: nfs-pv-10m
      namespace: raven
    spec:
      capacity:
        storage: 10m
      accessModes:
        - ReadWriteOnce
      persistentVolumeReclaimPolicy: Recycle
      storageClassName: slow
      nfs:  # 定义NFS类型的PV
        path: /nfs/data
        server: 172.20.173.235
    

    二、PVC(PersistentVolumeClaim)

    PVC 有点像Pod和PV之间的匹配器

    • 创建PVC对象
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: nfs-pvc
      namespace: raven
      labels:
        app: nfs-pvc
    spec:
      storageClassName: slow  # 与定义 PV.yaml 中的 storageClassName 保证一致
      accessModes:
      - ReadWriteOnce
      resources:
        requests:
          storage: 90M
    

    三、Pod 使用PVC

    apiVersion: v1
    kind: Pod
    metadata:
      name: "nfs-pod-pvc"
      namespace: raven
      labels:
        app: "nfs-pod-pvc"
    spec:
      containers:
      - name: nfs-pod-pvc
        image: "nginx"
        ports:
        - containerPort:  80
          name:  http
        volumeMounts:
        - name: localtime
          mountPath: /etc/localtime
        - name: pvc
          mountPath: /usr/share/nginx/html
      volumes:
        - name: localtime
          hostPath:
            path: /usr/share/zoneinfo/Asia/Shanghai
        - name: pvc
          persistentVolumeClaim:
            claimName: nfs-pvc
      restartPolicy: Always
    
    验证PVC挂载.png

    推荐把相关的多个资源写在一个YAML文件中,便于维护

    # 定义 Pod
    apiVersion: v1
    kind: Pod
    metadata:
      name: "nfs-pod-pvc"
      namespace: raven
      labels:
        app: "nfs-pod-pvc"
    spec:
      containers:
      - name: nfs-pod-pvc
        image: "nginx"
        ports:
        - containerPort:  80
          name:  http
        volumeMounts:
        - name: localtime
          mountPath: /etc/localtime
        - name: pvc
          mountPath: /usr/share/nginx/html
      volumes:
        - name: localtime
          hostPath:
            path: /usr/share/zoneinfo/Asia/Shanghai
        - name: pvc
          persistentVolumeClaim:
            claimName: nfs-pvc
      restartPolicy: Always
    ---
    # 定义 PVC
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: nfs-pvc
      namespace: raven
      labels:
        app: nfs-pvc
    spec:
      storageClassName: slow  # 与定义 PV.yaml 中的 storageClassName 保证一致
      accessModes:
      - ReadWriteOnce
      resources:
        requests:
          storage: 90M
    ---
    # 定义 PV
    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: nfs-pv-10m
      namespace: raven
    spec:
      capacity:
        storage: 10m
      accessModes:
        - ReadWriteOnce
      persistentVolumeReclaimPolicy: Recycle
      storageClassName: slow
      nfs:  # 定义NFS类型的PV
        path: /nfs/data
        server: 172.20.173.235
    
    

    相关文章

      网友评论

        本文标题:41-静态卷挂载-PV-PVC

        本文链接:https://www.haomeiwen.com/subject/sczzlrtx.html