What is pod affinity and node affinity?

0 votes
Hey i'm a newbie to Kubernetes. Could somebody help me in understanding Pod affinity and Node affinity? Thanks in advance.
Jul 17, 2019 in Kubernetes by Namik
• 1,210 points
1,105 views

1 answer to this question.

0 votes
  • Node Affinity ensures that pods are hosted on particular nodes.

  • Pod Affinity ensures two pods to be co-located in a single node.

Node Affinity

apiVersion: v1
kind: Pod
metadata:
name: with-node-affinity
spec:
affinity:
nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
          - key: Kubernetes.io/e2e-az-name
            operator: In
            values:
            - e2e-az1

Pod Affinity

apiVersion: v1
kind: Pod
metadata:
name: with-pod-affinity
spec:
  affinity:
    podAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchExpressions:
          - key: security
            operator: In
            values:
            - S1

The pod affinity rule says that the pod can be scheduled to a node only if that node is in the same zone as at least one already-running pod that has a label with key “security” and value “S1”

For a better understanding refer to: https://kubernetes.io/docs/concepts/configuration/assign-pod-node/

answered Jul 17, 2019 by Sirajul
• 50,320 points

Related Questions In Kubernetes

0 votes
1 answer
0 votes
1 answer

What is the difference between config map and secret in kubernetes?

Config maps ideally stores application configuration in ...READ MORE

answered Jul 17, 2019 in Kubernetes by Sirajul
• 50,320 points
404 views
0 votes
1 answer

What is Ingress ? Is it something that runs as a pod or on a pod?

An ingress is an object that holds ...READ MORE

answered Jul 19, 2019 in Kubernetes by Sirajul
• 50,320 points
134 views
0 votes
1 answer

What is Scaling in Kubernetes and how would you enable it?

On Google Compute Engine (GCE) and Google Container Engine (GKE) (and ...READ MORE

answered Feb 11 in Kubernetes by anonymous
• 50,320 points
91 views
0 votes
1 answer
0 votes
3 answers

Error while joining cluster with node

Hi Kalgi after following above steps it ...READ MORE

answered Jan 17, 2019 in Others by anonymous
5,478 views
+4 votes
1 answer

Installing Web UI (Dashboard):kubernetes-dashboard on main Ubuntu 16.04.6 LTS (Xenial Xerus) server

Follow these steps: $ kubeadm reset $ kubeadm init ...READ MORE

answered Apr 11, 2019 in Kubernetes by Kalgi
• 51,830 points

reshown Apr 12, 2019 by Kalgi 1,499 views
0 votes
1 answer

What is pod security policy and why is it important?

In Kubernetes, Pod Security Policies consists of ...READ MORE

answered Jul 17, 2019 in Kubernetes by Sirajul
• 50,320 points
132 views
0 votes
1 answer

Is it possible to force the pod to run on a specific node?

By default, anti-affinity is not attempted by ...READ MORE

answered Jul 15, 2019 in Kubernetes by Sirajul
• 50,320 points
98 views