Container 中的文件是临时存放的,会导致连个问题:
- 容器崩溃时文件丢失
- 多个容器共享文件会有问题
卷其实是一个目录。
目前支持:
- awsElasticBlockStore
- azureFile
- cephfs
- cinder
- ConfigMap
- dwonwardAPI
- emptyDir
- fc
- gcePersistentDisk
- glusterfs
- hostpath
- iscsi
- local
- nfs
- persistentVolumeClaim
- portworxVolume
- prjected
- rbd
- secret
- vsphereVolume
01 使用卷-awsEBS
apiVersion: v1kind: Podmetadata:name: test-ebsspec:containers:- image: k8s.gcr.io/test-webservername: test-container# .spec.containers.volumeMounts 声明在容器中的挂载位置volumeMounts:- mountPath: /test-ebs# 与 .spec.volumes.name 匹配name: test-volume# .spec.volumes 设置为 Pod 提供的卷volumes:- name: test-volume# 使用 AWS EBS 配置实例awsElasticBlockStore:volumeID: "<volume-id>"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
