Kubernetes
如何限制 pod 的根磁碟空間
我已將 pod 部署在 100gb 卷的節點上。我只希望一個 pod 有 50gb 的根磁碟空間。
所以我有這樣的配置
deployment.yaml
:resources: requests: ephemeral-storage: "50G" limits: ephemeral-storage: "70G"
但是當我檢查容器時(pod 中只有 1 個容器),我看到節點上的所有磁碟空間都分配給了 pod。從這裡閱讀,我認為
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
知道我做錯了什麼嗎?
整個空間 (
/
) 對您可見這一事實並不意味著該空間可用/可分配。
Kubelet
將監視您的臨時儲存的使用情況Pod
並採取相應措施(通過驅逐Pod
超出限制的儲存):臨時儲存消耗管理
如果 kubelet 將本地臨時儲存作為資源進行管理,那麼 kubelet 會測量儲存使用情況:
emptyDir
卷, tmpfsemptyDir
卷除外- 保存節點級日誌的目錄
- 可寫容器層 如果 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
其他資源: