前面說過,資源配額限制在指定名稱空間下,對資源物件數量和特定型別的資源的限制,你可以在ResourceQuota
中指定配額
建立名稱空間
我們建立一個新的名稱空間來演示
kubectl create namespace quota-object-example
建立資源配額
以下是資源配額物件的配置
admin/resource/quota-objects.yaml
apiVersion: v1
kind: ResourceQuota
metadata:
name: object-quota-demo
spec:
hard:
persistentvolumeclaims: "1"
services.loadbalancers: "2"
services.nodeports: "0"
通過kubectl apply
建立配額物件
kubectl apply -f https://k8s.io/examples/admin/resource/quota-objects.yaml --namespace=quota-object-example
檢視資源配額詳細資訊
kubectl get resourcequota object-quota-demo --namespace=quota-object-example --output=yaml
status:
hard:
persistentvolumeclaims: "1"
services.loadbalancers: "2"
services.nodeports: "0"
used:
persistentvolumeclaims: "0"
services.loadbalancers: "0"
services.nodeports: "0"
輸出資訊顯示,在quota-object-example
名稱空間下,只允許最多有一個PersistentVolumeClaim
,最多有兩個LoadBalancer
型別的服務,並且不允許有NodePort
型別的服務
建立一個PersistentVolumeClaim
以下是建立PersistentVolumeClaim
物件的檔案
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: pvc-quota-demo
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 3Gi
通過命令建立它
kubectl apply -f https://k8s.io/examples/admin/resource/quota-objects-pvc.yaml --namespace=quota-object-example
確認已經建立:
kubectl get `persistentvolumeclaims` --namespace=quota-object-example
NAME STATUS
pvc-quota-demo Pending
輸出資訊顯示persistentvolumeclaims
物件已經建立並且狀態為pending
下面嘗試再建立一個PersistentVolumeClaim
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: pvc-quota-demo-2
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 4Gi
執行建立命令
kubectl apply -f https://k8s.io/examples/admin/resource/quota-objects-pvc-2.yaml --namespace=quota-object-example
persistentvolumeclaims "pvc-quota-demo-2" is forbidden:
exceeded quota: object-quota-demo, requested: persistentvolumeclaims=1,
used: persistentvolumeclaims=1, limited: persistentvolumeclaims=1
通過輸出資訊可以看到,第二個persistantPersistentVolumeClaim
沒有被建立,因為超過了資源配額
注:以下字串型別可以被用來可以被資源配額使用
字串 | API 物件 |
---|---|
"pods" | Pod |
"services | Service |
"replicationcontrollers" | ReplicationController |
"resourcequotas" | ResourceQuota |
"secrets" | Secret |
"configmaps" | ConfigMap |
"persistentvolumeclaims" | PersistentVolumeClaim |
"services.nodeports" | Service of type NodePort |
"services.loadbalancers" | Service of type LoadBalancer |
刪除資源配額
kubectl delete namespace quota-object-example