pod scheduling

Kubernetes pod scheduling is the process by which the Kubernetes control plane assigns pods to nodes based on specified criteria. The scheduler is a component of the control plane responsible for making decisions about which node should run a pod. Here's how you can influence pod scheduling in Kubernetes:

1. Pod Affinity and Anti-Affinity:

Pod affinity and anti-affinity rules allow you to influence the scheduling of pods based on the presence of other pods.

  • Pod Affinity: Specify that a pod should be scheduled on nodes that have other pods with certain labels.

    affinity:
      podAffinity:
        requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
                - key: "app"
                  operator: In
                  values:
                    - "backend"
            topologyKey: "kubernetes.io/hostname"
  • Pod Anti-Affinity: Specify that a pod should not be scheduled on nodes that have other pods with certain labels.

    affinity:
      podAntiAffinity:
        requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
                - key: "app"
                  operator: In
                  values:
                    - "frontend"
            topologyKey: "kubernetes.io/hostname"

2. Node Affinity:

Node affinity rules allow you to specify conditions for pod scheduling based on node labels.

affinity:
  nodeAffinity:
    requiredDuringSchedulingIgnoredDuringExecution:
      nodeSelectorTerms:
        - matchExpressions:
            - key: "kubernetes.io/e2e-az-name"
              operator: In
              values:
                - e2e-az1
                - e2e-az2

3. Taints and Tolerations:

Taints and tolerations provide a way to repel or attract pods based on node conditions.

  • Taint a Node:

    kubectl taint nodes node-name key=value:NoSchedule
  • Toleration in Pod Spec:

    tolerations:
      - key: "key"
        operator: "Equal"
        value: "value"
        effect: "NoSchedule"

4. Resource Requests and Limits:

Specify resource requests and limits in the pod specification to influence scheduling based on resource availability.

resources:
  requests:
    memory: "64Mi"
    cpu: "250m"
  limits:
    memory: "128Mi"
    cpu: "500m"

5. Node Selector:

Use a node selector to specify requirements for the nodes where a pod can be scheduled.

nodeSelector:
  disktype: ssd

6. Pod Priority and Preemption:

Pod priority and preemption allow you to prioritize certain pods and make decisions about preemption when resources are scarce.

priorityClassName: "high-priority"

7. Inter-Pod Affinity with Topology Spread Constraints:

Use topology spread constraints to influence pod distribution across different zones.

topologySpreadConstraints:
  - maxSkew: 1
    topologyKey: topology.kubernetes.io/zone
    whenUnsatisfiable: DoNotSchedule
    labelSelector:
      matchLabels:
        app: frontend

8. Pod Disruption Budgets:

Pod Disruption Budgets (PDBs) allow you to control the disruption allowed for pods during voluntary disruptions.

apiVersion: policy/v1beta1
kind: PodDisruptionBudget
metadata:
  name: my-pdb
spec:
  maxUnavailable: 1

9. Scheduling Policies (kube-scheduler):

Customize the behavior of the kube-scheduler by modifying its configuration or using external schedulers.

  • Custom Scheduler Configuration:

    • Edit the kube-scheduler configuration file and restart the kube-scheduler process.

  • External Scheduler:

    • Implement a custom scheduler and register it with Kubernetes as an external scheduler.

These are some of the key techniques to influence pod scheduling in Kubernetes. The specific approach you choose depends on the requirements of your application and infrastructure. Consider the trade-offs and implications of each technique based on your use case.