你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
适用于: ✔️ AKS 自动版
Azure Kubernetes 服务 (AKS) Automatic为开发人员、DevOps 工程师和平台工程师提供最简单的托管 Kubernetes 体验。 AKS Automatic 可自动执行 AKS 群集设置和操作,并嵌入最佳做法配置,非常适合新式应用程序和 AI 应用程序。 任何技能级别的用户都可以从 AKS Automatic 对其应用程序的安全性、性能和可靠性中受益。 AKS 自动化还包括一个Pod 就绪性 SLA,该 SLA 保证 99.9% 的符合条件的 Pod 就绪性操作在 5 分钟内完成,确保应用程序拥有可靠和自我修复的基础设施。 本快速入门假设读者基本了解 Kubernetes 的概念。 有关详细信息,请参阅 Azure Kubernetes 服务 (AKS) 的 Kubernetes 核心概念。
在此快速入门中,你将了解:
- 创建虚拟网络。
- 创建对虚拟网络具有权限的托管标识。
- 在虚拟网络中部署专用 AKS 自动版群集。
- 连接到专用群集。
- 运行一个示例多容器应用程序,其中的一组微服务和 Web 前端模拟零售场景。
先决条件
- 如果没有Azure帐户,请创建一个 free 帐户。
- Azure CLI 2.86.0 或更高版本。 若要查找版本,请运行
az --version命令。 如果需要安装或升级,请参阅 Install Azure CLI。
注释
此示例使用 AzureRM 提供程序 azurerm_kubernetes_automatic_cluster 的资源创建 AKS 自动群集,该群集需要 AzureRM 提供程序版本 v4.81 或更高版本。 有关使用 AzAPI 提供程序的对应示例,请参阅 101-aks-automatic-private-custom-network-azapi 示例。
- 在 API 服务器子网上具有
Network Contributor内置角色分配的群集标识。 - 在虚拟网络上具有
Network Contributor内置角色分配的群集标识,用于支持节点自动预配。 - 使用
Azure Kubernetes 服务 Cluster User Role和Azure Kubernetes 服务 RBAC Writer访问群集的用户标识。 - 一个虚拟网络,其专用 API 服务器子网至少为
*/28大小,并分配给Microsoft.ContainerService/managedClusters。- 如果存在附加到子网的网络安全组 (NSG),请确保规则允许以下流量,这些流量存在于节点与 API 服务器之间、Azure 负载均衡器与 API 服务器之间以及 Pod 到 Pod 的通信之间。
- 如果存在 Azure 防火墙或其他出站限制方法或设备,请确保允许所需的出站网络规则和 FQDN。
- AKS Automatic 将在你的 AKS 群集上启用 Azure Policy,但为了获得更顺畅的体验,你应该先在你的订阅中预注册
Microsoft.PolicyInsights资源提供程序。 有关详细信息,请参阅 Azure 资源提供程序和类型。 - 使用
az extension remove -n aks-preview卸载 AKS-preview 扩展。
重要
从 AKS 1.36 开始,由于上游 Ingress NGINX 停用,新的 AKS 自动群集会默认启用通过应用程序路由加载项使用 Kubernetes 网关 API,而不是具有应用程序路由加载项的托管 NGINX Ingress。
现有自动群集不受影响,但应 通过应用程序路由加载项开始迁移到 Kubernetes 网关 API。
局限性
以下限制适用于 AKS 自动群集:
- AKS Automatic 已在以下区域正式发布:
australiacentral,newzealandnorth,belgiumcentral,austriaeast,brazilsouth,canadacentral,canadaeast,centralindia,centralus,australiasoutheast,denmarkeast,chilecentral,eastasia,eastus,eastus2,francecentral,malaysiawest,koreasouth,mexicocentral,koreacentral,japanwest,japaneast,northcentralus,italynorth,israelcentral,indonesiacentral,northeurope,australiaeast,uaenorth,switzerlandnorth,germanywestcentral,southcentralus,southeastasia,southafricanorth,southindia,polandcentral,spaincentral,westcentralus,uksouth,swedencentral,norwayeast,westeurope,westus,ukwest,westus3,westus2. - 默认情况下,新的 AKS 自动群集启用托管系统节点池和 LocalDNS。 不能在任何区域中创建没有托管系统节点池的 AKS 自动群集。
- AKS 自动群集已节点资源组锁定预配置,不允许更改
MC_资源组,从而阻止默认专用 DNS区域中的虚拟网络链接。 对于跨 VNet 或自定义 DNS 场景,请使用自定义网络和专用 DNS,并遵循在自定义虚拟网络中创建专用 Azure Kubernetes 服务 (AKS) 自动群集。 - Azure CLI版本 2.86.0 或更高版本是必需的。 若要查找版本,请运行
az --version命令。 如果需要安装或升级,请参阅 Install Azure CLI。 - 不支持以下扩展:
- 不支持 Windows 节点。
- 不支持从 AKS 基础 SKU 迁移到自动 SKU。
- 在托管系统节点池已正式可用的区域中,支持以预览版方式将不含托管系统节点池的现有 AKS Automatic 群集迁移到具有托管系统节点池的 AKS Automatic 群集。 不支持从具有托管系统节点池的 AKS 自动迁移回 AKS 自动,而无需托管系统节点池。
定义变量
定义后续步骤中使用的以下变量。
RG_NAME=automatic-rg
VNET_NAME=automatic-vnet
CLUSTER_NAME=automatic
IDENTITY_NAME=automatic-uami
LOCATION=eastus
SUBSCRIPTION_ID=$(az account show --query id -o tsv)
创建资源组
Azure资源组是在其中部署和管理Azure资源的逻辑组。
使用 az group create 命令创建资源组。
az group create -n ${RG_NAME} -l ${LOCATION}
以下示例输出类似于资源组成功创建:
{
"id": "/subscriptions/<guid>/resourceGroups/automatic-rg",
"location": "canadacentral",
"managedBy": null,
"name": "automatic-rg",
"properties": {
"provisioningState": "Succeeded"
},
"tags": null
}
创建虚拟网络
使用 az network vnet create 命令创建虚拟网络。 使用 az network vnet subnet create 命令创建 API 服务器子网和群集子网。
将自定义虚拟网络与 AKS 自动配合使用时,必须创建 API 服务器子网。 AKS 会代表你将该子网委派给 Microsoft.ContainerService/managedClusters,从而授予 AKS 服务将 API 服务器 Pod 和内部负载均衡器注入到该子网中的权限。 不能将子网用于任何其他工作负荷,但可以将其用于位于同一虚拟网络中的多个 AKS 群集的 API 服务器子网。 支持单个群集的最低支持的 API 服务器子网大小为 /28。
警告
AKS 群集在子网地址空间中至少保留 9 个 (9) 个 IP。 IP 地址不足可能会阻止 API 服务器缩放并导致 API 服务器中断。
az network vnet create --name ${VNET_NAME} \
--resource-group ${RG_NAME} \
--location ${LOCATION} \
--address-prefixes 172.19.0.0/16
az network vnet subnet create --resource-group ${RG_NAME} \
--vnet-name ${VNET_NAME} \
--name apiServerSubnet \
--delegations Microsoft.ContainerService/managedClusters \
--address-prefixes 172.19.0.0/28
az network vnet subnet create --resource-group ${RG_NAME} \
--vnet-name ${VNET_NAME} \
--name userNodeSubnet \
--address-prefixes 172.19.1.0/24
az network vnet subnet create --resource-group ${RG_NAME} \
--vnet-name ${VNET_NAME} \
--name managedSystemNodeSubnet \
--address-prefixes 172.19.0.64/26
网络安全组规则
默认情况下,允许虚拟网络中的所有流量。 但是,如果添加了网络安全组规则 (NSG) 来限制不同子网之间的流量,请确保 NSG 安全规则允许以下类型的通信:
| 目的地 | 来源 | 协议 | 港口 | 用途 |
|---|---|---|---|---|
| APIServer 子网 CIDR | 用户节点子网和系统节点子网 | TCP | 443 和 4443 | 如需启用节点与 API 服务器之间的通信,则必须支持。 |
| APIServer 子网 CIDR | Azure 负载均衡器 | TCP | 9988 | 启用Azure 负载均衡器与 API 服务器之间的通信所必需的。 还可以启用Azure 负载均衡器与 API 服务器子网 CIDR 之间的所有通信。 |
| 节点 CIDR | 节点 CIDR | 所有协议 | 所有端口 | 启用节点之间的通信时必需。 |
| 节点 CIDR | Pod CIDR | 所有协议 | 所有端口 | 路由服务流量时必需。 |
| Pod CIDR | Pod CIDR | 所有协议 | 所有端口 | Pod 到 Pod 和 Pod 到服务的流量(包括 DNS)所必需。 |
创建托管标识并为其授予对虚拟网络的权限
使用 az identity create 命令创建托管标识并检索主体 ID。 使用 命令将虚拟网络上的“网络参与者”角色分配给托管标识。az role assignment create
az identity create \
--resource-group ${RG_NAME} \
--name ${IDENTITY_NAME} \
--location ${LOCATION}
IDENTITY_PRINCIPAL_ID=$(az identity show --resource-group ${RG_NAME} --name ${IDENTITY_NAME} --query principalId -o tsv)
az role assignment create \
--scope "/subscriptions/${SUBSCRIPTION_ID}/resourceGroups/${RG_NAME}/providers/Microsoft.Network/virtualNetworks/${VNET_NAME}" \
--role "Network Contributor" \
--assignee-object-id "${IDENTITY_PRINCIPAL_ID}" \
--assignee-principal-type ServicePrincipal
在自定义虚拟网络中创建专用 AKS 自动群集
要创建专用 AKS 自动群集,请使用 az aks create 命令。 请注意 --enable-private-cluster 标志的使用。
注释
可以参考 专用群集 文档来配置其他选项,例如禁用群集的公共 FQDN 和配置专用 DNS 区域。
az aks create \
--resource-group ${RG_NAME} \
--name ${CLUSTER_NAME} \
--location ${LOCATION} \
--apiserver-subnet-id "/subscriptions/${SUBSCRIPTION_ID}/resourceGroups/${RG_NAME}/providers/Microsoft.Network/virtualNetworks/${VNET_NAME}/subnets/apiServerSubnet" \
--node-subnet-id "/subscriptions/${SUBSCRIPTION_ID}/resourceGroups/${RG_NAME}/providers/Microsoft.Network/virtualNetworks/${VNET_NAME}/subnets/userNodeSubnet" \
--system-node-subnet-id "/subscriptions/${SUBSCRIPTION_ID}/resourceGroups/${RG_NAME}/providers/Microsoft.Network/virtualNetworks/${VNET_NAME}/subnets/managedSystemNodeSubnet"
--assign-identity "/subscriptions/${SUBSCRIPTION_ID}/resourcegroups/${RG_NAME}/providers/Microsoft.ManagedIdentity/userAssignedIdentities/${IDENTITY_NAME}" \
--sku automatic \
--enable-private-cluster \
--no-ssh-key
片刻之后,该命令将会完成,并返回有关群集的 JSON 格式信息。
连接到群集
将 AKS 自动群集创建为专用群集时,API 服务器终结点没有公共 IP 地址。 若要管理 API 服务器,例如通过 kubectl,需要通过有权访问群集Azure虚拟网络的计算机进行连接。 有几种选项可用于建立与专用群集的网络连接:
- 使用
az vm create命令和--vnet-name标志,在与AKS自动化集群相同的虚拟网络中创建虚拟机。 - 在单独的虚拟网络中使用虚拟机并设置虚拟网络对等互连。
- 使用快速路由或 VPN 连接。
- 使用专用终结点连接。
在 AKS 群集所在的同一虚拟网络中创建虚拟机是最简单的选项。 ExpressRoute 和 VPN 增加了成本,并且需要其他网络复杂性。 虚拟网络对等互联要求计划网络 CIDR 范围,以确保不存在重叠范围。 有关详细信息,请参阅 用于连接到专用群集的选项。
要管理 Kubernetes 群集,请使用 Kubernetes 命令行客户端 kubectl。 如果使用Azure Cloud Shell,则已安装 kubectl。 要在本地安装 kubectl,请运行 az aks install-cli 命令。 AKS Automatic 群集配置为使用 Microsoft Entra ID 进行 Kubernetes 基于角色的访问控制 (RBAC)。
使用 kubectl 命令将 配置为连接到你的 Kubernetes 群集。 此命令将下载凭据,并将 Kubernetes CLI 配置为使用这些凭据。
az aks get-credentials --resource-group ${RG_NAME} --name ${CLUSTER_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-hostedpool-16652789-vms1 Ready <none> 19m v1.34.7
aks-hostedpool-16652789-vms2 Ready <none> 19m v1.34.7
aks-hostedpool-16652789-vms3 Ready <none> 19m v1.34.7
aks-system-surge-zq4d2 Ready <none> 19m v1.34.7
创建虚拟网络
此Bicep文件定义虚拟网络。
@description('The location of the managed cluster resource.')
param location string = resourceGroup().location
@description('The name of the virtual network.')
param vnetName string = 'aksAutomaticVnet'
@description('The address prefix of the virtual network.')
param addressPrefix string = '172.19.0.0/16'
@description('The name of the API server subnet.')
param apiServerSubnetName string = 'apiServerSubnet'
@description('The subnet prefix of the API server subnet.')
param apiServerSubnetPrefix string = '172.19.0.0/28'
@description('The name of the user node subnet.')
param userNodeSubnetName string = 'userNodeSubnet'
@description('The subnet prefix of the user node subnet.')
param userNodeSubnetPrefix string = '172.19.1.0/24'
@description('The name of the system node subnet.')
param systemNodeSubnetName string = 'systemNodeSubnet'
@description('The subnet prefix of the system node subnet.')
param systemNodeSubnetPrefix string = '172.19.0.64/26'
// Virtual network with an API server subnet, a user node subnet, and a system node subnet
resource virtualNetwork 'Microsoft.Network/virtualNetworks@2023-09-01' = {
name: vnetName
location: location
properties: {
addressSpace: {
addressPrefixes: [ addressPrefix ]
}
subnets: [
{
name: apiServerSubnetName
properties: {
addressPrefix: apiServerSubnetPrefix
}
}
{
name: userNodeSubnetName
properties: {
addressPrefix: userNodeSubnetPrefix
}
}
{
name: systemNodeSubnetName
properties: {
addressPrefix: systemNodeSubnetPrefix
}
}
]
}
}
output apiServerSubnetId string = resourceId('Microsoft.Network/virtualNetworks/subnets', vnetName, apiServerSubnetName)
output userNodeSubnetId string = resourceId('Microsoft.Network/virtualNetworks/subnets', vnetName, userNodeSubnetName)
output systemNodeSubnetId string = resourceId('Microsoft.Network/virtualNetworks/subnets', vnetName, systemNodeSubnetName)
保存 Bicep 文件 virtualNetwork.bicep 到本地计算机。
重要
Bicep文件将 vnetName 参数设置为 aksAutomaticVnet,将 addressPrefix 参数设置为 172.19.0.0/16, apiServerSubnetPrefix参数到 172.19.0.0/28,以及 apiServerSubnetPrefix 参数到 172.19.1.0/24。 如果你想使用不同的值,请确保将字符串更新为偏好的值。
使用Azure CLI部署Bicep文件。
az deployment group create --resource-group <resource-group> --template-file virtualNetwork.bicep
默认情况下,允许虚拟网络中的所有流量。 但是,如果添加了网络安全组规则 (NSG) 来限制不同子网之间的流量,请确保 NSG 安全规则允许以下类型的通信:
| 目的地 | 来源 | 协议 | 港口 | 用途 |
|---|---|---|---|---|
| APIServer 子网 CIDR | 用户节点子网和系统节点子网 | TCP | 443 和 4443 | 如需启用节点与 API 服务器之间的通信,则必须支持。 |
| APIServer 子网 CIDR | Azure 负载均衡器 | TCP | 9988 | 启用Azure 负载均衡器与 API 服务器之间的通信所必需的。 还可以启用Azure 负载均衡器与 API 服务器子网 CIDR 之间的所有通信。 |
| 节点 CIDR | 节点 CIDR | 所有协议 | 所有端口 | 启用节点之间的通信时必需。 |
| 节点 CIDR | Pod CIDR | 所有协议 | 所有端口 | 路由服务流量时必需。 |
| Pod CIDR | Pod CIDR | 所有协议 | 所有端口 | Pod 到 Pod 和 Pod 到服务的流量(包括 DNS)所必需。 |
创建托管标识
此Bicep文件定义用户分配的托管标识。
param location string = resourceGroup().location
param uamiName string = 'aksAutomaticUAMI'
resource userAssignedManagedIdentity 'Microsoft.ManagedIdentity/userAssignedIdentities@2023-01-31' = {
name: uamiName
location: location
}
output uamiId string = userAssignedManagedIdentity.id
output uamiPrincipalId string = userAssignedManagedIdentity.properties.principalId
output uamiClientId string = userAssignedManagedIdentity.properties.clientId
保存 Bicep 文件 uami.bicep 到本地计算机。
重要
Bicep文件将 uamiName 参数设置为 aksAutomaticUAMI。 如果你想使用不同的标识名称,请确保将字符串更新为偏好的名称。
使用Azure CLI部署Bicep文件。
az deployment group create --resource-group <resource-group> --template-file uami.bicep
分配对虚拟网络的网络参与者角色
此Bicep文件定义了在虚拟网络上的角色分配。
@description('The name of the virtual network.')
param vnetName string = 'aksAutomaticVnet'
@description('The principal ID of the user assigned managed identity.')
param uamiPrincipalId string
// Get a reference to the virtual network
resource virtualNetwork 'Microsoft.Network/virtualNetworks@2023-09-01' existing ={
name: vnetName
}
// Assign the Network Contributor role to the user assigned managed identity on the virtual network
// '4d97b98b-1d4f-4787-a291-c67834d212e7' is the built-in Network Contributor role definition
// See: https://learn.microsoft.com/en-us/azure/role-based-access-control/built-in-roles/networking#network-contributor
resource networkContributorRoleAssignmentToVirtualNetwork 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
name: guid(uamiPrincipalId, '4d97b98b-1d4f-4787-a291-c67834d212e7', resourceGroup().id, virtualNetwork.name)
scope: virtualNetwork
properties: {
roleDefinitionId: resourceId('Microsoft.Authorization/roleDefinitions', '4d97b98b-1d4f-4787-a291-c67834d212e7')
principalId: uamiPrincipalId
}
}
将 Bicep 文件 roleAssignments.bicep 保存到本地计算机。
重要
Bicep文件将 vnetName 参数设置为 aksAutomaticVnet。 如果你使用其他虚拟网络名称,请确保将字符串更新为偏好的虚拟网络名称。
使用Azure CLI部署Bicep文件。 需要提供用户分配的标识的主体 ID。
az deployment group create --resource-group <resource-group> --template-file roleAssignments.bicep \
--parameters uamiPrincipalId=<user assigned identity prinicipal id>
在自定义虚拟网络中创建专用 AKS 自动群集
此 Bicep 文件定义了 AKS 自动化群集。
注释
可以参考 专用群集 文档来配置其他选项,例如禁用群集公共 FQDN 和配置专用 DNS 区域。
@description('The name of the managed cluster resource.')
param clusterName string = 'aksPrivateAutomaticCluster'
@description('The location of the managed cluster resource.')
param location string = resourceGroup().location
@description('The resource ID of the API server subnet.')
param apiServerSubnetId string
@description('The resource ID of the user node subnet.')
param userNodeSubnetId string
@description('The resource ID of the system node subnet.')
param systemNodeSubnetId string
@description('The resource ID of the user assigned managed identity.')
param uamiId string
/// Create the private AKS Automatic cluster using the custom virtual network and user assigned managed identity
resource aks 'Microsoft.ContainerService/managedClusters@2024-03-02-preview' = {
name: clusterName
location: location
sku: {
name: 'Automatic'
}
properties: {
apiServerAccessProfile: {
subnetId: apiServerSubnetId
enablePrivateCluster: true
}
networkProfile: {
outboundType: 'loadBalancer'
}
hostedSystemProfile: {
systemNodeSubnetID: systemNodeSubnetId
nodeSubnetID: userNodeSubnetId
}
}
identity: {
type: 'UserAssigned'
userAssignedIdentities: {
'${uamiId}': {}
}
}
}
保存 Bicep 文件 aks.bicep 到本地计算机。
重要
Bicep文件将 clusterName 参数设置为 aksPrivateAutomaticCluster。 如果你想要其他群集名称,请确保将字符串更新为偏好的群集名称。
使用Azure CLI部署Bicep文件。 需要提供 API 服务器子网资源 ID、用户节点子网资源 ID、系统节点子网资源 ID 和用户分配的托管标识资源 ID。
az deployment group create --resource-group <resource-group> --template-file aks.bicep \
--parameters apiServerSubnetId=<API server subnet resource id> \
--parameters nodeSubnetId=<user node subnet resource id> \
--parameters systemNodeSubnetId=<system node subnet resource id> \
--parameters uamiPrincipalId=<user assigned identity prinicipal id>
连接到群集
将 AKS 自动群集创建为专用群集时,API 服务器终结点没有公共 IP 地址。 若要管理 API 服务器,例如通过 kubectl,需要通过有权访问群集Azure虚拟网络的计算机进行连接。 有几种选项可用于建立与专用群集的网络连接:
- 使用
az vm create命令和--vnet-name标志,在与AKS自动化集群相同的虚拟网络中创建虚拟机。 - 在单独的虚拟网络中使用虚拟机并设置虚拟网络对等互连。
- 使用快速路由或 VPN 连接。
- 使用专用终结点连接。
在 AKS 群集所在的同一虚拟网络中创建虚拟机是最简单的选项。 快速路由和 VPN 增加了成本,并需要更多的网络复杂性。 虚拟网络对等互联要求计划网络 CIDR 范围,以确保不存在重叠范围。 有关详细信息,请参阅 用于连接到专用群集的选项。
要管理 Kubernetes 群集,请使用 Kubernetes 命令行客户端 kubectl。 如果使用Azure Cloud Shell,则已安装 kubectl。 要在本地安装 kubectl,请运行 az aks install-cli 命令。 AKS Automatic 群集配置为使用 Microsoft Entra ID 进行 Kubernetes 基于角色的访问控制 (RBAC)。
重要
使用 Bicep 创建群集时,需要分配其中一个内置角色,例如 Azure Kubernetes 服务 RBAC Reader, Azure Kubernetes 服务 RBAC Writer、Azure Kubernetes 服务 RBAC Admin 或 Azure Kubernetes 服务 RBAC Cluster Admin,范围限定为群集或特定命名空间,例如使用 az role assignment create --role "Azure Kubernetes 服务 RBAC Cluster Admin" --scope <AKS cluster resource id> --assignee user@contoso.com。 此外,请确保用户具有 Azure Kubernetes 服务 Cluster User 内置角色,以便能够运行 az aks get-credentials,然后使用 az aks get-credentials 命令获取 AKS 群集的 kubeconfig。
使用 kubectl 命令将 配置为连接到你的 Kubernetes 群集。 此命令将下载凭据,并将 Kubernetes CLI 配置为使用这些凭据。
az aks get-credentials --resource-group <resource-group> --name <cluster-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-hostedpool-16652789-vms1 Ready <none> 19m v1.34.7
aks-hostedpool-16652789-vms2 Ready <none> 19m v1.34.7
aks-hostedpool-16652789-vms3 Ready <none> 19m v1.34.7
aks-system-surge-zq4d2 Ready <none> 19m v1.34.7
查看 Terraform 代码
注释
本文的示例代码位于 Azure Terraform GitHub 存储库中。 你可以查看包含当前和以前 Terraform 版本的测试结果的日志文件。
创建一个目录来测试示例 Terraform 代码,并将其设置为当前目录。
创建名为
providers.tf的文件并插入下列代码:terraform { required_version = ">= 1.0" required_providers { azurerm = { source = "hashicorp/azurerm" version = "~>5.0" } random = { source = "hashicorp/random" version = "~>3.0" } } } provider "azurerm" { features {} }创建一个名为
main.tf文件的文件,并添加以下代码:# Create a random name for the resource group using random_pet resource "random_pet" "rg_name" { prefix = var.resource_group_name_prefix } # Create a resource group using the generated random name resource "azurerm_resource_group" "rg" { location = var.resource_group_location name = random_pet.rg_name.id } # Create the custom virtual network that hosts the cluster resource "azurerm_virtual_network" "vnet" { name = var.virtual_network_name location = azurerm_resource_group.rg.location resource_group_name = azurerm_resource_group.rg.name address_space = var.virtual_network_address_space } # Create the subnet delegated to AKS for API Server VNet Integration resource "azurerm_subnet" "api_server" { name = "apiServerSubnet" resource_group_name = azurerm_resource_group.rg.name virtual_network_name = azurerm_virtual_network.vnet.name address_prefixes = var.api_server_subnet_address_prefixes delegation { name = "aks-delegation" service_delegation { name = "Microsoft.ContainerService/managedClusters" actions = ["Microsoft.Network/virtualNetworks/subnets/join/action"] } } } # Create the subnet that hosts the user node pools resource "azurerm_subnet" "user_nodes" { name = "userNodeSubnet" resource_group_name = azurerm_resource_group.rg.name virtual_network_name = azurerm_virtual_network.vnet.name address_prefixes = var.user_node_subnet_address_prefixes } # Create the subnet that hosts the managed system node pool resource "azurerm_subnet" "system_nodes" { name = "managedSystemNodeSubnet" resource_group_name = azurerm_resource_group.rg.name virtual_network_name = azurerm_virtual_network.vnet.name address_prefixes = var.system_node_subnet_address_prefixes lifecycle { # AKS adds its own managed cluster delegation to this subnet after the # cluster is created. ignore_changes = [delegation] } } # Create the user-assigned managed identity used by the cluster resource "azurerm_user_assigned_identity" "aks" { name = var.identity_name location = azurerm_resource_group.rg.location resource_group_name = azurerm_resource_group.rg.name } # Grant the cluster identity Network Contributor on the virtual network resource "azurerm_role_assignment" "network_contributor" { scope = azurerm_virtual_network.vnet.id role_definition_name = "Network Contributor" principal_id = azurerm_user_assigned_identity.aks.principal_id principal_type = "ServicePrincipal" } # Create a random name for the AKS Automatic cluster resource "random_pet" "cluster_name" { prefix = var.cluster_name_prefix } # Create the private AKS Automatic cluster in the custom virtual network resource "azurerm_kubernetes_automatic_cluster" "aks_automatic" { name = random_pet.cluster_name.id location = azurerm_resource_group.rg.location resource_group_name = azurerm_resource_group.rg.name identity { type = "UserAssigned" identity_ids = [azurerm_user_assigned_identity.aks.id] } api_server_access { subnet_id = azurerm_subnet.api_server.id } hosted_system { node_subnet_id = azurerm_subnet.user_nodes.id system_node_subnet_id = azurerm_subnet.system_nodes.id } # The private_cluster block gives the API server a private IP address in the # virtual network. Reaching it requires private connectivity and DNS # resolution of the private FQDN, which peering, a VPN, or ExpressRoute can # provide from outside this virtual network. private_cluster { public_fully_qualified_domain_name_enabled = var.public_fully_qualified_domain_name_enabled } depends_on = [azurerm_role_assignment.network_contributor] }创建一个名为
variables.tf文件的文件,并添加以下代码:variable "resource_group_location" { type = string default = "westus2" description = "Location of the resource group." } variable "resource_group_name_prefix" { type = string default = "rg" description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription." } variable "cluster_name_prefix" { type = string default = "aks-automatic" description = "Prefix of the AKS Automatic cluster name that's combined with a random ID so the name is unique in your Azure subscription." } variable "public_fully_qualified_domain_name_enabled" { type = bool default = false description = "Whether to provision a public FQDN for the private cluster." } variable "virtual_network_name" { type = string default = "aks-automatic-vnet" description = "Name of the custom virtual network that hosts the cluster." } variable "identity_name" { type = string default = "aks-automatic-identity" description = "Name of the user-assigned managed identity that the cluster uses." } variable "virtual_network_address_space" { type = list(string) default = ["172.19.0.0/16"] description = "Address space of the custom virtual network." } variable "api_server_subnet_address_prefixes" { type = list(string) default = ["172.19.0.0/28"] description = "Address prefixes of the subnet delegated to the cluster API server." } variable "user_node_subnet_address_prefixes" { type = list(string) default = ["172.19.1.0/24"] description = "Address prefixes of the subnet that hosts the user node pools." } variable "system_node_subnet_address_prefixes" { type = list(string) default = ["172.19.0.64/26"] description = "Address prefixes of the subnet that hosts the managed system node pool." }创建名为
outputs.tf的文件并插入下列代码:output "resource_group_name" { value = azurerm_resource_group.rg.name } output "cluster_name" { value = azurerm_kubernetes_automatic_cluster.aks_automatic.name } output "cluster_id" { value = azurerm_kubernetes_automatic_cluster.aks_automatic.id } output "node_resource_group_id" { value = azurerm_kubernetes_automatic_cluster.aks_automatic.node_resource_group_id } output "virtual_network_name" { value = azurerm_virtual_network.vnet.name } output "private_fully_qualified_domain_name" { value = azurerm_kubernetes_automatic_cluster.aks_automatic.private_fully_qualified_domain_name }
初始化 Terraform
运行 terraform init,将 Terraform 部署进行初始化。 此命令下载管理Azure资源所需的Azure提供程序。
terraform init -upgrade
创建 Terraform 执行计划
运行 terraform plan 以创建执行计划。
terraform plan -out main.tfplan
应用 Terraform 的执行计划
运行 terraform apply 以将执行计划应用到您的云基础架构。
terraform apply main.tfplan
创建 AKS 自动群集需要几分钟才能完成。
验证集群
获取Azure资源组名称和群集名称。
resource_group_name=$(terraform output -raw resource_group_name) cluster_name=$(terraform output -raw cluster_name)运行 az aks show 以显示群集并确认它是专用的,并使用
AutomaticSKU。az aks show --resource-group $resource_group_name --name $cluster_name --query "{name:name, sku:sku, provisioningState:provisioningState, privateCluster:apiServerAccessProfile.enablePrivateCluster}"
连接到群集
专用群集 API 服务器没有公共 IP 地址,因此,可以从具有对群集虚拟网络的网络访问权限的计算机对其进行管理。 有多个用于建立该连接的选项:
- 在群集所在的同一虚拟网络中创建虚拟机。
- 在单独的虚拟网络中使用虚拟机并设置虚拟网络对等互连。
- 使用Azure ExpressRoute或 VPN 连接。
有关详细信息,请参阅 用于连接到专用群集的选项。
在一台已连接到虚拟网络的计算机上,使用 kubectl 命令配置 以连接到你的 Kubernetes 集群。 AKS Automatic 群集配置为使用 Microsoft Entra ID 进行 Kubernetes 基于角色的访问控制 (RBAC)。
az aks get-credentials --resource-group $resource_group_name --name $cluster_name
使用 kubectl get 命令验证与群集的连接。
kubectl get nodes
部署应用程序
若要部署应用程序,请使用清单文件创建运行 AKS Store 应用程序所需的所有对象。 Kubernetes 清单文件定义群集的所需状态,例如,要运行哪些容器映像。 该清单包括以下 Kubernetes 部署和服务:
& lt;c2>& lt;c1>& lt;c0>& lt;sb0> Azure 商店示例架构的截图。& lt;/sb0>& lt;/c0>& lt;/c1>& lt;/c2>
- 门店:Web 应用程序,供客户查看产品和下单。
- 产品服务:显示产品信息。
- 订单服务:下单。
- Rabbit MQ:订单队列的消息队列。
注释
建议不要在没有持久性存储用于生产的情况下运行有状态容器(例如 Rabbit MQ)。 此处使用这些容器是为了简单起见,但我们建议使用托管服务,例如Azure Cosmos DB或Azure 服务总线。
创建命名空间
aks-store-demo以将 Kubernetes 资源部署到其中。kubectl create ns aks-store-demo使用 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 公开应用程序前端。 此过程可能需要几分钟才能完成。
使用 kubectl get pods 命令查看已部署的 Pod 的状态。 在继续操作之前,请确保所有 Pod 的状态为
Running。 如果这是您部署的第一个工作负载,节点自动预配可能需要几分钟才能创建用于运行 Pod 的节点池。kubectl get pods -n aks-store-demo检查应用商店前端应用程序的公共 IP 地址。 使用带有 参数的
--watch命令来监视进度。kubectl get ingress store-front -n aks-store-demo --watch服务的 ADDRESS 输出最初显示为空:
store-frontNAME CLASS HOSTS ADDRESS PORTS AGE store-front webapprouting.kubernetes.azure.com * 80 12m在 ADDRESS 从空白变为实际公共 IP 地址后,使用 停止
CTRL-C监视过程。kubectl以下示例输出显示向服务分配的有效公共 IP 地址:
NAME CLASS HOSTS ADDRESS PORTS AGE store-front webapprouting.kubernetes.azure.com * 4.255.22.196 80 12m打开 Web 浏览器以访问入口的外部 IP 地址,以查看Azure应用商店应用是否在运行中。
删除群集
如果不打算完成 AKS 教程,请清理不必要的资源以避免Azure费用。 运行 az group delete 命令以移除资源组、容器服务及所有相关资源。
az group delete --name <resource-group> --yes --no-wait
注释
AKS 群集是使用用户分配的托管标识创建的。 如果你不再需要该身份,则可以手动将其移除。
如果不再需要通过 Terraform 创建的资源,请运行 terraform 计划 并指定 destroy 标志。
terraform plan -destroy -out main.destroy.tfplan
运行 terraform apply 来应用执行计划。
terraform apply main.destroy.tfplan
后续步骤
在本快速入门中,你在自定义虚拟网络中使用 AKS Automatic 部署了专用 Kubernetes 群集,然后将一个简单的多容器应用程序部署到该群集。 此示例应用程序仅用于演示目的,并未展示出 Kubernetes 应用程序的所有最佳做法。 有关使用生产版 AKS 创建完整解决方案的指南,请参阅 AKS 解决方案指南。
由于此群集是专用的,因此其 API 服务器没有公共 IP 地址。 若要连接到和管理群集,请参阅 用于连接到专用群集的选项。 有关其他专用群集配置选项,请参阅 专用群集 文档。
若要详细了解 AKS Automatic,请继续阅读简介。