污染 kubernetes 实时集群中的节点

Taint a node in kubernetes live cluster

如何使用 YAML 文件实现相同的命令,以便我可以执行 kubectl apply -f?下面的命令有效并且有污点,但我不知道如何通过清单文件使用它。

$ kubectl taint nodes \
          172.4.5.2-3a1d4eeb \
          kops.k8s.io/instancegroup=loadbalancer \
          NoSchedule

使用 -o yaml 选项并保存生成的 YAML 文件,并确保删除状态和一些额外的东西,这将应用污点,但会为您提供以后可以用来执行 kubectl apply -f ,并将其保存到版本控制中(即使您从命令行创建资源并稍后获取 yaml 并应用它,它也不会重新创建资源,所以完全没问题)

注意:大多数命令都支持--dry-run,那只会生成yaml而不创建资源,但在这种情况下,我做不到使用 --dry-run ,可能是此命令不支持该标志。

C02W84XMHTD5:~ iahmad$ kubectl taint node minikube dedicated=foo:PreferNoSchedule -o yaml
apiVersion: v1
kind: Node
metadata:
  annotations:
    node.alpha.kubernetes.io/ttl: "0"
    volumes.kubernetes.io/controller-managed-attach-detach: "true"
  creationTimestamp: 2018-10-16T21:44:03Z
  labels:
    beta.kubernetes.io/arch: amd64
    beta.kubernetes.io/os: linux
    kubernetes.io/hostname: minikube
    node-role.kubernetes.io/master: ""
  name: minikube
  resourceVersion: "291136"
  selfLink: /api/v1/nodes/minikube
  uid: 99a1a304-d18c-11e8-9334-f2cf3c1f0864
spec:
  externalID: minikube
  taints:
  - effect: PreferNoSchedule
    key: dedicated
    value: foo

然后将 yaml 与 kubectl apply 一起使用:

apiVersion: v1
kind: Node
metadata:
  name: minikube
spec:
  taints:
  - effect: PreferNoSchedule
    key: dedicated
    value: foo

我的集群中有两个节点,请查看标签

kubectl get nodes --show-labels
NAME          STATUS   ROLES   AGE    VERSION   LABELS
172.16.2.53   Ready    node    7d4h   v1.19.7   type=primary
172.16.2.89   Ready    node    33m    v1.19.7   type=secondary

假设我想用“172.16.2.89”污染节点名称

kubectl taint node 172.16.2.89  type=secondary:NoSchedule 
node/172.16.2.89 tainted

例子-

kubectl taint node <node-name>  <label-key>=<value>:NoSchedule

我的集群中有两个节点,请查看标签

NoExecute means the pod will be evicted from the node.

NoSchedule means the scheduler will not place the pod onto the node