Advanced Scheduling in Kubernetes with Dynamic Node Pools — Part II

requiredDuringSchedulingIgnoredDuringExecution
preferredDuringSchedulingIgnoredDuringExecution

There’s more to pod affinity than node affinity

  • Scheduling can be notably slow on large clusters as pod affinity and anti-affinity requires a substantial amount of processing
  • Cannot have an empty topologyKey for pod affinity or anti-affinity which emphasizes that the nodes in the cluster should be consistently labeled with matching the topologyKey. Some references are added latter in this article if you are unfamiliar with the term topologyKey.
  • Application A directly interact with in-memory Cache for its workloads
  • Application B should spread across to avoid co-location on the same node as Application A as much as possible
apiVersion: apps/v1
kind: Deployment
metadata:
name: app-type-A-co-locate-with-cache
spec:
selector:
matchLabels:
app: app-A
replicas: 3
template:
metadata:
labels:
app: app-A
spec:
affinity:
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- cache
topologyKey: "kubernetes.io/hostname"
containers:
- name: web-app-A
image: k8s.gcr.io/web-app-image
apiVersion: apps/v1
kind: Deployment
metadata:
name: app-type-B-locate-away-from-A
spec:
selector:
matchLabels:
app: app-B
replicas: 2
template:
metadata:
labels:
app: app-B
spec:
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- app-A
topologyKey: "kubernetes.io/hostname"
containers:
- name: web-app-B
image: k8s.gcr.io/web-app-backend-image

References

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Upendra Kumarage

Upendra Kumarage

Cloud & DevOps enthusiast, Cloud Operations professional