{"id":635,"date":"2025-03-30T01:48:00","date_gmt":"2025-03-30T01:48:00","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=635"},"modified":"2025-03-30T01:48:00","modified_gmt":"2025-03-30T01:48:00","slug":"pod-priorityclass-preemption","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=635","title":{"rendered":"Kubernetes Pod Priority, PriorityClass and Preemption Explained"},"content":{"rendered":"<p>In this <a href=\"https:\/\/devopscube.com\/kubernetes-tutorials-beginners\/\">Kubernetes tutorial<\/a>, you will learn about important <a href=\"https:\/\/devopscube.com\/kubernetes-pod\/\">Kubernetes pod<\/a> scheduling concepts such as Pod priority, Preemption, and Pod PriorityClass<\/p>\n<h2 id=\"what-is-pod-priority-in-kubernetes\">What is Pod priority in Kubernetes?<\/h2>\n<p><strong>Pod priority<\/strong> is a Kubernetes scheduling feature that allows Kubernetes to make scheduling decisions comparing other pods based on priority number. Let&#8217;s look at the following two main concepts in pod priority.<\/p>\n<ol>\n<li>Pod Preemption<\/li>\n<li>Pod Priority Class<\/li>\n<\/ol>\n<h3 id=\"pod-preemption\">Pod Preemption<\/h3>\n<p>The pod preemption feature allows Kubernetes to preempt (evict) lower-priority pods from nodes when higher-priority pods are in the scheduling queue and no node resources are available.<\/p>\n<h3 id=\"kubernetes-pod-priority-class\">Kubernetes Pod Priority Class<\/h3>\n<p>To assign a pod a certain priority, you need a priority class.<\/p>\n<p>You can set a priority for a Pod using the <code>PriorityClass<\/code> object (non-namespaced) with a Value.<\/p>\n<p>The value determines the priority. It can be <strong>1,000,000,000 (one billion) or lower. <\/strong>Larger the number, the higher the priority.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/cdn.substack.com\/image\/fetch\/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep\/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F59a8997e-28af-411e-b91f-28fbfccb375e_423x198.png\" class=\"kg-image\" alt=\"Kubernetes Pod Priority value\" loading=\"lazy\" width=\"423\" height=\"198\"><\/figure>\n<p>The name of the priorityclass (<code>priorityClassName<\/code>) will be used in the pod specification to set the priority.<\/p>\n<div class=\"kg-card kg-callout-card kg-callout-card-grey\">\n<div class=\"kg-callout-text\">If you don&#8217;t want the priority class to preempt the pods, you can set <code spellcheck=\"false\" style=\"white-space: pre-wrap;\">PreemptionPolicy: Never<\/code>. By default, Priorityclasss use <code spellcheck=\"false\" style=\"white-space: pre-wrap;\">PreemptLowerPriority<\/code> policy.<\/div>\n<\/div>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/cdn.substack.com\/image\/fetch\/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep\/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F6aca5db6-9358-4279-b7ea-3440bac090bb_415x320.png\" class=\"kg-image\" alt=\"Kubernetes Pod Priorityclass name\" loading=\"lazy\" width=\"415\" height=\"320\"><\/figure>\n<h3 id=\"pod-priorityclass-example\">Pod PriorityClass Example<\/h3>\n<p>The following example has a PriorityClass object and a pod that uses the PriorityClass.<\/p>\n<pre><code>apiVersion: scheduling.k8s.io\/v1\nkind: PriorityClass\nmetadata:\n  name: high-priority-apps\nvalue: 1000000\npreemptionPolicy: PreemptLowerPriority\nglobalDefault: false\ndescription: \"Mission Critical apps.\"\n---\napiVersion: v1\nkind: Pod\nmetadata:\n  name: nginx\n  labels:\n    env: dev\nspec:\n  containers:\n  - name: web\n    image: nginx:latest\n    imagePullPolicy: IfNotPresent\n  priorityClassName: high-priority-apps<\/code><\/pre>\n<h3 id=\"kubernetes-system-high-priorityclass\">Kubernetes System High PriorityClass<\/h3>\n<p>How do you <strong>safeguard system-critical pods<\/strong> from preemption?<\/p>\n<p>Well, there are two default high-priority classes set by Kubernetes<\/p>\n<ol>\n<li><strong>system-node-critical:<\/strong> This class has a value of <code>2000001000<\/code>. Static pods Pods like etcd, kube-apiserver, kube-scheduler and Controller manager use this priority class.<\/li>\n<li><strong>system-cluster-critical:<\/strong> This class has a value of <code>2000000000<\/code>. Addon Pods like coredns, calico controller, metrics server, etc use this Priority class.<\/li>\n<\/ol>\n<h2 id=\"how-does-kubernetes-pod-priority-preemption-work\">How does Kubernetes Pod Priority &amp; Preemption work?<\/h2>\n<ol>\n<li>If a pod is deployed with <code>PriorityClassName<\/code>, the priority admission controller gets the priority value using the PriorityClassName value.<\/li>\n<li>If there are many pods in the scheduling queue, the scheduler arranges the scheduling order based on priority. Meaning, the scheduler places the high-priority pod ahead of low priority pods<\/li>\n<li>Now, if there are no nodes available with resources to accommodate a higher-priority pod, the preemption logic kicks in.<\/li>\n<li>The scheduler preempts (evicts) low priority pod from a node where it can schedule the higher-priority pod. The evicted pod gets a graceful default termination time of 30 seconds. If pods have <code>terminationGracePeriodSeconds<\/code> set for <code>preStop<\/code> <a href=\"https:\/\/kubernetes.io\/docs\/concepts\/containers\/container-lifecycle-hooks\/?ref=devopscube.com\" rel=\"noreferrer noopener\">container Lifecycle Hooks<\/a>, it overrides the default 30 seconds.<\/li>\n<li>However, if for some reason, the scheduling requirements are not met, the scheduler goes ahead with scheduling the lower-priority pods.<\/li>\n<\/ol>\n<figure class=\"kg-card kg-image-card kg-card-hascaption\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/pod-priorityclass-1.png\" class=\"kg-image\" alt=\"Pod priority and Preemption workflow\" loading=\"lazy\" width=\"624\" height=\"979\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/pod-priorityclass-1.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/pod-priorityclass-1.png 624w\"><figcaption><span style=\"white-space: pre-wrap;\">Click to view in HD<\/span><\/figcaption><\/figure>\n<p>Now we know how <strong>kubernetes pod scheduling priority works<\/strong> with Priorityclass and preemption.<\/p>\n<h2 id=\"pod-priority-faqs\">Pod Priority FAQs<\/h2>\n<p>The following are the frquesntly asked questions about Pod priority.<\/p>\n<h3 id=\"what-is-kubernetes-daemonset-priority\">What is Kubernetes DaemonSet Priority?<\/h3>\n<p><a href=\"https:\/\/devopscube.com\/kubernetes-daemonset\/\">Daemonset<\/a> has priority like any other pod. Therefore, if you want your Daemonsets to be stable and not evicted during a node resource crunch, you need to set a higher pod PriorityClass to the Daemonset.<\/p>\n<h3 id=\"how-is-pod-qos-related-to-pod-priority-preemption\">How is Pod QoS related to Pod Priority &amp; Preemption?<\/h3>\n<p>Kubelet first considers the QoS class and then the pod priority value to evict pods. This happens only when there is a resource shortage on the nodes.<\/p>\n<p>However, preemption logic kicks in only when high-priority pods are on the scheduling queue. The scheduler ignores the pod QoS during pod preemption. Whereas a QoS-based eviction happens without a scheduling queue due to a resource crunch.<\/p>\n<h3 id=\"what-is-the-significance-of-pod-priority\">What is the significance of Pod Priority?<\/h3>\n<p>When you deploy apps to Kubernetes in production, there are certain apps you don\u2019t want to get killed. For example, a metrics collector Daemonset, logging agents, payment service, etc. <\/p>\n<p>To ensure the availability of mission-critical pods, you can create a hierarchy of pod tiers with priorities; when there is a resource crunch in the clusters, kubelet tries to kill the low-priority pods to accommodate pods with higher PriorityClass. <\/p>\n<h3 id=\"what-is-the-effect-of-poddisruptionbudget-on-preemption\">What is the effect of PodDisruptionBudget on Preemption?<\/h3>\n<p>When the scheduler considers pods for preemption, it respects PDB constraints where possible. It tries not to violate a PDB by ensuring the minimum number of pods specified in the PDB remains running.<\/p>\n<p>However, if a very high-priority pod needs to be scheduled, Kubernetes will still remove lower-priority pods, even if it violates the PDB rules.<\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>As a <a href=\"https:\/\/devopscube.com\/become-devops-engineer\/\">DevOps Engineer,<\/a> it is very important to understand kubernetes pod priority when you work on Kubernetes implementations.<\/p>\n<p>Also, it is one of the important topics in <a href=\"https:\/\/devopscube.com\/kubernetes-certification-coupon\/\">Kubernetes certification<\/a> as well.<\/p>\n<p>Getting started with Kubernetes? check out the <a href=\"https:\/\/devopscube.com\/kubernetes-cluster-vagrant\/\">automated Kubernetes cluster setup using Vagrant<\/a><\/p>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/pod-priorityclass-preemption\/\" target=\"_blank\" rel=\"noopener noreferrer\">Kubernetes Pod Priority, PriorityClass and Preemption Explained \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/pod-priorityclass-preemption\/<\/p>\n","protected":false},"author":1,"featured_media":636,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-635","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/635","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=635"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/635\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/636"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=635"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=635"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=635"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}