Configure Memory and CPU Quotas for a Namespace

    This page shows how to set quotas for the total amount memory and CPU that can be used by all Pods running in a namespace. You specify quotas in a object.

    You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. It is recommended to run this tutorial on a cluster with at least two nodes that are not acting as control plane hosts. If you do not already have a cluster, you can create one by using or you can use one of these Kubernetes playgrounds:

    You must have access to create namespaces in your cluster.

    Each node in your cluster must have at least 1 GiB of memory.

    Create a namespace

    Create a namespace so that the resources you create in this exercise are isolated from the rest of your cluster.

    Here is a manifest for an example ResourceQuota:

    admin/resource/quota-mem-cpu.yaml

    1. kind: ResourceQuota
    2. metadata:
    3. name: mem-cpu-demo
    4. spec:
    5. hard:
    6. requests.cpu: "1"
    7. limits.cpu: "2"
    8. limits.memory: 2Gi

    Create the ResourceQuota:

    1. kubectl apply -f https://k8s.io/examples/admin/resource/quota-mem-cpu.yaml --namespace=quota-mem-cpu-example

    View detailed information about the ResourceQuota:

    The ResourceQuota places these requirements on the quota-mem-cpu-example namespace:

    • For every Pod in the namespace, each container must have a memory request, memory limit, cpu request, and cpu limit.
    • The memory request total for all Pods in that namespace must not exceed 1 GiB.
    • The memory limit total for all Pods in that namespace must not exceed 2 GiB.
    • The CPU request total for all Pods in that namespace must not exceed 1 cpu.
    • The CPU limit total for all Pods in that namespace must not exceed 2 cpu.

    See to learn what Kubernetes means by “1 CPU”.

    Create a Pod

    Configure Memory and CPU Quotas for a Namespace - 图2

    Create the Pod:

    1. kubectl apply -f https://k8s.io/examples/admin/resource/quota-mem-cpu-pod.yaml --namespace=quota-mem-cpu-example

    Verify that the Pod is running and that its (only) container is healthy:

    1. kubectl get pod quota-mem-cpu-demo --namespace=quota-mem-cpu-example

    Once again, view detailed information about the ResourceQuota:

    1. kubectl get resourcequota mem-cpu-demo --namespace=quota-mem-cpu-example --output=yaml

    The output shows the quota along with how much of the quota has been used. You can see that the memory and CPU requests and limits for your Pod do not exceed the quota.

    If you have the jq tool, you can also query (using JSONPath) for just the used values, and pretty-print that that of the output. For example:

    1. kubectl get resourcequota mem-cpu-demo --namespace=quota-mem-cpu-example -o jsonpath='{ .status.used }' | jq .

    Here is a manifest for a second Pod:

    admin/resource/quota-mem-cpu-pod-2.yaml

    1. apiVersion: v1
    2. kind: Pod
    3. metadata:
    4. name: quota-mem-cpu-demo-2
    5. containers:
    6. image: redis
    7. resources:
    8. limits:
    9. memory: "1Gi"
    10. cpu: "800m"
    11. requests:
    12. memory: "700Mi"
    13. cpu: "400m"

    In the manifest, you can see that the Pod has a memory request of 700 MiB. Notice that the sum of the used memory request and this new memory request exceeds the memory request quota: 600 MiB + 700 MiB > 1 GiB.

    Attempt to create the Pod:

    The second Pod does not get created. The output shows that creating the second Pod would cause the memory request total to exceed the memory request quota.

    Discussion

    Instead of managing total resource use within a namespace, you might want to restrict individual Pods, or the containers in those Pods. To achieve that kind of limiting, use a LimitRange.

    Delete your namespace:

    1. kubectl delete namespace quota-mem-cpu-example

    What’s next

    For app developers