kubernetes: podAntiAffinity in a daemonset is not accepted , k8s versions > 1.27.0

What happened?

During one of our tests with daemonsets, we find new pods being scheduled despite having a podAntiAffinity rule set into the daemonset.

Here’s our test case;

  1. We installed older version of our app.
  2. We apply a finalizer to a pod( rlinux-xs84n in this case).
  3. We upgrade(rolling update) to latest version of the app.(no change in Kubernetes version).
  4. At this point, rlinux-xs84n pod gets stuck in ‘Terminating’.
  5. As a new daemonset spins up, it succeeds in scheduling new pods on the same node( xxx.194 in this case).

Actual output: NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES rlinux-8p5qt 2/2 Running 0 3h47m xxx.194 vm7002 <none> <none> rlinux-b6z99 2/2 Running 0 3h47m xxx.105 vm8001 <none> <none> rlinux-hjpj7 2/2 Running 0 3h47m xxx.42 vm7001 <none> <none> rlinux-xs84n 0/2 Terminating 0 3h52m xxx.194 vm7002 <none> <none>

This behaviour is observed in K8S; 1.27.0 - 1.28 versions This behaviour is not observed is K8S; 1.23, 1.24, 1.25 and 1.26 versions

What did you expect to happen?

Expected output(A pod scheduled by the new DS should have been ‘Pending’ on the same node ‘vm7002’): NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES rlinux-8p5qt 2/2 Pending 0 3h47m xxx.194 vm7002 <none> <none> rlinux-b6z99 2/2 Running 0 3h47m xxx.105 vm8001 <none> <none> rlinux-hjpj7 2/2 Running 0 3h47m xxx.42 vm7001 <none> <none> rlinux-xs84n 0/2 Terminating 0 3h52m xxx.194 vm7002 <none> <none>

Please share a workaround to make the pod scheduling (like above) and let us know if there were any similar field issues were noticed earlier.

How can we reproduce it (as minimally and precisely as possible)?

Steps could be similar to that of our test.

Anything else we need to know?

The daemonset has a podAntiAffinity like below:

"spec": {
                        ...
                            "podAntiAffinity": {
                                "requiredDuringSchedulingIgnoredDuringExecution": [
                                    {
                                        "labelSelector": {
                                            "matchExpressions": [
                                                {
                                                    "key": "app",
                                                    "operator": "In",
                                                    "values": [
                                                        "node.rlinux.app.io"
                                                    ]
                                                }
                                            ]
                                        },
                                        "topologyKey": "kubernetes.io/hostname"
                                    }
                                ]
                            }
                        }

Also daemonset has a rollingUpdate strategy:

"updateStrategy": {
                    "rollingUpdate": {
                        "maxSurge": 0,
                        "maxUnavailable": 1
                    },
                    "type": "RollingUpdate"
                }

Kubernetes version

$ kubectl version
WARNING: This version information is deprecated and will be replaced with the output from kubectl version --short.  Use --output=yaml|json to get the full version.
Client Version: version.Info{Major:"1", Minor:"27", GitVersion:"v1.27.0", GitCommit:"xyz", GitTreeState:"clean", BuildDate:"2023-04-11T17:10:18Z", GoVersion:"go1.20.3", Compiler:"gc", Platform:"linux/amd64"}
Kustomize Version: v5.0.1
Server Version: version.Info{Major:"1", Minor:"27", GitVersion:"v1.27.5", GitCommit:"xtz", GitTreeState:"clean", BuildDate:"2023-08-24T00:42:11Z", GoVersion:"go1.20.7", Compiler:"gc", Platform:"linux/amd64"}

Cloud provider

OS version

# On Linux:
$ cat /etc/os-release
NAME="Red Hat Enterprise Linux"
VERSION="9.2 (Plow)"
ID="rhel"
ID_LIKE="fedora"
VERSION_ID="9.2"
PLATFORM_ID="platform:el9"
PRETTY_NAME="Red Hat Enterprise Linux 9.2 (Plow)"
ANSI_COLOR="0;31"
LOGO="fedora-logo-icon"
CPE_NAME="cpe:/o:redhat:enterprise_linux:9::baseos"
HOME_URL="https://www.redhat.com/"
DOCUMENTATION_URL="https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9"
BUG_REPORT_URL="https://bugzilla.redhat.com/"

REDHAT_BUGZILLA_PRODUCT="Red Hat Enterprise Linux 9"
REDHAT_BUGZILLA_PRODUCT_VERSION=9.2
REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux"
REDHAT_SUPPORT_PRODUCT_VERSION="9.2"

Install tools

Container runtime (CRI) and version (if applicable)

containerd://1.6.19

Related plugins (CNI, CSI, …) and versions (if applicable)

kube-flannel

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 17 (13 by maintainers)

Most upvoted comments

Let me take an experiment, kube-scheduler should take terminating pods into consideration.

A pod with phase=Succeeded and a deletionTimestamp is not a “terminating” pod, it is a TERMINATED pod.

As such, it is as if it doesn’t exist.

@alculquicondor Shared the terminating pod status on a 1.27.4 cluster.

apiVersion: v1 kind: Pod metadata: creationTimestamp: “2023-08-29T16:27:33Z” deletionGracePeriodSeconds: 0 deletionTimestamp: “2023-08-29T16:28:36Z” finalizers:

  • example.com/dummy generateName: dummy-node-rlinux- labels: app: node.label1 controller-revision-hash: c5d7fd7cd pod-template-generation: “1” name: dummy-node-rlinux-qr9pj namespace: dummy ownerReferences:
  • apiVersion: apps/v1 blockOwnerDeletion: true controller: true kind: DaemonSet name: dummy-node-rlinux uid: b9e66941-16ee-443f-90e7-06cc093e8030 resourceVersion: “3054” uid: 75f2689b-cd80-4338-b8a6-680b37405d0d spec: affinity: nodeAffinity: requiredDuringSchedulingIgnoredDuringExecution: nodeSelectorTerms: - matchFields: - key: metadata.name operator: In values: - node-name containers:
  • args:
    • –no_persistence
    • –k8s_pod
    • –rest=false
    • –csi_node_name=$(KUBE_NODE_NAME)
    • –csi_endpoint=$(CSI_ENDPOINT)
    • –csi_role=node
    • –log_format=text
    • –disable_audit_log=true
    • –http_request_timeout=90s
    • –https_rest
    • –https_port=17546
    • –enable_force_detach=false
    • -debug command:
    • /dummy.sh env:
    • name: KUBE_NODE_NAME valueFrom: fieldRef: apiVersion: v1 fieldPath: spec.nodeName
    • name: KUBELET_DIR value: /var/lib/kubelet
    • name: CSI_ENDPOINT value: unix://plugin/csi.sock
    • name: PATH value: /xyz:/bin image: dummy-image:1.2.3 imagePullPolicy: IfNotPresent livenessProbe: failureThreshold: 3 httpGet: path: /liveness port: 17546 scheme: HTTPS periodSeconds: 10 successThreshold: 1 timeoutSeconds: 1 name: container1 readinessProbe: failureThreshold: 5 httpGet: path: /readiness port: 17546 scheme: HTTPS initialDelaySeconds: 10 periodSeconds: 10 successThreshold: 1 timeoutSeconds: 1 resources: {} securityContext: allowPrivilegeEscalation: true privileged: true startupProbe: failureThreshold: 5 httpGet: path: /liveness port: 17546 scheme: HTTPS periodSeconds: 5 successThreshold: 1 timeoutSeconds: 1 terminationMessagePath: /dev/termination-log terminationMessagePolicy: File volumeMounts:
    • mountPath: /plugin name: plugin-dir
    • mountPath: /var/lib/kubelet/plugins mountPropagation: Bidirectional name: plugins-mount-dir
    • mountPath: /var/lib/kubelet/pods mountPropagation: Bidirectional name: pods-mount-dir
    • mountPath: /dev name: dev-dir
    • mountPath: /sys name: sys-dir
    • mountPath: /host mountPropagation: Bidirectional name: host-dir
    • mountPath: /var/lib/mount mountPropagation: Bidirectional name: xyz-tracking-dir
    • mountPath: /certs name: certs readOnly: true
    • mountPath: /var/run/secrets/kubernetes.io/serviceaccount name: kube-api-access-79jp8 readOnly: true
  • args:
    • –v=8
    • –csi-address=$(ADDRESS)
    • –kubelet-registration-path=$(REGISTRATION_PATH) env:
    • name: ADDRESS value: /plugin/csi.sock
    • name: REGISTRATION_PATH value: /var/lib/kubelet/plugins/csi.xyz.io/csi.sock
    • name: KUBE_NODE_NAME valueFrom: fieldRef: apiVersion: v1 fieldPath: spec.nodeName image: registry.k8s.io/sig-storage/csi-node-driver-registrar:v2.7.0 imagePullPolicy: IfNotPresent name: driver-registrar resources: {} terminationMessagePath: /dev/termination-log terminationMessagePolicy: File volumeMounts:
    • mountPath: /plugin name: plugin-dir
    • mountPath: /registration name: registration-dir
    • mountPath: /var/run/secrets/kubernetes.io/serviceaccount name: kube-api-access-79jp8 readOnly: true dnsPolicy: ClusterFirstWithHostNet enableServiceLinks: true hostIPC: true hostNetwork: true hostPID: true nodeName: scspa2881643001 nodeSelector: kubernetes.io/arch: amd64 kubernetes.io/os: linux preemptionPolicy: PreemptLowerPriority priority: 2000001000 priorityClassName: system-node-critical restartPolicy: Always schedulerName: default-scheduler securityContext: {} serviceAccount: dummy-node-rlinux serviceAccountName: dummy-node-rlinux terminationGracePeriodSeconds: 30 tolerations:
  • effect: NoExecute operator: Exists
  • effect: NoSchedule operator: Exists
  • effect: NoExecute key: node.kubernetes.io/not-ready operator: Exists
  • effect: NoExecute key: node.kubernetes.io/unreachable operator: Exists
  • effect: NoSchedule key: node.kubernetes.io/disk-pressure operator: Exists
  • effect: NoSchedule key: node.kubernetes.io/memory-pressure operator: Exists
  • effect: NoSchedule key: node.kubernetes.io/pid-pressure operator: Exists
  • effect: NoSchedule key: node.kubernetes.io/unschedulable operator: Exists
  • effect: NoSchedule key: node.kubernetes.io/network-unavailable operator: Exists volumes:
  • hostPath: path: /var/lib/kubelet/plugins/xyz.io/ type: DirectoryOrCreate name: plugin-dir
  • hostPath: path: /var/lib/kubelet/plugins_registry/ type: Directory name: registration-dir
  • hostPath: path: /var/lib/kubelet/plugins type: DirectoryOrCreate name: plugins-mount-dir
  • hostPath: path: /var/lib/kubelet/pods type: DirectoryOrCreate name: pods-mount-dir
  • hostPath: path: /dev type: Directory name: dev-dir
  • hostPath: path: /sys type: Directory name: sys-dir
  • hostPath: path: / type: Directory name: host-dir
  • hostPath: path: /var/lib/xyz/tracking type: DirectoryOrCreate name: xyz-tracking-dir
  • name: certs projected: defaultMode: 420 sources:
    • secret: name: xyz-csi
    • secret: name: xyz-encryption-keys
  • name: kube-api-access-79jp8 projected: defaultMode: 420 sources:
    • serviceAccountToken: expirationSeconds: 3607 path: token
    • configMap: items:
      • key: ca.crt path: ca.crt name: kube-root-ca.crt
    • downwardAPI: items:
      • fieldRef: apiVersion: v1 fieldPath: metadata.namespace path: namespace status: conditions:
  • lastProbeTime: null lastTransitionTime: “2023-08-29T16:27:33Z” reason: PodCompleted status: “True” type: Initialized
  • lastProbeTime: null lastTransitionTime: “2023-08-29T16:29:04Z” reason: PodCompleted status: “False” type: Ready
  • lastProbeTime: null lastTransitionTime: “2023-08-29T16:29:04Z” reason: PodCompleted status: “False” type: ContainersReady
  • lastProbeTime: null lastTransitionTime: “2023-08-29T16:27:33Z” status: “True” type: PodScheduled containerStatuses:
  • containerID: containerd://fca04453a5529d1930c2f42629d9b1a243ca2c08f310ff3819097ed14b534b0a image: registry.k8s.io/sig-storage/csi-node-driver-registrar:v2.7.0 imageID: registry.k8s.io/sig-storage/csi-node-driver-registrar@sha256:4a4cae5118c4404e35d66059346b7fa0835d7e6319ff45ed73f4bba335cf5183 lastState: {} name: driver-registrar ready: false restartCount: 0 started: false state: terminated: containerID: containerd://fca04453a5529d1930c2f42629d9b1a243ca2c08f310ff3819097ed14b534b0a exitCode: 0 finishedAt: “2023-08-29T16:28:36Z” reason: Completed startedAt: “2023-08-29T16:27:34Z”
  • containerID: containerd://08d29656d506720c86c5756150ad19708f5e75ed1f403de66df901b5747d7c95 image: docker_image:1.2.3 imageID: docker_image/xyz@sha lastState: {} name: container1 ready: false restartCount: 0 started: false state: terminated: containerID: containerd://08d29656d506720c86c5756150ad19708f5e75ed1f403de66df901b5747d7c95 exitCode: 0 finishedAt: “2023-08-29T16:29:04Z” reason: Completed startedAt: “2023-08-29T16:27:34Z” hostIP: 10.193.161.74 phase: Succeeded podIP: 10.193.161.74 podIPs:
  • ip: 10.193.161.74 qosClass: BestEffort startTime: “2023-08-29T16:27:33Z”

Thanks @tksm for the detailed investing and Aldo pointed out the key point. So after https://github.com/kubernetes/kubernetes/commit/3d68f362c372ea2af5165198c0c60f19ebc60a3f, Pod will be marked as succeed finally, then scheduler will ignore the pod, that’s the phenomenon currently. But we should fix the https://github.com/kubernetes/kubectl/issues/1595 to prevent confusion.

However, since pod is protected by the finalizer, should we mark the pod succeeded directly rather than waiting for the finalizer being removed? But I have no real sense yet …