快速入門:使用ARM範本部署 Azure Kubernetes Service (AKS) 叢集

Azure Kubernetes Service (AKS) 是受控 Kubernetes 服務,可讓您快速部署及管理叢集。 在本快速入門中,您將:

  • 使用 Azure Resource Manager 樣本部署 AKS 叢集。
  • 使用一組微服務和 Web 前端來模擬零售案例,執行範例多容器應用程式。

Azure Resource Manager 範本是 JavaScript 物件表示法 (JSON) 檔案,可定義專案的基礎結構和組態。 範本使用宣告式語法。 您不需要撰寫程式設計命令順序來建立部署,即可描述預定的部署。

注意

若要開始快速佈建 AKS 叢集,本文包含僅針對評估目的部署具有預設設定值之叢集的步驟。 在部署生產就緒叢集之前,建議您先熟悉我們的基準參考架構,考慮其如何符合您的業務需求。

開始之前

本文假設對 Kubernetes 概念有基本的瞭解。 如需詳細資訊,請參閱 Azure Kubernetes Services (AKS) 的 Kubernetes 核心概念

  • 如果您沒有 Azure 訂閱,請在開始之前,先建立 Azure 免費帳戶

  • 請確定您用來建立叢集的身分識別具有適當的最低許可權。 如需 AKS 存取和身分識別的詳細資訊,請參閱 Azure Kubernetes Service (AKS) 的存取和身分識別選項。

  • 若要部署 ARM 範本,您需要在您要部署的資源上寫入存取權,以及存取資源類型上所有作業的 Microsoft.Resources/deployments 存取權。 例如,若要部署虛擬機器,您需要 Microsoft.Compute/virtualMachines/writeMicrosoft.Resources/deployments/* 權限。 如需角色與權限的清單,請參閱 Azure 內建角色

從範本部署叢集之後,您可以使用 Azure CLI 或 Azure PowerShell 來連線到叢集並部署範例應用程式。

本文需要 Azure CLI 2.0.64 版或更新版本。 若您使用的是 Azure Cloud Shell,即已安裝最新版本。

建立 SSH 金鑰組

若要使用ARM樣本建立 AKS 叢集,請提供SSH 公鑰。 如果您需要此資源,請遵循本節中的步驟。 否則,請跳至 [ 檢閱範本 ] 區段。

若要存取 AKS 節點,您可以使用 SSH 金鑰組連線(公用和私人)。 若要建立 SSH 金鑰群組:

  1. 移至 https://shell.azure.com,並在您的瀏覽器中開啟 Cloud Shell。

  2. 使用 az sshkey create 命令或 ssh-keygen 命令建立 SSH 金鑰組。

    # Create an SSH key pair using Azure CLI
    az sshkey create --name "mySSHKey" --resource-group "myResourceGroup"
    
    # or
    
    # Create an SSH key pair using ssh-keygen
    ssh-keygen -t rsa -b 4096
    
  3. 若要部署範本,您必須從 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 金鑰。

檢閱範本

本快速入門中使用的範本是來自 Azure 快速入門範本

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "metadata": {
    "_generator": {
      "name": "bicep",
      "version": "0.26.170.59819",
      "templateHash": "14823542069333410776"
    }
  },
  "parameters": {
    "clusterName": {
      "type": "string",
      "defaultValue": "aks101cluster",
      "metadata": {
        "description": "The name of the Managed Cluster resource."
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "The location of the Managed Cluster resource."
      }
    },
    "dnsPrefix": {
      "type": "string",
      "metadata": {
        "description": "Optional DNS prefix to use with hosted Kubernetes API server FQDN."
      }
    },
    "osDiskSizeGB": {
      "type": "int",
      "defaultValue": 0,
      "minValue": 0,
      "maxValue": 1023,
      "metadata": {
        "description": "Disk size (in GB) to provision for each of the agent pool nodes. This value ranges from 0 to 1023. Specifying 0 will apply the default disk size for that agentVMSize."
      }
    },
    "agentCount": {
      "type": "int",
      "defaultValue": 3,
      "minValue": 1,
      "maxValue": 50,
      "metadata": {
        "description": "The number of nodes for the cluster."
      }
    },
    "agentVMSize": {
      "type": "string",
      "defaultValue": "standard_d2s_v3",
      "metadata": {
        "description": "The size of the Virtual Machine."
      }
    },
    "linuxAdminUsername": {
      "type": "string",
      "metadata": {
        "description": "User name for the Linux Virtual Machines."
      }
    },
    "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 azureuser@linuxvm'"
      }
    }
  },
  "resources": [
    {
      "type": "Microsoft.ContainerService/managedClusters",
      "apiVersion": "2024-02-01",
      "name": "[parameters('clusterName')]",
      "location": "[parameters('location')]",
      "identity": {
        "type": "SystemAssigned"
      },
      "properties": {
        "dnsPrefix": "[parameters('dnsPrefix')]",
        "agentPoolProfiles": [
          {
            "name": "agentpool",
            "osDiskSizeGB": "[parameters('osDiskSizeGB')]",
            "count": "[parameters('agentCount')]",
            "vmSize": "[parameters('agentVMSize')]",
            "osType": "Linux",
            "mode": "System"
          }
        ],
        "linuxProfile": {
          "adminUsername": "[parameters('linuxAdminUsername')]",
          "ssh": {
            "publicKeys": [
              {
                "keyData": "[parameters('sshRSAPublicKey')]"
              }
            ]
          }
        }
      }
    }
  ],
  "outputs": {
    "controlPlaneFQDN": {
      "type": "string",
      "value": "[reference(resourceId('Microsoft.ContainerService/managedClusters', parameters('clusterName')), '2024-02-01').fqdn]"
    }
  }
}

ARM 範本中定義的資源類型是 Microsoft.ContainerService/managedClusters

如需更多 AKS 範例,請參閱 AKS 快速入門範本 網站。

部署範本

  1. 選取 [部署至 Azure ] 以登入並開啟範本。

    Button to deploy the Resource Manager template to Azure.

  2. 在 [基本] 頁面上,保留 OS 磁碟大小 GB、代理程式計數代理程式 VM 大小OS 類型的預設值,並設定下列範本參數:

    • 訂用帳戶:選取 Azure 訂用帳戶。
    • 資源群組:選取 [ 新建]。 輸入資源群組的唯一名稱,例如 myResourceGroup,然後選取 [ 確定]。
    • 位置:選取位置,例如 美國東部。
    • 叢集名稱:輸入 AKS 叢集的唯一名稱,例如 myAKSCluster
    • DNS 前置詞:輸入叢集的唯一 DNS 前置詞,例如 myakscluster
    • Linux 管理員 用戶名稱:輸入用戶名稱以使用 SSH 進行連線,例如 azureuser
    • SSH 公鑰來源:選取 [使用現有的公鑰]。
    • 密鑰組名稱:複製並貼上 SSH 金鑰組的公用部分(根據預設,~/.ssh/id_rsa.pub 的內容)。
  3. 選取 [檢閱 + 建立]>[建立]

建立 AKS 叢集需要幾分鐘的時間。 等候叢集成功部署,再繼續進行下一個步驟。

連線至叢集

若要管理 Kubernetes 叢集,請使用 Kubernetes 命令行用戶端 kubectl

如果您使用 Azure Cloud Shell,則 kubectl 已安裝。 若要在本機安裝和執行 kubectl ,請呼叫 az aks install-cli 命令。

  1. 設定 kubectl 以使用 az aks get-credentials 命令連線到 Kubernetes 叢集。 此命令會下載認證,並設定 Kubernetes CLI 來使用這些認證。

    az aks get-credentials --resource-group myResourceGroup --name myAKSCluster
    
  2. 使用 kubectl get 命令確認叢集的連線。 此命令會傳回叢集節點的清單。

    kubectl get nodes
    

    下列範例輸出顯示先前步驟中建立的三個節點。 請確定節點狀態為 [ 就緒]。

    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
    

部署應用程式

若要部署應用程式,您可以使用指令清單檔案來建立執行 AKS 市集應用程式所需的所有物件。 Kubernetes 指令清單檔案會定義叢集所需的狀態,例如要執行的容器映像。 指令清單包含下列 Kubernetes 部署和服務:

Screenshot of Azure Store sample architecture.

  • 市集前端:供客戶檢視產品和下單的 Web 應用程式。
  • 產品服務:顯示產品資訊。
  • 訂單服務:下單。
  • Rabbit MQ:訂單佇列的訊息佇列。

注意

不建議執行具狀態容器,例如 Rabbit MQ,而不需持續儲存以供生產環境使用。 為了簡單起見,建議您使用受控服務,例如 Azure CosmosDB 或 Azure 服務匯流排。

  1. 在下列指令清單中建立名為 aks-store-quickstart.yaml 並複製的檔案:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: rabbitmq
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: rabbitmq
      template:
        metadata:
          labels:
            app: rabbitmq
        spec:
          nodeSelector:
            "kubernetes.io/os": linux
          containers:
          - name: rabbitmq
            image: mcr.microsoft.com/mirror/docker/library/rabbitmq:3.10-management-alpine
            ports:
            - containerPort: 5672
              name: rabbitmq-amqp
            - containerPort: 15672
              name: rabbitmq-http
            env:
            - name: RABBITMQ_DEFAULT_USER
              value: "username"
            - name: RABBITMQ_DEFAULT_PASS
              value: "password"
            resources:
              requests:
                cpu: 10m
                memory: 128Mi
              limits:
                cpu: 250m
                memory: 256Mi
            volumeMounts:
            - name: rabbitmq-enabled-plugins
              mountPath: /etc/rabbitmq/enabled_plugins
              subPath: enabled_plugins
          volumes:
          - name: rabbitmq-enabled-plugins
            configMap:
              name: rabbitmq-enabled-plugins
              items:
              - key: rabbitmq_enabled_plugins
                path: enabled_plugins
    ---
    apiVersion: v1
    data:
      rabbitmq_enabled_plugins: |
        [rabbitmq_management,rabbitmq_prometheus,rabbitmq_amqp1_0].
    kind: ConfigMap
    metadata:
      name: rabbitmq-enabled-plugins            
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: rabbitmq
    spec:
      selector:
        app: rabbitmq
      ports:
        - name: rabbitmq-amqp
          port: 5672
          targetPort: 5672
        - name: rabbitmq-http
          port: 15672
          targetPort: 15672
      type: ClusterIP
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: order-service
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: order-service
      template:
        metadata:
          labels:
            app: order-service
        spec:
          nodeSelector:
            "kubernetes.io/os": linux
          containers:
          - name: order-service
            image: ghcr.io/azure-samples/aks-store-demo/order-service:latest
            ports:
            - containerPort: 3000
            env:
            - name: ORDER_QUEUE_HOSTNAME
              value: "rabbitmq"
            - name: ORDER_QUEUE_PORT
              value: "5672"
            - name: ORDER_QUEUE_USERNAME
              value: "username"
            - name: ORDER_QUEUE_PASSWORD
              value: "password"
            - name: ORDER_QUEUE_NAME
              value: "orders"
            - name: FASTIFY_ADDRESS
              value: "0.0.0.0"
            resources:
              requests:
                cpu: 1m
                memory: 50Mi
              limits:
                cpu: 75m
                memory: 128Mi
          initContainers:
          - name: wait-for-rabbitmq
            image: busybox
            command: ['sh', '-c', 'until nc -zv rabbitmq 5672; do echo waiting for rabbitmq; sleep 2; done;']
            resources:
              requests:
                cpu: 1m
                memory: 50Mi
              limits:
                cpu: 75m
                memory: 128Mi    
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: order-service
    spec:
      type: ClusterIP
      ports:
      - name: http
        port: 3000
        targetPort: 3000
      selector:
        app: order-service
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: product-service
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: product-service
      template:
        metadata:
          labels:
            app: product-service
        spec:
          nodeSelector:
            "kubernetes.io/os": linux
          containers:
          - name: product-service
            image: ghcr.io/azure-samples/aks-store-demo/product-service:latest
            ports:
            - containerPort: 3002
            resources:
              requests:
                cpu: 1m
                memory: 1Mi
              limits:
                cpu: 1m
                memory: 7Mi
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: product-service
    spec:
      type: ClusterIP
      ports:
      - name: http
        port: 3002
        targetPort: 3002
      selector:
        app: product-service
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: store-front
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: store-front
      template:
        metadata:
          labels:
            app: store-front
        spec:
          nodeSelector:
            "kubernetes.io/os": linux
          containers:
          - name: store-front
            image: ghcr.io/azure-samples/aks-store-demo/store-front:latest
            ports:
            - containerPort: 8080
              name: store-front
            env: 
            - name: VUE_APP_ORDER_SERVICE_URL
              value: "http://order-service:3000/"
            - name: VUE_APP_PRODUCT_SERVICE_URL
              value: "http://product-service:3002/"
            resources:
              requests:
                cpu: 1m
                memory: 200Mi
              limits:
                cpu: 1000m
                memory: 512Mi
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: store-front
    spec:
      ports:
      - port: 80
        targetPort: 8080
      selector:
        app: store-front
      type: LoadBalancer
    

    如需 YAML 指令清單檔案的明細,請參閱 部署和 YAML 指令清單

    如果您在本機建立並儲存 YAML 檔案,則可以選取 [上傳/下載檔案] 按鈕,然後從本機文件系統中選取檔案,將指令清單檔案上傳至您的默認目錄。

  2. 使用 kubectl apply 命令部署應用程式,並指定 YAML 指令清單的名稱。

    kubectl apply -f aks-store-quickstart.yaml
    

    下列範例輸出顯示部署和服務:

    deployment.apps/rabbitmq 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
    

測試應用程式

  1. 使用 kubectl get pods 命令檢查已部署 Pod 的狀態。 讓所有 Pod 都在 Running 繼續之前。

    kubectl get pods
    
  2. 檢查存放區前端應用程式的公用IP位址。 使用 kubectl get service 命令搭配 自變數來 --watch 監視進度。

    kubectl get service store-front --watch
    

    store-front 服務的 EXTERNAL-IP 輸出一開始會顯示為擱置

    NAME          TYPE           CLUSTER-IP    EXTERNAL-IP   PORT(S)        AGE
    store-front   LoadBalancer   10.0.100.10   <pending>     80:30025/TCP   4h4m
    
  3. 當 EXTERNAL-IP 位址從擱置變更為實際的公用 IP 地址之後,請使用 CTRL-C 來停止kubectl監看程式。

    下列範例輸出顯示指派給服務的有效公用 IP 位址:

    NAME          TYPE           CLUSTER-IP    EXTERNAL-IP    PORT(S)        AGE
    store-front   LoadBalancer   10.0.100.10   20.62.159.19   80:30025/TCP   4h5m
    
  4. 開啟網頁瀏覽器至您服務的外部IP位址,以查看 Azure 市集應用程式的運作情形。

    Screenshot of AKS Store sample application.

選取叢集

如果您不打算進行 AKS 教學課程,請清除不必要的資源以避免 Azure 費用。

呼叫 az group delete 命令,以移除資源群組、容器服務和所有相關資源。

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

注意

AKS 叢集是使用系統指派的受控識別建立的,這是本快速入門中使用的預設身分識別選項。 平臺會管理此身分識別,因此您不需要手動移除它。

下一步

在本快速入門中,您已部署 Kubernetes 叢集,接著將簡單多容器應用程式部署到此叢集。 此範例應用程式僅供示範之用,並不代表 Kubernetes 應用程式的所有最佳做法。 如需針對生產使用 AKS 建立完整解決方案的指引,請參閱 AKS 解決方案指引

若要深入瞭解 AKS 並逐步解說完整的程式代碼到部署範例,請繼續進行 Kubernetes 叢集教學課程。