使用節點選取器或污點和容差來調整混合 OS Kubernetes 叢集的應用程式Adapt apps for mixed-OS Kubernetes clusters using node selectors or taints and tolerations

適用于: AKS on Azure Stack HCI、Windows Server 2019 Datacenter 上的 AKS runtimeApplies to: AKS on Azure Stack HCI, AKS runtime on Windows Server 2019 Datacenter

Azure Stack HCI 上的 Azure Kubernetes Service 可讓您以 Linux 和 Windows 節點執行 Kubernetes 叢集,但需要您對應用程式進行小型編輯,以便在這些混合作業系統叢集中使用。Azure Kubernetes Service on Azure Stack HCI enables you to run Kubernetes clusters with both Linux and Windows nodes, but requires you to make small edits to your apps for use in these mixed-OS clusters. 在本操作指南中,您將瞭解如何使用節點選取器或污點和容差,以確保您的應用程式會在正確的主機作業系統上排程。In this how-to guide, you learn how to ensure your application gets scheduled on the right host OS using either node selectors or taints and tolerations.

本操作指南假設您對 Kubernetes 概念有基本的瞭解。This how-to guide assumes a basic understanding of Kubernetes concepts. 如需詳細資訊,請參閱 Azure Stack HCI 上 Azure Kubernetes Service 的 Kubernetes 核心概念For more information, see Kubernetes core concepts for Azure Kubernetes Service on Azure Stack HCI.

節點選取器Node Selector

節點選取器 是 POD 規格 YAML 中的簡單欄位,可將 pod 限制為只排程至符合作業系統的狀況良好節點。Node Selector is a simple field in the pod specification YAML that constrains pods to only be scheduled onto healthy nodes matching the operating system. 在您的 pod 規格 YAML 中,指定  nodeSelector   -Windows 或 Linux,如下列範例所示。In your pod specification YAML, specify a nodeSelector - Windows or Linux, as shown in the examples below.

kubernetes.io/os = Windows

或者,or,

kubernetes.io/os = Linux

如需 nodeSelectors 的詳細資訊,請造訪 節點選取器For more information on nodeSelectors, visit node selectors.

污點和容差Taints and tolerations

污點容差 會一起運作,以確保不會在節點上意外排程 pod。Taints and tolerations work together to ensure that pods aren't scheduled on nodes unintentionally. 節點可以「來自感染」為不接受未透過 pod 規格 YAML 中的 ">toleration" 明確容忍其污點的 pod。A node can be "tainted" to not accept pods that don't explicitly tolerate its taint through a "toleration" in the pod specification YAML.

您可以使用下列索引鍵/值組,來自感染 Azure Stack HCI 上 Azure Kubernetes Service 中的 Windows OS 節點。Windows OS nodes in Azure Kubernetes Service on Azure Stack HCI can be tainted with the following key-value pair. 使用者不應使用不同的使用者。Users shouldn't use a different one.

node.kubernetes.io/os=Windows:NoSchedule

執行 kubectl get 並找出您想要污點的 Windows 背景工作節點。Run kubectl get and identify the Windows worker nodes you want to taint.

kubectl get nodes --all-namespaces -o=custom-columns=NAME:.metadata.name,OS:.status.nodeInfo.operatingSystem

輸出:Output:

NAME                                     OS
my-aks-hci-cluster-control-plane-krx7j   linux
my-aks-hci-cluster-md-md-1-5h4bl         windows
my-aks-hci-cluster-md-md-1-5xlwz         windows

使用污點 Windows server 背景工作節點 kubectl taint nodeTaint Windows server worker nodes using kubectl taint node.

kubectl taint node my-aks-hci-cluster-md-md-1-5h4bl node.kubernetes.io/os=Windows:NoSchedule
kubectl taint node my-aks-hci-cluster-md-md-1-5xlwz node.kubernetes.io/os=Windows:NoSchedule

您可以在 pod 規格 YAML 中指定 pod 的 >toleration。You specify a toleration for a pod in the pod specification YAML. 下列 >toleration 「比對」上述 kubectl 污點行所建立的污點,因此具有 >toleration 的 pod 可以排程至 aks-hci-md-md-1-5h4bl 或 my-aks-hci-md-1-5xlwz:The following toleration "matches" the taint created by the kubectl taint line above, and thus a pod with the toleration would be able to schedule onto my-aks-hci-cluster-md-md-1-5h4bl or my-aks-hci-cluster-md-md-1-5xlwz:

tolerations:
- key: node.kubernetes.io/os
  operator: Equal
  value: Windows
  effect: NoSchedule

如需有關污點和容差的詳細資訊,請造訪 污點和容差For more information on taints and tolerations, visit Taints and Tolerations.

後續步驟Next steps

在本操作指南中,您已瞭解如何使用 kubectl 將節點選取器或污點和容差新增至 Kubernetes 叢集。In this how-to guide, you learned how to add node selectors or taints and tolerations to your Kubernetes clusters using kubectl. 接著,您可以:Next, you can: