教學課程:執行 Azure Kubernetes Service (AKS) 中的應用程式Tutorial: Run applications in Azure Kubernetes Service (AKS)

Kubernetes 會提供容器化應用程式的分散式平台。Kubernetes provides a distributed platform for containerized applications. 您會建置自己的應用程式和服務,並將其部署至 Kubernetes 叢集,並讓該叢集管理可用性和連線能力。You build and deploy your own applications and services into a Kubernetes cluster, and let the cluster manage the availability and connectivity. 在本教學課程 (七個章節的第四部分) 中,已在 Kubernetes 叢集中部署一個應用程式範例。In this tutorial, part four of seven, a sample application is deployed into a Kubernetes cluster. 您會了解如何:You learn how to:

  • 更新 Kubernetes 資訊清單檔Update a Kubernetes manifest file
  • 在 Kubernetes 中執行應用程式Run an application in Kubernetes
  • 測試應用程式Test the application

在其他教學課程中,此應用程式會相應放大並且更新。In additional tutorials, this application is scaled out and updated.

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

開始之前Before you begin

在先前的教學課程中,已將應用程式封裝成容器映像、將此映像上傳至 Azure Container Registry,並已建立 Kubernetes 叢集。In previous tutorials, an application was packaged into a container image, this image was uploaded to Azure Container Registry, and a Kubernetes cluster was created.

若要完成本教學課程中的內容,您需要預先建立的 azure-vote-all-in-one-redis.yaml Kubernetes 資訊清單檔。To complete this tutorial, you need the pre-created azure-vote-all-in-one-redis.yaml Kubernetes manifest file. 在先前的教學課程中,此檔案已連同應用程式原始程式碼一起下載。This file was downloaded with the application source code in a previous tutorial. 請確認您擁有複製的存放庫,而且已將目錄變更為複製的目錄。Verify that you've cloned the repo, and that you have changed directories into the cloned repo. 如果您尚未完成這些步驟,而且想要跟著做,請從教學課程 1 – 建立容器映像開始。If you haven't done these steps, and would like to follow along, start with Tutorial 1 – Create container images.

在本教學課程中,您必須執行 Azure CLI 2.0.53 版或更新版本。This tutorial requires that you're running the Azure CLI version 2.0.53 or later. 執行 az --version 以尋找版本。Run az --version to find the version. 如果您需要安裝或升級,請參閱安裝 Azure CLIIf you need to install or upgrade, see Install Azure CLI.

更新資訊清單檔Update the manifest file

在這些教學課程中,Azure Container Registry (ACR) 執行個體會儲存範例應用程式的容器映像。In these tutorials, an Azure Container Registry (ACR) instance stores the container image for the sample application. 若要部署應用程式,您必須更新 Kubernetes 資訊清單檔中的映像名稱,以納入 ACR 登入伺服器名稱。To deploy the application, you must update the image name in the Kubernetes manifest file to include the ACR login server name.

使用 az acr list 命令取得 ACR 登入伺服器名稱,如下所示:Get the ACR login server name using the az acr list command as follows:

az acr list --resource-group myResourceGroup --query "[].{acrLoginServer:loginServer}" --output table

在第一個教學課程中叢 git 存放庫複製的範例資訊清單檔,會使用登入伺服器名稱 microsoftThe sample manifest file from the git repo cloned in the first tutorial uses the login server name of microsoft. 請確定您位於複製的 azure-voting-app-redis 目錄,然後使用文字編輯器開啟資訊清單檔,例如 viMake sure that you're in the cloned azure-voting-app-redis directory, then open the manifest file with a text editor, such as vi:

vi azure-vote-all-in-one-redis.yaml

microsoft 取代為您的 ACR 登入伺服器名稱。Replace microsoft with your ACR login server name. 您可以在資訊清單檔的第 51 行找到映像名稱。The image name is found on line 51 of the manifest file. 下列範例顯示預設映像名稱:The following example shows the default image name:

containers:
- name: azure-vote-front
  image: microsoft/azure-vote-front:v1

提供您自己的 ACR 登入伺服器名稱,使您的資訊清單檔看起來類似於下列範例:Provide your own ACR login server name so that your manifest file looks like the following example:

containers:
- name: azure-vote-front
  image: <acrName>.azurecr.io/azure-vote-front:v1

儲存並關閉檔案。Save and close the file. vi 中使用 :wqIn vi, use :wq.

部署應用程式Deploy the application

若要部署應用程式,請使用 kubectl apply 命令。To deploy your application, use the kubectl apply command. 此命令會剖析資訊清單檔,並建立已定義的 Kubernetes 物件。This command parses the manifest file and creates the defined Kubernetes objects. 指定範例資訊清單檔,如下列範例所示:Specify the sample manifest file, as shown in the following example:

kubectl apply -f azure-vote-all-in-one-redis.yaml

下列範例輸出顯示在 AKS 叢集中成功建立的資源:The following example output shows the resources successfully created in the AKS cluster:

$ kubectl apply -f azure-vote-all-in-one-redis.yaml

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:

azure-vote-front   10.0.34.242   <pending>     80:30676/TCP   7s

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   10.0.34.242   52.179.23.131   80:30676/TCP   2m

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

Azure 上 Kubernetes 叢集的影像

如果應用程式並未載入,可能是因為您的映像登錄發生授權問題。If the application didn't load, it might be due to an authorization problem with your image registry. 若要檢視容器的狀態,請使用 kubectl get pods 命令。To view the status of your containers, use the kubectl get pods command. 如果無法提取容器映像,請參閱允許使用 Kubernetes 祕密存取容器登錄If the container images can't be pulled, see allow access to Container Registry with a Kubernetes secret.

後續步驟Next steps

在本教學課程中,範例 Azure 投票應用程式已部署到 AKS 中的 Kubernetes 叢集。In this tutorial, a sample Azure vote application was deployed to a Kubernetes cluster in AKS. 您已了解如何︰You learned how to:

  • 更新 Kubernetes 資訊清單檔Update a Kubernetes manifest files
  • 在 Kubernetes 中執行應用程式Run an application in Kubernetes
  • 測試應用程式Test the application

請繼續進行下一個教學課程,以了解如何調整 Kubernetes 應用程式和基礎 Kubernetes 基礎架構。Advance to the next tutorial to learn how to scale a Kubernetes application and the underlying Kubernetes infrastructure.