Container 中的文件是临时存放的,会导致连个问题:

  • 容器崩溃时文件丢失
  • 多个容器共享文件会有问题

卷其实是一个目录

目前支持:

  • awsElasticBlockStore
  • azureFile
  • cephfs
  • cinder
  • ConfigMap
  • dwonwardAPI
  • emptyDir
  • fc
  • gcePersistentDisk
  • glusterfs
  • hostpath
  • iscsi
  • local
  • nfs
  • persistentVolumeClaim
  • portworxVolume
  • prjected
  • rbd
  • secret
  • vsphereVolume

    01 使用卷-awsEBS

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: test-ebs
  5. spec:
  6. containers:
  7. - image: k8s.gcr.io/test-webserver
  8. name: test-container
  9. # .spec.containers.volumeMounts 声明在容器中的挂载位置
  10. volumeMounts:
  11. - mountPath: /test-ebs
  12. # 与 .spec.volumes.name 匹配
  13. name: test-volume
  14. # .spec.volumes 设置为 Pod 提供的卷
  15. volumes:
  16. - name: test-volume
  17. # 使用 AWS EBS 配置实例
  18. awsElasticBlockStore:
  19. volumeID: "<volume-id>"
  20. fsType: ext4

02 使用卷-emptyDir

apiVersion: v1
kind: Pod
metadata:
    name: test-emptydir
spec:
    containers:
  - image: k8s.gcr.io/test-webserver
      name: test-container
    # .spec.containers.volumeMounts 声明在容器中的挂载位置
    volumeMounts:
        - mountPath: /test-empty
        # 与 .spec.volumes.name 匹配
          name: test-empty
  # .spec.volumes 设置为 Pod 提供的卷
  volumes:
      - name: test-empty
     # 使用 emptyDir 配置实例
        emptyDir: {}

03 使用卷 - hostPath

apiVersion: v1
kind: Pod
metadata:
    name: test-hostpath
spec:
    containers:
  - image: k8s.gcr.io/test-webserver
      name: test-container
    # .spec.containers.volumeMounts 声明在容器中的挂载位置
    volumeMounts:
        - mountPath: /test-hostpath
        # 与 .spec.volumes.name 匹配
          name: test-hostpath
  # .spec.volumes 设置为 Pod 提供的卷
  volumes:
      - name: test-empty
     # 使用 hostPath 配置实例
        hostPath:
          path: /data
        type: Directory

04 使用卷 - secret

apiVersion: v1
kind: Pod
metadata:
  name: volume-test
spec:
  containers:
  - name: container-test
    image: busybox
    volumeMounts:
    - name: test-secret
      mountPath: "/etc/secret"
      readOnly: true
  volumes:
  - name: test-secret
         secret:
      secretName: mysecret

06 使用 subPath

apiVersion: v1
kind: Pod
metadata:
  name: my-lamp-site
spec:
    containers:
    - name: mysql
      image: mysql
      env:
      - name: MYSQL_ROOT_PASSWORD
        value: "rootpasswd"
      volumeMounts:
      - mountPath: /var/lib/mysql
        name: site-data
        subPath: mysql
    - name: php
      image: php:7.0-apache
      volumeMounts:
      - mountPath: /var/www/html
        name: site-data
        subPath: html
    volumes:
    - name: site-data
      persistentVolumeClaim:
        claimName: my-lamp-site-data