0
0
Kubernetesdevops~10 mins

Pod affinity and anti-affinity in Kubernetes - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to specify pod affinity for pods with label app=frontend.

Kubernetes
affinity:
  podAffinity:
    requiredDuringSchedulingIgnoredDuringExecution:
    - labelSelector:
        matchExpressions:
        - key: app
          operator: In
          values:
          - [1]
      topologyKey: kubernetes.io/hostname
Drag options to blanks, or click blank then click option'
Abackend
Bfrontend
Cdatabase
Dcache
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing a label value that does not match the target pods.
Confusing pod affinity with pod anti-affinity.
2fill in blank
medium

Complete the code to specify pod anti-affinity to avoid scheduling pods on the same node with label app=database.

Kubernetes
affinity:
  podAntiAffinity:
    requiredDuringSchedulingIgnoredDuringExecution:
    - labelSelector:
        matchLabels:
          app: [1]
      topologyKey: kubernetes.io/hostname
Drag options to blanks, or click blank then click option'
Acache
Bfrontend
Cdatabase
Dworker
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong label value that does not match the pods to avoid.
Mixing up pod affinity and anti-affinity.
3fill in blank
hard

Fix the error in the pod affinity configuration by completing the missing operator.

Kubernetes
affinity:
  podAffinity:
    requiredDuringSchedulingIgnoredDuringExecution:
    - labelSelector:
        matchExpressions:
        - key: role
          operator: [1]
          values:
          - frontend
      topologyKey: failure-domain.beta.kubernetes.io/zone
Drag options to blanks, or click blank then click option'
AIn
BExists
CNotIn
DDoesNotExist
Attempts:
3 left
💡 Hint
Common Mistakes
Using Exists or DoesNotExist when values are specified.
Using NotIn which negates the match.
4fill in blank
hard

Fill both blanks to create a pod anti-affinity rule that avoids pods with label tier=backend on the same node.

Kubernetes
affinity:
  podAntiAffinity:
    preferredDuringSchedulingIgnoredDuringExecution:
    - weight: 100
      podAffinityTerm:
        labelSelector:
          matchExpressions:
          - key: tier
            operator: [1]
            values:
            - [2]
        topologyKey: kubernetes.io/hostname
Drag options to blanks, or click blank then click option'
AIn
Bbackend
Cfrontend
DNotIn
Attempts:
3 left
💡 Hint
Common Mistakes
Using NotIn which would invert the match.
Using the wrong tier value.
5fill in blank
hard

Fill all three blanks to define pod affinity that prefers pods with label env=prod and role=frontend on the same zone.

Kubernetes
affinity:
  podAffinity:
    preferredDuringSchedulingIgnoredDuringExecution:
    - weight: 50
      podAffinityTerm:
        labelSelector:
          matchExpressions:
          - key: env
            operator: [1]
            values:
            - [2]
          - key: role
            operator: [3]
            values:
            - frontend
        topologyKey: failure-domain.beta.kubernetes.io/zone
Drag options to blanks, or click blank then click option'
AIn
Bprod
DNotIn
Attempts:
3 left
💡 Hint
Common Mistakes
Using NotIn which would exclude the desired pods.
Mixing operator types between the two label selectors.