快速入門:使用 Azure CLI 部署 Azure Kubernetes Service (AKS) 叢集Quickstart: Deploy an Azure Kubernetes Service (AKS) 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 容器 (目前在 AKS 中為預覽版本),請參閱建立支援 Windows Server 容器的 AKS 叢集If you want to use Windows Server containers (currently in preview in AKS), see Create an AKS cluster that supports Windows Server containers.

瀏覽至 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.

使用 Azure Cloud ShellUse Azure Cloud Shell

Azure Cloud Shell 是裝載於 Azure 中的互動式殼層環境,可在瀏覽器中使用。Azure hosts Azure Cloud Shell, an interactive shell environment that you can use through your browser. Cloud Shell 可讓您使用 bashPowerShell 以與 Azure 服務搭配使用。Cloud Shell lets you use either bash or PowerShell to work with Azure services. Azure Cloud Shell 已預先安裝一些命令,可讓您執行本文提到的程式碼,而不必在本機環境上安裝任何工具。You can use the Cloud Shell pre-installed commands to run the code in this article without having to install anything on your local environment.

若要啟動 Azure Cloud Shell:To launch Azure Cloud Shell:

選項Option 範例/連結Example/Link
選取程式碼區塊右上角的 [試試看] 。Select Try It in the upper-right corner of a code block. 選取 [試用] 並不會自動將程式碼複製到 Cloud Shell 中。Selecting Try It doesn't automatically copy the code to Cloud Shell. Azure Cloud Shell 的試試看範例
請前往 https://shell.azure.com 或選取 [啟動 Cloud Shell] 按鈕,在瀏覽器中開啟 Cloud Shell。Go to https://shell.azure.com or select the Launch Cloud Shell button to open Cloud Shell in your browser. 在新視窗中啟動 Cloud ShellLaunch Cloud Shell in a new window
選取 Azure 入口網站右上角功能表列中的 [Cloud Shell] 按鈕。Select the Cloud Shell button on the top-right menu bar in the Azure portal. Azure 入口網站中的 [Cloud Shell] 按鈕

若要在 Azure Cloud Shell 中執行本文中的程式碼:To run the code in this article in Azure Cloud Shell:

  1. 啟動 Cloud Shell。Launch Cloud Shell.

  2. 選取程式碼區塊上的 [複製] 按鈕,複製程式碼。Select the Copy button on a code block to copy the code.

  3. 在 Windows 和 Linux 上按 Ctrl+Shift+V;或在 macOS 上按 Cmd+Shift+V,將程式碼貼到 Cloud Shell工作階段中。Paste the code into the Cloud Shell session with Ctrl+Shift+V on Windows and Linux, or Cmd+Shift+V on macOS.

  4. 按下 Enter 鍵執行程式碼。Press Enter to run the code.

如果您選擇在本機安裝和使用 CLI,本快速入門會要求您執行 Azure CLI 2.0.64 版或更新版本。If you choose to install and use the CLI locally, this quickstart requires that you are running the Azure CLI version 2.0.64 or later. 執行 az --version 以尋找版本。Run az --version to find the version. 如果您需要安裝或升級,請參閱安裝 Azure CLIIf you need to install or upgrade, see Install Azure CLI.

注意

如果在本機 (而不是 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. 使用 --enable-addons monitoring 參數也可啟用適用於容器的 Azure 監視器。Azure Monitor for containers is also enabled using the --enable-addons monitoring parameter. 這需要數分鐘的時間才能完成。This will take several minutes to complete.

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.

連接到叢集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

若要驗證叢集的連線,請使用 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.

提示

在本快速入門中,您會以手動方式建立應用程式資訊清單,並將其部署至 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.

建立名為 azure-vote.yaml 的檔案,然後將下列 YAML 定義複製進來。Create a file named azure-vote.yaml and copy in the following YAML definition. 如果您使用 Azure Cloud Shell,可以使用 vinano 建立這個檔案,猶如使用虛擬或實體系統:If you use the Azure Cloud Shell, this file can be created using 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: 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 投票的影像

建立 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.

取得程式碼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.