{"id":414744,"date":"2022-03-11T15:00:46","date_gmt":"2022-03-11T12:00:46","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/what-is-pod-affinity-and-anti-affinity-in-kubernetes-cloudsavvy-it\/"},"modified":"2022-03-11T15:00:46","modified_gmt":"2022-03-11T12:00:46","slug":"what-is-pod-affinity-and-anti-affinity-in-kubernetes-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/what-is-pod-affinity-and-anti-affinity-in-kubernetes-cloudsavvy-it\/","title":{"rendered":"#What Is Pod Affinity and Anti-Affinity In Kubernetes? \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a28b75e618a3\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a28b75e618a3\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-pod-affinity-and-anti-affinity-in-kubernetes-cloudsavvy-it\/#%E2%80%9CWhat_Is_Pod_Affinity_and_Anti-Affinity_In_Kubernetes_%E2%80%93_CloudSavvy_IT%E2%80%9D\" >&#8220;What Is Pod Affinity and Anti-Affinity In Kubernetes? \u2013 CloudSavvy IT&#8221;<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-pod-affinity-and-anti-affinity-in-kubernetes-cloudsavvy-it\/#How_Does_Affinity_Work\" >How Does Affinity Work?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-pod-affinity-and-anti-affinity-in-kubernetes-cloudsavvy-it\/#Types_of_Affinity_Condition\" >Types of Affinity Condition<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-pod-affinity-and-anti-affinity-in-kubernetes-cloudsavvy-it\/#Setting_Node_Affinities\" >Setting Node Affinities<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-pod-affinity-and-anti-affinity-in-kubernetes-cloudsavvy-it\/#Setting_Inter-Pod_Affinities\" >Setting Inter-Pod Affinities<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-pod-affinity-and-anti-affinity-in-kubernetes-cloudsavvy-it\/#Other_Scheduling_Constraints\" >Other Scheduling Constraints<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-pod-affinity-and-anti-affinity-in-kubernetes-cloudsavvy-it\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"%E2%80%9CWhat_Is_Pod_Affinity_and_Anti-Affinity_In_Kubernetes_%E2%80%93_CloudSavvy_IT%E2%80%9D\"><\/span>&#8220;What Is Pod Affinity and Anti-Affinity In Kubernetes? \u2013 CloudSavvy IT&#8221;<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage aligncenter size-full wp-image-9632\" data-pagespeed-lazy-srcset=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/02\/748108a6.jpg?width=398&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 400w, https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/02\/748108a6.jpg?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 1200w\" sizes=\"auto, 400w, 1200w\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/02\/748108a6.jpg?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Graphic showing the Kubernetes logo\" width=\"1602\" height=\"902\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Kubernetes is a distributed system that\u2019s designed to scale replicas of your services across multiple physical environments. In many cases this works well out-of-the-box. The Kubernetes scheduler automatically places your Pods (container instances) onto Nodes (worker machines) that have enough resources to support them.<\/p>\n<p>Despite its best efforts, sometimes the scheduler won\u2019t select a plan you agree with. You might want Pods to be colocated if they\u2019ll be regularly communicating over the network; alternatively, some compute-intensive Pods might be best allocated to separate Nodes wherever possible.<\/p>\n<p>Kubernetes has <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/kubernetes.io\/docs\/concepts\/scheduling-eviction\/assign-pod-node\/#affinity-and-anti-affinity\">several mechanisms<\/a> which let you guide the scheduler\u2019s decision-making process so Pods end up on particular Nodes. In this article, we\u2019ll focus specifically on the \u201caffinity\u201d and \u201canti-affinity\u201d concepts that give you granular control of scheduling. Affinities define rules that either must or should be met before a Pod can be allocated to a Node.<\/p>\n<h2 id=\"how-does-affinity-work\"><span class=\"ez-toc-section\" id=\"How_Does_Affinity_Work\"><\/span>How Does Affinity Work?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Affinities are used to express Pod scheduling constraints that can match characteristics of candidate Nodes and the Pods that are already running on those Nodes. A Pod that has an \u201caffinity\u201d to a given Node is more likely to be scheduled to it; conversely, an \u201canti-affinity\u201d makes it less probable it\u2019ll be scheduled. The overall balance of these weights is used to determine the final placement of each Pod.<\/p>\n<p>Affinity assessments can produce either hard or soft outcomes. A \u201chard\u201d result means the Node <em>must<\/em> have the characteristics defined by the affinity expression. \u201cSoft\u201d affinities act as a preference, indicating to the scheduler that it should use a Node with the characteristics if one is available. A Node that doesn\u2019t meet the condition will still be selected if necessary.<\/p>\n<h2 id=\"types-of-affinity-condition\"><span class=\"ez-toc-section\" id=\"Types_of_Affinity_Condition\"><\/span>Types of Affinity Condition<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>There are currently two different kinds of affinity that you can define:<\/p>\n<ul>\n<li><strong>Node Affinity<\/strong> \u2013 Used to constrain the Nodes that can receive a Pod by matching labels of those Nodes. Node Affinity can only be used to set positive affinities that attract Pods to the Node.<\/li>\n<li><strong>Inter-Pod Affinity<\/strong> \u2013 Used to constrain the Nodes that can receive a Pod by matching labels <em>of the existing Pods already running on each of those Nodes<\/em>. Inter-Pod Affinity can be either an attracting affinity or a repelling anti-affinity.<\/li>\n<\/ul>\n<p>In the simplest possible example, a Pod that includes a Node Affinity condition of <code>label=value<\/code> will only be scheduled to Nodes with a <code>label=value<\/code> label. A Pod with the same condition but defined as an Inter-Pod Affinity will be scheduled to a Node that already hosts a <em>Pod<\/em> with a <code>label=value<\/code> label.<\/p>\n<h2 id=\"setting-node-affinities\"><span class=\"ez-toc-section\" id=\"Setting_Node_Affinities\"><\/span>Setting Node Affinities<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Node Affinity has two distinct sub-types:<\/p>\n<ul>\n<li><strong><code>requiredDuringSchedulingIgnoredDuringExecution<\/code><\/strong> \u2013 This is the \u201chard\u201d affinity matcher that <em>requires<\/em> the Node meet the constraints you define.<\/li>\n<li><strong><code>preferredDuringSchedulingIgnoredDuringExecution<\/code><\/strong> \u2013 This is the \u201csoft\u201d matcher to express a preference that\u2019s ignored when it can\u2019t be fulfilled.<\/li>\n<\/ul>\n<p>The <code>IgnoredDuringExecution<\/code> part of these verbose names makes it explicit that affinity is only considered while scheduling Pods. Once a Pod has made it onto a Node, affinity isn\u2019t re-evaluated. Changes to the Node won\u2019t cause a Pod eviction due to changed affinity values. A future Kubernetes release could add support for this behavior via the reserved <code>requiredDuringSchedulingRequiredDuringExecution<\/code> phrase.<\/p>\n<p>Node affinities are attached to Pods via their <code>spec.affinity.nodeAffinity<\/code> manifest field:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"yaml\">\n<pre class=\"de1\"><span class=\"co3\">apiVersion<\/span><span class=\"sy2\">: <\/span>v1<span class=\"co3\">\nkind<\/span><span class=\"sy2\">: <\/span>Pod<span class=\"co4\">\nmetadata<\/span>:<span class=\"co3\">\n  name<\/span><span class=\"sy2\">: <\/span>demo-pod<span class=\"co4\">\nspec<\/span>:<span class=\"co4\">\n  containers<\/span>:<span class=\"co3\">\n    - name<\/span><span class=\"sy2\">: <\/span>demo-container\n    <span class=\"co1\"># ...<\/span><span class=\"co4\">\n  affinity<\/span>:<span class=\"co4\">\n    nodeAffinity<\/span>:<span class=\"co4\">\n      requiredDuringSchedulingIgnoredDuringExecution<\/span>:<span class=\"co4\">\n        nodeSelectorTerms<\/span>:<span class=\"co4\">\n          - matchExpressions<\/span>:<span class=\"co3\">\n            - key<\/span><span class=\"sy2\">: <\/span>hardware-class<span class=\"co3\">\n              operator<\/span><span class=\"sy2\">: <\/span>In<span class=\"co4\">\n              values<\/span><span class=\"sy2\">:\n<\/span>                - a\n                - b\n                - c<span class=\"co4\">\n          - matchExpressions<\/span>:<span class=\"co3\">\n            - key<\/span><span class=\"sy2\">: <\/span>internal<span class=\"co3\">\n              operator<\/span><span class=\"sy2\">: <\/span>Exists<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>This manifest creates a hard affinity rule that schedules the Pod to a Node meeting the following criteria:<\/p>\n<ul>\n<li>It has a <code>hardware-class<\/code> label with either <code>a<\/code>, <code>b<\/code>, or <code>c<\/code> as the value.<\/li>\n<li>It has an <code>internal<\/code> label with any value.<\/li>\n<\/ul>\n<p>You can attach additional conditions by repeating the <code>matchExpressions<\/code> clause. Supported operators for value comparisons are <code>In<\/code>, <code>NotIn<\/code>, <code>Exists<\/code>, <code>DoesNotExist<\/code>, <code>Gt<\/code> (greater than), and <code>Lt<\/code> (less than).<\/p>\n<p>The <code>matchExpression<\/code> clauses grouped under a single <code>nodeSelectorTerms<\/code> clause are combined with a boolean <code>AND<\/code>. They all need to match for a Pod to gain affinity to a particular Node. You can use multiple <code>nodeSelectorTerms<\/code> clauses too; these will be combined as a logical <code>OR<\/code> operation. You can easily assemble complex scheduling criteria by utilizing both of these structures.<\/p>\n<p>\u201cSoft\u201d scheduling preferences are set up in a similar way. Use <code>nodeAffinity.preferredDuringSchedulingIgnoredDuringExecution<\/code> instead of or as well as <code>requiredDuringSchedulingIgnoredDuringExecution<\/code> to configure these. Define each of your optional constraints as a <code>matchExpressions<\/code> clause within a <code>preference<\/code> field:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"yaml\">\n<pre class=\"de1\"><span class=\"co3\">apiVersion<\/span><span class=\"sy2\">: <\/span>v1<span class=\"co3\">\nkind<\/span><span class=\"sy2\">: <\/span>Pod<span class=\"co4\">\nmetadata<\/span>:<span class=\"co3\">\n  name<\/span><span class=\"sy2\">: <\/span>demo-pod<span class=\"co4\">\nspec<\/span>:<span class=\"co4\">\n  containers<\/span>:<span class=\"co3\">\n    - name<\/span><span class=\"sy2\">: <\/span>demo-container\n    <span class=\"co1\"># ...<\/span><span class=\"co4\">\n  affinity<\/span>:<span class=\"co4\">\n    nodeAffinity<\/span>:<span class=\"co4\">\n      preferredDuringSchedulingIgnoredDuringExecution<\/span>:<span class=\"co3\">\n      - weight<\/span><span class=\"sy2\">: <\/span>1<span class=\"co4\">\n        preference<\/span>:<span class=\"co4\">\n          matchExpressions<\/span>:<span class=\"co3\">\n          - key<\/span><span class=\"sy2\">: <\/span>hardware-class<span class=\"co3\">\n            operator<\/span><span class=\"sy2\">: <\/span>In<span class=\"co4\">\n            values<\/span><span class=\"sy2\">:\n<\/span>              - a\n              - b\n              - c<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Preference-based rules have an additional field called <code>weight<\/code> that accepts an integer from 1 to 100. Each Node that matches a preference has its total affinity weight incremented by the set amount; the Node that ends up with the highest overall weight will be allocated the Pod.<\/p>\n<h2 id=\"setting-inter-pod-affinities\"><span class=\"ez-toc-section\" id=\"Setting_Inter-Pod_Affinities\"><\/span>Setting Inter-Pod Affinities<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Inter-Pod Affinities work very similarly to Node Affinities but do have some important differences. The \u201chard\u201d and \u201csoft\u201d modes are indicated using the same <code>requiredDuringSchedulingIgnoredDuringExecution<\/code> and <code>preferredDuringSchedulingIgnoredDuringExecution<\/code> fields. These should be nested under the <code>spec.affinity.podAffinity<\/code> or <code>spec.affinity.podAntiAffinity<\/code> fields depending on whether you want to increase or reduce the Pod\u2019s affinity upon a successful match.<\/p>\n<p>Here\u2019s a simple example that demonstrates both affinity and anti-affinity:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"yaml\">\n<pre class=\"de1\"><span class=\"co3\">apiVersion<\/span><span class=\"sy2\">: <\/span>v1<span class=\"co3\">\nkind<\/span><span class=\"sy2\">: <\/span>Pod<span class=\"co4\">\nmetadata<\/span>:<span class=\"co3\">\n  name<\/span><span class=\"sy2\">: <\/span>demo-pod<span class=\"co4\">\nspec<\/span>:<span class=\"co4\">\n  containers<\/span>:<span class=\"co3\">\n    - name<\/span><span class=\"sy2\">: <\/span>demo-container\n    <span class=\"co1\"># ...<\/span><span class=\"co4\">\n  affinity<\/span>:<span class=\"co4\">\n    podAffinity<\/span>:<span class=\"co4\">\n      requiredDuringSchedulingIgnoredDuringExecution<\/span>:<span class=\"co4\">\n        - labelSelector<\/span>:<span class=\"co4\">\n            matchExpressions<\/span>:<span class=\"co3\">\n              - key<\/span><span class=\"sy2\">: <\/span>hardware-class<span class=\"co3\">\n                operator<\/span><span class=\"sy2\">: <\/span>In<span class=\"co4\">\n                values<\/span><span class=\"sy2\">:\n<\/span>                  - a\n                  - b\n                  - c<span class=\"co3\">\n          topologyKey<\/span><span class=\"sy2\">: <\/span>topology.kubernetes.io\/zone<span class=\"co4\">\n    podAntiAffinity<\/span>:<span class=\"co4\">\n      preferredDuringSchedulingIgnoredDuringExecution<\/span>:<span class=\"co3\">\n        - weight<\/span><span class=\"sy2\">: <\/span>1<span class=\"co4\">\n          podAffinityTerm<\/span>:<span class=\"co4\">\n            - labelSelector<\/span>:<span class=\"co4\">\n                matchExpressions<\/span>:<span class=\"co3\">\n                  - key<\/span><span class=\"sy2\">: <\/span><a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>-component<span class=\"co3\">\n                    operator<\/span><span class=\"sy2\">: <\/span>In<span class=\"co4\">\n                    values<\/span><span class=\"sy2\">:\n<\/span>                      - background-worker<span class=\"co3\">\n          topologyKey<\/span><span class=\"sy2\">: <\/span>topology.kubernetes.io\/zone<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>The format differs slightly from Node Affinity. Each <code>matchExpressions<\/code> constraint needs to be nested under a <code>labelSelector<\/code>. For soft matches, this in turn should be located within a <code>podAffinityTerm<\/code>. Pod affinities also offer a reduced set of comparison operators: you can use <code>In<\/code>, <code>NotIn<\/code>, <code>Exists<\/code> and <code>DoesNotExist<\/code>.<\/p>\n<p>Pod affinities need a <code>topologyKey<\/code> field. This is used to limit the overall set of Nodes that are considered eligible for scheduling, before the <code>matchExpressions<\/code> are evaluated. The rules above will schedule the Pod to a Node with the <code>topology.kubernetes.io\/zone<\/code> label and an existing Pod with the <code>hardware-class<\/code> label set to <code>a<\/code>, <code>b<\/code>, or <code>c<\/code>. Nodes that also have a Pod with the <code>app-component=background-worker<\/code> label will be given a reduced affinity.<\/p>\n<p>Inter-Pod affinities are a powerful mechanism for controlling colocation of Pods. However they do have a significant impact on performance: <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/kubernetes.io\/docs\/concepts\/scheduling-eviction\/assign-pod-node\/#inter-pod-affinity-and-anti-affinity\">Kubernetes warns against<\/a> using them in clusters with more than a few hundred Nodes. Each new Pod scheduling request needs to check every other Pod on all the other Nodes to assess compatibility.<\/p>\n<h2 id=\"other-scheduling-constraints\"><span class=\"ez-toc-section\" id=\"Other_Scheduling_Constraints\"><\/span>Other Scheduling Constraints<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>While we\u2019ve focused on affinities in this article, Kubernetes provides other <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/kubernetes.io\/docs\/concepts\/scheduling-eviction\/assign-pod-node\/#node-isolation-restriction\">scheduler constraint mechanisms<\/a> too. These are typically simpler but less automated approaches that work well for smaller clusters and deployments.<\/p>\n<p>The most basic constraint is the <code>nodeSelector<\/code> field. It\u2019s defined on Pods as a set of label key-value pairs that must exist on Nodes hosting the Pod:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"yaml\">\n<pre class=\"de1\"><span class=\"co3\">apiVersion<\/span><span class=\"sy2\">: <\/span>v1<span class=\"co3\">\nkind<\/span><span class=\"sy2\">: <\/span>Pod<span class=\"co4\">\nmetadata<\/span>:<span class=\"co3\">\n  name<\/span><span class=\"sy2\">: <\/span>demo<span class=\"co4\">\nspec<\/span>:<span class=\"co4\">\n  containers<\/span>:<span class=\"co3\">\n    - name<\/span><span class=\"sy2\">: <\/span>demo\n      <span class=\"co1\"># ...<\/span><span class=\"co4\">\n  nodeSelector<\/span>:<span class=\"co3\">\n    hardware-class<\/span><span class=\"sy2\">: <\/span>a<span class=\"co3\">\n    internal<\/span><span class=\"sy2\">: <\/span>true<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>This manifest instructs Kubernetes to only schedule the Pod to Nodes with both the <code>hardware-class: a<\/code> and <code>internal: true<\/code> labels.<\/p>\n<p>Node selection with the <code>nodeSelector<\/code> field is a good way to quickly scaffold static configuration based on long-lived attributes of your Nodes. The affinity system is much more flexible when you want to express complex rules and optional preferences.<\/p>\n<h2 id=\"conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Affinities and anti-affinities are used to set up versatile Pod scheduling constraints in Kubernetes. Compared to other options like <code>nodeSelector<\/code>, affinities are complex but give you more ways to identify compatible Nodes.<\/p>\n<p>Affinities can act as soft preferences that signal a Pod\u2019s \u201cideal\u201d environment to Kubernetes even if it can\u2019t be im<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/social-mediaa\/\" data-internallinksmanager029f6b8e52c=\"1\" title=\"Social Media\" target=\"_blank\" rel=\"noopener\">media<\/a>tely satisfied. The system also has the unique ability of filtering Nodes based on their existing workloads so you can implement Pod colocation rules.<\/p>\n<p>One final point to note is that affinity isn\u2019t the end of the scheduling process. A Pod with a strong computed affinity to a Node might still end up elsewhere because of <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/kubernetes.io\/docs\/concepts\/scheduling-eviction\/taint-and-toleration\">the input of Node taints<\/a>. This mechanism lets you manage scheduling requests from the perspective of your Nodes. Taints actively repel incoming Pods away to other Nodes, effectively the opposite of the magnetic attraction of affinities. Node selectors, affinities, taints, and tolerations are all balanced to determine the final in-cluster location of each new Pod.\n<\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/15665\/what-is-pod-affinity-and-anti-affinity-in-kubernetes\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;What Is Pod Affinity and Anti-Affinity In Kubernetes? \u2013 CloudSavvy IT&#8221; Kubernetes is a distributed system that\u2019s designed to scale replicas of your services across multiple physical environments. In many cases this works well out-of-the-box. The Kubernetes scheduler automatically places your Pods (container instances) onto Nodes (worker machines) that have enough resources to support them&#8230;.<\/p>\n","protected":false},"author":1,"featured_media":414745,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/02\/748108a6.jpg","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-414744","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/414744","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=414744"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/414744\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/414745"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=414744"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=414744"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=414744"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}