教程:在 Azure Kubernetes 服务 (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 容器注册表Push 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容器注册表,同时,你创建了 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

若要验证已更新的容器映像是否显示所做的更改,请打开一个本地 Web 浏览器并访问 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

若要正确使用已更新的映像,请使用 ACR 注册表的登录服务器名称标记 azure-vote-front 映像。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 注册表时遇到问题,请确保已运行 az acr login 命令。If you experience issues pushing to your ACR registry, make sure that you have run the az acr login command.

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

现在,请打开本地 Web 浏览器并访问服务的 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 容器注册表Push 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.