Maksim
Maksim

Reputation: 349

Why doesn't Kubernetes set a 1 CPU limit for a pod?

My version Kubernetes is 1.22

I'm taking a Kubernetes CKAD course.

In the resources and limits section it says:

Let's now look at a container running on a node in the Docker world.

A Docker container has no limit to the resources it can consume on a node.

Say a container starts with one CPU on a node.

It can go up and consume as much resource as it requires suffocating the native processes on the node or other containers of resources.

However, you can set a limit for the resource usage on these parts by default.

Kubernetes sets a limit of one CPU to containers, so if you do not specify explicitly, a container will be limited to consume only one CPU from the node.

The same goes with memory.

By default, Kubernetes sets a limit of 512 Mi on containers.

If you don't like the default limits, you can change them by adding a limit section under the resources section in your file.

Specify new limits for the memory and CPU like this when the pod is created.

I created podr nginx, in which I specified only reguest:

apiVersion: v1
kind: Pod
metadata:
  labels:
    run: nginx-test
  name: nginx-test
spec:
  containers:
  - image: nginx
    name: nginx-test
    resources:
      requests:
        memory: "64Mi"
        cpu: "250m"
  dnsPolicy: ClusterFirst
  restartPolicy: Always

After creating a pod, I saw that the CPU and RAM are unlimited:

  Namespace                   Name                                                              CPU Requests  CPU Limits  Memory Requests  Memory Limits  Age
  ---------                   ----                                                              ------------  ----------  ---------------  -------------  ---
  only-test                   nginx-test                                                        250m (0%)     0 (0%)      64Mi (0%)        0 (0%)         8m33s

Maybe this is true for Docker? I am using Containerd.

Upvotes: 0

Views: 1278

Answers (1)

Maksim
Maksim

Reputation: 349

I figured it out. To use the default resources, you need to create LimitRange in a specific namespace.

Examples:

apiVersion: v1
kind: LimitRange
metadata:
  name: mem-limit-range
spec:
  limits:
  - default:
      memory: 512Mi
    defaultRequest:
      memory: 256Mi
    type: Container
apiVersion: v1
kind: LimitRange
metadata:
  name: cpu-limit-range
spec:
  limits:
  - default:
      cpu: 1
    defaultRequest:
      cpu: 0.5
    type: Container

Upvotes: 1

Related Questions