Kubernetes
CKAD
Affinity

✅ Summary Table with Field Mapping and Examples

Affinity TypeRequired FieldPreferred Field
nodeAffinityrequiredDuringSchedulingIgnoredDuringExecution → nodeSelectorTermspreferredDuringSchedulingIgnoredDuringExecution → preference
podAffinityrequiredDuringSchedulingIgnoredDuringExecution → labelSelector, topologyKeypreferredDuringSchedulingIgnoredDuringExecution → podAffinityTerm
podAntiAffinityrequiredDuringSchedulingIgnoredDuringExecution → labelSelector, topologyKeypreferredDuringSchedulingIgnoredDuringExecution → podAffinityTerm

🔷 1. nodeAffinity

▶ Required Example

apiVersion: v1
kind: Pod
metadata:
  name: required-node-affinity-pod
spec:
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
          - key: disktype
            operator: In
            values:
            - ssd
  containers:
  - name: app
    image: nginx

➡️ Behavior: Pod will only be scheduled on nodes with disktype=ssd label.


▶ Preferred Example

apiVersion: v1
kind: Pod
metadata:
  name: preferred-node-affinity-pod
spec:
  affinity:
    nodeAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 1
        preference:
          matchExpressions:
          - key: disktype
            operator: In
            values:
            - ssd
  containers:
  - name: app
    image: nginx

➡️ Behavior: Pod will prefer nodes with disktype=ssd, but can run on others.


🔷 2. podAffinity

▶ Required Example

apiVersion: v1
kind: Pod
metadata:
  name: required-pod-affinity-pod
spec:
  affinity:
    podAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchLabels:
            app: redis
        topologyKey: "kubernetes.io/hostname"
  containers:
  - name: backend
    image: busybox
    command: ["sleep", "3600"]

➡️ Behavior: Pod will only run on the same node as pods labeled app=redis.


▶ Preferred Example

apiVersion: v1
kind: Pod
metadata:
  name: preferred-pod-affinity-pod
spec:
  affinity:
    podAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 100
        podAffinityTerm:
          labelSelector:
            matchLabels:
              app: redis
          topologyKey: "kubernetes.io/hostname"
  containers:
  - name: backend
    image: busybox
    command: ["sleep", "3600"]

➡️ Behavior: Pod will prefer to run near pods with label app=redis, but it can run elsewhere.


🔷 3. podAntiAffinity

▶ Required Example

apiVersion: v1
kind: Pod
metadata:
  name: required-pod-anti-affinity-pod
spec:
  affinity:
    podAntiAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchLabels:
            app: frontend
        topologyKey: "kubernetes.io/hostname"
  containers:
  - name: frontend
    image: nginx

➡️ Behavior: Pod will not be placed on the same node as another pod labeled app=frontend.


▶ Preferred Example

apiVersion: v1
kind: Pod
metadata:
  name: preferred-pod-anti-affinity-pod
spec:
  affinity:
    podAntiAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 50
        podAffinityTerm:
          labelSelector:
            matchLabels:
              app: frontend
          topologyKey: "kubernetes.io/hostname"
  containers:
  - name: frontend
    image: nginx

➡️ Behavior: Pod will try to avoid nodes with app=frontend pods, but may still land there.


📌 Notes

  • topologyKey: kubernetes.io/hostname = "same node"
  • You can use other topology keys like topology.kubernetes.io/zone for zone-aware placement.
  • Use required when pod must be scheduled in a certain way.
  • Use preferred when it's a soft rule or optimization hint.


🧙 AI Wizard - Instant Page Insights

Click the button below to analyze this page.
Get an AI-generated summary and key insights in seconds.
Powered by Perplexity AI!