kubernetes: [FG:InPlacePodVerticalScaling] K8s fails to throttle pod so that it follows resized CPU limit

What happened?

I have a pod which initially has 0.5 CPU limit. I then resize it to follow 1.5 CPU limit and pod starts to use more resources until 1.5CPU. Problem arises when I resize pod CPU limit back to 0.5CPU. Instead of throttling the pod, kubernetes doesn’t do anything and pod continues to use up to 1.5CPU resource.

I have followed instructions here to resize limits of pods. I am monitoring it using metrics server and I can see that new limits are applied but when you increase limit and then decrease it, kubernetes fails to throttle pod so that it follows new decreased limit.

What did you expect to happen?

I expected kubernetes to throttle the pod so that it doesn’t use more than 0.5CPU but all I observe is that it is using more CPU Resource than limit.

How can we reproduce it (as minimally and precisely as possible)?

you can use any pod stressing the CPU (for example image:polinux/stress) and try to increase CPU resource limit and then decrease it.

test.yml:

apiVersion: v1
kind: Pod
metadata:
  name: pod-demo
  namespace: stress-test
spec:
  containers:
  - name: pod-demo
    image: polinux/stress
    resources:
      limits:
        memory: "600Mi"
        cpu: 0.2
      requests:
        memory: "600Mi"
        cpu: 0.1 
    command: ["stress"]
    args: ["--vm", "1", "--vm-bytes", "256M", "-c", "2", "--vm-hang", "1"]

Create a namespace stress-test : kubectl create namespace stress-test

Apply the yaml file: kubectl apply -f test.yml

Resize to increase the CPU limit: kubectl -n stress-test patch pod pod-demo --patch '{"spec":{"containers":[{"name":"pod-demo", "resources":{"requests":{"cpu":"100m","memory":"600Mi"} ,"limits":{"cpu":"800m", "memory":"600Mi"}}}]}}'

After 1/2 minute resize to decrease the CPU limit: kubectl -n stress-test patch pod pod-demo --patch '{"spec":{"containers":[{"name":"pod-demo", "resources":{"requests":{"cpu":"100m","memory":"600Mi"} ,"limits":{"cpu":"200m", "memory":"600Mi"}}}]}}'

Check the CPU resource usage and you will notice that it still follows the increased limit even though limit has been applied to the pod:

image

Kubernetes version

clientVersion:
  buildDate: "2023-05-17T14:13:27Z"
  compiler: gc
  gitCommit: 7f6f68fdabc4df88cfea2dcf9a19b2b830f1e647
  gitTreeState: clean
  gitVersion: v1.27.2
  goVersion: go1.20.4
  major: "1"
  minor: "27"
  platform: darwin/arm64
kustomizeVersion: v5.0.1
serverVersion:
  buildDate: "2023-04-11T17:04:24Z"
  compiler: gc
  gitCommit: 1b4df30b3cdfeaba6024e81e559a6cd09a089d65
  gitTreeState: clean
  gitVersion: v1.27.0
  goVersion: go1.20.3
  major: "1"
  minor: "27"
  platform: linux/arm64

Cloud provider

I have tried it locally

OS version

ProductName:            macOS
ProductVersion:         13.0
BuildVersion:           22A380

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 37 (30 by maintainers)

Most upvoted comments

I can’t reproduce this issue, you can check if my process is different from yours.

Create pod

➜  ~ cat pod.yml
apiVersion: v1
kind: Pod
metadata:
  name: pod-demo
  namespace: stress-test
spec:
  containers:
  - name: pod-demo
    image: polinux/stress
    resources:
      limits:
        memory: "600Mi"
        cpu: 0.2
      requests:
        memory: "600Mi"
        cpu: 0.1
    command: ["stress"]
    args: ["--vm", "1", "--vm-bytes", "256M", "-c", "2", "--vm-hang", "1"]
➜  ~ kubectl apply -f pod.yml
pod/pod-demo created

Check cgroup configuration

➜  ~ kubectl get pods -n stress-test  -oyaml | grep uid
    uid: e5090cc3-8d82-4bd2-9213-7e8df96cfe9b
➜  ~
➜  ~ cat /sys/fs/cgroup/kubepods/burstable/pode5090cc3-8d82-4bd2-9213-7e8df96cfe9b/cpu.max
20000 100000
➜  ~ systemd-cgtop
CGroup                                                                     Tasks   %CPU   Memory  Input/s Output/s                                                                                                                
kubepods/burstable/pode5090cc3-8d82-4bd2-9213-7e8df96cfe9b                  5      19.8    257.2M        -        -

Resize to increase the CPU limit:

➜  ~ kubectl -n stress-test patch pod pod-demo --patch '{"spec":{"containers":[{"name":"pod-demo", "resources":{"requests":{"cpu":"100m","memory":"600Mi"} ,"limits":{"cpu":"800m", "memory":"600Mi"}}}]}}'
pod/pod-demo patched
➜  ~ cat /sys/fs/cgroup/kubepods/burstable/pode5090cc3-8d82-4bd2-9213-7e8df96cfe9b/cpu.max
80000 100000
➜  ~ systemd-cgtop
CGroup                                                                 Tasks   %CPU   Memory  Input/s Output/s
kubepods/burstable/pode5090cc3-8d82-4bd2-9213-7e8df96cfe9b             5       79.5   257.2M        -        -

Resize to decrease the CPU limit:

➜  ~ kubectl -n stress-test patch pod pod-demo --patch '{"spec":{"containers":[{"name":"pod-demo", "resources":{"requests":{"cpu":"100m","memory":"600Mi"} ,"limits":{"cpu":"200m", "memory":"600Mi"}}}]}}'
pod/pod-demo patched
➜  ~ cat /sys/fs/cgroup/kubepods/burstable/pode5090cc3-8d82-4bd2-9213-7e8df96cfe9b/cpu.max
20000 100000
➜  ~ systemd-cgtop
CGroup                                                               Tasks   %CPU   Memory  Input/s Output/s
kubepods/burstable/pode5090cc3-8d82-4bd2-9213-7e8df96cfe9b           5       19.4   257.4M        -        -

I started the cluster based on the master branch. Can you try it on the latest version to see if you can reproduce this issue?

Changing the runtime with --container-runtime=containerd may be one of solutions.

Although I’m not familiar with minikube, it looks that the minikube docker driver uses cri-dockerd that may not fully support InPlacePodVerticalScaling. This looks like the same situation described at the first item in the known issues. At a glance, cri-dockerd does not set Resources to ContainerStatus (code) while containerd sets it (code).

Could you please try a different value from the initial value at decreasing the CPU limits?

This may need more information as it seems to be not reproducible.

@netcamo can you reproduce it in another cluster? You seems to run a minikube with kubernetes v1.27 on Mac.(The checkpoint bug is fixed in v1.29 with https://github.com/kubernetes/kubernetes/pull/117615. Is that related? Probably, you may upgrade to test using v1.29 Kubernetes)