pod 数据持久化
1. Volume
1.1 k8s中的volume提供了在容器中挂载外部储存的能力
pod需要设置数据卷来源(spec.volume)和挂载点(spec.containers.volumeMounts)两个信息可以使用相应的volume
挂载点就是你要持久化的目录
1.2 卷的来源:Kubernetes 支持下列类型的卷:
awsElasticBlockStore
azureDisk
azureFile
cephfs
cinder
configMap
csi
downwardAPI
emptyDir
fc (fibre channel)
flexVolume
flocker
gcePersistentDisk
gitRepo (deprecated)
glusterfs
hostPath
iscsi
local
nfs
persistentVolumeClaim
projected
portworxVolume
quobyte
rbd
scaleIO
secret
storageos
vsphereVolume
本地卷:pod所在的主机目录 但是这种方式 后期pod的迁移是不太友好的
本地卷:hostPath emptyDir
网络卷:网络卷 意思就是 不在你k8s集群里面 而是在你k8s集群之外就是一个专门的存储系统 为k8s提供存储服务 挂载到pod里面通过网络写到存储
网络卷:nfs cep(cephfs文件存储 rbd块存) glusterfs
公有云存储:awsElasticBlockStore azureDisk
k8s资源: configMap secret downwardAPI oss
docker对应的
emptyDir=volume
hostPath=bindmount 日志采集 监控agent /proc
emptyDir方式挂载
通过docker ps 查看容器名 找到对应的pod-id
/var/lib/kubelet/pods/6bae7f5d-ec10-4efc-b3b6-063f9ce04338/volumes/kubernetes.io~empty-dir/wr
hostPath挂载
使用场景pod需要使用宿主机文件
是一个对应的映射关系
nfs挂载
/root/kube *(insecure,rw,async,no_root_squash)
mount -t 192.168.1.11:/root/kube ./k8s-pv/
==========================================
pod-挂载nfs-yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: web
name: web
spec:
replicas: 1
selector:
matchLabels:
app: web
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: web
spec:
containers:
- image: nginx
name: nginx
resources: {}
volumeMounts:
- name: data1
mountPath: /usr/share/nginx/html
volumes:
- name: data1
nfs:
server: 192.168.1.11
path: /root/kube
==================================================
2. PersistentVolume(pv):对存储资源创建和使用抽象 使得存储作为集群中的资源管理
静态
动态供给
pv相当于 你要多少硬盘我就给你多少硬盘
3. PersistentVolume (pvc) 让用户不需要关心具体的volume实现过程
通过pvc去定我部署应用程序需要多大空间 就给多大空间
#申请pv
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc
spec:
#访问模式
accessModes:
- ReadWriteMany
#请求容量大小
resources:
requests:
storage: 5G
容量不是必须匹配(pv不一定=pvc) 而是就近选择合适的pv
扩容: 1.11版本支持动态扩容(k8s层面) 具体还是需要后端储存是否能够动态扩容(ceph支持动态扩容)
==============================
数据卷定yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: my-pv
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteMany
nfs:
path: /k8s/nfs/data
server: 192.168.0.200
=============================
通过storage自动创建pv
https://github.com/kubernetes-incubator/external-storage/tree/master/nfs-client/deploy、 #社区自动供给pvyaml地址
因为 nfs cephfs 不支持 pv自动供给 只能通过社区的 k8s-selector 的进行自动创建pv\
需要 三个yaml文件 一个rbdc权限控制 deploy控制器yaml 还有一个class s
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: test-claim
spec:
storageClassName: "managed-nfs-storage" #声名pv自动供给
accessModes:
- ReadWriteMany
resources:
requests:
storage: 1Mi