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

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

開啟 Azure Cloud ShellOpen Azure Cloud Shell

Azure Cloud Shell 是免費的互動式 Shell,可讓您用來執行本文中的步驟。Azure Cloud Shell is a free, interactive shell that you can use to run the steps in this article. Cloud Shell 中已預先安裝和設定共用 Azure 工具,以便您搭配自己的帳戶使用。Common Azure tools are preinstalled and configured in Cloud Shell for you to use with your account. 請選取 [複製] 來複製程式碼,將它貼到 Cloud Shell 中,然後按 Enter 鍵來執行它。Select Copy to copy the code, paste it in Cloud Shell, and then press Enter to run it. 以下有幾種開啟 Cloud Shell 的方式:There are a few ways to open Cloud Shell:

選取程式碼區塊右上角的 [試試看]。Select Try It in the upper-right corner of a code block. Azure Cloud Shell 的試試看範例
在您的瀏覽器中開啟 Cloud Shell。Open Cloud Shell in your browser. 啟動 Azure Cloud Shell 按鈕Launch Azure Cloud Shell button
選取 Azure 入口網站右上角功能表上的 [Cloud Shell] 按鈕。Select the Cloud Shell button on the menu in the upper-right corner of the Azure portal. Azure 入口網站中的 [Cloud Shell] 按鈕

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

建立資源群組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.

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.9.11

執行應用程式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 投票的影像

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

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

若要查看目前的狀態、運作時間,以及 Azure 投票 pod 的資源使用量,請完成下列步驟:To see current status, uptime, and resource usage for the Azure Vote pods, complete the following steps:

  1. 開啟網頁瀏覽器並移至 Azure 入口網站https://portal.azure.comOpen a web browser to the Azure portal https://portal.azure.com.
  2. 選取您的資源群組 (例如 myResourceGroup),然後選取您的 AKS 叢集 (例如 myAKSCluster)。Select your resource group, such as myResourceGroup, then select your AKS cluster, such as myAKSCluster.
  3. 在左側的 [監視] 底下,選擇 [見解]Under Monitoring on the left-hand side, choose Insights
  4. 在頂端選擇 [+ 新增篩選器]Across the top, choose to + Add Filter
  5. 選取 [命名空間] 作為屬性,然後選擇 <除了 kube-system 以外的所有項目>Select Namespace as the property, then choose <All but kube-system>
  6. 選擇檢視容器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

若不再需要叢集,可使用 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 儀表板,以使用您剛才建立的叢集。Access the Kubernetes web dashboard for the cluster you just created.

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