search by tags

for the user

adventures into the land of the command line

k8s affinity

affinity is a property of a pod and provides a powerful mechanism for either attracting it to or repelling it from other pods and nodes

affinity comes in some flavours:

- node affinity attracts a pod to certain nodes
- pod affinity attracts a pod to certain pods
- pod anti-affinity repels a pod from other pods
- node anti-affinity repels a pod from certain nodes

a node affinity example for ensuring an elasticsearch pod is scheduled to a specific node

apiVersion: apps/v1beta2
kind: Deployment
metadata:
.
.
spec:
  template:
    spec:
      containers:
      - name:
      .
      .
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: kubernetes.io/hostname
                operator: In
                values:
                - es-node

a pod anti-affinity example to ensure an elasticsearch pod is not scheduled on a node which is already running another elasticsearch pod

apiVersion: apps/v1beta2
kind: Deployment
metadata:
.
.
spec:
  template:
    spec:
      containers:
      - name:
      .
      .
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
            matchExpressions:
            - key: component
              operator: In
              values:
              - elasticsearch
            - key: role
              operator: In
              values:
              - data
            topologyKey: kubernetes.io/hostname