GitHub EN PT

K8s em Exemplos: Taints & Tolerations

Taints marcam nós para repelir Pods; tolerations permitem que Pods sejam agendados em nós com taint. Diferente de affinity (atrair Pods para nós), taints funcionam por repulsão. Use para: nós dedicados, nós GPU, manutenção, prevenir workloads em hardware específico.

taint-effects.yaml

Três efeitos de taint: NoSchedule (novos Pods não serão agendados), PreferNoSchedule (preferência suave para evitar), NoExecute (Pods existentes são despejados, novos Pods não serão agendados).

kubectl taint nodes worker-1 dedicated=gpu:NoSchedule

kubectl taint nodes worker-2 preferred=lowpriority:PreferNoSchedule

kubectl taint nodes worker-3 maintenance=true:NoExecute
toleration-exact.yaml

O operador Equal requer correspondência exata de chave e valor. Este Pod pode executar em nós com taint dedicated=gpu:NoSchedule. Sem esta toleration, seria rejeitado.

apiVersion: v1
kind: Pod
metadata:
  name: gpu-workload
spec:
  tolerations:
    - key: "dedicated"
      operator: "Equal"
      value: "gpu"
      effect: "NoSchedule"
  containers:
    - name: ml-training
      image: ml-trainer:v1
      resources:
        limits:
          nvidia.com/gpu: 1
toleration-exists.yaml

O operador Exists corresponde a qualquer taint com a chave especificada, independente do valor. Útil quando você não se importa com o valor específico do taint.

apiVersion: v1
kind: Pod
metadata:
  name: tolerate-any-dedicated
spec:
  tolerations:
    - key: "dedicated"
      operator: "Exists"
      effect: "NoSchedule"
  containers:
    - name: app
      image: my-app:v1
---
tolerations:
  - operator: "Exists"
terminal

Nós GPU dedicados para inferência LLM: aplique taint em nós GPU para prevenir workloads regulares de agendar lá. Também adicione labels para direcionamento via affinity. Taints repelem, labels atraem.

$ kubectl taint nodes gpu-node-1 nvidia.com/gpu=true:NoSchedule
node/gpu-node-1 tainted

$ kubectl taint nodes gpu-node-2 nvidia.com/gpu=true:NoSchedule
node/gpu-node-2 tainted

$ kubectl label nodes gpu-node-1 gpu-node-2 accelerator=nvidia-a100
node/gpu-node-1 labeled
node/gpu-node-2 labeled

Verifique se taints foram aplicados. Nós GPU agora rejeitam Pods sem tolerations correspondentes. Workloads regulares automaticamente agendam em nós apenas CPU.

$ kubectl get nodes -L accelerator
NAME         STATUS   ROLES    ACCELERATOR
cpu-node-1   Ready    <none>
cpu-node-2   Ready    <none>
gpu-node-1   Ready    <none>   nvidia-a100
gpu-node-2   Ready    <none>   nvidia-a100

$ kubectl describe node gpu-node-1 | grep Taints
Taints:             nvidia.com/gpu=true:NoSchedule
llm-inference-deployment.yaml

Deployment de inferência LLM com toleration GPU. A toleration permite agendamento em nós GPU com taint. Sem ela, o scheduler rejeita o Pod dos nós GPU.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: llm-inference
spec:
  replicas: 2
  selector:
    matchLabels:
      app: llm-inference
  template:
    metadata:
      labels:
        app: llm-inference
    spec:
      tolerations:
        - key: "nvidia.com/gpu"
          operator: "Equal"
          value: "true"
          effect: "NoSchedule"

Combine toleration com nodeAffinity para garantir que Pods rodem apenas em nós GPU. Toleration permite nós GPU; affinity requer eles. Solicite recurso nvidia.com/gpu para alocação real de GPU.

      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
              - matchExpressions:
                  - key: accelerator
                    operator: In
                    values: ["nvidia-a100"]
      containers:
        - name: vllm
          image: vllm/vllm-openai:latest
          resources:
            limits:
              nvidia.com/gpu: 1
          ports:
            - containerPort: 8000
terminal

Verifique o posicionamento: Pods LLM rodam em nós GPU, Pods regulares sem tolerations vão para nós CPU. Taints protegem capacidade GPU cara de workloads não-GPU.

$ kubectl get pods -o wide
NAME                   READY   NODE
llm-inference-abc12    1/1     gpu-node-1
llm-inference-def34    1/1     gpu-node-2
web-frontend-xyz99     1/1     cpu-node-1

$ kubectl run test --image=nginx
pod/test created

$ kubectl get pod test -o wide
NAME   READY   STATUS    NODE
test   1/1     Running   cpu-node-1
terminal

Gerencie taints com kubectl. Adicione taints para repelir Pods, remova taints (com sufixo menos) para permitir agendamento.

$ kubectl taint nodes worker-1 dedicated=gpu:NoSchedule
node/worker-1 tainted

$ kubectl describe node worker-1 | grep -A3 Taints
Taints:             dedicated=gpu:NoSchedule

$ kubectl taint nodes worker-1 dedicated=gpu:NoSchedule-
node/worker-1 untainted

Índice | Use as setas do teclado para navegar