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

教程:使用 Azure DevOps Projects 将 ASP.NET Core 应用部署到 Azure Kubernetes 服务Tutorial: Deploy ASP.NET Core apps to Azure Kubernetes Service with Azure DevOps Projects

Azure DevOps Projects 提供一种简化的体验,你在其中既可使用现有的代码和 Git 存储库,也可选择一个示例应用程序,以便创建连接到 Azure 的持续集成 (CI) 和持续交付 (CD) 管道。Azure DevOps Projects presents a simplified experience where you can bring your existing code and Git repo or choose a sample application to create a continuous integration (CI) and continuous delivery (CD) pipeline to Azure.

DevOps Projects 还可以:DevOps Projects also:

  • 自动创建 Azure 资源,例如 Azure Kubernetes 服务 (AKS)。Automatically creates Azure resources, such as Azure Kubernetes Service (AKS).
  • 在 Azure DevOps 中创建并配置一个发布管道,用于设置 CI/CD 的生成和发布管道。Creates and configures a release pipeline in Azure DevOps that sets up a build and release pipeline for CI/CD.
  • 创建用于监视的 Azure Application Insights 资源。Creates an Azure Application Insights resource for monitoring.
  • 启用用于容器的 Azure Monitor 以监视 AKS 群集上容器工作负载的性能Enables Azure Monitor for containers to monitor performance for the container workloads on the AKS cluster

在本教程中,将:In this tutorial, you will:

  • 使用 DevOps Projects 将 ASP.NET Core 应用部署到 AKSUse DevOps Projects to deploy an ASP.NET Core app to AKS
  • 配置 Azure DevOps 和 Azure 订阅Configure Azure DevOps and an Azure subscription
  • 检查 AKS 群集Examine the AKS cluster
  • 检查 CI 管道Examine the CI pipeline
  • 检查 CD 管道Examine the CD pipeline
  • 提交对 Git 所做的更改并将其自动部署到 AzureCommit changes to Git and automatically deploy them to Azure
  • 清理资源Clean up resources

先决条件Prerequisites

使用 DevOps Projects 将 ASP.NET Core 应用部署到 AKSUse DevOps Projects to deploy an ASP.NET Core app to AKS

DevOps Projects 在 Azure Pipelines 中创建 CI/CD 管道。DevOps Projects creates a CI/CD pipeline in Azure Pipelines. 可以创建新的 Azure DevOps 组织,或使用现有的组织。You can create a new Azure DevOps organization or use an existing organization. DevOps Projects 还会在所选的 Azure 订阅中创建 Azure 资源,例如 AKS 群集。DevOps Projects also creates Azure resources, such as an AKS cluster, in the Azure subscription of your choice.

  1. 登录到 Azure 门户Sign in to the Azure portal.

  2. 在左窗格中,选择“创建资源”。In the left pane, select Create a resource.

  3. 在搜索框中键入 DevOps Projects,然后选择“创建”。In the search box, type DevOps Projects, and then select Create.

    DevOps Projects 仪表板

  4. 依次选择“.NET”、“下一步”。Select .NET, and then select Next.

  5. 在“选择应用程序框架”下,选择“ASP.NET Core”。Under Choose an application framework, select ASP.NET Core.

  6. 依次选择“Kubernetes 服务”、“下一步”。Select Kubernetes Service, and then select Next.

配置 Azure DevOps 和 Azure 订阅Configure Azure DevOps and an Azure subscription

  1. 创建新的 Azure DevOps 组织,或选择现有的组织。Create a new Azure DevOps organization, or select an existing organization.

  2. 输入 Azure DevOps 项目的名称。Enter a name for your Azure DevOps project.

  3. 选择 Azure 订阅。Select your Azure subscription.

  4. 若要查看其他 Azure 配置设置并确定 AKS 群集的节点数,请选择“更改”。To view additional Azure configuration settings and to identify the number of nodes for the AKS cluster, select Change.
    此窗格显示了各种用于配置 Azure 服务类型和位置的选项。This pane displays various options for configuring the type and location of Azure services.

  5. 退出 Azure 配置区域,然后选择“完成”。Exit the Azure configuration area, and then select Done.
    配置过程在几分钟后完成。After a few minutes, the process is completed. 此过程会在你的 Azure DevOps 组织的 Git 存储库中设置一个示例 ASP.NET Core 应用,创建 AKS 群集,执行 CI/CD 管道,并将应用部署到 Azure。A sample ASP.NET Core app is set up in a Git repo in your Azure DevOps organization, an AKS cluster is created, a CI/CD pipeline is executed, and your app is deployed to Azure.

    完成所有操作后,Azure DevOps Projects 仪表板会显示在 Azure 门户中。After all this is completed, the Azure DevOps Project dashboard is displayed in the Azure portal. 也可直接从 Azure 门户中的“所有资源”直接转到 DevOps Projects 仪表板。You can also go to the DevOps Projects dashboard directly from All resources in the Azure portal.

    在此仪表板中可以查看 Azure DevOps 代码存储库、CI/CD 管道和 AKS 群集。This dashboard provides visibility into your Azure DevOps code repository, your CI/CD pipeline, and your AKS cluster. 可在 Azure DevOps 管道中配置其他 CI/CD 选项。You can configure additional CI/CD options in your Azure DevOps pipeline. 在右侧,选择“浏览”即可查看正在运行的应用。At the right, select Browse to view your running app.

检查 AKS 群集Examine the AKS cluster

DevOps Projects 会自动配置一个 AKS 群集,你可以浏览和自定义此群集。DevOps Projects automatically configures an AKS cluster, which you can explore and customize. 若要了解 AKS 群集,请执行以下操作:To familiarize yourself with the AKS cluster, do the following:

  1. 转到 DevOps Projects 仪表板。Go to the DevOps Projects dashboard.

  2. 在右侧,选择“AKS 服务”。At the right, select the AKS service.
    此时会打开 AKS 群集的窗格。A pane opens for the AKS cluster. 可在此视图中执行各种操作,例如,监视容器运行状况、搜索日志、打开 Kubernetes 仪表板。From this view you can perform various actions, such as monitoring container health, searching logs, and opening the Kubernetes dashboard.

  3. 在右侧,选择“查看 Kubernetes 仪表板”。At the right, select View Kubernetes dashboard.
    (可选)执行相关步骤打开 Kubernetes 仪表板。Optionally, follow the steps to open the Kubernetes dashboard.

检查 CI 管道Examine the CI pipeline

DevOps Projects 自动在你的 Azure DevOps 组织中配置一个 CI/CD 管道。DevOps Projects automatically configures a CI/CD pipeline in your Azure DevOps organization. 可以浏览和自定义此管道。You can explore and customize the pipeline. 若要了解此管道,请执行以下操作:To familiarize yourself with it, do the following:

  1. 转到 DevOps Projects 仪表板。Go to the DevOps Projects dashboard.

  2. 在 DevOps Projects 仪表板顶部,选择“生成管道”。At the top of the DevOps Projects dashboard, select Build Pipelines.
    浏览器标签页会显示新项目的生成管道。A browser tab displays the build pipeline for your new project.

  3. 指向“状态”字段,然后选择省略号 (...)。Point to the Status field, and then select the ellipsis (...).
    菜单中会显示多个选项,例如,将新生成排队、暂停某个生成,以及编辑生成管道。A menu displays several options, such as queueing a new build, pausing a build, and editing the build pipeline.

  4. 选择“编辑”。Select Edit.

  5. 在此窗格中,可以检查生成管道的各种任务。In this pane, you can examine the various tasks for your build pipeline.
    该生成会执行各种任务,例如,从 Git 存储库提取源、还原依赖项,以及发布用于部署的输出。The build performs various tasks, such as fetching sources from the Git repo, restoring dependencies, and publishing outputs used for deployments.

  6. 在生成管道的顶部,选择生成管道名称。At the top of the build pipeline, select the build pipeline name.

  7. 将生成管道的名称更改为更具描述性的名称,选择“保存并排队”,然后选择“保存”。Change the name of your build pipeline to something more descriptive, select Save & queue, and then select Save.

  8. 在生成管道名称下,选择“历史记录”。Under your build pipeline name, select History.
    此窗格显示最近针对生成所做的更改的审核线索。This pane displays an audit trail of your recent changes for the build. Azure DevOps 会跟踪对生成管道所做的任何更改,并允许进行版本比较。Azure DevOps keeps track of any changes made to the build pipeline, and it allows you to compare versions.

  9. 选择“触发器”。Select Triggers.
    DevOps Projects 会自动创建一个 CI 触发器,每次向存储库提交内容都会启动新的生成。DevOps Projects automatically creates a CI trigger, and every commit to the repo starts a new build. (可选)可以选择在 CI 过程中包括或排除分库。Optionally, you can choose to include or exclude branches from the CI process.

  10. 选择“保留期”。Select Retention.
    可以根据方案指定策略,以保留或删除特定数目的生成。Depending on your scenario, you can specify policies to keep or remove a certain number of builds.

检查 CD 发布管道Examine the CD release pipeline

DevOps Projects 会自动创建并配置从 Azure DevOps 组织部署到 Azure 订阅所要执行的步骤。DevOps Projects automatically creates and configures the necessary steps to deploy from your Azure DevOps organization to your Azure subscription. 这些步骤包括配置 Azure 服务连接,以便在 Azure 订阅中进行 Azure DevOps 身份验证。These steps include configuring an Azure service connection to authenticate Azure DevOps to your Azure subscription. 自动化还会创建一个发布管道,该管道提供到 Azure 的 CD。The automation also creates a release pipeline, which provides the CD to Azure. 若要详细了解发布管道,请执行以下操作:To learn more about the release pipeline, do the following:

  1. 选择“生成和发布”,然后选择“发布”。Select Build and Release, and then select Releases.
    DevOps Projects 会创建一个发布管道,用于管理到 Azure 的部署。DevOps Projects creates a release pipeline to manage deployments to Azure.

  2. 选择发布管道旁边的省略号 (...),然后选择“编辑”。Select the ellipsis (...) next to your release pipeline, and then select Edit.
    发布管道包含一个管道,用于定义发布过程。The release pipeline contains a pipeline, which defines the release process.

  3. 在“项目”下选择“删除”。Under Artifacts, select Drop.
    在前述步骤中检查过的生成管道会生成用于项目的输出。The build pipeline you examined in the previous steps produces the output that's used for the artifact.

  4. 在“删除”图标的右侧,选择“持续部署触发器”。At the right of the Drop icon, select Continuous deployment trigger.
    此发布管道有一个已启用的 CD 触发器,每次有新的生成项目可用时,此触发器就会执行部署。This release pipeline has an enabled CD trigger, which executes a deployment every time a new build artifact is available. (可选)可以禁用此触发器,这样就需要手动执行部署。Optionally, you can disable the trigger so that your deployments require manual execution.

  5. 在右侧选择“查看发布”,以显示发布历史记录。At the right, select View releases to display a history of releases.

  6. 选择某个发布旁边的省略号 (...),然后选择“打开”。Select the ellipsis (...) next to a release, and then select Open.
    可以浏览多个菜单,例如发布摘要、关联的工作项和测试。You can explore several menus, such as a release summary, associated work items, and tests.

  7. 选择“提交”。Select Commits.
    此视图显示与此部署关联的代码提交。This view shows code commits that are associated with this deployment. 请对发布进行比较,查看不同部署之间的提交差异。Compare releases to view the commit differences between deployments.

  8. 选择“日志”。Select Logs.
    日志包含有关部署过程的有用信息。The logs contain useful information about the deployment process. 可以在部署期间和之后查看日志。You can view them both during and after deployments.

提交对 Azure Repos 所做的更改并将其自动部署到 AzureCommit changes to Azure Repos and automatically deploy them to Azure

备注

以下过程通过进行简单的文本更改来测试 CI/CD 管道。The following procedure tests the CI/CD pipeline by making a simple text change.

现在,可以使用可将最新工作部署到网站的 CI/CD 过程,与某个团队展开应用协作。You're now ready to collaborate with a team on your app by using a CI/CD process that automatically deploys your latest work to your website. 每次对 Git 存储库进行更改都会在 Azure DevOps 中启动一个生成,CD 管道可以执行到 Azure 的部署。Each change to the Git repo starts a build in Azure DevOps, and a CD pipeline executes a deployment to Azure. 遵循本部分所述的过程,或使用其他方法提交对存储库所做的更改。Follow the procedure in this section, or use another technique to commit changes to your repo. 例如,可以在常用工具或 IDE 中克隆 Git 存储库,然后将更改推送到此存储库。For example, you can clone the Git repo in your favorite tool or IDE, and then push changes to this repo.

  1. 在 Azure DevOps 菜单中选择“代码” > “文件”,然后转到你的存储库。In the Azure DevOps menu, select Code > Files, and then go to your repo.

  2. 转到 Views\Home 目录,选择 Index.cshtml 文件旁边的省略号 (...),然后选择“编辑”。Go to the Views\Home directory, select the ellipsis (...) next to the Index.cshtml file, and then select Edit.

  3. 对该文件进行更改,例如,在某个 div 标记内部添加一些文本。Make a change to the file, such as adding some text within one of the div tags.

  4. 在右上角选择“提交”,然后再次选择“提交”以推送更改。At the top right, select Commit, and then select Commit again to push your change.
    片刻之后,某个生成会在 Azure DevOps 中启动,并且某个发布将会执行以部署更改。After a few moments, a build starts in Azure DevOps and a release executes to deploy the changes. 使用 Azure DevOps 组织通过 DevOps Projects 仪表板或浏览器监视生成状态。Monitor the build status on the DevOps Projects dashboard or in the browser with your Azure DevOps organization.

  5. 发布完成后,刷新应用以验证更改。After the release is completed, refresh your app to verify your changes.

清理资源Clean up resources

测试后,可以清理资源,以避免产生费用。If you are testing, you can avoid accruing billing charges by cleaning up your resources. 不再需要本教程中创建的 AKS 群集和相关资源时,可将其删除。When they are no longer needed, you can delete the AKS cluster and related resources that you created in this tutorial. 为此,可以使用 DevOps Projects 仪表板上的“删除”功能。To do so, use the Delete functionality on the DevOps Projects dashboard.

重要

以下过程会永久删除资源。The following procedure permanently deletes resources. “删除”功能会销毁 DevOps Projects 中的项目在 Azure 和 Azure DevOps 中创建的数据,删除后无法检索这些数据。The Delete functionality destroys the data that's created by the project in DevOps Projects in both Azure and Azure DevOps, and you will be unable to retrieve it. 只能在仔细阅读提示后才使用此过程。Use this procedure only after you've carefully read the prompts.

  1. 在 Azure 门户中,转到 DevOps Projects 仪表板。In the Azure portal, go to the DevOps Projects dashboard.
  2. 在右上角选择“删除”。At the top right, select Delete.
  3. 出现提示时,请选择“是”以永久删除这些资源。At the prompt, select Yes to permanently delete the resources.

后续步骤Next steps

可以选择根据所在团队的需求修改这些生成和发布管道。You can optionally modify these build and release pipelines to meet the needs of your team. 也可以将此 CI/CD 模式用作其他管道的模板。You can also use this CI/CD pattern as a template for your other pipelines. 本教程介绍了如何:In this tutorial, you learned how to:

  • 使用 DevOps Projects 将 ASP.NET Core 应用部署到 AKSUse DevOps Projects to deploy an ASP.NET Core app to AKS
  • 配置 Azure DevOps 和 Azure 订阅Configure Azure DevOps and an Azure subscription
  • 检查 AKS 群集Examine the AKS cluster
  • 检查 CI 管道Examine the CI pipeline
  • 检查 CD 管道Examine the CD pipeline
  • 提交对 Git 所做的更改并将其自动部署到 AzureCommit changes to Git and automatically deploy them to Azure
  • 清理资源Clean up resources

若要详细了解如何使用 Kubernetes 仪表板,请参阅:To learn more about using the Kubernetes dashboard, see: