教學課程:更新 Azure Kubernetes Service (AKS) 中的應用程式Tutorial: Update an application in Azure Kubernetes Service (AKS)

在 Kubernetes 中部署應用程式之後,您可以藉由指定新的容器映像或映像版本來進行更新。After an application has been deployed in Kubernetes, it can be updated by specifying a new container image or image version. 更新會分段進行,因此,只有一部分的部署會同時更新。An update is staged so that only a portion of the deployment is updated at the same time. 此分段更新方式可讓應用程式在更新期間保持運作,This staged update enables the application to keep running during the update. 此外也能當作部署失敗時的復原機制。It also provides a rollback mechanism if a deployment failure occurs.

在本教學課程 (6/7 部分) 中,已更新範例 Azure Vote 應用程式。In this tutorial, part six of seven, the sample Azure Vote app is updated. 您會了解如何:You learn how to:

  • 更新前端應用程式程式碼Update the front-end application code
  • 建立已更新的容器映像Create an updated container image
  • 將容器映像推送至 Azure Container RegistryPush the container image to Azure Container Registry
  • 部署已更新的容器映像Deploy the updated container image

開始之前Before you begin

在先前的教學課程中,已將應用程式封裝為容器映像。In previous tutorials, an application was packaged into a container image. 此映像已上傳至 Azure Container Registry,而您已建立 AKS 叢集。This image was uploaded to Azure Container Registry, and you created an AKS cluster. 接著已將應用程式部署至 AKS 叢集。The application was then deployed to the AKS cluster.

應用程式存放庫也會一併複製,其中包括應用程式原始程式碼,以及本教學課程使用的預先建立 Docker Compose 檔案。An application repository was also cloned that includes the application source code, and a pre-created Docker Compose file used in this tutorial. 請確認您已建立存放庫的複製品,而且已將目錄變更為複製的目錄。Verify that you've created a clone of the repo, and have changed directories into the cloned directory. 如果您尚未完成這些步驟,並且想要跟著做,請從教學課程 1 – 建立容器映像開始。If you haven't completed these steps, and want 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 an application

我們將變更範例應用程式,然後更新已部署至 AKS 叢集的版本。Let's make a change to the sample application, then update the version already deployed to your AKS cluster. 請確定您位於複製的 azure-voting-app-redis 目錄。Make sure that you're in the cloned azure-voting-app-redis directory. 您接著可在 azure-vote 目錄中找到範例應用程式的原始程式碼。The sample application source code can then be found inside the azure-vote directory. 使用編輯器 (例如 vi) 開啟 config_file.cfg 檔案:Open the config_file.cfg file with an editor, such as vi:

vi azure-vote/azure-vote/config_file.cfg

VOTE1VALUEVOTE2VALUE 的值變更為不同的值,例如色彩。Change the values for VOTE1VALUE and VOTE2VALUE to different values, such as colors. 下列範例顯示已更新的值:The following example shows the updated values:

# UI Configurations
TITLE = 'Azure Voting App'
VOTE1VALUE = 'Blue'
VOTE2VALUE = 'Purple'
SHOWHOST = 'false'

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

更新容器映像Update the container image

若要重新建立前端映像並測試已更新的應用程式,請使用 docker-composeTo re-create the front-end image and test the updated application, use docker-compose. --build 引數可用來指示 Docker Compose 重新建立應用程式映像:The --build argument is used to instruct Docker Compose to re-create the application image:

docker-compose up --build -d

在本機測試應用程式Test the application locally

若要確認更新的容器映像已顯示您的變更,請開啟本機網頁瀏覽器並進入 http://localhost:8080To verify that the updated container image shows your changes, open a local web browser to http://localhost:8080.

Azure 上 Kubernetes 叢集的影像

config_file.cfg 檔案中提供的更新值會顯示於執行中的應用程式上。The updated values provided in the config_file.cfg file are displayed in your running application.

標記並推送映像Tag and push the image

若要正確使用更新的映像,請為 azure-vote-front 映像標記您 ACR 登錄的登入伺服器名稱。To correctly use the updated image, tag the azure-vote-front image with the login server name of your ACR registry. 使用 az acr list 命令取得登入伺服器名稱:Get the login server name with the az acr list command:

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

使用 docker tag 來標記映像。Use docker tag to tag the image. <acrLoginServer> 取代為您的 ACR 登入伺服器名稱或公用登錄主機名稱,並將映像版本更新為 :v2,如下所示:Replace <acrLoginServer> with your ACR login server name or public registry hostname, and update the image version to :v2 as follows:

docker tag azure-vote-front <acrLoginServer>/azure-vote-front:v2

現在,使用 docker push 將映像上傳至您的登錄。Now use docker push to upload the image to your registry. <acrLoginServer> 取代為您的 ACR 登入伺服器名稱。Replace <acrLoginServer> with your ACR login server name.

注意

如果您在推送到 ACR 登錄時發生問題,請確定您仍處於登入狀態。If you experience issues pushing to your ACR registry, make sure that you are still logged in. 使用 建立 Azure Container Registry 步驟中建立的 Azure Container Registry 名稱,執行 az acr login 命令。Run the az acr login command using the name of your Azure Container Registry that you created in the Create an Azure Container Registry step. 例如: az acr login --name <azure container registry name>For example, az acr login --name <azure container registry name>.

docker push <acrLoginServer>/azure-vote-front:v2

部署已更新的應用程式Deploy the updated application

若要提供最大執行時間,應用程式 Pod 必須有多個執行個體正在執行中。To provide maximum uptime, multiple instances of the application pod must be running. 使用 kubectl get pods 命令確認執行中的前端執行個體數目:Verify the number of running front-end instances with the kubectl get pods command:

$ kubectl get pods

NAME                               READY     STATUS    RESTARTS   AGE
azure-vote-back-217588096-5w632    1/1       Running   0          10m
azure-vote-front-233282510-b5pkz   1/1       Running   0          10m
azure-vote-front-233282510-dhrtr   1/1       Running   0          10m
azure-vote-front-233282510-pqbfk   1/1       Running   0          10m

如果您沒有多個前端 Pod,請調整 azure-vote-front 部署,如下所示:If you don't have multiple front-end pods, scale the azure-vote-front deployment as follows:

kubectl scale --replicas=3 deployment/azure-vote-front

若要更新應用程式,請使用 kubectl set 命令。To update the application, use the kubectl set command. 以容器登錄的登入伺服器或主機名稱來更新 <acrLoginServer>,並指定 v2 應用程式版本:Update <acrLoginServer> with the login server or host name of your container registry, and specify the v2 application version:

kubectl set image deployment azure-vote-front azure-vote-front=<acrLoginServer>/azure-vote-front:v2

若要監視部署,請使用 kubectl get pod 命令。To monitor the deployment, use the kubectl get pod command. 部署已更新的應用程式後,您的 pod 會終止並以新的容器映像重建。As the updated application is deployed, your pods are terminated and re-created with the new container image.

kubectl get pods

下列範例輸出顯示在部署進行期間正在終止的 Pod 和執行中的新執行個體:The following example output shows pods terminating and new instances running as the deployment progresses:

$ kubectl get pods

NAME                               READY     STATUS        RESTARTS   AGE
azure-vote-back-2978095810-gq9g0   1/1       Running       0          5m
azure-vote-front-1297194256-tpjlg  1/1       Running       0          1m
azure-vote-front-1297194256-tptnx  1/1       Running       0          5m
azure-vote-front-1297194256-zktw9  1/1       Terminating   0          1m

測試已更新的應用程式Test the updated application

若要檢視更新應用程式,請先取得 azure-vote-front 服務的外部 IP 位址:To view the update application, first get the external IP address of the azure-vote-front service:

kubectl get service azure-vote-front

現在,開啟本機網頁瀏覽器並前往您服務的 IP 位址:Now open a local web browser to the IP address of your service:

Azure 上 Kubernetes 叢集的影像

後續步驟Next steps

在本教學課程中,您已更新應用程式,並將此更新推出至您的 AKS 叢集。In this tutorial, you updated an application and rolled out this update to your AKS cluster. 您已了解如何︰You learned how to:

  • 更新前端應用程式程式碼Update the front-end application code
  • 建立已更新的容器映像Create an updated container image
  • 將容器映像推送至 Azure Container RegistryPush the container image to Azure Container Registry
  • 部署已更新的容器映像Deploy the updated container image

繼續進行下一個教學課程,以了解如何將 AKS 叢集升級為新版的 Kubernetes。Advance to the next tutorial to learn how to upgrade an AKS cluster to a new version of Kubernetes.