⎈ k8s knowledge compiler

Configure Default Memory Requests and Limits for a Namespace [page]deterministic

Define a default memory resource limit for a namespace, so that every new Pod in that namespace has a memory resource limit configured.

tasks

This page shows how to configure default memory requests and limits for a [namespace](#gloss:namespace).

A Kubernetes cluster can be divided into namespaces. Once you have a namespace that has a default memory [limit](/docs/concepts/configuration/manage-resources-containers/#requests-and-limits), and you then try to create a Pod with a container that does not specify its own memory limit, then the [control plane](#gloss: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 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.

```shell kubectl create namespace default-mem-example ```

## Create a LimitRange and a Pod

Here's a manifest for an example [LimitRange](#gloss:limitrange). The manifest specifies a default memory request and a default memory limit.

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

```shell 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 [control plane](#gloss:control-plane) 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.

Create the Pod.

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

View detailed information about the Pod:

```shell kubectl get pod default-mem-demo --output=yaml --namespace=default-mem-example ```

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.

```shell containers: - image: nginx imagePullPolicy: Always name: default-mem-demo-ctr resources: limits: memory: 512Mi requests: memory: 256Mi ```

Delete your Pod:

```shell 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:

Create the Pod:

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

View detailed information about the Pod:

```shell kubectl get pod default-mem-demo-2 --output=yaml --namespace=default-mem-example ```

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.

``` resources: limits: memory: 1Gi requests: memory: 1Gi ```

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

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

Create the Pod:

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

View the Pod's specification:

```shell 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.

``` resources: limits: memory: 512Mi requests: memory: 128Mi ```

> Note: A `LimitRange` does not check the consistency of the default values i …(trimmed)

Sources

tasks/administer-cluster/manage-resources/memory-default-namespace.md · docConfigure Default Memory Requests and Limits for a Namespace

Related (15)

references Namespacenamespace conf=1
references Control Planecontrol plane conf=1
references LimitRangeLimitRange conf=1
references ResourceQuotaresource quota conf=1
part_of {{% heading "prerequisites" %}}describes conf=1
part_of Create a namespacedescribes conf=1
part_of Create a LimitRange and a Poddescribes conf=1
part_of Clean updescribes conf=1
part_of {{% heading "whatsnext" %}}describes conf=1
part_of For cluster administratorsdescribes conf=1
part_of For app developersdescribes conf=1
api_for Namespacedocuments API object conf=1

← all Docs