@zhangsiming65965
2020-06-02T08:12:16.000000Z
字数 3720
阅读 255
Kubernetes
ephemeral
storage
我们都知道kuberenets虽然是以pod为最小单位,pod底层运行的还是docker;既然是docker,就不可避免的会有一个特性,即docker重启之后,里面的数据就没有了,docker采用volume进行持久化数据。
所以本文主要讲解的是如何在kubernetes中持久化数据,以及一些持久化方式的对比。
处理数据持久化的第一种方法就是---volume。
官方解释:
Emptydir: It is a type of volume that is created when a Pod is first assigned to a Node. It remains active as long as the Pod is running on that node. The volume is initially empty and the containers in the pod can read and write the files in the emptyDir volume. Once the Pod is removed from the node, the data in the emptyDir is erased.
Emptydir特点:
1.Emptydir实际占用的是Pod所在宿主机本地的磁盘空间
2.Pod内的Containers共享Emptydir
3.Pod重新调度去到别的节点,磁盘数据会清除
官方解释:
hostPath: This type of volume mounts a file or directory from the host node’s filesystem into your pod.
hostPath特点:
1.使用具体的node节点宿主机作为存储
2.即使pod重启或者删除,宿主机该目录下文件也不会丢失
3.但是pod重新调度之后,如果是不同的node,可能找不回原来的数据;一般需要配置调度策略调度回同一个节点相当于数据持久化
官方解释:
nfs: An nfs volume allows an existing NFS (Network File System) to be mounted into your pod. The data in an nfs volume is not erased when the Pod is removed from the node. The volume is only unmounted.
nfs特点:
1.可以挂载远程的nfs服务器中的一个目录
2.pod删除nfs中的数据也还存在,只是umount了
3.重新启动pod可以找回原来的数据(相当于重新mount回来这个nfs目录了)
处理数据持久化的第二种方法是---pv&pvc。
# example
apiVersion: v1
kind: PersistentVolume
metadata:
name: local-pv-1baf11df
spec:
accessModes:
- ReadWriteOnce
capacity:
storage: 10015Mi
local:
fsType: ext4
path: /kubernetes-local-volume/etcd
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- ip-10-20-12-160
persistentVolumeReclaimPolicy: Delete
storageClassName: local-volume
使用"nodeAffinity"调度到具体的节点,采用宿主机的目录作为永久存储,即使pod重启删除数据也不会丢失。
# example
apiVersion: v1
kind: PersistentVolume
metadata:
name: pvc-c0a77972-fd1a-11e9-be3f-06eaf86084d0
spec:
accessModes:
- ReadWriteOnce
capacity:
storage: 2Gi
mountOptions:
- vers=4.0
- rsize=16384
- wsize=16384
nfs:
path: /data/nfs/test
persistentVolumeReclaimPolicy: Delete
storageClassName: nfs-10-20-11-105
持久化的数据采用nfs进行存储,无论pod是否正常都不影响里面的数据。
这里就有点纳闷?Volume和pvc好像是一样的功能啊,host模式对应local模式、nfs模式对应nfs模式,都是持久化数据,具体应该使用哪个呢?
回答:
1.volume和pvc对于持久化数据的效果确实是一样的,没有任何区别
2.volumemount的方式需要把provider的信息,都写入pod控制器(deployment...)中,用户需要整体了解所有的配置才能上线服务
3.pv&pvc的方式只需要用户书写pvc,由管理员创建对应的pv即可实现一样的需求,其实是减少了用户输入provider的内容(比如nfs的地址,目录等...),实现了服务和存储的解耦,推荐!
既然说到了这里,就聊一聊k8s的"ephemeral storage"吧,我们在线上的服务中,偶尔会碰到,如下的报错:
Pod ephemeral local storage usage exceeds the total limit of containers xxGi
下述配置表示,"ephemeral-storage"请求了1Gi,"ephemeral-storage"限制最多"2Gi",如果超过了2Gi,pod会被立马Evicted。
# Resource limits for instances
# After the chart is released, according to the actual resource occupation displayed in kubesphere-test, modify the resource request/limit in time to avoid resource wasting
resources: # {}
limits:
cpu: "200m" # CPU limit, such as 100m, 500m, 1, 2, ...
memory: "200Mi" # MEM limit, such as 50Mi, 1Gi
ephemeral-storage: "2Gi" # Disk limit(almost for temporary storage and log), such as 50Mi, 1Gi
requests:
cpu: "100m" # 100m, 500m, 1, 2, ...
memory: "20Mi"
ephemeral-storage: "1Gi"
如果超过了"ephemeral storage",我们该如何定位是哪里的容量比较大,占用比较多呢?
Kubernetes如下处理三种类型的临时存储:
1.Pod的可写层,容器里未被持久化下来的部分
2.标准输出和错误输出的日志
3.emptydir挂载的目录
这部分数据可以通过切入容器目录,"du -sh"查看有没有那些文件的大小非常大。
这里多说一句,kubernetes是不会进行日志的清理的,日志部分的处理完全取决于容器引擎层(docker)的日志处理机制,我们可以通过编辑"/etc/docker/daemon.json":
{
"registry-mirrors": ["https://ht6aappv.mirror.aliyuncs.com"],
"log-opts": {
"max-size": "500m",
"max-file": "3"
}
}
来限制,日志每次到500m自动切割,最多保存3个文件,即最大日志限制为1.5G;所以,如果日志比较大,可以去节点"ll /var/log/container..."去进行排查删除。
emptydir虽然是数据卷,但是并不算做持久存储内,这个依然算临时存储。也需要切入容器查看这个目录是否占用了过多的空间才是。
参考链接🔗:
https://qiita.com/shmurata/items/1dfa4df87baedf60052c
https://blog.csdn.net/sdmei/article/details/101017405
https://cloud.tencent.com/developer/article/1456389