Configure Default Memory Requests and Limits for a Namespace

    This page shows how to configure default memory requests and limits for a namespace.

    A Kubernetes cluster can be divided into namespaces. Once you have a namespace that has a default memory , and you then try to create a Pod with a container that does not specify its own memory limit, then the control plane assigns the default memory limit to that container.

    Kubernetes assigns a default memory request under certain conditions that are explained later in this topic.

    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 minikube 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 2 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’s a manifest for an example . The manifest specifies a default memory request and a default memory limit.

    admin/resource/memory-defaults.yaml

    1. kind: LimitRange
    2. metadata:
    3. name: mem-limit-range
    4. spec:
    5. limits:
    6. - default:
    7. memory: 512Mi
    8. defaultRequest:
    9. memory: 256Mi
    10. type: Container

    Create the LimitRange in the default-mem-example namespace:

    1. kubectl apply -f https://k8s.io/examples/admin/resource/memory-defaults.yaml --namespace=default-mem-example

    Now if you create a Pod in the default-mem-example namespace, and any container within that Pod does not specify its own values for memory request and memory limit, then the applies default values: a memory request of 256MiB and a memory limit of 512MiB.

    Here’s an example manifest for a Pod that has one container. The container does not specify a memory request and limit.

    admin/resource/memory-defaults-pod.yaml Configure Default Memory Requests and Limits for a Namespace - 图2

    1. apiVersion: v1
    2. kind: Pod
    3. metadata:
    4. name: default-mem-demo
    5. containers:
    6. - name: default-mem-demo-ctr
    7. image: nginx
    1. kubectl apply -f https://k8s.io/examples/admin/resource/memory-defaults-pod.yaml --namespace=default-mem-example

    View detailed information about the Pod:

    The output shows that the Pod’s container has a memory request of 256 MiB and a memory limit of 512 MiB. These are the default values specified by the LimitRange.

    1. - image: nginx
    2. imagePullPolicy: Always
    3. name: default-mem-demo-ctr
    4. resources:
    5. limits:
    6. memory: 512Mi
    7. requests:
    8. memory: 256Mi

    Delete your Pod:

    1. kubectl delete pod default-mem-demo --namespace=default-mem-example

    What if you specify a container’s limit, but not its request?

    Here’s a manifest for a Pod that has one container. The container specifies a memory limit, but not a request:

    admin/resource/memory-defaults-pod-2.yaml

    1. apiVersion: v1
    2. kind: Pod
    3. metadata:
    4. name: default-mem-demo-2
    5. spec:
    6. containers:
    7. - name: default-mem-demo-2-ctr
    8. image: nginx
    9. resources:
    10. limits:
    11. memory: "1Gi"

    Create the Pod:

    View detailed information about the Pod:

    The output shows that the container’s memory request is set to match its memory limit. Notice that the container was not assigned the default memory request value of 256Mi.

    1. resources:
    2. limits:
    3. requests:
    4. memory: 1Gi

    Here’s a manifest for a Pod that has one container. The container specifies a memory request, but not a limit:

    admin/resource/memory-defaults-pod-3.yaml Configure Default Memory Requests and Limits for a Namespace - 图4

    1. apiVersion: v1
    2. kind: Pod
    3. metadata:
    4. name: default-mem-demo-3
    5. spec:
    6. containers:
    7. - name: default-mem-demo-3-ctr
    8. image: nginx
    9. resources:
    10. requests:
    11. memory: "128Mi"

    Create the Pod:

    1. kubectl apply -f https://k8s.io/examples/admin/resource/memory-defaults-pod-3.yaml --namespace=default-mem-example

    View the Pod’s specification:

    1. kubectl get pod default-mem-demo-3 --output=yaml --namespace=default-mem-example

    The output shows that the container’s memory request is set to the value specified in the container’s manifest. The container is limited to use no more than 512MiB of memory, which matches the default memory limit for the namespace.

    Motivation for default memory limits and requests

    If your namespace has a memory resource quota configured, it is helpful to have a default value in place for memory limit. Here are three of the restrictions that a resource quota imposes on a namespace:

    • For every Pod that runs in the namespace, the Pod and each of its containers must have a memory limit. (If you specify a memory limit for every container in a Pod, Kubernetes can infer the Pod-level memory limit by adding up the limits for its containers).
    • Memory limits apply a resource reservation on the node where the Pod in question is scheduled. The total amount of memory reserved for all Pods in the namespace must not exceed a specified limit.
    • The total amount of memory actually used by all Pods in the namespace must also not exceed a specified limit.

    When you add a LimitRange:

    If any Pod in that namespace that includes a container does not specify its own memory limit, the control plane applies the default memory limit to that container, and the Pod can be allowed to run in a namespace that is restricted by a memory ResourceQuota.

    Delete your namespace:

    What’s next

    For app developers