迁移到 Azure Kubernetes 服务 (AKS)Migrate to Azure Kubernetes Service (AKS)

本文可帮助你规划并成功执行到 Azure Kubernetes 服务 (AKS) 的迁移。This article helps you plan and execute a successful migration to Azure Kubernetes Service (AKS). 为帮助你做出关键决策,本指南详细提供了 AKS 的当前建议配置。To help you make key decisions, this guide provides details for the current recommended configuration for AKS. 本文不介绍每种方案,在适当的情况下,本文将包含有关规划成功迁移的更详细信息的链接。This article doesn't cover every scenario, and where appropriate, the article contains links to more detailed information for planning a successful migration.

可以借助本文档来支持以下方案:This document can be used to help support the following scenarios:

迁移时,请确保目标 Kubernetes 版本在 AKS 支持的范围内。When migrating, ensure your target Kubernetes version is within the supported window for AKS. 如果使用的版本较低,它可能不在支持范围内,需要进行升级才会受到 AKS 的支持。If using an older version, it may not be within the supported range and require upgrading versions to be supported by AKS. 有关详细信息,请参阅 AKS 支持的 Kubernetes 版本See AKS supported Kubernetes versions for more information.

若要迁移到较新版本的 Kubernetes,请参阅 Kubernetes 版本和版本偏差支持策略If you're migrating to a newer version of Kubernetes, review Kubernetes version and version skew support policy.

根据具体的方案,可以借助多个开源工具来完成迁移:Several open-source tools can help with your migration, depending on your scenario:

本文将汇总以下各项的迁移详细信息:In this article we will summarize migration details for:

  • 包含标准负载均衡器和虚拟机规模集的 AKSAKS with Standard Load Balancer and Virtual Machine Scale Sets
  • 现有的附加 Azure 服务Existing attached Azure Services
  • 确保有效配额Ensure valid quotas
  • 高可用性和业务连续性High Availability and business continuity
  • 无状态应用程序的注意事项Considerations for stateless applications
  • 有状态应用程序的注意事项Considerations for stateful applications
  • 群集配置的部署Deployment of your cluster configuration

包含标准负载均衡器和虚拟机规模集的 AKSAKS with Standard Load Balancer and Virtual Machine Scale Sets

AKS 是一个托管服务,它提供独特的功能和较低的管理开销。AKS is a managed service offering unique capabilities with lower management overhead. 既然是托管服务,必须从 AKS 支持的一系列区域中进行选择。As a result of being a managed service, you must select from a set of regions which AKS supports. 从现有群集过渡到 AKS 可能需要修改现有的应用程序,使其在 AKS 托管控制平面上保持正常。The transition from your existing cluster to AKS may require modifying your existing applications so they remain healthy on the AKS managed control plane.

我们建议使用虚拟机规模集Azure 标准负载均衡器支持的 AKS 群集,以确保获得多个节点池授权的 IP 范围群集自动缩放程序等功能,以及已发布的其他新功能。We recommend using AKS clusters backed by Virtual Machine Scale Sets and the Azure Standard Load Balancer to ensure you get features such as multiple node pools, Authorized IP ranges, Cluster Autoscaler, and other new features as they are released.

虚拟机可用性集支持的 AKS 群集缺少上述许多功能的支持。AKS clusters backed by Virtual Machine Availability Sets lack support for many of these features.

以下示例使用虚拟机规模集支持的单个节点池创建 AKS 群集。The following example creates an AKS cluster with single node pool backed by a virtual machine scale set. 它使用标准负载均衡器。It uses a standard load balancer. 它还在群集的节点池中启用群集自动缩放程序,并将节点的最小数目设置为 1,最大数目设置为 3It also enables the cluster autoscaler on the node pool for the cluster and sets a minimum of 1 and maximum of 3 nodes:

# First create a resource group
az group create --name myResourceGroup --location chinaeast2

# Now create the AKS cluster and enable the cluster autoscaler
az aks create \
  --resource-group myResourceGroup \
  --name myAKSCluster \
  --node-count 1 \
  --vm-set-type VirtualMachineScaleSets \
  --load-balancer-sku standard \
  --enable-cluster-autoscaler \
  --min-count 1 \
  --max-count 3

现有的附加 Azure 服务Existing attached Azure Services

在迁移群集时,你可能附加了外部 Azure 服务。When migrating clusters you may have attached external Azure services. 这些服务不需要重新创建资源,但需要更新从旧群集到新群集的连接,才能保持现有的功能。These do not require resource recreation, but they will require updating connections from previous to new clusters to maintain functionality.

  • Azure 容器注册表Azure Container Registry
  • Log AnalyticsLog Analytics
  • Application InsightsApplication Insights
  • 流量管理器Traffic Manager
  • 存储帐户Storage Account
  • 外部数据库External Databases

确保有效配额Ensure valid quotas

由于在迁移期间要将其他虚拟机部署到订阅中,因此,应该检查配额和限制是否足以应对这些资源。Because additional virtual machines will be deployed into your subscription during migration, you should verify that your quotas and limits are sufficient for these resources. 可能需要请求提高 vCPU 配额You may need to request an increase in vCPU quota.

可能需要请求提高网络配额,以确保不会耗尽 IP。You may need to request an increase for Network quotas to ensure you don't exhaust IPs. 有关更多信息,请参阅 AKS 的网络和 IP 范围See networking and IP ranges for AKS for additional information.

有关详细信息,请参阅 Azure 订阅和服务限制For more information, see Azure subscription and service limits. 若要查看当前配额,请在 Azure 门户中转到订阅边栏选项卡,选择自己的订阅,然后选择“用量 + 配额”。To check your current quotas, in the Azure portal, go to the subscriptions blade, select your subscription, and then select Usage + quotas.

高可用性和业务连续性High Availability and Business Continuity

如果应用程序无法处理停机,则你需要遵循高可用性迁移方案的最佳做法。If your application cannot handle downtime, you will need to follow best practices for high availability migration scenarios. 有关复杂业务连续性规划、灾难恢复和最大化运行时间的最佳做法超出了本文档的范畴。Best practices for complex business continuity planning, disaster recovery, and maximizing uptime are beyond the scope of this document. 请阅读 Azure Kubernetes 服务 (AKS) 中实现业务连续性和灾难恢复的最佳做法了解详细信息。Read more about Best practices for business continuity and disaster recovery in Azure Kubernetes Service (AKS) to learn more.

对于复杂的应用程序,我们通常会分阶段迁移,而不是一次性整个迁移。For complex applications, you'll typically migrate over time rather than all at once. 这意味着,新旧环境可能需要通过网络进行通信。That means that the old and new environments might need to communicate over the network. 以前能够使用 ClusterIP 服务进行通信的应用程序可能需要公开为 LoadBalancer 类型,并得到相应的保护。Applications that previously used ClusterIP services to communicate might need to be exposed as type LoadBalancer and be secured appropriately.

若要完成迁移,需将客户端指向 AKS 上运行的新服务。To complete the migration, you'll want to point clients to the new services that are running on AKS. 建议通过将 DNS 更新为指向 AKS 群集前面的负载均衡器,来重定向流量。We recommend that you redirect traffic by updating DNS to point to the Load Balancer that sits in front of your AKS cluster.

Azure 流量管理器可将客户定向到所需的 Kubernetes 群集和应用程序实例。Azure Traffic Manager can direct customers to the desired Kubernetes cluster and application instance. 流量管理器是可以在区域间分布网络流量的基于 DNS 的流量负载均衡器。Traffic Manager is a DNS-based traffic load balancer that can distribute network traffic across regions. 为获得最佳性能和冗余,在进入 AKS 群集之前,通过流量管理器来定向所有应用程序流量。For the best performance and redundancy, direct all application traffic through Traffic Manager before it goes to your AKS cluster. 在多群集部署中,客户应连接到指向每个 AKS 群集上的服务的流量管理器 DNS 名称。In a multicluster deployment, customers should connect to a Traffic Manager DNS name that points to the services on each AKS cluster. 使用流量管理器终结点定义这些服务。Define these services by using Traffic Manager endpoints. 每个终结点都是服务负载均衡器 IP。Each endpoint is the service load balancer IP. 使用此配置可将网络流量从一个区域的流量管理器终结点定向到另一个区域的终结点。Use this configuration to direct network traffic from the Traffic Manager endpoint in one region to the endpoint in a different region.

将 AKS 与流量管理器配合使用

无状态应用程序的注意事项Considerations for stateless applications

无状态应用程序的迁移最直截了当。Stateless application migration is the most straightforward case. 将资源定义(YAML 或 Helm)应用到新群集,确保一切按预期进行,然后重定向流量以激活新群集。Apply your resource definitions (YAML or Helm) to the new cluster, make sure everything works as expected, and redirect traffic to activate your new cluster.

有状态应用程序的注意事项Considerations for stateful applications

精心规划有状态应用程序的迁移,以避免数据丢失或意外停机。Carefully plan your migration of stateful applications to avoid data loss or unexpected downtime.

如果使用 Azure 文件存储,则可以将文件共享作为卷装载到新群集中:If you use Azure Files, you can mount the file share as a volume into the new cluster:

如果使用 Azure 托管磁盘,则只能装载未附加到任何 VM 的磁盘:If you use Azure Managed Disks, you can only mount the disk if unattached to any VM:

如果这两种方法都不起作用,可以使用备份和还原选项:If neither of those approaches work, you can use a backup and restore options:

Azure 文件Azure Files

与磁盘不同,Azure 文件可同时装载到多个主机。Unlike disks, Azure Files can be mounted to multiple hosts concurrently. 在 AKS 群集中,Azure 和 Kubernetes 都不会阻止你创建 ACS 群集仍在使用的 Pod。In your AKS cluster, Azure and Kubernetes don't prevent you from creating a pod that your ACS cluster still uses. 若要防止数据丢失和意外行为,请确保这两个群集不会同时向相同的文件写入数据。To prevent data loss and unexpected behavior, ensure that the clusters don't write to the same files at the same time.

如果应用程序可以托管指向同一文件共享的多个副本,请遵循无状态迁移步骤,将 YAML 定义部署到新群集。If your application can host multiple replicas that point to the same file share, follow the stateless migration steps and deploy your YAML definitions to your new cluster. 否则,可以采用包括以下步骤的可行迁移方法:If not, one possible migration approach involves the following steps:

  • 验证应用程序是否正常工作。Validate your application is working correctly.
  • 将实时流量指向新的 AKS 群集。Point your live traffic to your new AKS cluster.
  • 断开旧群集的连接。Disconnect the old cluster.

若要从空共享开始,然后创建源数据的副本,可以使用 az storage file copy 命令迁移数据。If you want to start with an empty share and make a copy of the source data, you can use the az storage file copy commands to migrate your data.

迁移永久性卷。Migrating persistent volumes

将现有的永久性卷迁移到 AKS 时,通常需要遵循以下步骤:If you're migrating existing persistent volumes to AKS, you'll generally follow these steps:

  • 暂停写入到应用程序。Quiesce writes to the application. (此步骤是可选的,需要停机。)(This step is optional and requires downtime.)
  • 创建磁盘的快照。Take snapshots of the disks.
  • 从快照创建新的托管磁盘。Create new managed disks from the snapshots.
  • 在 AKS 中创建永久性卷。Create persistent volumes in AKS.
  • 将 Pod 规范更新为使用现有卷而不是 PersistentVolumeClaims(静态预配)。Update pod specifications to use existing volumes rather than PersistentVolumeClaims (static provisioning).
  • 将应用程序部署到 AKS。Deploy your application to AKS.
  • 验证应用程序是否正常工作。Validate your application is working correctly.
  • 将实时流量指向新的 AKS 群集。Point your live traffic to your new AKS cluster.

重要

如果不暂停写入,则需要将数据复制到新部署。If you choose not to quiesce writes, you'll need to replicate data to the new deployment. 否则在创建磁盘快照后,写入的数据将会丢失。Otherwise you'll miss the data that was written after you took the disk snapshots.

可以借助一些开源工具来创建托管磁盘,并在 Kubernetes 群集之间迁移卷:Some open-source tools can help you create managed disks and migrate volumes between Kubernetes clusters:

群集配置的部署Deployment of your cluster configuration

建议使用现有的持续集成 (CI) 和持续交付 (CD) 管道将已知正常的配置部署到 AKS。We recommend that you use your existing Continuous Integration (CI) and Continuous Deliver (CD) pipeline to deploy a known-good configuration to AKS. 可以使用 Azure Pipelines 生成应用程序并将其部署到 AKSYou can use Azure Pipelines to build and deploy your applications to AKS. 克隆现有的部署任务,并确保 kubeconfig 指向新的 AKS 群集。Clone your existing deployment tasks and ensure that kubeconfig points to the new AKS cluster.

如果无法做到这一点,请从现有 Kubernetes 群集导出资源定义,并将其应用到 AKS。If that's not possible, export resource definitions from your existing Kubernetes cluster and then apply them to AKS. 可以使用 kubectl 导出对象。You can use kubectl to export objects.

kubectl get deployment -o=yaml --export > deployments.yaml

将现有资源移到另一个区域Moving existing resources to another region

你可能需要将 AKS 群集移到 AKS 支持的不同区域You may want to move your AKS cluster to a different region supported by AKS. 我们建议你在其他区域中创建一个新群集,然后将资源和应用程序部署到新群集。We recommend that you create a new cluster in the other region then deploy your resources and applications to your new cluster. 此外,如果在 AKS 群集上运行任何服务(例如 Azure Dev Spaces),则还需要在新区域中的群集上安装和配置这些服务。In addition, if you have any services such as Azure Dev Spaces running on your AKS cluster, you will also need to install and configure those services on your cluster in the new region.

本文汇总了以下各项的迁移详细信息:In this article we summarized migration details for:

  • 包含标准负载均衡器和虚拟机规模集的 AKSAKS with Standard Load Balancer and Virtual Machine Scale Sets
  • 现有的附加 Azure 服务Existing attached Azure Services
  • 确保有效配额Ensure valid quotas
  • 高可用性和业务连续性High Availability and business continuity
  • 无状态应用程序的注意事项Considerations for stateless applications
  • 有状态应用程序的注意事项Considerations for stateful applications
  • 群集配置的部署Deployment of your cluster configuration