如何自动更新 Google 云托管实例组

How to auto-update Google Cloud Managed Instance Group

我使用 google 带有实例模板(容器注册表)的云实例组来管理许多实例。但是我的 MIG 有一些问题。

我在实例模板中将容器注册表路径设置为 gcr.io/prok/server

我希望 MIG 在我将新版本 docker 映像推送到容器注册表时自动更新,但事实并非如此。 MIG 不会自动更新。

许多关于 MIG 的文章都说要为更新创建新实例模板 (v1->v2) 并将 MIG 中的实例模板更改为新版本模板...但我希望 MIG 自动更新而不创建新实例模板。

我该怎么做?

可以使整个过程自动化,但需要使用 Pub/Sub notifications 来触发新模板的创建:

When changes are made to your Container Registry repository, such as when images are pushed, tagged, or deleted, you can receive notifications using Pub/Sub.

Pub/Sub publishes messages about your repository to named resources called topics. These messages are received by applications subscribed to Pub/Sub topics. Subscriber applications send notifications when your repository's state changes.

当您更新图像或将新图像上传到 Artifact Registry 时,将生成一条发布-订阅消息,此时唯一要做的就是检查是否有新消息,例如 gcloud(这只是一个例子):

gcloud pubsub subscriptions pull sub1
DATA: {"action":"INSERT","digest":"gcr.io/project-name/my-repo/busybox@sha256:34efe68cca33507682b1673c851700ec66839ecf94d19b928176e20d20e02413","tag":"gcr.io/project-name/my-repo/busybox:latest"}
MESSAGE_ID: 3401997393579
ORDERING_KEY:
ATTRIBUTES:
DELIVERY_ATTEMPT:
ACK_ID: UAYWLF1GSFE3GAoRRAIAE8CKF15MFc8QV98BT4NGXJ9

这当然必须使用专用 VM 来完成,该 VM 将为您的更新进行检查和创建新模板;你可以再次 create onegcloud: gcloud compute instance-templates create-with-container INSTANCE_TEMPLATE_NAME --container-image=CONTAINER_IMAGE

模板准备就绪后,您可以运行一个rolling update:

gcloud compute instance-groups managed rolling-action start-update INSTANCE_GROUP_NAME \
    --version=template=INSTANCE_TEMPLATE \
    --type=opportunistic|proactive \
    [--zone=ZONE | --region=REGION]

我还找到了 this answer describing the process but with the addition of Cloud Scheduler - 您可能会发现它非常有用。