Assign Memory Resources to Containers and Pods [page]deterministic
This page shows how to assign a memory *request* and a memory *limit* to a Container. A Container is guaranteed to have as much memory as it requests, but is not allowed to use more memory than its limit.
##
Each node in your cluster must have at least 300 MiB of memory.
A few of the steps on this page require you to run the [metrics-server](https://github.com/kubernetes-sigs/metrics-server) service in your cluster. If you have the metrics-server running, you can skip those steps.
If you are running Minikube, run the following command to enable the metrics-server:
```shell minikube addons enable metrics-server ```
To see whether the metrics-server is running, or another provider of the resource metrics API (`metrics.k8s.io`), run the following command:
```shell kubectl get apiservices ```
If the resource metrics API is available, the output includes a reference to `metrics.k8s.io`.
```shell NAME v1beta1.metrics.k8s.io ```
## 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 mem-example ```
## Specify a memory request and a memory limit
To specify a memory request for a container, include the `resources.requests.memory` field in the container’s resource manifest. To specify a memory limit, include `resources.limits.memory`.
In this exercise, you create a Pod that has one Container. The Container has a memory request of 100 MiB and a memory limit of 200 MiB. Here's the configuration file for the Pod:
The `args` section in the configuration file provides arguments for the Container when it starts. The `"--vm-bytes", "150M"` arguments tell the Container to attempt to allocate 150 MiB of memory.
Create the Pod:
```shell kubectl apply -f https://k8s.io/examples/pods/resource/memory-request-limit.yaml --namespace=mem-example ```
Verify that the Pod Container is running:
```shell kubectl get pod memory-demo --namespace=mem-example ```
View detailed information about the Pod:
```shell kubectl get pod memory-demo --output=yaml --namespace=mem-example ```
The output shows that the one Container in the Pod has a memory request of 100 MiB and a memory limit of 200 MiB.
```yaml ... resources: requests: memory: 100Mi limits: memory: 200Mi ... ```
Run `kubectl top` to fetch the metrics for the pod:
```shell kubectl top pod memory-demo --namespace=mem-example ```
The output shows that the Pod is using about 162,900,000 bytes of memory, which is about 150 MiB. This is greater than the Pod's 100 MiB request, but within the Pod's 200 MiB limit.
``` NAME CPU(cores) MEMORY(bytes) memory-demo <something> 162856960 ```
Delete your Pod:
```shell kubectl delete pod memory-demo --namespace=mem-example ```
## Exceed a Container's memory limit
A Container can exceed its memory request if the Node has memory available. But a Container is not allowed to use more than its memory limit. If a Container allocates more memory than its limit, the Container becomes a candidate for termination. If the Container continues to consume memory beyond its limit, the Container is terminated. If a terminated Container can be restarted, the kubelet restarts it, as with any other type of runtime failure.
In this exercise, you create a Pod that attempts to allocate more memory than its limit. Here is the configuration file for a Pod that has one Container with a memory request of 50 MiB and a memory limit of 100 MiB:
In the `args` section of the configuration file, you can see that the Container will attempt to allocate 250 MiB of memory, which is well above the 100 MiB limit.
Create the Pod:
```shell kubectl apply -f https://k8s.io/examples/pods/resource/memory-request-limit-2.yaml --namespace=mem-example ```
View detailed information about the Pod:
```shell kubectl get pod memory-demo-2 --namespace=mem-example ```
At this point, the Container might b …(trimmed)