你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

快速入门:创建 Azure Kubernetes 服务 (AKS) 自动版群集

适用于: ✔️ AKS 自动版

Azure Kubernetes 服务 (AKS) 自动版为开发人员、DevOps 工程师和平台工程师提供最简单的托管 Kubernetes 体验。 AKS Automatic 可自动执行 AKS 群集设置和操作,并嵌入最佳做法配置,非常适合新式应用程序和 AI 应用程序。 任何技能级别的用户都可以从 AKS Automatic 对其应用程序的安全性、性能和可靠性中受益。

在此快速入门中,你将了解:

  • 部署 AKS Automatic 群集。
  • 运行示例多容器应用程序,其中的一组微服务和 Web 前端模拟零售应用场景。

在您开始之前

  • 本文需要 2.77.0 或更高版本的 Azure CLI。 如果使用的是 Azure Cloud Shell,则表示已安装最新版本。
  • 如果你有多个 Azure 订阅,请使用 az account set 命令选择应在其中计收资源费用的相应订阅 ID。
  • 若要部署 Bicep 文件,需要对所创建的资源拥有写入访问权限,并且需要对 Microsoft.Resources/deployments 资源类型上的所有操作拥有访问权限。 例如,若要创建虚拟机,需要 Microsoft.Compute/virtualMachines/writeMicrosoft.Resources/deployments/* 权限。 有关角色和权限的列表,请参阅 Azure 内置角色

局限性

  • AKS 自动群集的系统节点池需要在至少支持三个 可用性区域、临时 OS 磁盘和 Azure Linux OS 的 Azure 区域中部署。
  • 只能在 API Server VNet 集成正式发布 (GA) 的区域创建 AKS 自动版群集。

重要

AKS Automatic 尝试根据订阅中可用的容量动态选择 system 节点池的虚拟机大小。 确保你的订阅在要部署群集的区域中具有以下任意大小的 16 个 vCPU 的配额:Standard_D4lds_v5Standard_D4ads_v5Standard_D4ds_v5Standard_D4d_v5Standard_D4d_v4Standard_DS3_v2Standard_DS12_v2Standard_D4alds_v6Standard_D4lds_v6Standard_D4alds_v5。 可以通过 Azure 门户查看特定 VM 系列的配额并提交配额增加请求。 如果还有其他问题,请通过 故障排除文档了解详细信息。

创建资源组

Azure 资源组是用于部署和管理 Azure 资源的逻辑组。

以下示例在“eastus”位置创建名为“myResourceGroup”的资源组。

使用 az group create 命令创建资源组。

az group create --name myResourceGroup --location eastus

以下示例输出类似于资源组成功创建:

{
  "id": "/subscriptions/<guid>/resourceGroups/myResourceGroup",
  "location": "eastus",
  "managedBy": null,
  "name": "myResourceGroup",
  "properties": {
    "provisioningState": "Succeeded"
  },
  "tags": null
}

创建 AKS Automatic 群集

若要创建 AKS 自动版群集,请使用 az aks create 命令。 以下示例创建一个名为 myAKSAutomaticCluster 且启用了托管 Prometheus 和 Container Insights 集成的群集

az aks create \
    --resource-group myResourceGroup \
    --name myAKSAutomaticCluster \
    --sku automatic

片刻之后,该命令将会完成,并返回有关群集的 JSON 格式信息。

连接至群集

若要管理 Kubernetes 群集,请使用 Kubernetes 命令行客户端 kubectl。 如果使用的是 Azure Cloud Shell,则 kubectl 已安装。 若要在本地安装 kubectl,请运行 az aks install-cli 命令。 AKS Automatic 群集配置为使用 Microsoft Entra ID 进行 Kubernetes 基于角色的访问控制 (RBAC)

注释

使用 Azure CLI 创建群集时,系统会为用户

使用 kubectl 命令将 az aks get-credentials 配置为连接到你的 Kubernetes 群集。 此命令将下载凭据,并将 Kubernetes CLI 配置为使用这些凭据。

az aks get-credentials --resource-group myResourceGroup --name myAKSAutomaticCluster

使用 kubectl get 命令验证与群集之间的连接。 此命令将返回群集节点的列表。

kubectl get nodes

以下示例输出展示了登录的要求。

To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code AAAAAAAAA to authenticate.

登录后,以下示例输出会显示托管系统节点池。 确保节点状态为“就绪”。

NAME                                STATUS   ROLES   AGE     VERSION
aks-nodepool1-13213685-vmss000000   Ready    agent   2m26s   v1.28.5
aks-nodepool1-13213685-vmss000001   Ready    agent   2m26s   v1.28.5
aks-nodepool1-13213685-vmss000002   Ready    agent   2m26s   v1.28.5

创建自动版 Kubernetes 群集

  1. 要创建 AKS Automatic 群集,请搜索“Kubernetes 服务”,然后从下拉列表选项中选择“自动 Kubernetes 群集”

    在 Azure 门户中创建 AKS Automatic 群集时入口点的屏幕截图。

  2. 在“基本信息”选项卡上,填写开始使用所需的所有必填字段(订阅、资源组、Kubernetes 群集名称和区域):

    Azure 门户中 AKS Automatic 群集的“创建 - 基本信息”选项卡的屏幕截图。

  3. 在“监视”选项卡上,从 Azure Monitor、托管 Prometheus、Grafana 仪表板和/或配置警报中选择监视配置。 启用托管 Grafana(可选)、添加标记(可选),并继续创建群集。

    在 Azure 门户中创建 AKS Automatic 群集时“监视”选项卡的屏幕截图。

  4. 开始从 GitHub 配置你的第一个应用程序并设置自动化部署管道。

    在 Azure 门户中创建 AKS Automatic 群集后,“概述”边栏选项卡上的“开始使用”选项卡的屏幕截图。

连接至群集

若要管理 Kubernetes 群集,请使用 Kubernetes 命令行客户端 kubectl。 如果使用的是 Azure Cloud Shell,则 kubectl 已安装。 要在本地安装 kubectl,请运行 az aks install-cli 命令。 AKS Automatic 群集配置为使用 Microsoft Entra ID 进行 Kubernetes 基于角色的访问控制 (RBAC)。 使用 Azure 门户创建群集时,系统会为用户

使用 kubectl 命令将 az aks get-credentials 配置为连接到你的 Kubernetes 群集。 此命令将下载凭据,并将 Kubernetes CLI 配置为使用这些凭据。

az aks get-credentials --resource-group myResourceGroup --name myAKSAutomaticCluster

使用 kubectl get 命令验证与群集之间的连接。 此命令将返回群集节点的列表。

kubectl get nodes

以下示例输出展示了登录的要求。

To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code AAAAAAAAA to authenticate.

登录后,以下示例输出会显示托管系统节点池。 确保节点状态为“就绪”。

NAME                                STATUS   ROLES   AGE     VERSION
aks-nodepool1-13213685-vmss000000   Ready    agent   2m26s   v1.28.5
aks-nodepool1-13213685-vmss000001   Ready    agent   2m26s   v1.28.5
aks-nodepool1-13213685-vmss000002   Ready    agent   2m26s   v1.28.5

创建资源组

Azure 资源组是用于部署和管理 Azure 资源的逻辑组。 创建资源组时,系统会提示你指定一个位置。 此位置是资源组元数据的存储位置,也是资源在 Azure 中运行的位置(如果你在创建资源期间未指定其他区域)。

以下示例在“eastus”位置创建名为“myResourceGroup”的资源组。

使用 az group create 命令创建资源组。

az group create --name myResourceGroup --location eastus

以下示例输出类似于资源组成功创建:

{
  "id": "/subscriptions/<guid>/resourceGroups/myResourceGroup",
  "location": "eastus",
  "managedBy": null,
  "name": "myResourceGroup",
  "properties": {
    "provisioningState": "Succeeded"
  },
  "tags": null
}

查阅 Bicep 文件

此 Bicep 文件定义 AKS Automatic 群集。 在预览版中,需要指定系统节点池代理池配置文件

@description('The name of the managed cluster resource.')
param clusterName string = 'myAKSAutomaticCluster'

@description('The location of the managed cluster resource.')
param location string = resourceGroup().location

resource aks 'Microsoft.ContainerService/managedClusters@2024-03-02-preview' = {
  name: clusterName
  location: location  
  sku: {
	name: 'Automatic'
  }
  properties: {
    agentPoolProfiles: [
      {
        name: 'systempool'
        mode: 'System'
	count: 3
      }
    ]
  }
  identity: {
    type: 'SystemAssigned'
  }
}

有关 Bicep 文件中定义的资源的详细信息,请参阅 Microsoft.ContainerService/managedClusters 参考。

部署 Bicep 文件

  1. 将该 Bicep 文件另存为本地计算机上的 main.bicep

    重要

    Bicep 文件将 clusterName 参数设置为字符串 myAKSAutomaticCluster。 如果要使用其他群集名称,请确保先将字符串更新为首选群集名称,然后再将文件保存到计算机。

  2. 使用 Azure CLI 部署 Bicep 文件。

    az deployment group create --resource-group myResourceGroup --template-file main.bicep
    

    创建 AKS 群集需要几分钟时间。 等待群集成功部署,然后转到下一步骤。

连接至群集

若要管理 Kubernetes 群集,请使用 Kubernetes 命令行客户端 kubectl。 如果使用的是 Azure Cloud Shell,则 kubectl 已安装。 要在本地安装 kubectl,请运行 az aks install-cli 命令。 AKS Automatic 群集配置为使用 Microsoft Entra ID 进行 Kubernetes 基于角色的访问控制 (RBAC)

重要

使用 Bicep 创建群集时,你需要为用户分配一个内置角色,例如 Azure Kubernetes Service RBAC ReaderAzure Kubernetes Service RBAC WriterAzure Kubernetes Service RBAC AdminAzure Kubernetes Service RBAC Cluster Admin,范围限定为群集或特定命名空间,例如使用 az role assignment create --role "Azure Kubernetes Service RBAC Cluster Admin" --scope <AKS cluster resource id> --assignee user@contoso.com。 另请确保用户具有 Azure Kubernetes Service Cluster User 内置角色以便能够运行 az aks get-credentials,然后使用 az aks get-credentials 命令获取 AKS 群集的 kubeconfig。

使用 kubectl 命令将 az aks get-credentials 配置为连接到你的 Kubernetes 群集。 此命令将下载凭据,并将 Kubernetes CLI 配置为使用这些凭据。

az aks get-credentials --resource-group myResourceGroup --name 

使用 kubectl get 命令验证与群集之间的连接。 此命令将返回群集节点的列表。

kubectl get nodes

以下示例输出展示了登录的要求。

To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code AAAAAAAAA to authenticate.

登录后,以下示例输出会显示托管系统节点池。 确保节点状态为“就绪”。

NAME                                STATUS   ROLES   AGE     VERSION
aks-nodepool1-13213685-vmss000000   Ready    agent   2m26s   v1.28.5
aks-nodepool1-13213685-vmss000001   Ready    agent   2m26s   v1.28.5
aks-nodepool1-13213685-vmss000002   Ready    agent   2m26s   v1.28.5

部署应用程序

要部署该应用程序,请使用清单文件创建运行 AKS 应用商店应用程序所需的所有对象。 Kubernetes 清单文件定义群集的所需状态,例如,要运行哪些容器映像。 该清单包括以下 Kubernetes 部署和服务:

Azure 应用商店示例体系结构的屏幕截图。

  • 门店:Web 应用程序,供客户查看产品和下单
  • 产品服务:显示产品信息。
  • 订单服务:下单。
  • Rabbit MQ:订单队列的消息队列

注释

建议不要在没有持久性存储用于生产的情况下运行有状态容器(例如 Rabbit MQ)。 为简单起见,建议使用托管服务,例如 Azure Cosmos DB 或 Azure 服务总线。

  1. 创建命名空间 aks-store-demo 以将 Kubernetes 资源部署到其中。

    kubectl create ns aks-store-demo
    
  2. 使用 kubectl apply 命令将应用程序部署到 aks-store-demo 命名空间中。 定义部署的 YAML 文件位于 GitHub 上。

    kubectl apply -n aks-store-demo -f https://raw.githubusercontent.com/Azure-Samples/aks-store-demo/main/aks-store-ingress-quickstart.yaml
    

    以下示例输出显示部署和服务:

    statefulset.apps/rabbitmq created
    configmap/rabbitmq-enabled-plugins created
    service/rabbitmq created
    deployment.apps/order-service created
    service/order-service created
    deployment.apps/product-service created
    service/product-service created
    deployment.apps/store-front created
    service/store-front created
    ingress/store-front created
    

测试应用程序

应用程序运行时,Kubernetes 服务将向 Internet 公开应用程序前端。 此过程可能需要几分钟才能完成。

  1. 使用 kubectl get pods 命令查看已部署的 Pod 的状态。 在继续操作之前,请确保所有 Pod 的状态为 Running。 如果这是你要部署的第一个工作负载,节点自动预配可能需要花费几分钟时间来创建节点池,以运行 Pod。

    kubectl get pods -n aks-store-demo
    
  2. 检查应用商店前端应用程序的公共 IP 地址。 使用带有 参数的 --watch 命令来监视进度。

    kubectl get ingress store-front -n aks-store-demo --watch
    

    服务的 ADDRESS 输出最初显示为空:store-front

    NAME          CLASS                                HOSTS   ADDRESS        PORTS   AGE
    store-front   webapprouting.kubernetes.azure.com   *                      80      12m
    
  3. 在 ADDRESS 从空白变为实际公共 IP 地址后,使用 停止 CTRL-C 监视过程。kubectl

    以下示例输出显示向服务分配的有效公共 IP 地址:

    NAME          CLASS                                HOSTS   ADDRESS        PORTS   AGE
    store-front   webapprouting.kubernetes.azure.com   *       4.255.22.196   80      12m
    
  4. 若要查看 Azure Store 应用的实际效果,请打开 Web 浏览器并转到入口的外部 IP 地址。

    AKS 存储示例应用程序的屏幕截图。

删除群集

如果不打算完成 AKS 教程,请清理不必要的资源以避免产生 Azure 费用。 运行 az group delete 命令以移除资源组、容器服务及所有相关资源。

az group delete --name myResourceGroup --yes --no-wait

注释

AKS 群集是使用系统分配的托管标识创建的,这是本快速入门中使用的默认标识选项。 平台负责管理此标识,因此你无需手动移除它。

后续步骤

在本快速入门中,你使用 AKS Automatic 部署了一个 Kubernetes 群集,然后在其中部署了示例多容器应用程序。 此示例应用程序仅用于演示目的,并未展示出 Kubernetes 应用程序的所有最佳做法。 有关使用生产版 AKS 创建完整解决方案的指南,请参阅 AKS 解决方案指南

若要详细了解 AKS Automatic,请继续阅读简介。