您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

教程:将已更新的容器映像推送到异地复制的容器注册表以进行区域性 Web 应用部署Tutorial: Push an updated container image to a geo-replicated container registry for regional web app deployments

本文是由三个部分构成的教程系列的第三部分。This is part three in a three-part tutorial series. 前一篇教程中,已针对两个不同的区域性 Web 应用部署配置了异地复制。In the previous tutorial, geo-replication was configured for two different regional Web App deployments. 在本教程中,我们首先修改应用程序,然后生成新的容器映像并将其推送到异地复制的注册表。In this tutorial, you first modify the application, then build a new container image and push it to your geo-replicated registry. 最后,查看 Azure 容器注册表 Webhook 在两个 Web 应用实例中自动部署的更改。Finally, you view the change, deployed automatically by Azure Container Registry webhooks, in both Web App instances.

本教程(教程系列的最后一部分)的内容包括:In this tutorial, the final part in the series:

  • 修改 Web 应用程序 HTMLModify the web application HTML
  • 生成并标记 Docker 映像Build and tag the Docker image
  • 将更改推送到 Azure 容器注册表Push the change to Azure Container Registry
  • 查看两个不同区域中已更新的应用View the updated app in two different regions

如果尚未配置两个“用于容器的 Web 应用”区域部署,请返回到本教程系列的前一篇教程:从 Azure 容器注册表部署 Web 应用If you've not yet configured the two Web App for Containers regional deployments, return to the previous tutorial in the series, Deploy web app from Azure Container Registry.

修改 Web 应用程序Modify the web application

此步骤对 Web 应用程序进行更改。将更新的容器映像推送到 Azure 容器注册表之后,用户很容易发现这项更改。In this step, make a change to the web application that will be highly visible once you push the updated container image to Azure Container Registry.

在学习前一篇教程时从 GitHub 克隆的应用程序源中找到 AcrHelloworld/Views/Home/Index.cshtml 文件,并在偏好的文本编辑器中将其打开。Find the AcrHelloworld/Views/Home/Index.cshtml file in the application source you cloned from GitHub in a previous tutorial and open it in your favorite text editor. 在现有 <h1> 行下面添加以下行:Add the following line below the existing <h1> line:

<h1>MODIFIED</h1>

修改后的 Index.cshtml 应如下所示:Your modified Index.cshtml should look similar to:

@{
    ViewData["Title"] = "Azure Container Registry :: Geo-replication";
}
<style>
    body {
        background-image: url('images/azure-regions.png');
        background-size: cover;
    }
    .footer {
        position: fixed;
        bottom: 0px;
        width: 100%;
    }
</style>

<h1 style="text-align:center;color:blue">Hello World from:  @ViewData["REGION"]</h1>
<h1>MODIFIED</h1>
<div class="footer">
    <ul>
        <li>Registry URL: @ViewData["REGISTRYURL"]</li>
        <li>Registry IP: @ViewData["REGISTRYIP"]</li>
        <li>Registry Region: @ViewData["REGION"]</li>
    </ul>
</div>

重新生成映像Rebuild the image

更新 Web 应用程序后,请重新生成其容器映像。Now that you've updated the web application, rebuild its container image. 如前所述,对标记使用完全限定的映像名称,包括登录服务器的完全限定域名 (FQDN):As before, use the fully qualified image name, including the login server's fully qualified domain name (FQDN), for the tag:

docker build . -f ./AcrHelloworld/Dockerfile -t <acrName>.azurecr.io/acr-helloworld:v1

向 Azure 容器注册表推送映像Push image to Azure Container Registry

接下来,将更新的 acr-helloworld 容器映像推送到异地复制的注册表。Next, push the updated acr-helloworld container image to your geo-replicated registry. 在这里,我们会执行一条 docker push 命令将更新的映像同时部署到“美国西部”和“美国东部”区域的注册表副本。Here, you're executing a single docker push command to deploy the updated image to the registry replicas in both the West US and East US regions.

docker push <acrName>.azurecr.io/acr-helloworld:v1

docker push 输出应如下所示:Your docker push output should be similar to the following:

$ docker push uniqueregistryname.azurecr.io/acr-helloworld:v1
The push refers to a repository [uniqueregistryname.azurecr.io/acr-helloworld]
5b9454e91555: Pushed
d6803756744a: Layer already exists
b7b1f3a15779: Layer already exists
a89567dff12d: Layer already exists
59c7b561ff56: Layer already exists
9a2f9413d9e4: Layer already exists
a75caa09eb1f: Layer already exists
v1: digest: sha256:4c3f2211569346fbe2d1006c18cbea2a4a9dcc1eb3a078608cef70d3a186ec7a size: 1792

查看 Webhook 日志View the webhook logs

复制映像时,可以看到 Azure 容器注册表 Webhook 正在触发。While the image is being replicated, you can see the Azure Container Registry webhooks being triggered.

若要查看在前一篇教程中将容器部署到“用于容器的 Web 应用”时创建的区域 Webhook,请在 Azure 门户中导航到容器注册表,选择“服务”下的“Webhook”。To see the regional webhooks that were created when you deployed the container to Web Apps for Containers in a previous tutorial, navigate to your container registry in the Azure portal, then select Webhooks under SERVICES.

Azure 门户中的容器注册表 Webhook

选择每个 Webhook 可查看其调用和响应的历史记录。Select each Webhook to see the history of its calls and responses. 在两个 Webhook 的日志中,应会看到 push 操作有一个对应的行。You should see a row for the push action in the logs of both Webhooks. 此处,位于“美国西部”区域的 Webhook 日志显示了由上一步骤中的 docker push 触发的 push 操作:Here, the log for the Webhook located in the West US region shows the push action triggered by the docker push in the previous step:

Azure 门户中的容器注册表 Webhook 日志(美国西部)

查看已更新的 Web 应用View the updated web app

Webhook 通知 Web 应用,新的映像已推送到注册表,后者会自动将更新的容器自动部署到两个区域性 Web 应用。The Webhooks notify Web Apps that a new image has been pushed to the registry, which automatically deploys the updated container to the two regional web apps.

在 Web 浏览器中导航到这两个区域性 Web 应用部署,验证应用程序是否已在这两个部署中更新。Verify that the application has been updated in both deployments by navigating to both regional Web App deployments in your web browser. 请注意,可在每个“应用服务概述”选项卡的右上角找到已部署 Web 应用的 URL。As a reminder, you can find the URL for the deployed web app in the top-right of each App Service overview tab.

Azure 门户中的“应用服务概述”

若要查看已更新的应用程序,请在“应用服务概述”中选择相应的链接。To see the updated application, select the link in the App Service overview. 下面是在“美国西部”运行的应用的示例视图:Here's an example view of the app running in West US:

在“美国西部”区域运行的已修改 Web 应用的浏览器视图

在浏览器中查看已更新的容器映像,验证它是否同时已部署到“美国东部”部署。Verify that the updated container image was also deployed to the East US deployment by viewing it in your browser.

在“美国东部”区域运行的已修改 Web 应用的浏览器视图

使用单个 docker push,已自动更新这两个区域 Web 应用部署中运行的 Web 应用程序。With a single docker push, you've automatically updated the web application running in both regional Web App deployments. 并且,Azure 容器注册表提供离每个部署最近的存储库中的容器映像。And, Azure Container Registry served the container images from the repositories located closest to each deployment.

后续步骤Next steps

在本教程中,我们已更新一个新版 Web 应用程序容器并将其推送到了异地复制的注册表。In this tutorial, you updated and pushed a new version of the web application container to your geo-replicated registry. Azure 容器注册表中的 Webhook 告知用于容器的 Web 应用已发生此项更新,更新触发了从最近的注册表副本进行的本地提取操作。Webhooks in Azure Container Registry notified Web Apps for Containers of the update, which triggered a local pull from the nearest registry replica.

ACR Build:自动映像生成和修补ACR Build: Automated image build and patch

除了异地复制外,ACR Build 是 Azure 容器注册表的另一项功能,可帮助优化容器部署管道。In addition to geo-replication, ACR Build is another feature of Azure Container Registry that can help optimize your container deployment pipeline. 从 ACR Build 概述着手,了解其功能:Start with the ACR Build overview to get an idea of its capabilities:

使用 ACR Build 自动执行 OS 和框架修补Automate OS and framework patching with ACR Build