教程:在 Azure Kubernetes 服务 (AKS) 中升级 KubernetesTutorial: Upgrade Kubernetes in Azure Kubernetes Service (AKS)

在应用程序和群集生命周期中,你可能希望升级到最新可用的 Kubernetes 版本并使用新功能。As part of the application and cluster lifecycle, you may wish to upgrade to the latest available version of Kubernetes and use new features. 可使用 Azure CLI 升级 Azure Kubernetes 服务 (AKS) 群集。An Azure Kubernetes Service (AKS) cluster can be upgraded using the Azure CLI.

在本教程的第 7 部分中,升级了 Kubernetes 群集。In this tutorial, part seven of seven, a Kubernetes cluster is upgraded. 学习如何:You learn how to:

  • 确定 Kubernetes 的当前版本和可用版本Identify current and available Kubernetes versions
  • 升级 Kubernetes 节点Upgrade the Kubernetes nodes
  • 验证升级是否成功Validate a successful upgrade

开始之前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. 如果尚未完成这些步骤,并且想要逐一完成,请先参阅教程 1 - 创建容器映像If you have not done these steps, and would like to follow along, start with Tutorial 1 - Create container images.

本教程需要运行 Azure CLI 2.0.53 或更高版本。This tutorial requires that you are 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.

获取可用的群集版本Get available cluster versions

在升级群集之前,请使用 az aks get-upgrades 命令检查哪些 Kubernetes 版本可用于升级:Before you upgrade a cluster, use the az aks get-upgrades command to check which Kubernetes releases are available for upgrade:

az aks get-upgrades --resource-group myResourceGroup --name myAKSCluster

在以下示例中,当前版本为 1.15.11,可用版本将显示在“升级”下 。In the following example, the current version is 1.15.11, and the available versions are shown under upgrades.

{
  "agentPoolProfiles": null,
  "controlPlaneProfile": {
    "kubernetesVersion": "1.15.11",
    ...
    "upgrades": [
      {
        "isPreview": null,
        "kubernetesVersion": "1.16.8"
      },
      {
        "isPreview": null,
        "kubernetesVersion": "1.16.9"
      }
    ]
  },
  ...
}

升级群集Upgrade a cluster

为尽量减少对正在运行的应用程序造成中断,AKS 节点已进行仔细隔离和排空。To minimize disruption to running applications, AKS nodes are carefully cordoned and drained. 在此过程中,执行以下步骤:In this process, the following steps are performed:

  1. Kubernetes 计划程序阻止在要升级的节点上计划其他 Pod。The Kubernetes scheduler prevents additional pods being scheduled on a node that is to be upgraded.
  2. 计划在群集中的其他节点上运行该节点上运行的 Pod。Running pods on the node are scheduled on other nodes in the cluster.
  3. 创建运行最新 Kubernetes 组件的节点。A node is created that runs the latest Kubernetes components.
  4. 准备好新节点并将其加入群集后,Kubernetes 计划程序开始在该群集上运行 Pod。When the new node is ready and joined to the cluster, the Kubernetes scheduler begins to run pods on it.
  5. 删除旧节点,群集的下一个节点随即开始隔离和排空进程。The old node is deleted, and the next node in the cluster begins the cordon and drain process.

使用 az aks upgrade 命令升级 AKS 群集。Use the az aks upgrade command to upgrade the AKS cluster.

az aks upgrade \
    --resource-group myResourceGroup \
    --name myAKSCluster \
    --kubernetes-version KUBERNETES_VERSION

备注

一次只能升级一个次要版本。You can only upgrade one minor version at a time. 例如,可以从 1.14.x 升级到 1.15.x,但不能从 1.14.x 直接升级到 1.16.x 。For example, you can upgrade from 1.14.x to 1.15.x, but cannot upgrade from 1.14.x to 1.16.x directly. 若要从 1.14.x 升级到 1.16.x,请先从 1.14.x 升级到 1.15.x,然后再执行一次升级从 1.15.x 升级到 1.16.x 。To upgrade from 1.14.x to 1.16.x, first upgrade from 1.14.x to 1.15.x, then perform another upgrade from 1.15.x to 1.16.x.

以下精简示例输出显示升级到 1.16.8 的结果。The following condensed example output shows the result of upgrading to 1.16.8. 请注意,kubernetesVersion 现报告 1.16.8 :Notice the kubernetesVersion now reports 1.16.8:

{
  "agentPoolProfiles": [
    {
      "count": 3,
      "maxPods": 110,
      "name": "nodepool1",
      "osType": "Linux",
      "storageProfile": "ManagedDisks",
      "vmSize": "Standard_DS1_v2",
    }
  ],
  "dnsPrefix": "myAKSClust-myResourceGroup-19da35",
  "enableRbac": false,
  "fqdn": "myaksclust-myresourcegroup-19da35-bd54a4be.hcp.chinaeast2.cx.prod.service.azk8s.cn",
  "id": "/subscriptions/<Subscription ID>/resourcegroups/myResourceGroup/providers/Microsoft.ContainerService/managedClusters/myAKSCluster",
  "kubernetesVersion": "1.16.8",
  "location": "chinaeast2",
  "name": "myAKSCluster",
  "type": "Microsoft.ContainerService/ManagedClusters"
}

验证升级Validate an upgrade

使用 az aks show 命令确认升级成功,如下所示:Confirm that the upgrade was successful using the az aks show command as follows:

az aks show --resource-group myResourceGroup --name myAKSCluster --output table

以下示例输出显示 AKS 群集运行 KubernetesVersion 1.16.8:The following example output shows the AKS cluster runs KubernetesVersion 1.16.8:

Name          Location    ResourceGroup    KubernetesVersion    ProvisioningState    Fqdn
------------  ----------  ---------------  -------------------  -------------------  ----------------------------------------------------------------
myAKSCluster  chinaeast2      myResourceGroup  1.16.8               Succeeded            myaksclust-myresourcegroup-19da35-bd54a4be.hcp.chinaeast2.cx.prod.service.azk8s.cn

删除群集Delete the cluster

由于此教程是该系列的最后一部分,因此可能需要删除 AKS 群集。As this tutorial is the last part of the series, you may want to delete the AKS cluster. 当 Kubernetes 节点在 Azure 虚拟机 (VM) 上运行时,即使你不使用群集,它们也会继续产生费用。As the Kubernetes nodes run on Azure virtual machines (VMs), they continue to incur charges even if you don't use the cluster. 可以使用 az group delete 命令删除资源组、容器服务及所有相关资源。Use the az group delete command to remove the resource group, container service, and all related resources.

az group delete --name myResourceGroup --yes --no-wait

备注

删除群集时,AKS 群集使用的 Azure Active Directory 服务主体不会被删除。When you delete the cluster, the Azure Active Directory service principal used by the AKS cluster is not removed. 有关如何删除服务主体的步骤,请参阅 AKS 服务主体的注意事项和删除For steps on how to remove the service principal, see AKS service principal considerations and deletion. 如果你使用了托管标识,则该标识由平台托管,不需要你预配或轮换任何机密。If you used a managed identity, the identity is managed by the platform and does not require you to provision or rotate any secrets.

后续步骤Next steps

在本教程中,在 AKS 群集中升级了 Kubernetes。In this tutorial, you upgraded Kubernetes in an AKS cluster. 你已了解如何执行以下操作:You learned how to:

  • 确定 Kubernetes 的当前版本和可用版本Identify current and available Kubernetes versions
  • 升级 Kubernetes 节点Upgrade the Kubernetes nodes
  • 验证升级是否成功Validate a successful upgrade

请单击以下链接了解有关 AKS 的详细信息。Follow this link to learn more about AKS.