volume是k8s中由于持久化数据的一种方式,volume是pod中能够被多个容器访问的共享目录,它被定义在pod上,然后被一个Pod里的多个容器挂载到具体的文件目录下;
- 简单存储:EmptyDir、HostPath等等
- 高级存储:动态PV、静态PV、PVC
- 配置存储:ConfigMap、Secret
emptydir:随着pod的销毁而销毁
apiVersion: v1
kind: Pod
metadata:
name: test
namespace: dev
spec:
containers:
- name: nginx
image: nginx:1.17.1
ports:
- containerPort: 80
volumeMounts: # 将logs-volume挂在到nginx容器中,对应的目录为 /var/log/nginx
- name: logs-volume
mountPath: /var/log/nginx
- name: busybox
image: busybox:1.30
command: ["/bin/sh","-c","tail -f /logs/access.log"] # 初始命令,动态读取指定文件中内容
volumeMounts: # 将logs-volume 挂在到busybox容器中,对应的目录为 /logs
- name: logs-volume
mountPath: /logs
volumes: # 声明volume, name为logs-volume,类型为emptyDir
- name: logs-volume
emptyDir: {}
hostpath:存在node的节点,不会随着pod的飘移而转移
apiVersion: v1
kind: Pod
metadata:
name: test
namespace: dev
spec:
containers:
- name: nginx
image: nginx:1.17.1
ports:
- containerPort: 80
volumeMounts:
- name: logs-volume
mountPath: /var/log/nginx
- name: busybox
image: busybox:1.30
command: ["/bin/sh","-c","tail -f /logs/access.log"]
volumeMounts:
- name: logs-volume
mountPath: /logs
volumes:
- name: logs-volume
hostPath:
path: /root/logs #宿主机目录
type: DirectoryOrCreate # 目录存在就使用,不存在就先创建后使用
#type的类型有:
#DirectoryOrCreate 目录存在就使用,不存在就先创建后使用
#Directory 目录必须存在
#FileOrCreate 文件存在就使用,不存在就先创建后使用
#File 文件必须存在
#Socket unix套接字必须存在
#CharDevice 字符设备必须存在
#BlockDevice 块设备必须存在
PV:
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv2
spec:
存储源: # 存储类型,与底层真正存储对应
capacity: # 存储能力,目前只支持存储空间的设置
storage: 2Gi
accessModes: # 访问模式
storageClassName: # 存储类别
persistentVolumeReclaimPolicy: # 回收策略
volumes类型有:
- cephfs - CephFS volume
- csi - 容器存储接口 (CSI)
- fc - Fibre Channel (FC) 存储
- hostPath - HostPath 卷 (仅供单节点测试使用;不适用于多节点集群;请尝试使用 local 卷作为替代)
- iscsi - iSCSI (SCSI over IP) 存储
- local - 节点上挂载的本地存储设备
- nfs - 网络文件系统 (NFS) 存储
- rbd - Rados 块设备 (RBD) 卷
- 以下的持久卷已被弃用。这意味着当前仍是支持的,但是 Kubernetes 将来的发行版会将其移除。
- awsElasticBlockStore - AWS 弹性块存储(EBS) (于 v1.17 弃用)
- azureDisk - Azure Disk (于 v1.19 弃用)
- azureFile - Azure File (于 v1.21 弃用)
- cinder - Cinder(OpenStack 块存储)(于 v1.18 弃用)
- flexVolume - FlexVolume (于 v1.23 弃用)
- gcePersistentDisk - GCE Persistent Disk (于 v1.17 弃用)
- glusterfs - Glusterfs 卷 (于 v1.25 弃用)
- portworxVolume - Portworx 卷 (于 v1.25 弃用)
- vsphereVolume - vSphere VMDK 卷 (于 v1.19 弃用)
- 旧版本的 Kubernetes 仍支持这些“树内(In-Tree)”持久卷类型:
- photonPersistentDisk - Photon 控制器持久化盘。(v1.15 之后 不可用)
- scaleIO - ScaleIO 卷(v1.21 之后 不可用)
- flocker - Flocker 存储 (v1.25 之后 不可用)
- quobyte - Quobyte 卷 (v1.25 之后 不可用)
- storageos - StorageOS 卷 (v1.25 之后 不可用)
- [emptyDir
- hostPath
- gcePersistentDisk
- awsElasticBlockStore
- nfs
- iscsi
- flocker
- glusterfs
- rbd
- cephfs
- gitRepo
- secret
- persistentVolumeClaim
- downwardAPI
- azureFileVolume
- azureDisk
- vsphereVolume
- Quobyte
- PortworxVolume
- ScaleIO
- FlexVolume
- StorageOS
- local]
PV类型与支持的访问模式对应关系:
|
卷插件 |
ReadWriteOnce |
ReadOnlyMany |
ReadWriteMany |
ReadWriteOncePod |
|
AWSElasticBlockStore |
✓ |
- |
- |
- |
|
AzureFile |
✓ |
✓ |
✓ |
- |
|
AzureDisk |
✓ |
- |
- |
- |
|
CephFS |
✓ |
✓ |
✓ |
- |
|
Cinder |
✓ |
- |
- |
|
|
CSI |
取决于驱动 |
取决于驱动 |
取决于驱动 |
取决于驱动 |
|
FC |
✓ |
✓ |
- |
- |
|
FlexVolume |
✓ |
✓ |
取决于驱动 |
- |
|
GCEPersistentDisk |
✓ |
✓ |
- |
- |
|
Glusterfs |
✓ |
✓ |
✓ |
- |
|
HostPath |
✓ |
- |
- |
- |
|
iSCSI |
✓ |
✓ |
- |
- |
|
NFS |
✓ |
✓ |
✓ |
- |
|
RBD |
✓ |
✓ |
- |
- |
|
VsphereVolume |
✓ |
- |
-(Pod 运行于同一节点上时可行) |
- |
回收策略有:
Retain
Recycle
Delete
secrect:
- base64对数据进行编码
echo -n '账号' | base64
echo -n '密码' | base64
- 接下来编写secret.yaml,并创建secret
apiVersion: v1
kind: Secret
metadata:
name: secret
namespace: test
type: Opaque
data:
username: base64后的字符串
password: base64后的字符串
- 挂载secrect进去应用pod
apiVersion: v1
kind: Pod
metadata:
name: pod-secret
namespace: test
spec:
containers:
- name: nginx
image: nginx:1.17.1
volumeMounts: # 将secret挂载到目录
- name: config
mountPath: /secret/config
volumes:
- name: config
secret:
secretName: secret
浙公网安备 33010602011771号