ACS通過Kubernetes原生的ResourceQuota機制限制和控制命名空間中的資源使用。通過ResourceQuota,您可以限制和監控命名空間中各種資源的使用方式,如CPU、記憶體、儲存、副本數和服務數。這樣就可以防止某個命名空間中的資源被其他應用程式過度佔用,從而保證應用程式的穩定性和可靠性。
資源配額(ResourceQuota)
您可以在ACS叢集中使用Kubernetes原生的資源配額機制來控制每個命名空間的資源消耗總量。通過定義ResourceQuota對象,可以設定命名空間中某種資源類型的總量上限,包括CPU、記憶體和擴充資源等。當在命名空間下建立Pod或Service等K8s資源時,Kubernetes的配額系統會跟蹤叢集的資源使用方式,並保證資源總用量不超過命名空間下ResourceQuota定義的硬性(Hard)限額。
在使用資源配額時:
每個團隊或者應用使用獨立的命名空間。
叢集管理員針對不同的命名空間建立一個或者多個ResourceQuota對象。
如果用量超過了ResourceQuota的硬性限額,後續建立的資源會被拒絕。
如果使用ResourceQuota配置了CPU和記憶體,建立的Pod必須要設定request和limit,否則將會拒絕建立。
對於其他資源(例如擴充資源):無需為該資源設定request和limit。提示:可使用LimitRanger准入控制器來為沒有設定計算資源需求的Pod設定預設值。
ResourceQuota對象的名稱必須是合法的DNS子網域名稱。
配額的修改不會影響已經建立的資源使用對象。
啟用資源配額
通過ACS控制台建立的Kubernetes叢集已經預設開啟了資源配額機制。您只需要在新增的命名空間下建立ResourceQuota對象,就可以對這個命名空間開啟資源配額。
支援的資源類型
ACS完全相容Kubernetes原生資源配額機制,因此標準資源類型和擴充資源都可以通過ResourceQuota進行配置。
標準資源類型
資源名稱 | 描述 |
limits.cpu | 所有非終止狀態的Pod,其CPU限額總量不能超過該值。 |
limits.memory | 所有非終止狀態的Pod,其記憶體限額總量不能超過該值。 |
requests.cpu | 所有非終止狀態的Pod,其CPU需求總量不能超過該值。 |
requests.memory | 所有非終止狀態的Pod,其記憶體需求總量不能超過該值。 |
hugepages-<size> | 對於所有非終止狀態的Pod,針對指定大小的HugePage請求總數不能超過此值。 |
cpu | 與 |
memory | 與 |
擴充資源的資源配額
因為K8s不支援擴充資源超量分配(即limit>request),因此只需要配置首碼為requests.的配額項。更多詳細資料,請參見資源配額。
樣本
以下示範如何通過kubectl查看和建立ResourceQuota。
執行以下命令,建立命名空間test。
建立ResourceQuota,限制命名空間test最多隻能使用4000m CPU。
執行以下命令,查看ResourceQuota。
kubectl -n test describe resourcequota test-quota預期輸出:
Namespace: test Resource Used Hard -------- ---- ---- limits.cpu 0 4 requests.cpu 0 4建立4個Pod,每個Pod使用1000m CPU。
執行以下命令,查看Pod建立結果。
kubectl -n test get pod預期輸出:
NAME READY STATUS RESTARTS AGE test-app-5ddxxxxx94-jdv4m 1/1 Running 0 35s test-app-5ddxxxxx94-jhmtb 1/1 Running 0 35s test-app-5ddxxxxx94-mr8vq 1/1 Running 0 35s test-app-5ddxxxxx94-pjdfn 1/1 Running 0 35s可以看出4個Pod都處於Running狀態。
執行以下命令,查看ResourceQuota狀態。
kubectl -n test describe resourcequota預期輸出:
Name: test-quota Namespace: test Resource Used Hard -------- ---- ---- limits.cpu 4 4 requests.cpu 4 4可以看到test-quota的Used資源量與Hard資源量相等。
再嘗試擴容一個新的Pod,驗證ResourceQuota的准入控制器攔截功能。
先嘗試擴容一個新的Pod。
kubectl -n test scale deploy test-app --replicas 5查看對應的ReplicaSet,發現DESIRED是5,但CURRENT是4,證明有個Pod出現異常。
kubectl -n test get rs預期輸出:
NAME DESIRED CURRENT READY AGE test-app-5ddxxxxx94 5 4 4 3m10s查看ReplicaSet的Event,可以發現新的Pod被ResourceQuota的准入控制器攔截了
kubectl -n test describe rs test-app-5ddxxxxx94預期輸出:
Name: test-app-5ddc68c994 Namespace: test Selector: app=test-app,pod-template-hash=5ddc68c994 Labels: app=test-app pod-template-hash=5ddc68c994 Annotations: deployment.kubernetes.io/desired-replicas: 5 deployment.kubernetes.io/max-replicas: 7 deployment.kubernetes.io/revision: 1 Controlled By: Deployment/test-app Replicas: 4 current / 5 desired Pods Status: 4 Running / 0 Waiting / 0 Succeeded / 0 Failed Pod Template: Labels: app=test-app pod-template-hash=5ddc68c994 Containers: test: Image: busybox:latest Port: <none> Host Port: <none> Command: sleep 360000000 Limits: cpu: 1 Environment: <none> Mounts: <none> Volumes: <none> Conditions: Type Status Reason ---- ------ ------ ReplicaFailure True FailedCreate Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal SuccessfulCreate 3m18s replicaset-controller Created pod: test-app-5ddc68c994-pjdfn Normal SuccessfulCreate 3m18s replicaset-controller Created pod: test-app-5ddc68c994-jdv4m Normal SuccessfulCreate 3m18s replicaset-controller Created pod: test-app-5ddc68c994-jhmtb Normal SuccessfulCreate 3m18s replicaset-controller Created pod: test-app-5ddc68c994-mr8vq Warning FailedCreate 3m18s replicaset-controller Error creating: pods "test-app-5ddc68c994-5s4ph" is forbidden: exceeded quota: test-quota, requested: limits.cpu=1,requests.cpu=1, used: limits.cpu=4,requests.cpu=4, limited: limits.cpu=4,requests.cpu=
kubectl create namespace testcat << EOF | kubectl apply -f -
apiVersion: v1
kind: ResourceQuota
metadata:
name: test-quota
namespace: test
spec:
hard:
requests.cpu: "4000m"
limits.cpu: "4000m"
EOFcat << EOF | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: test-app
name: test-app
namespace: test
spec:
replicas: 5
selector:
matchLabels:
app: test-app
template:
metadata:
labels:
app: test-app
spec:
containers:
- image: registry.cn-hangzhou.aliyuncs.com/acs/busybox:v1.29.2
imagePullPolicy: IfNotPresent
name: test
command:
- sleep
- "360000000"
resources:
limits:
cpu: "1"
EOF