Kubernetes的namespaces下的空间配额

时间:June 23, 2020 分类:

目录:

namespaces下的空间配额

资源分为以下三种。

  • 计算资源配额:cpu,memory
  • 存储资源配置:requests.storage,pvc,某storage class下的限制(例如针对fast类型的sc的限制)
  • 对象数量配置:cm,service,pod的个数。
apiVersion: v1
kind: ResourceQuota
metadata:
  name: compute-resources
  namespace: why
spec:
  hard:
    limits.cpu: "2"
    limits.memory: 2Gi
    persistentvolumeclaims: "8"
    pods: "2"
    requests.cpu: "1"
    requests.memory: 1Gi
    requests.storage: 30Gi

创建测试Pod

apiVersion: v1
kind: Pod
metadata:
  name: quota-test
  namespace: why
spec:
  containers:
  - name: quota-mem-cpu-demo-ctr
    image: nginx
    resources:
      limits:
        memory: "4Gi"
        cpu: "800m" 
      requests:
        memory: "3Gi"
        cpu: "500m"

会有报错

$ kubectl apply -f test.yaml 
Error from server (Forbidden): error when creating "test.yaml": pods "quota-test" is forbidden: exceeded quota: compute-resources, requested: limits.memory=4Gi,requests.memory=3Gi, used: limits.memory=0,requests.memory=0, limited: limits.memory=2Gi,requests.memory=1Gi

当开启了resource quota,创建Pod必须指定CPU,Mem的requests和limits,否则创建会失败,可以考虑搭配limitRange

apiVersion: v1
kind: LimitRange
metadata:
  name: default
  namespace: why
spec:
  limits:
  - default:
      cpu: "6"
    defaultRequest:
      cpu: 200m
    type: Container