{"id":6190,"date":"2021-05-11T18:39:51","date_gmt":"2021-05-11T13:09:51","guid":{"rendered":"https:\/\/opstree.com\/blog\/\/?p=6190"},"modified":"2021-05-12T17:11:08","modified_gmt":"2021-05-12T11:41:08","slug":"taints-and-tolerations-usage-with-node-selector-in-kubernetes-scheduling","status":"publish","type":"post","link":"https:\/\/opstree.com\/blog\/2021\/05\/11\/taints-and-tolerations-usage-with-node-selector-in-kubernetes-scheduling\/","title":{"rendered":"Taints and Tolerations Usage with Node Selector in Kubernetes Scheduling"},"content":{"rendered":"\n<p>Earlier, while writing deployment files in k8s, I found that the pods were getting scheduled in any random node. The pods of small deployments got scheduled in large nodes, due to which large deployment pods were staying in a pending state. Therefore, I had to delete the small deployment pods, so that the large deployment pods could get scheduled in that particular node. <br><br>One day, I decided to get rid of this problem. While looking for a solution and exploring about Kubernetes, I got to know about Node taints and pod tolerations in Kubernetes. Here, in this blog, I&#8217;ll talk about node taints and pod toleration and how we can use it with nodeselector in kubernetes deployments.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h3 class=\"wp-block-heading\">Node taints&nbsp;<\/h3>\n\n\n\n<p>Taint is a<strong><em> property of a node(applied to nodes only)<\/em><\/strong> that allows you to repel a set of pods unless those pods explicitly tolerate the node taint.<br>In simple words, if we apply taints to a node it will create a restriction shield around the node which will prevent the pods to schedule inside that node.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"643\" height=\"496\" src=\"https:\/\/opstree.com\/blog\/\/wp-content\/uploads\/2021\/04\/screenshot-from-2021-04-12-09-33-13.png?w=643\" alt=\"\" class=\"wp-image-6556\" \/><figcaption>Node taint<\/figcaption><\/figure>\n\n\n\n<p class=\"has-text-align-justify\">When I got to know about Node taints, the first question that came to my mind was- <em>In Kubernetes,<\/em> we have 2 nodes Master &amp; Worker and my pod always gets scheduled in the worker node but why not in the master node. Are<em> there any taints applied to the master node<\/em>?<br><br>The answer is yes, the master node is tainted with the \u201cNoSchedule\u201d effect by default so that no pod gets scheduled into it.<br><br><a rel=\"noreferrer noopener\" href=\"https:\/\/stackoverflow.com\/questions\/54463287\/why-kubernetes-taints-the-master-node-with-noschedule-by-default#:~:text=Why%20kubernetes%20taints%20the%20master%20node%20with%20%22NoSchedule%22%20by%20default%3F,-kubernetes&amp;text=It%20tells%20that%20it%20is,command%20to%20remove%20that%20taint\" target=\"_blank\">Visit here<\/a> for more information.<\/p>\n\n\n\n<p><em>Taint has three arguments<\/em> i.e. a key, value and effect.<br>Command for applying Taint to the node .<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl taint node &lt;Node_Name&gt; &lt;key=value:TAINT_EFFECT&gt;<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"631\" height=\"320\" src=\"https:\/\/opstree.com\/blog\/\/wp-content\/uploads\/2021\/04\/screenshot-from-2021-04-08-19-53-34-1.png?w=631\" alt=\"\" class=\"wp-image-6497\" \/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Taint Effects<\/strong><\/h4>\n\n\n\n<p>There are three type\u2019s of taint effect which we can apply to a node and&nbsp;<br><strong>1- NoSchedule<\/strong><br>     If we apply this taint effect to a node then it will only allow the pods which have a toleration effect equal to NoSchedule. But if a pod is already scheduled in a node and then you apply taint to the node having effect NoSchedule, then the pod will remain scheduled in the node.<br><\/p>\n\n\n\n<p><strong>2- PreferNoSchedule<\/strong><br>      In this effect, it will first prefer for no scheduling of pod but if you have a single node and a PreferNoSchedule taint is applied on it. Then even if the pod didn&#8217;t tolerate the taint it will get schedule inside the node which has a taint effect: PreferNoSchedule.<br><\/p>\n\n\n\n<p><strong>3- NoExecute<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/opstree.com\/blog\/\/wp-content\/uploads\/2021\/04\/200-1.gif?w=261\" alt=\"\" class=\"wp-image-6431\" width=\"611\" height=\"468\" \/><\/figure><\/div>\n\n\n\n<p>This effect will not only restrict the pod to get scheduled in the node but also if a pod is already scheduled a specific node and we have applied a taint of effect NoExecute to the specific node, it will immediately throw out the pod outside the node.<br><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Pod Toleration<\/h3>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"582\" height=\"493\" src=\"https:\/\/opstree.com\/blog\/\/wp-content\/uploads\/2021\/04\/screenshot-from-2021-04-12-09-35-32.png?w=582\" alt=\"\" class=\"wp-image-6559\" \/><figcaption>Node Taint with Pod Toleration.<\/figcaption><\/figure>\n\n\n\n<p>As we know that node taints will repel a pod from scheduling in it. So, in order to prevent this, Kubernetes provides a concept of  pod toleration which gives pod an authority to get scheduled on the tainted node, if the toleration matches the node taint.  Tolerations are specified in PodSpec:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>apiVersion: apps\/v1\nkind: Deployment\nmetadata:\n  name: noschedule-deployment\nspec:\n  replicas: 1\n  selector:\n    matchExpressions:\n    - key: name\n      operator: In\n      values:\n      - nginx\n  template:\n    metadata:\n      labels:\n        name: nginx\n    spec:\n      containers:\n      - name: nginx-container\n        image: nginx\n        ports:\n        - containerPort: 80\n      tolerations:\n      - key: size\n        operator: \"Equal\"\n        value: large\n        effect: NoSchedule<\/code><\/pre>\n\n\n\n<p>If you take a look at the above deployment you will see tolerations block inside podSpec  and inside that you will find some keywords like:<br><strong>1 &#8211; key <\/strong>: The value which you have specified while applying node taint.<br><strong>2 &#8211; value<\/strong> : Value that you have mentioned while applying the node taint.<br><strong>3 &#8211; effect<\/strong> : Effect that you have mentioned while applying the node taint.<br><strong>4 &#8211; Operator<\/strong> : There are 2 values of operator Equal and Exists.<br>         <em>Equal: If we specify operator as Equal, then we have to specify all the key , value, and effect option.<br>         Exists: If we specify operator as Exists then it&#8217;s not compulsory to mention key, value, and effect option.<\/em><\/p>\n\n\n\n<p>If you want to allow your pod to tolerate every node taint then inside the pod toleration&#8217;s part, you should mention only the <code>operator : \"Exists\"<\/code> .<br>By defining this your pod able will tolerate every taint which was applied on the node.<\/p>\n\n\n\n<figure class=\"wp-block-video wp-block-embed is-type-video is-provider-videopress wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"VideoPress Video Player\" aria-label='VideoPress Video Player' width='840' height='473' src='https:\/\/videopress.com\/embed\/PgNiiyLP?preloadContent=metadata&amp;hd=0&amp;cover=1' frameborder='0' allowfullscreen allow='clipboard-write'><\/iframe><script src='https:\/\/v0.wordpress.com\/js\/next\/videopress-iframe.js?m=1674852142'><\/script>\n<\/div><figcaption>sample video showing the concept of taints and tolerations<\/figcaption><\/figure>\n\n\n\n<p><em><strong>If there is only one node and then we are applying node taints and pod toleration&#8217;s then it&#8217;s<\/strong> <strong>fine but what if we have a number of nodes, then in that case, can we assure that our pod will get scheduled in a specific node?<\/strong><\/em><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"700\" height=\"700\" src=\"https:\/\/opstree.com\/blog\/\/wp-content\/uploads\/2021\/04\/giphy.gif?w=700\" alt=\"\" class=\"wp-image-6554\" \/><\/figure>\n\n\n\n<p>So, the answer is <strong>yes<\/strong>. We can also assure that our pods get scheduled in a specific node by mentioning the nodeSelector property inside our pod specifications. We just have to mention the node label in which we want our pod to get scheduled.<\/p>\n\n\n\n<p>You can use default labels of the node or you can apply custom labels to the node through :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl label nodes &lt;node-name&gt; &lt;label-key&gt;=&lt;label-value&gt;<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/opstree.com\/blog\/\/wp-content\/uploads\/2021\/04\/screenshot-from-2021-04-12-10-33-40.png?w=731\" alt=\"\" class=\"wp-image-6568\" width=\"630\" height=\"35\" \/><\/figure>\n\n\n\n<p>Yaml file showing nodeSelector property mentioned inside podSpec:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>apiVersion: apps\/v1\nkind: Deployment\nmetadata:\n  name: noschedule-deployment\nspec:\n  replicas: 1\n  selector:\n    matchExpressions:\n    - key: name\n      operator: In\n      values:\n      - nginx\n  template:\n    metadata:\n      labels:\n        name: nginx\n    spec:\n      containers:\n      - name: nginx-container\n        image: nginx\n        ports:\n        - containerPort: 80\n      tolerations:\n      - key: size\n        operator: \"Equal\"\n        value: large\n        effect: NoExecute\n      nodeSelector:\n        size: large\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"641\" height=\"532\" src=\"https:\/\/opstree.com\/blog\/\/wp-content\/uploads\/2021\/04\/screenshot-from-2021-04-12-10-40-52.png?w=641\" alt=\"\" class=\"wp-image-6573\" \/><figcaption>taints and tolerations with node selector<\/figcaption><\/figure>\n\n\n\n<p style=\"font-size:30px;\"><strong>What\u2019s Next?<\/strong><\/p>\n\n\n\n<p>In the upcoming blog we will learn more into kubernetes scheduling types for example Node Affinity and Anti affinity as well as pod affinity and Anti affinity.<\/p>\n\n\n\n<p>Stay Connected \ud83d\ude42<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><a href=\"https:\/\/giphy.com\/\" target=\"_blank\" rel=\"noopener\">GIF references<\/a><\/p>\n\n\n\n<p><strong>Blog Pundit:&nbsp;<a href=\"https:\/\/opstree.com\/blog\/\/author\/abhishekbhardwaj510\/\"><\/a><a href=\"https:\/\/opstree.com\/blog\/\/author\/abhishekbhardwaj510\/\"><strong> <strong><\/strong><\/strong><\/a><strong><strong><a rel=\"noreferrer noopener\" href=\"https:\/\/opstree.com\/blog\/\/author\/kapendrasingh\/\" target=\"_blank\">Kapendra Singh<\/a><\/strong><\/strong><\/strong><\/p>\n\n\n\n<p>Opstree is an End to End DevOps solution provider<\/p>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link\" href=\"https:\/\/www.opstree.com\/contact-us\" target=\"_blank\" rel=\"noreferrer noopener\">CONTACT US<\/a><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Earlier, while writing deployment files in k8s, I found that the pods were getting scheduled in any random node. The pods of small deployments got scheduled in large nodes, due to which large deployment pods were staying in a pending state. Therefore, I had to delete the small deployment pods, so that the large deployment &hellip; <a href=\"https:\/\/opstree.com\/blog\/2021\/05\/11\/taints-and-tolerations-usage-with-node-selector-in-kubernetes-scheduling\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Taints and Tolerations Usage with Node Selector in Kubernetes Scheduling&#8221;<\/span><\/a><\/p>\n","protected":false},"author":203724631,"featured_media":29900,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[28070474],"tags":[718548511,768739309,706906376,718548565,718548555],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/opstree.com\/blog\/wp-content\/uploads\/2025\/11\/DevSecOps-1.jpg","jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pfDBOm-1BQ","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/posts\/6190"}],"collection":[{"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/users\/203724631"}],"replies":[{"embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/comments?post=6190"}],"version-history":[{"count":25,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/posts\/6190\/revisions"}],"predecessor-version":[{"id":6679,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/posts\/6190\/revisions\/6679"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/media\/29900"}],"wp:attachment":[{"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/media?parent=6190"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/categories?post=6190"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/tags?post=6190"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}