练习 - 在 AKS 群集上配置多个节点并使缩放为零
使用 Azure Kubernetes 服务可以创建不同的节点池,以便将特定工作负荷与每个节点池中运行的节点匹配。
回顾无人机跟踪示例,你的团队开发了一个新的预测建模服务,用于处理极端天气条件下的飞行路径信息并创建最佳飞行路线。 此服务需要基于 GPU 的虚拟机(VM)支持,并且仅在一周内的特定日期运行。 团队希望确保在服务未运行时不使用任何 VMS。
在这里,你将创建一个 Azure Kubernetes 服务(AKS)托管的 Kubernetes 群集。 接下来,将群集配置为支持多个节点池,并允许群集缩放节点池中的节点。 然后,添加第二个节点池,以支持具有动态节点计数的用户工作负荷。 最后,将节点计数缩放为零,以减少 AKS 群集中使用的节点的成本。
创建新的资源组
使用 Azure 帐户登录到 Azure Cloud Shell。 选择 Cloud Shell 的 Bash 版本。
你将在此模块中的所有练习中重复使用在此处创建的值。 保存输出以供将来使用。
在 Cloud Shell 窗口顶部,选择 “设置>转到经典版本”。
选择要托管资源组的区域。 后续练习中的功能并非在所有地区都可用。 出于此原因,建议使用 eastus 作为区域。 如果选择使用其他值,请更改值
REGION_NAME
。运行以下命令来注册变量:
REGION_NAME=eastus RESOURCE_GROUP=rg-akscostsaving AKS_CLUSTER_NAME=akscostsaving-$RANDOM
小窍门
可以使用“复制”按钮将命令复制到剪贴板。 要粘贴,请右键单击 Cloud Shell 终端中的新行,然后选择“粘贴”,或使用 Shift+Insert 键盘快捷方式(在 macOS 上为 ⌘+V)。
可以通过运行
echo
命令来检查每个值;例如echo $REGION_NAME
。记下你的
AKS_CLUSTER_NAME
。 在整个练习中,此值稍后用于群集的清理和配置设置。echo $AKS_CLUSTER_NAME
创建名为 rg-akscostsaving 的新资源组。 你将在这些练习中创建的所有资源都将在此资源组中进行部署。 单个资源组可在完成模块后更轻松地清理资源。
az group create \ --name $RESOURCE_GROUP \ --___location $REGION_NAME
创建 AKS 群集
创建资源组后,可以在组中创建 AKS 群集。 第一步是在所选区域中获取 Kubernetes 版本。 此版本用于配置群集。
若要获取 Kubernetes 版本,请运行
az aks get-versions
命令。 以下查询返回非预览版 Kubernetes 版本。 将该值存储在名为VERSION
.. 的 Bash 变量中。 若要检索和存储版本号,请运行以下命令:VERSION=$(az aks get-versions \ --___location $REGION_NAME \ --query "values[?isPreview==null].version | [1]" \ --output tsv) echo $VERSION
az aks create
运行命令以创建 AKS 群集。 群集在系统节点池中使用两个节点运行。 此命令可能需要几分钟才能完成。az aks create \ --resource-group $RESOURCE_GROUP \ --name $AKS_CLUSTER_NAME \ --___location $REGION_NAME \ --kubernetes-version $VERSION \ --node-count 2 \ --load-balancer-sku standard \ --vm-set-type VirtualMachineScaleSets \ --generate-ssh-keys
该
az aks create
命令具有多个参数,用于对 Kubernetes 群集进行精确配置。 在为集群配置用于缩放和多个节点池的正确支持时,有两个重要参数:参数和值 DESCRIPTION --load-balancer-sku standard
AKS 中的默认负载均衡器支持为 basic
。basic
负载均衡器在使用多个节点池时不受支持。 将该值设置为standard
。--vm-set-type VirtualMachineScaleSets
若要使用 AKS 中的缩放功能,需要虚拟机规模集。 此参数启用对规模集的支持。 请注意,在默认节点池中,使用
--node-count 2
参数配置了两个节点。 回想一下上述说明,基本系统服务在此系统节点池中运行。 为了群集操作的可靠性,生产群集至少需要使用--node-count 3
。 在本练习中,我们仅使用两个节点来考虑成本。az aks nodepool list
运行以下命令,列出新群集中的节点池:az aks nodepool list --resource-group $RESOURCE_GROUP --cluster-name $AKS_CLUSTER_NAME
下面是命令输出的示例:
[ { "agentPoolType": "VirtualMachineScaleSets", "availabilityZones": null, "count": 2, "enableAutoScaling": null, "enableNodePublicIp": false, "id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourcegroups/rg-akscostsaving/providers/Microsoft.ContainerService/managedClusters/akscostsaving-17835/agentPools/nodepool1", "mode": "System", "name": "nodepool1", ... "type": "Microsoft.ContainerService/managedClusters/agentPools", "upgradeSettings": null, "vmSize": "Standard_DS2_v2", "vnetSubnetId": null } ]
请注意,节点池的
mode
被设置为System
,并且name
已自动分配。
添加节点池
你的群集只有一个节点池。 通过运行
az aks nodepool add
命令添加第二个节点池。 运行此步骤中的命令,创建具有三个节点和名称batchprocpl
的用户节点池。 请记住,节点池名称必须以小写字母开头,并且仅包含字母数字字符。 对于 Linux 节点池,节点池名称限制为 12 个字符,Windows 节点池的 6 个字符。运行下面的命令:
az aks nodepool add \ --resource-group $RESOURCE_GROUP \ --cluster-name $AKS_CLUSTER_NAME \ --name batchprocpl \ --node-count 2
az aks nodepool list
运行以下命令,列出新群集中的新节点池:az aks nodepool list --resource-group $RESOURCE_GROUP --cluster-name $AKS_CLUSTER_NAME
下面是命令输出的示例:
[ { "agentPoolType": "VirtualMachineScaleSets", "availabilityZones": null, "count": 2, "enableAutoScaling": null, "enableNodePublicIp": false, "id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourcegroups/rg-akscostsaving/providers/Microsoft.ContainerService/managedClusters/akscostsaving-17835/agentPools/batchprocpl", "mode": "User", "name": "batchprocpl", ... "type": "Microsoft.ContainerService/managedClusters/agentPools", "upgradeSettings": { "maxSurge": null }, "vmSize": "Standard_DS2_v2", "vnetSubnetId": null }, { "agentPoolType": "VirtualMachineScaleSets", "availabilityZones": null, "count": 2, "enableAutoScaling": null, "enableNodePublicIp": false, "id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourcegroups/rg-akscostsaving/providers/Microsoft.ContainerService/managedClusters/akscostsaving-17835/agentPools/nodepool1", "mode": "System", "name": "nodepool1", ... "type": "Microsoft.ContainerService/managedClusters/agentPools", "upgradeSettings": null, "vmSize": "Standard_DS2_v2", "vnetSubnetId": null } ]
请注意,新节点池的
mode
设置为User
,并且name
是batchprocpl
。
将节点池的节点数量设置为零
az aks nodepool scale
运行命令以手动缩放节点池中的节点。
az aks nodepool scale
运行命令并使用--node-count
参数将节点计数值设置为 0。
下面是命令的示例:
az aks nodepool scale \
--resource-group $RESOURCE_GROUP \
--cluster-name $AKS_CLUSTER_NAME \
--name batchprocpl \
--node-count 0
下面是命令输出的示例:
{
"agentPoolType": "VirtualMachineScaleSets",
"availabilityZones": null,
"count": 0,
"enableAutoScaling": null,
"enableNodePublicIp": false,
"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourcegroups/rg-akscostsaving/providers/Microsoft.ContainerService/managedClusters/akscostsaving-17835/agentPools/batchprocpl",
"maxCount": null,
"maxPods": 110,
"minCount": null,
"mode": "User",
"name": "batchprocpl",
"nodeImageVersion": "AKSUbuntu-1604-2020.06.10",
"nodeLabels": null,
"nodeTaints": null,
"orchestratorVersion": "1.17.9",
"osDiskSizeGb": 128,
"osType": "Linux",
"provisioningState": "Succeeded",
"proximityPlacementGroupId": null,
"resourceGroup": "rg-akscostsaving",
"scaleSetEvictionPolicy": null,
"scaleSetPriority": null,
"spotMaxPrice": null,
"tags": null,
"type": "Microsoft.ContainerService/managedClusters/agentPools",
"upgradeSettings": {
"maxSurge": null
},
"vmSize": "Standard_DS2_v2",
"vnetSubnetId": null
}
请注意,节点池 count
参数值设置为 0,值 enableAutoScaling
设置为 null
。 若要计划工作负荷,必须手动增加此节点池的节点计数,因为默认情况下不会自动创建节点。
配置 Kubernetes 上下文
在上一命令的输出中,节点池计数设置为 0。 可以通过运行 kubectl get nodes
命令来确认群集中的可用节点。
运行
kubectl
以与群集的 API 服务器交互。 必须配置 Kubernetes 群集上下文以允许kubectl
连接。 上下文包含群集的地址、用户和命名空间。 运行以下命令az aks get-credentials
,在 Cloud Shell 中配置 Kubernetes 上下文。通过运行以下命令检索群集凭据:
az aks get-credentials \ --resource-group $RESOURCE_GROUP \ --name $AKS_CLUSTER_NAME
下面是命令输出的示例。
Merged "akscostsaving-17835" as current context in /home/user/.kube/config
列出节点池中的节点。
kubectl get nodes
下面是命令输出的示例:
NAME STATUS ROLES AGE VERSION aks-nodepool1-37990379-vmss000000 Ready agent 32m v1.17.9 aks-nodepool1-37990379-vmss000001 Ready agent 32m v1.17.9
请注意,尽管
az aks nodepool list
命令列出了两个节点池,但群集中只有两个可用节点,两个节点都来自nodepool1
。
若要在直接管理工作负载需求时优化 AKS 的成本,一种有效的策略是:
- 手动调整节点池中的节点数量。
- 将基于 NV 的成本较高的用户节点池缩放到零。
让我们看看需要扩展节点但不能直接控制需求的策略。