快速入门:部署 Azure Kubernetes 服务 (AKS) Automatic 群集(预览版)

适用于:✔️ AKS Automatic(预览版)

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

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

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

开始之前

本快速入门假设读者基本了解 Kubernetes 的概念。 有关详细信息,请参阅 Azure Kubernetes 服务 (AKS) 的 Kubernetes 核心概念

可以使用本地 Azure CLI。

  • 如果需要,请安装 Azure CLI 来运行 CLI 参考命令。

  • 本地 Azure CLI,请了解如何安装 Azure CLI。 如果在 Windows 或 macOS 上运行,请考虑在 Docker 容器中运行 Azure CLI。 有关详细信息,请参阅如何在 Docker 容器中运行 Azure CLI

    • 通过使用 az login 命令登录到 Azure CLI。 若要完成身份验证过程,请遵循终端中显示的步骤。 有关其他登录选项,请参阅使用 Azure CLI 登录

    • 出现提示时,请在首次使用时安装 Azure CLI 扩展。 有关扩展详细信息,请参阅使用 Azure CLI 的扩展

    • 运行 az version 以查找安装的版本和依赖库。 若要升级到最新版本,请运行 az upgrade

  • 本文需要 2.57.0 或更高版本的 Azure CLI。 如果你使用的是 Azure Cloud Shell,则表示已安装最新版本。

  • 本文需要 aks-preview Azure CLI 扩展版本 3.0.0b13 或更高版本。

  • 如果有多个 Azure 订阅,请使用 az account set 命令选择应在其中计收资源费用的相应订阅 ID。

  • 在 Azure 订阅中注册 AutomaticSKUPreview 功能。

  • 创建群集的标识还应对订阅具有以下权限

    • Microsoft.Authorization/policyAssignments/write
    • Microsoft.Authorization/policyAssignments/read

重要

请确保对于要在其中部署群集的区域,订阅具有 Standard_DS4_v2 虚拟机 24 个 vCPU 的配额。 .png

  • 要部署 Bicep 文件,需要对创建的资源具有写入访问权限,并有权访问 Microsoft.Resources/deployments 资源类型上的所有操作。 例如,若要创建虚拟机,需要 Microsoft.Compute/virtualMachines/writeMicrosoft.Resources/deployments/* 权限。 有关角色和权限的列表,请参阅 Azure 内置角色

安装 aks-preview Azure CLI 扩展

重要

AKS 预览功能是可选择启用的自助功能。 预览功能是“按现状”和“按可用”提供的,不包括在服务级别协议和有限保证中。 AKS 预览功能是由客户支持尽最大努力部分覆盖。 因此,这些功能并不适合用于生产。 有关详细信息,请参阅以下支持文章:

若要安装 aks-preview 扩展,请运行以下命令:

az extension add --name aks-preview

运行以下命令以更新到已发布的最新扩展版本:

az extension update --name aks-preview

注册功能标志

要使用 AKS Automatic 预览版,必须注册其他所需功能的功能标志。 使用 az feature register 命令注册以下标志。

az feature register --namespace Microsoft.ContainerService --name EnableAPIServerVnetIntegrationPreview
az feature register --namespace Microsoft.ContainerService --name NRGLockdownPreview
az feature register --namespace Microsoft.ContainerService --name SafeguardsPreview
az feature register --namespace Microsoft.ContainerService --name NodeAutoProvisioningPreview
az feature register --namespace Microsoft.ContainerService --name DisableSSHPreview
az feature register --namespace Microsoft.ContainerService --name AutomaticSKUPreview

使用 az feature show 命令验证注册状态。 需要花费几分钟时间,状态才会显示为“已注册”

az feature show --namespace Microsoft.ContainerService --name AutomaticSKUPreview

当状态反映为“已注册”时,使用 az provider register 命令刷新 Microsoft.ContainerService 资源提供程序的注册:

az provider register --namespace Microsoft.ContainerService

创建资源组

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

以下示例在 chinanorth3 位置创建了一个名为 myResourceGroup 的资源组。

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

az group create --name myResourceGroup --location chinanorth3

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

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

创建 AKS Automatic 群集

要创建 AKS Automatic 群集,请使用 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 群集通过用于 Kubernetes 基于角色的访问控制 (RBAC) 的 Microsoft Entra ID 进行配置。 使用 Azure CLI 创建群集时,系统会为用户分配用于 Azure Kubernetes Service RBAC Cluster Admin 的内置角色

使用 az aks get-credentials 命令将 kubectl 配置为连接到你的 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-default-f8vj2                   Ready    agent   2m26s   v1.28.5
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. 在“基本信息”选项卡上,填写开始使用所需的所有必填字段:“订阅”、“资源组”、“群集名称”和“区域”

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

    如果未满足先决条件,并且订阅需要注册预览标志,则“订阅”字段下会显示错误:

    在 Azure 门户中创建 AKS Automatic 群集时,订阅未注册预览标志时显示的错误的屏幕截图。

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

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

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

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

连接到群集

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

使用 az aks get-credentials 命令将 kubectl 配置为连接到你的 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-default-f8vj2                   Ready    agent   2m26s   v1.28.5
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 中运行的位置(如果你在创建资源期间未指定其他区域)。

以下示例在 chinanorth3 位置创建了一个名为 myResourceGroup 的资源组。

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

az group create --name myResourceGroup --location chinanorth3

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

{
  "id": "/subscriptions/<guid>/resourceGroups/myResourceGroup",
  "location": "chinanorth3",
  "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'
  		tier: 'Standard'
  }
  properties: {
    agentPoolProfiles: [
      {
        name: 'systempool'
        count: 3
        vmSize: 'Standard_DS4_v2'
        osType: 'Linux'
        mode: 'System'
      }
    ]
  }
  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 群集通过用于 Kubernetes 基于角色的访问控制 (RBAC) 的 Microsoft Entra ID 进行配置。 使用 Bicep 创建群集时,需要分配一个内置角色,例如 Azure Kubernetes Service RBAC ReaderAzure Kubernetes Service RBAC WriterAzure Kubernetes Service RBAC AdminAzure Kubernetes Service RBAC Cluster Admin,并将其范围限定为群集或特定命名空间。 另请确保用户具有 Azure Kubernetes Service Cluster User 内置角色,以便能够运行 az aks get-credentials,然后使用 az aks get-credentials 命令获取 AKS 群集的 kubeconfig。

使用 az aks get-credentials 命令将 kubectl 配置为连接到你的 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-default-f8vj2                   Ready    agent   2m26s   v1.28.5
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 service 命令来监视进度。

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

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

    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. 打开 Web 浏览器访问入口的外部 IP 地址,以查看 Azure 应用商店应用的实际效果。

    AKS 应用商店示例应用程序的屏幕截图。

删除群集

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

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

注意

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

后续步骤

在本快速入门中,你使用 AKS Automatic 部署了一个 Kubernetes 群集,然后在其中部署了示例多容器应用程序。 此示例应用程序仅用于演示目的,并未展示出 Kubernetes 应用程序的所有最佳做法。

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