集群管理员和应用运维人员可以在 Standard 模式集群中享受 Google Kubernetes Engine (GKE) Autopilot 的优势,例如定价和预配置的设置。本页面介绍了如何使用 ComputeClasses 在标准集群中部署 Autopilot 工作负载。您应该已经熟悉以下概念:
Autopilot ComputeClasses 简介
GKE 提供名为 ComputeClasses 的 Kubernetes 自定义资源,这些资源可以像任何其他 Kubernetes 资源一样部署在集群中。ComputeClass 定义节点配置的列表,例如机器类型或 Spot 虚拟机。您可以在工作负载中选择 ComputeClasses,以向 GKE 指示任何新节点都应使用该列表中的一种配置。
如果工作负载选择的 ComputeClass 启用了 autopilot
字段,GKE 会以 Autopilot 模式运行 Pod。GKE 创建的节点由 Google 管理,并包含许多默认的 Autopilot 功能和安全设置。如需详细了解在 Standard 集群中运行 Autopilot 工作负载的影响,包括部署这些工作负载时可能会注意到的差异,请参阅 GKE Standard 中的 Autopilot 模式工作负载简介。
Autopilot 计算类的类型
GKE 提供内置的 Autopilot ComputeClass,可用于大多数通用工作负载。您还可以配置新的或现有的自定义 ComputeClass 以使用 Autopilot 模式。您使用的 Autopilot ComputeClass 类型取决于工作负载是否需要特定硬件,如下所示:
- 通用工作负载:使用内置的 Autopilot ComputeClass 之一,将 Pod 放置在容器优化型计算平台上。
- 需要特定硬件的工作负载:为任何自定义 ComputeClass 启用 Autopilot 模式,将该 ComputeClass 部署到集群,然后在工作负载中选择该 ComputeClass。
如需详细了解这些选项、何时使用这些选项以及每种选项的价格,请参阅 Autopilot ComputeClasses 中的硬件选择。
价格
GKE Autopilot 价格适用于使用 Autopilot ComputeClass 的工作负载和节点。适用的价格模式取决于您使用的是内置 Autopilot ComputeClass 还是自定义 Autopilot ComputeClass。如需了解详情,请参阅“关于 GKE Standard 中的 Autopilot 模式工作负载”中的价格。
准备工作
在开始之前,请确保您已执行以下任务:
- 启用 Google Kubernetes Engine API。 启用 Google Kubernetes Engine API
- 如果您要使用 Google Cloud CLI 执行此任务,请安装并初始化 gcloud CLI。 如果您之前安装了 gcloud CLI,请运行
gcloud components update
以获取最新版本。
- 使用运行 1.33.1-gke.1107000 版或更高版本且已注册快速发布渠道的 GKE Standard 集群。如需创建新集群,请参阅创建区域级集群。
- 为避免工作负载被拒绝,请了解 Autopilot 的要求和安全限制。如需了解详情,请参阅 Autopilot 节点的预定义设置。
要求
集群中必须至少有一个节点池没有节点污点。
此节点池用于运行无法在 Standard 集群中的 Autopilot 节点上运行的 GKE Standard 系统 Pod,因为 GKE 会向这些节点添加污点。
安全强化型 GKE 节点是必需的,并且默认处于启用状态。
您必须使用 VPC 原生集群。
限制
- 仅支持快速发布渠道。
- 如需更新集群中的现有
ComputeClass
资源以使用 Autopilot 模式,您必须使用更新后的规范重新创建该 ComputeClass。如需了解详情,请参阅为现有的自定义 ComputeClass 启用 Autopilot。 - 您无法在自己的 ComputeClass 中使用
podFamily
优先级规则。此规则仅适用于内置的 Autopilot ComputeClass。 - 内置的 Autopilot ComputeClasses 不支持为整个集群启用机密 GKE 节点。如果您为集群启用机密 GKE 节点,则选择内置 Autopilot ComputeClasses 的任何新 Pod 都会无限期地保持
Pending
状态。
所需的角色和权限
如需获得部署 ComputeClass 所需的权限,请让您的管理员为您授予集群或项目的 Kubernetes Engine Developer (roles/container.developer
) IAM 角色。
如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限。
在工作负载中选择 Autopilot ComputeClass
如需在工作负载中选择 Autopilot ComputeClass,请使用 cloud.google.com/compute-class
标签的节点选择器。此标签与您在 GKE 中选择任何其他 ComputeClass 时使用的标签相同。以下步骤展示了如何创建选择 ComputeClass 的示例 Deployment,并验证 Pod 是否在 Autopilot 模式下运行:
将以下示例 Deployment 保存为
autopilot-cc-deployment.yaml
:将
COMPUTE_CLASS
替换为要使用的计算类的名称。此值可以是以下任一值:以下内置 Autopilot ComputeClass 之一,用于将通用工作负载放置在 Autopilot 容器优化型计算平台上:
autopilot
autopilot-spot
您创建的 ComputeClass,例如配置自定义 Autopilot ComputeClass 部分中的
autopilot-n2-class
ComputeClass。
部署工作负载:
kubectl apply -f autopilot-cc-deployment.yaml
如需验证 Pod 是否使用 Autopilot,请检查宿主节点的名称:
kubectl get pods -l=app=hello -o wide
输出类似于以下内容:
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES helloweb-79b9f6f75-5wwc9 1/1 Running 0 152m 10.102.1.135 gk3-cluster-1-nap-10abc8ya1-f66c6cef-wg5g <none> <none> helloweb-79b9f6f75-9skb9 1/1 Running 0 4d3h 10.102.0.140 gk3-cluster-1-nap-10abc8ya1-632bac02-hjl6 <none> <none> helloweb-79b9f6f75-h7bdv 1/1 Running 0 152m 10.102.1.137 gk3-cluster-1-nap-10abc8ya1-f66c6cef-wg5g <none> <none>
在此输出中,节点列中的
gk3-
前缀表示相应节点由 Autopilot 管理。
配置自定义 Autopilot ComputeClass
您可以配置自定义 ComputeClass 以使用 Autopilot。如果工作负载需要特定硬件(例如 GPU 或特定 Compute Engine 机器系列)才能以最佳状态运行,请使用自定义 Autopilot ComputeClass。
如果您的工作负载不需要特定硬件,我们建议您改用内置的 Autopilot ComputeClass。如需选择内置的 Autopilot ComputeClass,请参阅上文中的在工作负载中选择 Autopilot ComputeClass 部分。
创建新的自定义 Autopilot ComputeClass
将以下 ComputeClass 示例清单保存为
autopilot-n2-class.yaml
:apiVersion: cloud.google.com/v1 kind: ComputeClass metadata: name: autopilot-n2-class spec: autopilot: enabled: true priorities: - machineFamily: n2 spot: true minCores: 64 - machineFamily: n2 spot: true - machineFamily: n2 spot: false activeMigration: optimizeRulePriority: true whenUnsatisfiable: DoNotScaleUp
此清单包含以下字段:
autopilot
:为 ComputeClass 启用 Autopilot 模式。如果您在部署到 Autopilot 集群的 ComputeClass 中指定此字段,GKE 会忽略该字段。priorities
:定义了三个不同的 N2 机器系列配置的数组。activeMigration
:让 GKE 在资源可用时将 Pod 迁移到优先级列表中更高的配置。whenUnsatisfiable
:此字段中的DoNotScaleUp
值会强制 GKE 为新节点使用priorities
字段中的某条规则。如果无法满足上述任何优先级规则,GKE 会将 Pod 保持在Pending
状态,直到资源可用为止。DoNotScaleUp
是此字段的默认值和建议值。
部署 ComputeClass:
kubectl apply -f autopilot-n2-class.yaml
验证 ComputeClass 是否存在:
kubectl get computeclasses
输出类似于以下内容:
NAME AGE autopilot-n2-class 3s
为现有自定义 ComputeClass 启用 Autopilot
您可以在现有自定义 ComputeClass 中启用 Autopilot,这些 ComputeClass 位于 Standard 集群中,且规范中的 nodePoolAutoCreation.enabled
字段设置为 true
。在 Autopilot 集群中的计算类中启用 Autopilot 没有任何效果,因为整个集群都使用 Autopilot 模式。
为现有 ComputeClass 启用 Autopilot 后,GKE 会使用 Autopilot 来运行选择该 ComputeClass 的新 Pod。如果您在选择 Autopilot ComputeClass 的 Standard 节点上已有 Pod,则这些 Pod 仅在重新创建时使用 Autopilot。
如需更新现有自定义 ComputeClass 以使用 Autopilot 模式,请按以下步骤操作:
在文本编辑器中,更新现有 ComputeClass 的清单文件,以添加
spec.autopilot
字段:spec: autopilot: enabled: true
将 Kubernetes API 中现有的
ComputeClass
资源替换为更新后的规范:kubectl replace --force -f PATH_TO_UPDATED_MANIFEST
将
PATH_TO_UPDATED_MANIFEST
替换为更新后的清单文件的路径。如需触发新节点的创建,请重新创建使用计算类的任何工作负载。
应用更新后的清单后,GKE 为此 ComputeClass 创建的任何新节点都会使用 Autopilot。GKE 不会修改更新之前创建的任何现有节点。
验证工作负载是否使用 Autopilot
使用
kubectl get
命令获取集群中的 Pod 列表:kubectl get pods --output=custom-columns=Pod:.metadata.name,Node:.spec.nodeName
输出类似于以下内容:
Pod Node helloweb-84c865764b-nzhxt gk3-cluster-1-pool-3-68fc8dca-t54b # lines omitted for clarity
节点名称中的
gk3-
前缀表示 Pod 在 Autopilot 模式下运行。
默认应用 Autopilot ComputeClass
GKE 允许您将 ComputeClass 设置为命名空间的默认值。命名空间默认类适用于该命名空间中未明确选择其他 ComputeClass 的所有 Pod。将 Autopilot ComputeClass 设置为默认值意味着,除非工作负载选择其他选项,否则您可以默认以 Autopilot 模式运行命名空间中的所有 Pod。
如需了解详情,请参阅为命名空间配置默认 ComputeClass。
后续步骤
- 如需了解您可以在 ComputeClass 中指定的参数,请参阅 ComputeClass CustomResourceDefinition。