快速入門:使用 Azure 入口網站部署 Azure Kubernetes Service (AKS) 叢集Quickstart: Deploy an Azure Kubernetes Service (AKS) cluster using the Azure portal

Azure Kubernetes Service (AKS) 是受控 Kubernetes 服務,可讓您快速部署及管理叢集。Azure Kubernetes Service (AKS) is a managed Kubernetes service that lets you quickly deploy and manage clusters. 在本快速入門中,您會使用 Azure 入口網站來部署 AKS 叢集。In this quickstart, you deploy an AKS cluster using the Azure portal. 在叢集上執行包含 Web 前端和 Redis 執行個體的多容器應用程式。A multi-container application that includes a web front end and a Redis instance is run in the cluster. 然後,您會了解如何監視叢集的健康情況和執行您應用程式的 Pod。You then see how to monitor the health of the cluster and pods that run your application.

瀏覽至 Azure 投票應用程式範例的影像

本快速入門假設您已有 Kubernetes 概念的基本知識。This quickstart assumes a basic understanding of Kubernetes concepts. 如需詳細資訊,請參閱 Azure Kubernetes Services (AKS) 的 Kubernetes 核心概念For more information, see Kubernetes core concepts for Azure Kubernetes Service (AKS).

如果您沒有 Azure 訂用帳戶,請在開始前建立 免費帳戶If you don't have an Azure subscription, create a free account before you begin.

登入 AzureSign in to Azure

https://portal.azure.com 登入 Azure 入口網站。Sign in to the Azure portal at https://portal.azure.com.

建立 AKS 叢集Create an AKS cluster

在 Azure 入口網站的左上角,選取 [+ 建立資源] > [容器] > [Kubernetes 服務] 。In the top left-hand corner of the Azure portal, select + Create a resource > Containers > Kubernetes Service.

若要建立 AKS 叢集,請完成下列步驟:To create an AKS cluster, complete the following steps:

  1. 在 [基本資料] 頁面上設定下列選項:On the Basics page, configure the following options:

    • 專案詳細資料 :選取 Azure 訂用帳戶,然後選取或建立 Azure 資源群組,例如 myResourceGroupPROJECT DETAILS: Select an Azure subscription, then select or create an Azure resource group, such as myResourceGroup. 輸入 Kubernetes 叢集名稱,例如 myAKSCluster 。Enter a Kubernetes cluster name, such as myAKSCluster.

    • 叢集詳細資料 :選取 AKS 叢集的區域、Kubernetes 版本及 DNS 名稱前置詞。CLUSTER DETAILS: Select a region, Kubernetes version, and DNS name prefix for the AKS cluster.

    • 主要節點集區:選取 AKS 節點的 VM 大小。PRIMARY NODE POOL: select a VM size for the AKS nodes. VM 大小無法在 AKS 叢集部署完畢後變更。The VM size cannot be changed once an AKS cluster has been deployed.

      • 選取要部署到叢集的節點數目。Select the number of nodes to deploy into the cluster. 本快速入門將 [節點計數] 設為「1」 。For this quickstart, set Node count to 1. 節點計數可以在叢集部署完畢後調整。Node count can be adjusted after the cluster has been deployed.

      建立 AKS 叢集 - 提供基本資訊

      完成時,選取 [下一步: 調整]。Select Next: Scale when complete.

  2. 在 [調整] 頁面上,保留預設選項。On the Scale page, keep the default options. 按一下畫面底部的 [下一步: 驗證] 。At the bottom of the screen, click Next:Authentication.

  3. 在 [驗證] 頁面上,設定下列選項:On the Authentication page, configure the following options:

    • 讓 [服務主體] 欄位保留 (新的) 預設服務主體,以建立新的服務主體。Create a new service principal by leaving the Service Principal field with (new) default service principal. 您也可以選擇 [設定服務主體] 以使用現有服務主體。Or you can choose Configure service principal to use an existing one. 如果您使用現有服務主體,您必須提供 SPN 用戶端識別碼和祕密。If you use an existing one, you will need to provide the SPN client ID and secret.
    • 啟用 Kubernetes 角色型存取控制 (RBAC) 的選項。Enable the option for Kubernetes role-based access controls (RBAC). 這會針對 AKS 叢集中部署的 Kubernetes 資源提供更細部的存取控管。This will provide more fine-grained control over access to the Kubernetes resources deployed in your AKS cluster.

    根據預設,會使用「基本」 網路,並啟用適用於容器的 Azure 監視器。By default, Basic networking is used, and Azure Monitor for containers is enabled. 按一下 [檢閱 + 建立] ,然後在驗證完成時按一下 [建立] 。Click Review + create and then Create when validation completes.

建立 AKS 叢集需要幾分鐘的時間。It takes a few minutes to create the AKS cluster. 完成部署時,請按一下 [移至資源] ,或瀏覽至 AKS 叢集資源群組 (例如 myResourceGroup ),然後選取 AKS 資源 (例如 myAKSCluster )。When your deployment is complete, click Go to resource, or browse to the AKS cluster resource group, such as myResourceGroup, and select the AKS resource, such as myAKSCluster. AKS 叢集儀表板隨即顯示,如此範例所示:The AKS cluster dashboard is shown, as in this example:

Azure 入口網站中的 AKS 儀表板範例

連接到叢集Connect to the cluster

若要管理 Kubernetes 叢集,請使用 Kubernetes 命令列用戶端:kubectlTo manage a Kubernetes cluster, you use kubectl, the Kubernetes command-line client. kubectl 用戶端會預先安裝在 Azure Cloud Shell 中。The kubectl client is pre-installed in the Azure Cloud Shell.

使用 Azure 入口網站頂端的 >_ 按鈕開啟 Cloud Shell。Open Cloud Shell using the >_ button on the top of the Azure portal.

在入口網站中開啟 Azure Cloud Shell

若要設定 kubectl 以連線到 Kubernetes 叢集,請使用 az aks get-credentials 命令。To configure kubectl to connect to your Kubernetes cluster, use the az aks get-credentials command. 此命令會下載憑證並設定 Kubernetes CLI 以供使用。This command downloads credentials and configures the Kubernetes CLI to use them. 下列範例會針對 myResourceGroup 資源群組中的叢集名稱 myAKSCluster 取得認證:The following example gets credentials for the cluster name myAKSCluster in the resource group named myResourceGroup:

az aks get-credentials --resource-group myResourceGroup --name myAKSCluster

若要驗證叢集的連線,請使用 kubectl get 命令來傳回叢集節點的清單。To verify the connection to your cluster, use the kubectl get command to return a list of the cluster nodes.

kubectl get nodes

下列輸出範例會顯示上一個步驟中建立的單一節點。The following example output shows the single node created in the previous steps. 請確定節點的狀態為 ReadyMake sure that the status of the node is Ready:

NAME                       STATUS    ROLES     AGE       VERSION
aks-agentpool-14693408-0   Ready     agent     15m       v1.11.5

執行應用程式Run the application

Kubernetes 資訊清單檔會定義所需的叢集狀態,例如要執行哪些容器映像。A Kubernetes manifest file defines a desired state for the cluster, such as what container images to run. 在本教學課程中,資訊清單可用來建立執行 Azure 投票應用程式所需的所有物件。In this quickstart, a manifest is used to create all objects needed to run the Azure Vote application. 此資訊清單包含兩個 Kubernetes 部署:一個適用於範例 Azure 投票 Python 應用程式,而另一個適用於 Redis 執行個體。This manifest includes two Kubernetes deployments - one for the sample Azure Vote Python applications, and the other for a Redis instance. 還會建立兩個 Kubernetes 服務:內部服務用於 Redis 執行個體,而外部服務用於從網際網路存取 Azure 投票應用程式。Two Kubernetes Services are also created - an internal service for the Redis instance, and an external service to access the Azure Vote application from the internet.

提示

在本快速入門中,您會以手動方式建立應用程式資訊清單,並將其部署至 AKS 叢集。In this quickstart, you manually create and deploy your application manifests to the AKS cluster. 在更貼近現實的案例中,您可以使用 Azure Dev Spaces 快速地逐一查看程式碼,並直接在 AKS 叢集中進行偵錯。In more real-world scenarios, you can use Azure Dev Spaces to rapidly iterate and debug your code directly in the AKS cluster. 您可以跨作業系統平台和開發環境來使用 Dev Spaces,並與小組中的其他人一起工作。You can use Dev Spaces across OS platforms and development environments, and work together with others on your team.

在 Cloud Shell 中,使用 nanovi 建立名為 azure-vote.yaml 的檔案,然後將下列 YAML 定義複製進來:In the cloud shell, use nano or vi to create a file named azure-vote.yaml and copy in the following YAML definition:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: azure-vote-back
spec:
  replicas: 1
  selector:
    matchLabels:
      app: azure-vote-back
  template:
    metadata:
      labels:
        app: azure-vote-back
    spec:
      nodeSelector:
        "beta.kubernetes.io/os": linux
      containers:
      - name: azure-vote-back
        image: redis
        resources:
          requests:
            cpu: 100m
            memory: 128Mi
          limits:
            cpu: 250m
            memory: 256Mi
        ports:
        - containerPort: 6379
          name: redis
---
apiVersion: v1
kind: Service
metadata:
  name: azure-vote-back
spec:
  ports:
  - port: 6379
  selector:
    app: azure-vote-back
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: azure-vote-front
spec:
  replicas: 1
  selector:
    matchLabels:
      app: azure-vote-front
  template:
    metadata:
      labels:
        app: azure-vote-front
    spec:
      nodeSelector:
        "beta.kubernetes.io/os": linux
      containers:
      - name: azure-vote-front
        image: microsoft/azure-vote-front:v1
        resources:
          requests:
            cpu: 100m
            memory: 128Mi
          limits:
            cpu: 250m
            memory: 256Mi
        ports:
        - containerPort: 80
        env:
        - name: REDIS
          value: "azure-vote-back"
---
apiVersion: v1
kind: Service
metadata:
  name: azure-vote-front
spec:
  type: LoadBalancer
  ports:
  - port: 80
  selector:
    app: azure-vote-front

使用 kubectl apply 命令來部署應用程式並指定 YAML 資訊清單的名稱:Deploy the application using the kubectl apply command and specify the name of your YAML manifest:

kubectl apply -f azure-vote.yaml

下列範例輸出會顯示已成功建立的部署和服務:The following example output shows the Deployments and Services created successfully:

deployment "azure-vote-back" created
service "azure-vote-back" created
deployment "azure-vote-front" created
service "azure-vote-front" created

測試應用程式Test the application

執行應用程式時,Kubernetes 服務會向網際網路公開前端應用程式。When the application runs, a Kubernetes service exposes the application front end to the internet. 此程序需要數分鐘的時間完成。This process can take a few minutes to complete.

若要監視進度,請使用 kubectl get service 命令搭配 --watch 引數。To monitor progress, use the kubectl get service command with the --watch argument.

kubectl get service azure-vote-front --watch

一開始,azure-vote-front 服務的 EXTERNAL-IP 會顯示為 pendingInitially the EXTERNAL-IP for the azure-vote-front service is shown as pending.

NAME               TYPE           CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
azure-vote-front   LoadBalancer   10.0.37.27   <pending>     80:30572/TCP   6s

EXTERNAL-IP 位址從 pending 變成實際的公用 IP 位址時,請使用 CTRL-C 停止 kubectl 監看式流程。When the EXTERNAL-IP address changes from pending to an actual public IP address, use CTRL-C to stop the kubectl watch process. 下列範例輸出會顯示已指派給服務的有效公用 IP 位址:The following example output shows a valid public IP address assigned to the service:

azure-vote-front   LoadBalancer   10.0.37.27   52.179.23.131   80:30572/TCP   2m

若要查看 Azure 投票應用程式的實際運作情況,請開啟網頁瀏覽器並瀏覽至服務的外部 IP 位址。To see the Azure Vote app in action, open a web browser to the external IP address of your service.

瀏覽至 Azure 投票應用程式範例的影像

監視健康情況和記錄Monitor health and logs

當您建立叢集後,就已啟用容器的 Azure 監視器。When you created the cluster, Azure Monitor for containers was enabled. 此監視功能可針對 AKS 叢集和叢集上執行的 Pod 提供健康情況計量。This monitoring feature provides health metrics for both the AKS cluster and pods running on the cluster.

可能需要幾分鐘,此資料才會填入至 Azure 入口網站。It may take a few minutes for this data to populate in the Azure portal. 若要查看 Azure 投票應用程式 Pod 的目前狀態、執行時間及資源使用量,請往回瀏覽到 Azure 入口網站中的 AKS 資源,例如 myAKSCluster 。To see current status, uptime, and resource usage for the Azure Vote pods, browse back to the AKS resource in the Azure portal, such as myAKSCluster. 然後,您可以存取健康狀態,如下所示:You can then access the health status as follows:

  1. 在左側的 [監視] 底下,選擇 [見解] Under Monitoring on the left-hand side, choose Insights
  2. 在頂端選擇 [+ 新增篩選器] Across the top, choose to + Add Filter
  3. 選取 [命名空間] 作為屬性,然後選擇 <除了 kube-system 以外的所有項目> Select Namespace as the property, then choose <All but kube-system>
  4. 選擇檢視容器Choose to view the Containers.

系統會顯示 azure-vote-back 和 azure-vote-front 容器,如下列範例所示:The azure-vote-back and azure-vote-front containers are displayed, as shown in the following example:

檢視 AKS 中執行的容器健康情況

若要查看 azure-vote-front Pod 的記錄,請選取容器清單右側的 [檢視容器記錄] 連結。To see logs for the azure-vote-front pod, select the View container logs link on the right-hand side of the containers list. 這些記錄包含來自容器的 stdout 和 stderr 資料流。These logs include the stdout and stderr streams from the container.

在 AKS 中檢視容器記錄

刪除叢集Delete cluster

不再需要叢集時,請刪除叢集資源,這將會刪除所有相關的資源。When the cluster is no longer needed, delete the cluster resource, which deletes all associated resources. 此操作可以在 Azure 入口網站中選取 AKS 叢集儀表板上的 [刪除] 按鈕來完成。This operation can be completed in the Azure portal by selecting the Delete button on the AKS cluster dashboard. 或者,您可以在 Cloud Shell 中使用 az aks delete 命令:Alternatively, the az aks delete command can be used in the Cloud Shell:

az aks delete --resource-group myResourceGroup --name myAKSCluster --no-wait

注意

當您刪除叢集時,不會移除 AKS 叢集所使用的 Azure Active Directory 服務主體。When you delete the cluster, the Azure Active Directory service principal used by the AKS cluster is not removed. 如需有關如何移除服務主體的步驟,請參閱 AKS 服務主體的考量和刪除For steps on how to remove the service principal, see AKS service principal considerations and deletion.

取得程式碼Get the code

在本快速入門中,預先建立的容器映像已用來建立 Kubernetes 部署。In this quickstart, pre-created container images were used to create a Kubernetes deployment. 相關的應用程式程式碼、Dockerfile 和 Kubernetes 資訊清單檔案,都可以在 GitHub 上取得。The related application code, Dockerfile, and Kubernetes manifest file are available on GitHub.

https://github.com/Azure-Samples/azure-voting-app-redis

後續步驟Next steps

在本快速入門中,您已部署 Kubernetes 叢集,並將多容器應用程式部署到此叢集。In this quickstart, you deployed a Kubernetes cluster and deployed a multi-container application to it.

若要深入了解 AKS,並逐步完成部署範例的完整程式碼,請繼續 Kubernetes 叢集教學課程。To learn more about AKS, and walk through a complete code to deployment example, continue to the Kubernetes cluster tutorial.