快速入門:使用 Azure CLI 部署 Azure Kubernetes Service 叢集Quickstart: Deploy an Azure Kubernetes Service cluster using the Azure CLI

在此快速入門中,您將使用 Azure CLI 部署 Azure Kubernetes Service (AKS) 叢集。In this quickstart, you deploy an Azure Kubernetes Service (AKS) cluster using the Azure CLI. AKS 是受控 Kubernetes 服務,可讓您快速部署及管理叢集。AKS is a managed Kubernetes service that lets you quickly deploy and manage clusters. 在叢集上執行包含 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.

若要深入了解如何建立 Windows Server 節點集區,請參閱建立支援 Windows Server 容器的 AKS 叢集To learn more about creating a Windows Server node pool, see Create an AKS cluster that supports Windows Server containers.

Azure Kubernetes Service 中部署的投票應用程式

本快速入門假設您已有 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.

必要條件Prerequisites

  • 使用 Bash 環境的 Azure Cloud ShellUse Azure Cloud Shell using the Bash environment.

    在新視窗中啟動 Cloud ShellLaunch Cloud Shell in a new window

  • 如果想要,請安裝 Azure CLI 以執行 CLI 參考命令。If you prefer, install the Azure CLI to run CLI reference commands.

    • 如果您使用的是本機安裝,請使用 az login 命令,透過 Azure CLI 來登入。If you're using a local installation, sign in to the Azure CLI by using the az login command. 請遵循您終端機上顯示的步驟,完成驗證程序。To finish the authentication process, follow the steps displayed in your terminal. 如需其他登入選項,請參閱使用 Azure CLI 登入For additional sign-in options, see Sign in with the Azure CLI.
    • 出現提示時,請在第一次使用時安裝 Azure CLI 擴充功能。When you're prompted, install Azure CLI extensions on first use. 如需擴充功能詳細資訊,請參閱使用 Azure CLI 擴充功能For more information about extensions, see Use extensions with the Azure CLI.
    • 執行 az version 以尋找已安裝的版本和相依程式庫。Run az version to find the version and dependent libraries that are installed. 若要升級至最新版本,請執行 az upgradeTo upgrade to the latest version, run az upgrade.
  • 本文需要 2.0.64 版或更新版本的 Azure CLI。This article requires version 2.0.64 or later of the Azure CLI. 如果您是使用 Azure Cloud Shell,就已安裝最新版本。If using Azure Cloud Shell, the latest version is already installed.

注意

如果在本機 (而不是 Azure Cloud Shell) 執行本快速入門中的命令,請確定您是以系統管理員身分執行命令。If running the commands in this quickstart locally (instead of Azure Cloud Shell), ensure you run the commands as administrator.

建立資源群組Create a resource group

Azure 資源群組是部署及管理 Azure 資源所在的邏輯群組。An Azure resource group is a logical group in which Azure resources are deployed and managed. 建立資源群組時,系統會要求您指定位置。When you create a resource group, you are asked to specify a location. 此位置是儲存資源群組中繼資料的位置,如果您未在資源建立期間指定另一個區域,此位置也會是您在 Azure 中執行資源的位置。This location is where resource group metadata is stored, it is also where your resources run in Azure if you don't specify another region during resource creation. 使用 az group create 命令來建立資源群組。Create a resource group using the az group create command.

下列範例會在 eastus 位置建立名為 myResourceGroup 的資源群組。The following example creates a resource group named myResourceGroup in the eastus location.

az group create --name myResourceGroup --location eastus

下列範例輸出顯示已成功建立的資源群組:The following example output shows the resource group created successfully:

{
  "id": "/subscriptions/<guid>/resourceGroups/myResourceGroup",
  "location": "eastus",
  "managedBy": null,
  "name": "myResourceGroup",
  "properties": {
    "provisioningState": "Succeeded"
  },
  "tags": null
}

建立 AKS 叢集Create AKS cluster

使用 az aks create 命令來建立 AKS 叢集。Use the az aks create command to create an AKS cluster. 下列範例會建立名為 myAKSCluster 並包含一個節點的叢集。The following example creates a cluster named myAKSCluster with one node. 這需要數分鐘的時間才能完成。This will take several minutes to complete.

注意

您可以使用 --enable-addons monitoring 參數來啟用容器的 Azure 監視器,這需要 Microsoft.OperationsManagement 和 Microsoft.OperationalInsights,才能在您的訂用帳戶上註冊。Azure Monitor for containers is enabled using the --enable-addons monitoring parameter, which requires Microsoft.OperationsManagement and Microsoft.OperationalInsights to be registered on you subscription. 檢查註冊狀態:To check the registration status:

az provider show -n Microsoft.OperationsManagement -o table
az provider show -n Microsoft.OperationalInsights -o table

如果未註冊,請使用下列命令來註冊 Microsoft.OperationsManagement 和 Microsoft.OperationalInsights:If they are not registered, use the following command to register Microsoft.OperationsManagement and Microsoft.OperationalInsights:

az provider register --namespace Microsoft.OperationsManagement
az provider register --namespace Microsoft.OperationalInsights
az aks create --resource-group myResourceGroup --name myAKSCluster --node-count 1 --enable-addons monitoring --generate-ssh-keys

在幾分鐘之後,此命令就會完成,並以 JSON 格式傳回叢集的相關資訊。After a few minutes, the command completes and returns JSON-formatted information about the cluster.

注意

建立 AKS 叢集時,系統會自動建立第二個資源群組來儲存 AKS 資源。When creating an AKS cluster a second resource group is automatically created to store the AKS resources. 如需詳細資訊,請參閱為何會使用 AKS 建立兩個資源群組?For more information see Why are two resource groups created with AKS?

連線至叢集Connect to the cluster

若要管理 Kubernetes 叢集,請使用 Kubernetes 命令列用戶端:kubectlTo manage a Kubernetes cluster, you use kubectl, the Kubernetes command-line client. 如果您使用 Azure Cloud Shell,則 kubectl 已安裝。If you use Azure Cloud Shell, kubectl is already installed. 若要在本機安裝 kubectl,請使用 az aks install-cli 命令:To install kubectl locally, use the az aks install-cli command:

az aks install-cli

若要設定 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.

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

注意

上述命令會使用 Kubernetes 組態檔的預設位置,也就是 ~/.kube/configThe above command uses the default location for the Kubernetes configuration file, which is ~/.kube/config. 您可以使用 --file,為您的 Kubernetes 組態檔指定不同的位置。You can specify a different location for your Kubernetes configuration file using --file.

若要驗證針對您叢集的連線,請使用 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-nodepool1-31718369-0   Ready    agent   6m44s   v1.12.8

執行應用程式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.

建立名為 azure-vote.yaml 的檔案,然後將下列 YAML 定義複製進來。Create a file named azure-vote.yaml and copy in the following YAML definition. 如果您使用 Azure Cloud Shell,可以使用 codevinano 建立這個檔案,猶如使用虛擬或實體系統:If you use the Azure Cloud Shell, this file can be created using code, vi, or nano as if working on a virtual or physical system:

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: mcr.microsoft.com/oss/bitnami/redis:6.0.8
        env:
        - name: ALLOW_EMPTY_PASSWORD
          value: "yes"
        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: mcr.microsoft.com/azuredocs/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 Kubernetes Service 中部署的投票應用程式

建立 AKS 叢集時,會啟用適用於容器的 Azure 監視器來擷取叢集節點和 pod 的健康狀態計量。When the AKS cluster was created, Azure Monitor for containers was enabled to capture health metrics for both the cluster nodes and pods. 在 Azure 入口網站中可取得這些健康狀態度量。These health metrics are available in the Azure portal.

選取叢集Delete the cluster

若要避免 Azure 費用,您應該清除不需要的資源。To avoid Azure charges, you should clean up unneeded resources. 若不再需要叢集,可使用 az group delete 命令來移除資源群組、容器服務和所有相關資源。When the cluster is no longer needed, use the az group delete command to remove the resource group, container service, and all related resources.

az group delete --name myResourceGroup --yes --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. 如果您使用受控識別,則身分識別會由平台負責管理,您不需要刪除。If you used a managed identity, the identity is managed by the platform and does not require removal.

取得程式碼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. 您也可以存取 Kubernetes Web 儀表板,以供您的 AKS 叢集使用。You can also access the Kubernetes web dashboard for your AKS cluster.

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