Kubernetes那点事儿——存储之存储卷
- 前言
- 一、K8s数据卷
- 一、临时存储卷emptyDir
- 二、节点存储卷hostPath
- 三、网络存储NFS
前言
在K8s中用Volume为容器提供了外部的存储能力。
Pod需要设置卷来源(spec.volume)和挂载点(spec.containers.volumeMounts)两个信息后才可以使用相应的Volume。
一、K8s数据卷
常用的数据卷:
- 本地(hostPath,emptyDir)
- 网络(NFS,Ceph,GlusterFS)
- 公有云(AWS EBS)
- K8s资源(Configmap,Secret)
其中像网络存储(NFS,Ceph,GlusterFS)在K8s中主要使用PV/PVC来实现,不仅提高了安全性和还解决了组织分工的问题,我们在后续章节详细研究PV/PVC。
K8s资源存储:
ConfigMap:一般用于应用程序配置文件存储,,支持变量和文件。
Secret:与ConfigMap类似,区别在于Secret主要存储敏感数据,所有的数据要经过base64编码,一般用于凭证存储。
一、临时存储卷emptyDir
emptyDir卷:是一个临时存储卷,与Pod生命周期绑定一起,如果Pod删除了卷也会被删除。
主要应用场景:Pod中容器之间数据共享。
# cat emptyDir.yamlapiVersion: v1
kind: Pod
metadata:name: my-pod-empty
spec:containers:- name: writeimage: centoscommand: ["bash","-c","for i in {1..100};do
echo $i >> /data/hello;sleep 1;done"]volumeMounts:- name: datamountPath: /data- name: readimage: centoscommand: ["bash","-c","tail -f /data/hello"]volumeMounts:- name: datamountPath: /datavolumes:- name: dataemptyDir: {}
二、节点存储卷hostPath
hostPath卷:挂载Node文件系统(Pod所在节点)上文件或者目录到Pod中的容器。
主要应用场景:Pod中容器需要访问宿主机文件。
# hostPath示例.yamlapiVersion: v1
kind: Pod
metadata:name: my-pod-hostpath
spec:containers:- name: busyboximage: busybox:latestargs:- /bin/sh- -c- sleep 36000volumeMounts:- name: datamountPath: /datavolumes:- name: datahostPath:path: /tmp/hostPath # 主机必须存在此目录type: Directory
三、网络存储NFS
NFS卷:提供对NFS挂载支持,可以自动将NFS共享路径挂载到Pod中。
# cat nfs_volum.yamlapiVersion: apps/v1
kind: Deployment
metadata:name: web
spec:selector:matchLabels:app: nginx-nfsreplicas: 2template:metadata:labels:app: nginx-nfsspec:containers:- name: nginximage: nginxvolumeMounts:- name: wwwrootmountPath: /usr/share/nginx/htmlports:- containerPort: 80volumes:- name: wwwrootnfs:server: 10.7.7.222path: /ifs/nfsdir