⎈ k8s knowledge compiler

Configure Minimum and Maximum Memory Constraints for a Namespace [page]deterministic

Define a range of valid memory resource limits for a namespace, so that every new Pod in that namespace falls within the range you configure.

tasks

This page shows how to set minimum and maximum values for memory used by containers running in a [namespace](#gloss:namespace). You specify minimum and maximum memory values in a [LimitRange](/docs/reference/kubernetes-api/policy-resources/limit-range-v1/) object. If a Pod does not meet the constraints imposed by the LimitRange, it cannot be created in the namespace.

##

You must have access to create namespaces in your cluster.

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

## 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 constraints-mem-example ```

## Create a LimitRange and a Pod

Here's an example manifest for a LimitRange:

Create the LimitRange:

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

View detailed information about the LimitRange:

```shell kubectl get limitrange mem-min-max-demo-lr --namespace=constraints-mem-example --output=yaml ```

The output shows the minimum and maximum memory constraints as expected. But notice that even though you didn't specify default values in the configuration file for the LimitRange, they were created automatically.

``` limits: - default: memory: 1Gi defaultRequest: memory: 1Gi max: memory: 1Gi min: memory: 500Mi type: Container ```

Now whenever you define a Pod within the constraints-mem-example namespace, Kubernetes performs these steps:

* If any container in that Pod does not specify its own memory request and limit, the control plane assigns the default memory request and limit to that container.

* Verify that every container in that Pod requests at least 500 MiB of memory.

* Verify that every container in that Pod requests no more than 1024 MiB (1 GiB) of memory.

Here's a manifest for a Pod that has one container. Within the Pod spec, the sole container specifies a memory request of 600 MiB and a memory limit of 800 MiB. These satisfy the minimum and maximum memory constraints imposed by the LimitRange.

Create the Pod:

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

Verify that the Pod is running and that its container is healthy:

```shell kubectl get pod constraints-mem-demo --namespace=constraints-mem-example ```

View detailed information about the Pod:

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

The output shows that the container within that Pod has a memory request of 600 MiB and a memory limit of 800 MiB. These satisfy the constraints imposed by the LimitRange for this namespace:

```yaml resources: limits: memory: 800Mi requests: memory: 600Mi ```

Delete your Pod:

```shell kubectl delete pod constraints-mem-demo --namespace=constraints-mem-example ```

## Attempt to create a Pod that exceeds the maximum memory constraint

Here's a manifest for a Pod that has one container. The container specifies a memory request of 800 MiB and a memory limit of 1.5 GiB.

Attempt to create the Pod:

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

The output shows that the Pod does not get created, because it defines a container that requests more memory than is allowed:

``` Error from server (Forbidden): error when creating "examples/admin/resource/memory-constraints-pod-2.yaml": pods "constraints-mem-demo-2" is forbidden: maximum memory usage per Container is 1Gi, but limit is 1536Mi. ```

## Attempt to create a Pod that does not meet the minimum memory request

Here's a manifest for a Pod that has one container. That container specifies a memory request of 100 MiB and a memory limit of 800 MiB.

Attempt to create the Pod:

```sh …(trimmed)

Sources

tasks/administer-cluster/manage-resources/memory-constraint-namespace.md · docConfigure Minimum and Maximum Memory Constraints for a Namespace

Related (14)

references Namespacenamespace 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