⎈ k8s knowledge compiler

Assign CPU Resources to Containers and Pods [page]deterministic

tasks

This page shows how to assign a CPU *request* and a CPU *limit* to a container. Containers cannot use more CPU than the configured limit. Provided the system has CPU time free, a container is guaranteed to be allocated as much CPU as it requests.

##

Your cluster must have at least 1 CPU available for use to run the task examples.

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](#gloss:minikube), run the following command to enable metrics-server:

```shell minikube addons enable metrics-server ```

To see whether metrics-server (or another provider of the resource metrics API, `metrics.k8s.io`) is running, type the following command:

```shell kubectl get apiservices ```

If the resource metrics API is available, the output will include a reference to `metrics.k8s.io`.

``` NAME v1beta1.metrics.k8s.io ```

## Create a namespace

Create a [namespace](#gloss:namespace) so that the resources you create in this exercise are isolated from the rest of your cluster.

```shell kubectl create namespace cpu-example ```

## Specify a CPU request and a CPU limit

To specify a CPU request for a container, include the `resources.requests.cpu` field in the container’s resource manifest. To specify a CPU limit, include `resources.limits.cpu`.

In this exercise, you create a Pod that has one container. The container has a request of 0.5 CPU and a limit of 1 CPU. Here is the configuration file for the Pod:

The `args` section of the configuration file provides arguments for the container when it starts. The `-cpus "2"` argument tells the Container to attempt to use 2 CPUs.

Create the Pod:

```shell kubectl apply -f https://k8s.io/examples/pods/resource/cpu-request-limit.yaml --namespace=cpu-example ```

Verify that the Pod is running:

```shell kubectl get pod cpu-demo --namespace=cpu-example ```

View detailed information about the Pod:

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

The output shows that the one container in the Pod has a CPU request of 500 milliCPU and a CPU limit of 1 CPU.

```yaml resources: limits: cpu: "1" requests: cpu: 500m ```

Use `kubectl top` to fetch the metrics for the Pod:

```shell kubectl top pod cpu-demo --namespace=cpu-example ```

This example output shows that the Pod is using 974 milliCPU, which is slightly less than the limit of 1 CPU specified in the Pod configuration.

``` NAME CPU(cores) MEMORY(bytes) cpu-demo 974m <something> ```

Recall that by setting `-cpu "2"`, you configured the Container to attempt to use 2 CPUs, but the Container is only being allowed to use about 1 CPU. The container's CPU use is being throttled, because the container is attempting to use more CPU resources than its limit.

> Note: Another possible explanation for the CPU use being below 1.0 is that the Node might not have enough CPU resources available. Recall that the prerequisites for this exercise require your cluster to have at least 1 CPU available for use. If your Container runs on a Node that has only 1 CPU, the Container cannot use more than 1 CPU regardless of the CPU limit specified for the Container.

## CPU units

The CPU resource is measured in *CPU* units. One CPU, in Kubernetes, is equivalent to:

* 1 AWS vCPU * 1 GCP Core * 1 Azure vCore * 1 Hyperthread on a bare-metal Intel processor with Hyperthreading

Fractional values are allowed. A Container that requests 0.5 CPU is guaranteed half as much CPU as a Container that requests 1 CPU. You can use the suffix m to mean milli. For example 100m CPU, 100 milliCPU, and 0.1 CPU are all the same. Precision finer than 1m is not allowed.

CPU is always requested as an absolute quantity, never as a relative quantity; 0.1 is the same amount of CPU on a single …(trimmed)

Sources

tasks/configure-pod-container/assign-cpu-resource.md · docAssign CPU Resources to Containers and Pods

Related (15)

references Minikubeminikube conf=1
references Namespacenamespace conf=1
part_of {{% heading "prerequisites" %}}describes conf=1
part_of Create a namespacedescribes conf=1
part_of Specify a CPU request and a CPU limitdescribes conf=1
part_of CPU unitsdescribes conf=1
part_of If you do not specify a CPU limitdescribes conf=1
part_of Motivation for CPU requests and limitsdescribes conf=1
part_of Clean updescribes conf=1
part_of {{% heading "whatsnext" %}}describes conf=1
part_of For app developersdescribes conf=1
part_of For cluster administratorsdescribes conf=1
api_for Containerdocuments API object conf=1

← all Docs