Search code examples
google-cloud-platformgoogle-cloud-instances

How to auto-update Google Cloud Managed Instance Group


I use google cloud instance group with instance template (container registry) to manage many instances. But I have some problem with my MIG.

I set the container registry path to gcr.io/prok/server in my instance template.

I expect MIG to be updated automatically when I push new version docker image to container registry but it's not. MIG doesn't auto-update.

Many articles about MIG's said to create new instance template for update (v1->v2) and change instance template in MIG to new version template... but I want the MIG to automatically update without creating new instance template.

How do I do this?


Solution

  • It's possible to automate the entire process but it will require using Pub/Sub notifications to trigger the creation of the new temaplate:

    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.

    When you update your image or upload a new one to Artifact Registry a pub-sub message will be generated and at this point the only thing is to check if there are new messages, for example with gcloud (this is just an example):

    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
    

    This of course has to be done using a dedicated VM that will be doing the checking and creating new temaples for your update; again you can create one with gcloud: gcloud compute instance-templates create-with-container INSTANCE_TEMPLATE_NAME --container-image=CONTAINER_IMAGE

    After the template is ready you can run a 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]
    

    I also found this answer describing the process but with the addition of Cloud Scheduler - you may find it quite informative.