轮替控制平面 IP


本页面介绍如何在运行版本 1.16.10-gke.8 及更高版本的 Google Kubernetes Engine (GKE) 集群中执行控制层面 IP 地址轮替。

概览

如需更改控制平面用于处理来自 Kubernetes API 的请求的 IP 地址,您可以执行 IP 轮替。控制平面 IP 地址是静态地址,除非您执行 IP 地址轮替,否则该地址不会改变。

IP 地址轮替会更改 SSL 证书和集群证书授权机构 (CA),从而限制先前地址与新地址的连接。

IP 地址轮替也是凭据轮替的一部分。

准备工作

在开始之前,请确保您已执行以下任务:

  • 启用 Google Kubernetes Engine API。
  • 启用 Google Kubernetes Engine API
  • 如果您要使用 Google Cloud CLI 执行此任务,请安装初始化 gcloud CLI。 如果您之前安装了 gcloud CLI,请运行 gcloud components update 以获取最新版本。

执行 IP 地址轮替

IP 轮替涉及多个步骤:

  1. 当您启动 IP 轮替时,除了原始 IP 地址以外,控制层面还会开始通过新 IP 地址提供服务。
  2. GKE 会重新创建节点池以使用新的 IP 地址。
  3. 启动轮替后,您必须更新集群的 API 客户端(例如使用 kubectl 命令行界面的开发机器)才能开始通过新 IP 地址与控制层面通信。
  4. 完成轮替后,控制层面便会停止通过旧 IP 地址处理流量。

启动轮替

  1. 要启动 IP 轮替,请运行以下命令:

    gcloud container clusters update CLUSTER_NAME \
        --start-ip-rotation
    

    CLUSTER_NAME 替换为您的集群的名称。

    输出类似于以下内容:

    This will start an IP Rotation on cluster CLUSTER_NAME.
    The master will be updated to serve on a new IP address in addition to
    the current IP address. Google Kubernetes Engine will then schedule recreation of all nodes
    to point to the new IP address.  If maintenance window is
    used, nodes are not recreated until a maintenance window occurs. See
    documentation on how to manually update nodes. This operation is
    long-running and will block other operations on the cluster (including
    delete) until it has run to completion.
    Do you want to continue (Y/n)?
    

    此命令会将控制平面配置为通过两个 IP 地址(原始地址和新地址)处理流量。

  2. 确认轮替并使 shell 保持打开状态,以使操作完成。

重新创建节点

重新配置 API 服务器以在新 IP 地址上提供服务后,GKE 会自动更新您的节点以使用新的 IP 地址。GKE 会将所有节点升级到最接近的受支持节点版本,从而重新创建节点。如需了解详情,请参阅节点池升级

默认情况下,GKE 会在您启动操作七天后自动完成 IP 地址轮替。如果集群中的活跃维护窗口或排除项会阻止 GKE 在这七天内重新创建某些节点,则 IP 地址轮替无法完成。

  • 如果您使用可能导致轮替失败的维护排除项或维护窗口,请手动升级集群以强制重新创建节点:

    gcloud container clusters upgrade CLUSTER_NAME \
        --location=LOCATION \
        --cluster-version=VERSION
    

    VERSION 替换为集群已在使用的同一 GKE 版本

    如需了解详情,请参阅维护窗口的注意事项

检查节点池重新创建的进度

  1. 要启动轮替操作,请运行以下命令:

    gcloud container operations list \
        --filter="operationType=UPGRADE_NODES AND status=RUNNING" \
        --format="value(name)"
    

    此命令返回节点升级操作的 ID

  2. 要轮询操作,请将操作 ID 传递给以下命令:

    gcloud container operations wait OPERATION_ID
    

节点池会逐一重新创建,并且每个节点池都有自己的操作。如果您有多个节点池,请按照以下说明轮询每个操作。

更新 API 客户端

启动 IP 地址轮替后,您必须更新集群外的所有 API 客户端(例如开发者机器上的 kubectl)以指向新的 IP 地址。

要更新 API 客户端,请为每个客户端运行以下命令:

gcloud container clusters get-credentials CLUSTER_NAME

更新硬编码 IP 地址和防火墙规则

如果您在环境中对控制平面的 IP 地址进行了硬编码,或者有防火墙规则以控制平面的 IP 地址为目标,请将地址更新为新的 IP 地址。如果您完成轮替但未在应用和防火墙规则中更新 IP 地址,则当 GKE 停止在之前的控制平面 IP 地址上提供服务时,这些资源可能会中断。

完成轮替

更新集群外部的 API 客户端后,完成轮替以将控制平面配置为仅通过新 IP 地址处理流量。

要完成轮替,请运行以下命令:

gcloud container clusters update CLUSTER_NAME \
    --complete-ip-rotation

输出内容类似如下:

This will complete the in-progress IP Rotation on cluster CLUSTER_NAME.
The master will be updated to stop serving on the old IP address and only
serve on the new IP address. Make sure all API clients have been updated
to communicate with the new IP address (e.g. by running `gcloud container
clusters get-credentials --project PROJECT_ID --region COMPUTE_REGION
CLUSTER_NAME`). This operation is long-running and will
block other operations on the cluster (including delete) until it has
run to completion.

如果 IP 地址轮替无法完成,并返回类似于以下内容的错误消息,请参阅问题排查

ERROR: (gcloud.container.clusters.update) ResponseError: code=400, message=Node pool "test-pool-1" requires recreation.

后续步骤