快速入门:使用 Azure 资源管理器 模板部署 Kubernetes 群集

适用于:Azure Stack HCI 版本 23H2

本快速入门介绍如何使用 Azure 资源管理器模板在 AKS Arc 中部署 Kubernetes 群集。 Azure Arc 将 Azure 管理功能扩展到任何位置的 Kubernetes 群集,从而提供统一的方法来管理不同的环境。

准备阶段

本文假定你对 Kubernetes 概念有基本的了解。

若要部署资源管理器模板,需要对要部署的资源具有写入访问权限,以及对 Microsoft.Resources/deployments 资源类型的所有操作的访问权限。 例如,若要部署虚拟机,需要 Microsoft.Compute/virtualMachines/writeMicrosoft.Resources/deployments/* 权限。 有关角色和权限的列表,请参阅 Azure 内置角色

先决条件

  • 具有活动订阅的 Azure 帐户。
  • Azure Stack HCI 23H2 群集。
  • 最新的 Azure CLI 版本。

步骤 1:准备 Azure 帐户

  1. 登录到 Azure:打开终端或命令提示符,并使用 Azure CLI 登录到 Azure 帐户:

    az login
    
  2. 设置订阅:将 替换为 <your-subscription-id> 订阅 ID:

    az account set --subscription "<your-subscription-id>"
    

步骤 2:使用 Azure CLI 创建 SSH 密钥对

az sshkey create --name "mySSHKey" --resource-group "myResourceGroup"

或者,使用 ssh-keygen 创建 SSH 密钥对

ssh-keygen -t rsa -b 4096

若要部署模板,必须提供 SSH 对中的公钥。 若要检索公钥,请使用 az sshkey show 命令:

az sshkey show --name "mySSHKey" --resource-group "myResourceGroup" --query "publicKey"

默认情况下,SSH 密钥文件在 ~/.ssh 目录中创建。 az sshkey create运行 或 ssh-keygen 命令以覆盖同名的任何现有 SSH 密钥对。

有关创建 SSH 密钥的详细信息,请参阅在 Azure 中创建和管理用于身份验证的 SSH 密钥

步骤 3:查看模板

本快速入门中使用的模板来自 Azure 快速入门模板存储库:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
      "provisionedClusterName": {
          "type": "string",
          "defaultValue": "aksarc-armcluster",
          "metadata": {
              "description": "The name of the AKS Arc Cluster resource."
          }
      },
      "location": {
          "type": "string",
          "defaultValue": "eastus",
          "metadata": {
              "description": "The location of the AKS Arc Cluster resource."
          }
      },
      "resourceTags": {
            "type": "object",
            "defaultValue": {}
        },
      "sshRSAPublicKey": {
          "type": "string",
          "metadata": {
              "description": "Configure all linux machines with the SSH RSA public key string. Your key should include three parts, for example 'ssh-rsa AAAAB...snip...UcyupgH '"
          }
      },
       "enableAHUB": {
            "type": "string",
            "defaultValue": "NotApplicable",
            "metadata": {
                "description": "Azure Hybrid Benefit for Windows Server licenses. NotApplicable, True, False."
            }
        },
       "agentName": {
              "type": "string",
              "defaultValue": "nodepool",
              "metadata": {
                  "description": "The name of the node pool."
              }
          },
        "agentVMSize": {
            "type": "string",
            "defaultValue": "Standard_A4_v2",
            "metadata": {
                  "description": "The VM size for node pools."
            }
        },
        "agentCount": {
              "type": "int",
              "defaultValue": 1,
              "minValue": 1,
              "maxValue": 50,
              "metadata": {
                  "description": "The number of nodes for the cluster."
              }
          },
          "agentOsType": {
              "type": "string",
              "defaultValue": "Linux",
              "metadata": {
                  "description": "The OS Type for the agent pool. Values are Linux and Windows."
              }
          },
         "loadBalancerCount": {
            "type": "int",
            "defaultValue": 0,
            "metadata": {
                "description": "The number of load balancers."
            }
        },
          "kubernetesVersion": {
              "type": "string",
              "metadata": {
                  "description": "The version of Kubernetes."
              }
          },
          "controlPlaneNodeCount": {
              "type": "int",
              "defaultValue": 1,
              "minValue": 1,
              "maxValue": 5,
              "metadata": {
                  "description": "The number of control plane nodes for the cluster."
              }
          },
          "controlPlaneIp": {
            "type": "string",
            "defaultValue": "<default_value>",
              "metadata": {
                  "description": "Control plane IP address."
              }
         },
          "controlPlaneVMSize": {
              "type": "string",
              "defaultValue": "Standard_A4_v2",
              "metadata": {
                  "description": "The VM size for control plane."
              }
          },
          "vnetSubnetIds": {
              "type": "array",
              "metadata": {
                  "description": "List of subnet Ids for the AKS cluster."
              }
          },
          "podCidr": {
            "type": "string",
            "defaultValue": "10.244.0.0/16",
            "metadata": {
                  "description": "The VM size for control plane."
              }
          },
          "networkPolicy": {
            "type": "string",
            "defaultValue": "calico",
            "metadata": {
                  "description": "Network policy to use for Kubernetes pods. Only options supported is calico."
              }
          },
          "customLocation": {
            "type": "string",
            "metadata": {
                  "description": "Fully qualified custom location resource Id."
              }
          }
      },
      "resources": [
      {
          "apiVersion": "2024-01-01",
          "type": "Microsoft.Kubernetes/ConnectedClusters",
          "kind": "ProvisionedCluster",
          "location": "[parameters('location')]",
          "name": "[parameters('provisionedClusterName')]",
          "tags": "[parameters('resourceTags')]",
          "identity": {
              "type": "SystemAssigned"
          },
          "properties": {
              "agentPublicKeyCertificate":"" ,
              "aadProfile": {
                  "enableAzureRBAC": false
              }
          }
      },
      {
          "apiVersion": "2024-01-01",
          "type": "microsoft.hybridcontainerservice/provisionedclusterinstances",
          "name": "default",
          "scope": "[concat('Microsoft.Kubernetes/ConnectedClusters', '/', parameters('provisionedClusterName'))]",
          "dependsOn": [
              "[resourceId('Microsoft.Kubernetes/ConnectedClusters', parameters('provisionedClusterName'))]"
          ],
          "properties": {
          "agentPoolProfiles": [
            {
              "count": "[parameters('agentCount')]",
              "name":"[parameters('agentName')]",
              "osType": "[parameters('agentOsType')]",
              "vmSize": "[parameters('agentVMSize')]"
            }
          ],
          "cloudProviderProfile": {
            "infraNetworkProfile": {
                  "vnetSubnetIds": "[parameters('vnetSubnetIds')]"
            }
          },
          "controlPlane": {
            "count": "[parameters('controlPlaneNodeCount')]",
            "controlPlaneEndpoint": {
                        "hostIP": "[parameters('controlPlaneIp')]"
                    },
            "vmSize": "[parameters('controlPlaneVMSize')]"
          },
         "licenseProfile": {
            "azureHybridBenefit": "[parameters('enableAHUB')]"
         },
          "kubernetesVersion": "[parameters('kubernetesVersion')]",
          "linuxProfile": {
            "ssh": {
              "publicKeys": [
                {
                  "keyData": "[parameters('sshRSAPublicKey')]"
                }
              ]
            }
          },
        "networkProfile": {
          "loadBalancerProfile": {
            "count": "[parameters('loadBalancerCount')]"
          },
          "networkPolicy": "[parameters('networkPolicy')]",
          "podCidr": "[parameters('podCidr')]"
        },
        "storageProfile": {
          "nfsCsiDriver": {
            "enabled": false
          },
          "smbCsiDriver": {
            "enabled": false
          }
        }
        },
        "extendedLocation": {
            "name": "[parameters('customLocation')]",
            "type": "CustomLocation"
        }
      }
    ]
  }

步骤 4:部署模板

若要部署模板,请运行以下命令以部署 Kubernetes 群集:

az deployment group create \
--name "<deployment-name>" \
--resource-group "<resource-group-name>" \
--template-uri "https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/quickstarts/microsoft.kubernetes/aks-arc/azuredeploy.json" \
--parameters provisionedClusterName="<cluster-name> location="eastus" sshRSApublicKey="" etc..."

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

步骤 5:验证部署

部署完成后,使用以下命令验证 Kubernetes 群集是否已启动并运行:

az aksarc show --resource-group "<resource-group-name>" --name "<cluster-name>" --output table

步骤 6:连接到群集

  1. 若要连接到群集,请 az connectedk8s proxy 运行 命令。 命令在客户端计算机上下载并运行代理二进制文件,并提取与群集关联的 kubeconfig 文件:

    az connectedk8s proxy --name <cluster name> -g <resource group>
    

    或者,使用 Kubernetes 命令行客户端 kubectl。 如果使用 Azure Cloud Shell,则已安装 kubectl。 若要在本地安装并运行 kubectl ,请 az aksarc install-cli 运行 命令。

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

    az aksarc get-credentials --resource-group "<resource-group-name>" --name "<cluster-name>"
    
  2. 使用 kubectl get 命令验证与群集之间的连接。 此命令返回群集节点的列表:

    kubectl get nodes -A --kubeconfig .\<path to kubecofig> 
    

    以下示例输出显示了在前面的步骤中创建的三个节点。 确保节点状态为 Ready

    NAME                                STATUS   ROLES   AGE   VERSION
    aks-agentpool-27442051-vmss000000   Ready    agent   10m   v1.27.7
    aks-agentpool-27442051-vmss000001   Ready    agent   10m   v1.27.7
    aks-agentpool-27442051-vmss000002   Ready    agent   11m   v1.27.7
    

模板资源

connectedClusters

名称 说明
type 资源类型。 Microsoft.Kubernetes/ConnectedClusters
apiVersion 资源 API 版本。 2024-01-01
name 资源名称。 需要字符串 ()
字符限制:1-63
有效字符:字母数字、下划线和连字符。
以字母数字开头和结尾。
location 资源所在的地理位置。 需要字符串 () 。
tags 资源标记。 标记名称和值的字典。 请参阅 模板中的标记
extendedLocation 虚拟机的扩展位置。 ExtendedLocation
identity 已连接群集的标识(如果已配置)。
properties 已连接群集的属性。

ProvisionedClusterInstances

名称 说明
type 资源类型 microsoft.hybridcontainerservice/provisionedclusterinstances
apiVersion 资源 API 版本 2024-01-01
name 资源名称 需要字符串 () 。 不要将此更改为 默认值
properties 已连接群集的属性。
extendedLocation 群集的扩展位置。 ExtendedLocation

ExtendedLocation

名称 说明
name 扩展位置的 ID。 字符串
type 扩展位置的类型。 CustomLocation

后续步骤

AKS Arc 概述