Skip to content

4. 存储 10%

1. 了解存储类、持久卷

Understand storage classes, persistent volumes

  1. PV
    集群中的一块存储 emptyDir # 容器中 hostPath # 宿主机中 local
  2. PVC
    用户对存储的请求
  3. StorageClass
    存储 "类"的描述方法,通过描述存储类,提供动态分配存储的能力

2. 了解卷模式、访问模式和卷回收策略

Understand volume mode, access modes and reclaim policies for volumes

  1. volume mode
    • Filesystem(文件系统)
    • Block(块)
  2. access modes
    • ReadWriteOnce -- 卷可以被一个节点以读写方式挂载;
    • ReadOnlyMany -- 卷可以被多个节点以只读方式挂载;
    • ReadWriteMany -- 卷可以被多个节点以读写方式挂载。
  3. reclaim policies
    • Retain -- 手动回收
    • Recycle -- 基本擦除 (rm -rf /thevolume/*)
    • Delete -- 诸如 AWS EBS、GCE PD、Azure Disk 或 OpenStack Cinder 卷这类关联存储资产也被删除

3. 理解持久容量声明原语

Understand persistent volume claims primitive

apiVersion: v1
kind: PersistentVolume
metadata:
    name: pv0003
spec:
    volumeMode: Filesystem
    accessModes:
    - ReadWriteOnce
    capacity:
    storage: 5Gi
    persistentVolumeReclaimPolicy: Recycle
    storageClassName: slow
    mountOptions:
    - hard
    - nfsvers=4.1
    nfs:
    path: /tmp
    server: 172.17.0.2
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
    name: myclaim
spec:
    volumeMode: Filesystem
    accessModes:
    - ReadWriteOnce
    storageClassName: slow
    resources:
    requests:
        storage: 8Gi
    selector:
    matchLabels:
        release: "stable"
    matchExpressions:
        - {key: environment, operator: In, values: [dev]}
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
    name: standard
provisioner: kubernetes.io/aws-ebs
parameters:
    type: gp2
reclaimPolicy: Retain
allowVolumeExpansion: true
mountOptions:
    - debug
volumeBindingMode: Immediate

4. 了解如何配置具有持久性存储的应用程序

Know how to configure applications with persistent storage

参考

题目

  1. 列出环境内所有的 pv 并以 name 字段排序(使用 kubectl 自带排序功能)

    kubectl get pv -A --sort-by='.metadata.name'
    
  2. 创建一个 pv,类型是 hostPath ,位于 /data 中,大小 1G,模式 ReadOnlyMany

    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: pv1
      namespace: xx
    spec:
      accessModes:
        - ReadOnlyMany
      hostPath:
        path: /data
      capacity:
        storage: 1Gi
    
  3. 创建一个 pod 名称为 test,镜像为 nginx ,Volume 名称 cache-volume 为挂在在 /data 目录下,且 Volume 是 non-Persistent 的

    apiVersion: v1
    kind: Deployment
    metdata:
      name: test
    spec:
      template:
        metadata:
          name: test
        spec:
          conatiners:
            - name: nginx
              image: nginx
              volumeMounts:
                name: cache-volume
                mountPath: /data
    
  4. List all PVs sorted by name,saving the full kubectl output to /opt/KUCC0010/my_volumes. Use kubectl own functionally for sorting the output, and do not manipulate it any further.

    
    
  5. pod中挂载volume