Answers: <p>According to <a href="https://stackoverflow.com/a/58296752">https://stackoverflow.com/a/58296752</a> you need to set requests.cpu really low for the background pod (like requests.cpu: 50m, limits.cpu: 4000m) and high for the foreground pod (like requests.cpu 3950m, limits.cpu: 4000m).
  That will make the background pod wait until there is unused CPU available on best effort basis.</p>
  
Answers: CC BY-SA 4.0
Answers: 2024-04-14T14:57:35.670Z
Answers: 2024-04-14T14:57:35.670Z
Answers: insp
Answers: <p>pod.spec.priorityClass can be used for influencing which pods get evicted in case of the cluster being overloaded (according to pod.spec.resources). But the k8s scheduler merely decides which pods go on which nodes. It does not influence the second-to-second CPU scheduling during the lifetime of the pod - this is the domain of the Linux Kernel.</p>
  <p>If you have a known-to-be-cpu-intensive pod, it would make sense to run it with <code>nice</code> on the command line to give it an <em>actually</em> lower CPU priority.</p>
  
Answers: CC BY-SA 4.0
Answers: 2024-12-03T14:24:48.413Z
Answers: 2024-12-03T14:24:48.413Z
Answers: Karl Jørgensen
Body: <p>Consider this scenario:</p>
  <ul>
  <li>1 worker node with 4 CPU cores</li>
  <li>2 pods there, one of which is some CPU intensive bg task, another one is a web server with unpredictable CPU load during the day.</li>
  </ul>
  <p>I need to somehow tell k8s that my web server has higher priority than bg task in terms of CPU. Because I want to get is 100% load by bg task when there is no load on web server and e.g. 25/75% load when web server is on peak.</p>
  <p>I tried to do something with resource requests and limits, but it works differently than what I expected. For example if I limit bg task to one core and then dedicate 3 cores to web server to handle highload peaks, it will obviously mean that CPU in my cluster is under loaded most of the time and I want to maximize utilization. If I do not set limits for CPU for both pods at all, on peaks web server works slower than needed.</p>
  <p>Another approach which also does not work - is to schedule changes in resource requests to adapt to times when web server load is increasing, because peaks for web server are unpredictable and rather short, so I need a server up and running waiting for them.</p>
  <p>So considering I want to use k8s as a cluster orchestrator, is there a way to define CPU priorities for particular pods?</p>
  
Comments: CC BY-SA 4.0
Comments: 2024-03-28T13:25:28.133Z
Comments: Research tells me you can use PriorityClasses, Pod priority and resource requests and limits to manage this.
Comments: George Udosen
Tags: <kubernetes>
Title: CPU priorities for pods in k8s cluster
UserDisplayName: Kivan
ThreadText: # CPU priorities for pods in k8s cluster
  
  (Asked by: username_0 on 2024-03-22)
  
  Consider this scenario:
  
  * 1 worker node with 4 CPU cores
  * 2 pods there, one of which is some CPU intensive bg task, another one is a web server with unpredictable CPU load during the day.
  
  I need to somehow tell k8s that my web server has higher priority than bg task in terms of CPU. Because I want to get is 100% load by bg task when there is no load on web server and e.g. 25/75% load when web server is on peak.
  
  I tried to do something with resource requests and limits, but it works differently than what I expected. For example if I limit bg task to one core and then dedicate 3 cores to web server to handle highload peaks, it will obviously mean that CPU in my cluster is under loaded most of the time and I want to maximize utilization. If I do not set limits for CPU for both pods at all, on peaks web server works slower than needed.
  
  Another approach which also does not work - is to schedule changes in resource requests to adapt to times when web server load is increasing, because peaks for web server are unpredictable and rather short, so I need a server up and running waiting for them.
  
  So considering I want to use k8s as a cluster orchestrator, is there a way to define CPU priorities for particular pods?
  
  **Tags:** kubernetes
  
  > **Comments:**
  > **username_1**: Research tells me you can use PriorityClasses, Pod priority and resource requests and limits to manage this.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2024-04-14. Score: 0
  
  According to <https://stackoverflow.com/a/58296752> you need to set requests.cpu really low for the background pod (like requests.cpu: 50m, limits.cpu: 4000m) and high for the foreground pod (like requests.cpu 3950m, limits.cpu: 4000m).
  That will make the background pod wait until there is unused CPU available on best effort basis.
  
  
  ---
  
  ### Answer by username_3 on 2024-12-03. Score: 0
  
  pod.spec.priorityClass can be used for influencing which pods get evicted in case of the cluster being overloaded (according to pod.spec.resources). But the k8s scheduler merely decides which pods go on which nodes. It does not influence the second-to-second CPU scheduling during the lifetime of the pod - this is the domain of the Linux Kernel.
  
  If you have a known-to-be-cpu-intensive pod, it would make sense to run it with `nice` on the command line to give it an *actually* lower CPU priority.
  
  
  ---
  
  