Search code examples
kuberneteskubernetes-podaffinity

Pod affinity settings is not working properly in my deployment


I try to deploy 2 replicas on k3s - each one to a different node. According to documentation, it should be pretty easy. However, I must be doing some silly mistake I am not able to find. When I apply the deploy file, both of my pods are running the same node (node1). In case I switch that node off, these pods start on another 2 nodes (node2, node3). When I start the node1 back and redeploy the app, it runs again on the same node1. If somebody can advise please, what I have wrong in my configuration, I would be really grateful. (I run fresh new k3s on 3 servers with the same HW configuration)

apiVersion: apps/v1
kind: Deployment
metadata:
  name: tbd-node-js-runner
  namespace: default
spec:
  replicas: 2
  selector:
    matchLabels:
      app: tbd-node-js-runner
  template:
    metadata:
      labels:
        app: tbd-node-js-runner
    spec:
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - tbd-node-js-runner
            topologyKey: topology.kubernetes.io/hostname
      containers:
        - name: tbd-node-js-runner
          image: tabidoo/nodejs-runner:latest
          ports:
          - containerPort: 
          env:
          - name: CONNECTION_STRING
            value: "..."
            ...
          imagePullPolicy: Always
      imagePullSecrets:
        - name: regcred

Solution

    • It is due to incorrect topologyKey , It should be 'kubernetes.io/hostname' not 'topology.kubernetes.io/hostname' .

    • So it would be as following :

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: tbd-node-js-runner
      namespace: default
    spec:
      replicas: 2
      selector:
        matchLabels:
          app: tbd-node-js-runner
      template:
        metadata:
          labels:
            app: tbd-node-js-runner
        spec:
          affinity:
            podAntiAffinity:
              requiredDuringSchedulingIgnoredDuringExecution:
              - labelSelector:
                  matchExpressions:
                  - key: app
                    operator: In
                    values:
                    - tbd-node-js-runner
                topologyKey: "kubernetes.io/hostname"
          containers:
            - name: tbd-node-js-runner
              image: tabidoo/nodejs-runner:latest