更换损坏的 GKE 节点实例的推荐方法是什么?

What's the recommended way to replace a bad GKE node instance?

使用 gcloud container clusters resize 我可以轻松地扩大和缩小集群。但是,我找不到在调整大小时将特定计算实例 vm 定位为删除的方法。

场景:我们的 Compute Engine 日志表明一个实例无法从早已消失的 Kubernetes pod 卸载卷。集群大小合适,故障节点正常服务容器,但处于最大 CPU 负载。

显然,我希望在关闭旧节点之前准备好新的 Kubernetes 节点。使用 gcloud compute 简单地调整大小然后删除实例是否安全,或者是否有一些容器感知的方法来执行此操作?

However I find no way to target a specific compute instance vm for removal when resizing down.

无法使用 GKE API 指定要删除哪个 VM,但您可以使用托管实例组 API 到 delete individual instances from the group(这会缩小您的节点数乘以删除的实例数,因此如果要替换节点,则需要扩展集群以进行补偿)。您可以通过 运行ning:

找到实例组名称
$ gcloud container clusters describe CLUSTER | grep instanceGroupManagers

Is it safe to simply resize up and then delete the instance using gcloud compute, or is there some container-aware way to do this?

如果您删除一个实例,托管实例组将用一个新实例替换它(因此,如果您扩大一个实例,然后删除有问题的实例,这将为您留下一个额外的节点)。如果您不担心暂时的容量损失,您可以删除 VM 并让它重新创建。

在删除实例之前,您可以运行 kubectl drain 从实例中删除工作负载。这将导致 pods 的重新调度比您简单地删除实例并等待控制器注意到它已经消失要快。

我们现在使用 multi-zone 集群,这意味着我需要一种新方法来获取实例组名称。当前 shell 个命令:

BAD_INSTANCE=[your node name from kubectl get nodes]

kubectl cordon $BAD_INSTANCE

kubectl drain $BAD_INSTANCE

gcloud compute instances describe --format='value[](metadata.items.created-by)' $BAD_INSTANCE

gcloud compute instance-groups managed delete-instances --instances=$BAD_INSTANCE --zone=[from describe output] [grp from describe output]

我不确定它是否有保证,但我尝试过两次,在缩小时,自动缩放器会选择耗尽的节点。所以为了替换一个节点,我扩大了规模,耗尽了节点,然后缩小了规模。

您可以使用以下命令重新创建坏节点:

gcloud compute instance-groups managed recreate-instances \
  --instances="$BAD_INSTANCE" \
  --zone="$ZONE" \
  "$INSTANCE_GROUP"

例如:

#!/bin/bash
set -e

BAD_INSTANCE=""
FULL_ZONE=$(gcloud compute instances describe --format='value[](zone)' "$BAD_INSTANCE")
FULL_INSTANCE_GROUP=$(gcloud compute instances describe --format='value[](metadata.items.created-by)' "$BAD_INSTANCE")
ZONE=${FULL_ZONE##*/}
INSTANCE_GROUP=${FULL_INSTANCE_GROUP##*/}

echo "Recreating node '$BAD_INSTANCE' from zone '$ZONE' in instance group '$INSTANCE_GROUP'"
sleep 10

gcloud compute instance-groups managed recreate-instances \
  --instances="$BAD_INSTANCE" \
  --zone="$ZONE" \
  "$INSTANCE_GROUP"

我使用自动缩放集群。一旦我封锁 UI 中的一个节点,系统似乎将其 pods 移动到其他节点,然后最终删除该节点。

根据之前的回答,我创建了这个shell脚本

#!/bin/bash

function gcp_delete_node_instance() {
  (
    local node=""
    set -e
    echo "Cordoning $node"
    kubectl cordon "$node"
    echo "Draining $node"
    kubectl drain "$node" --force --ignore-daemonsets
    zone="$(kubectl get node "$node" -o jsonpath='{.metadata.labels.topology\.gke\.io/zone}')"
    instance_group=$(gcloud compute instances describe --zone="$zone" --format='value[](metadata.items.created-by)' "$node")
    instance_group="${instance_group##*/}"
    echo "Deleting instance for node '$node' in zone '$zone' instance group '$instance_group'"
    gcloud compute instance-groups managed delete-instances --instances="$node" --zone="$zone" "$instance_group"
    echo "Deleting instance for node '$node' completed."
  )
}

gcp_delete_node_instance ""

GCP 自动创建替换节点并将其添加到池中。