Configure Default Memory Requests and Limits for a Namespace

    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:

    To check the version, enter .

    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 the configuration file for a LimitRange object. The configuration specifies a default memory request and a default memory limit.

    admin/resource/memory-defaults.yaml

    1. apiVersion: v1
    2. kind: LimitRange
    3. metadata:
    4. name: mem-limit-range
    5. spec:
    6. limits:
    7. - default:
    8. memory: 512Mi
    9. defaultRequest:
    10. memory: 256Mi
    11. 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 a Container is created in the default-mem-example namespace, and the Container does not specify its own values for memory request and memory limit, the Container is given a default memory request of 256 MiB and a default memory limit of 512 MiB.

    Here’s the configuration file for a Pod that has one Container. The Container does not specify a memory request and limit.

    Configure Default Memory Requests and Limits for a Namespace - 图2

    1. apiVersion: v1
    2. kind: Pod
    3. metadata:
    4. spec:
    5. containers:
    6. - name: default-mem-demo-ctr
    7. image: nginx

    Create 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. containers:
    2. - image: nginx
    3. imagePullPolicy: Always
    4. name: default-mem-demo-ctr
    5. resources:
    6. limits:
    7. memory: 512Mi
    8. requests:
    9. 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 the configuration file for a Pod that has one Container. The Container specifies a memory limit, but not a request:

    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:

    Create the Pod:

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

    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. limits:
    2. memory: 1Gi
    3. requests:
    4. memory: 1Gi

    Here’s the configuration file 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

    Motivation for default memory limits and requests

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

    • Every Container that runs in the namespace must have its own memory limit.
    • The total amount of memory used by all Containers in the namespace must not exceed a specified limit.

    If a Container does not specify its own memory limit, it is given the default limit, and then it can be allowed to run in a namespace that is restricted by a quota.

    Delete your namespace:

    1. kubectl delete namespace default-mem-example

    What’s next

    For app developers