![如何限制 pod 的根磁盘空间](https://linux22.com/image/768124/%E5%A6%82%E4%BD%95%E9%99%90%E5%88%B6%20pod%20%E7%9A%84%E6%A0%B9%E7%A3%81%E7%9B%98%E7%A9%BA%E9%97%B4.png)
我已将 pod 部署在具有 100gb 卷的节点上。我只希望 pod 具有 50gb 根磁盘空间。
所以我有这样的配置deployment.yaml
:
resources:
requests:
ephemeral-storage: "50G"
limits:
ephemeral-storage: "70G"
但当我检查容器时(容器中只有 1 个容器),我发现节点上的所有磁盘空间都分配给了容器。从这里,我认为ephemeral-storage
控制为 pod 分配多少磁盘空间。
# df -h
Filesystem Size Used Avail Use% Mounted on
overlay 100G 6.5G 94G 7% /
tmpfs 64M 0 64M 0% /dev
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/nvme0n1p1 100G 6.5G 94G 7% /etc/hosts
shm 64M 0 64M 0% /dev/shm
tmpfs 1.9G 0 1.9G 0% /proc/acpi
tmpfs 1.9G 0 1.9G 0% /sys/firmware
知道我做错了什么吗?
答案1
整个空间(/
)对您可见并不意味着该空间可用/可分配。
Kubelet
将监控您的临时存储的使用情况Pod
并采取相应措施(通过驱逐Pod
超出限制的):
临时存储消耗管理
如果 kubelet 将本地临时存储作为资源进行管理,则 kubelet 会通过以下方式测量存储使用情况:
emptyDir
卷,除了临时文件emptyDir
卷- 保存节点级日志的目录
- 可写容器层如果 Pod 使用的临时存储超出您允许的范围,则 kubelet 会设置一个驱逐信号,触发 Pod 驱逐。
请看下面的例子:
假设您有以下Pod
清单:
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx
resources:
requests:
ephemeral-storage: "2Gi"
limits:
ephemeral-storage: "4Gi"
为临时存储配置的限制是4Gi
。要检查会发生什么,您可以运行:
kubectl exec -it nginx -- fallocate -l 10GB /evict.img
附注!
fallocate
用于操作文件分配的磁盘空间,可以解除分配或预分配。
一段时间后你应该会看到:
$ kubectl describe pod nginx
Name: nginx
Namespace: default
Priority: 0
Node: XYZ
Start Time: Mon, 05 Jul 2021 09:47:08 +0200
Labels: <none>
Annotations: <none>
Status: Failed # <-- IMPORTANT!
Reason: Evicted # <-- IMPORTANT!
Message: Pod ephemeral local storage usage exceeds the total limit of containers 4Gi. # <-- IMPORTANT!
<-- REDACTED -->
Limits:
ephemeral-storage: 4Gi # <-- IMPORTANT!
Requests:
ephemeral-storage: 2Gi
<-- REDACTED -->
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 32s default-scheduler Successfully assigned default/nginx to XYZ
Normal Pulling 31s kubelet Pulling image "nginx"
Normal Pulled 31s kubelet Successfully pulled image "nginx" in 360.098619ms
Normal Created 31s kubelet Created container nginx
Normal Started 31s kubelet Started container nginx
Warning Evicted 3s kubelet Pod ephemeral local storage usage exceeds the total limit of containers 4Gi. # <-- IMPORTANT!
Normal Killing 3s kubelet Stopping container nginx # <-- IMPORTANT!
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 0/1 Evicted 0 9m7s
其他资源: