Rafik Avtoyan
Rafik Avtoyan

Reputation: 433

Subscribe for instances list update in GCE autoscaled group

Is there a way to get/subscribe the/for running instances list in GCE autoscaled group.
Via gcloud tool, we can periodically call for the list, but I would like to subscribe for the list update. I doubt that there is such API implementation for now in GCE(except project metadata), but I need to have such functionality in my application so I could write a logic on that.
Maybe someone has experience with a similar case or know any "hack" for this?

Upvotes: 0

Views: 53

Answers (2)

Anthony Metzidis
Anthony Metzidis

Reputation: 412

You can build this easily using (1) a pubsub topic "instance-group-changes" and (2) pushing events to this in your startup & shutdown scripts.

(1) Create the "instance-group-changes" topic

gcloud init
gcloud pubsub topics create instance-group-changes

(2) Modify the startup script for the instance group to send an addInstance event

note: be sure to add the "cloud pubsub api access scope" in the instance template

Use the meta-data service to obtain the instance-id, hostname, etc.

TOPIC=instance-group-changes
instance_id=`curl -s  http://metadata.google.internal/0.1/meta-data/instance-id`
gcloud pubsub topics publish "$TOPIC" \
   --attribute 'event=addInstance' \
   --message "instance_id=$instance_id"

(3) Modify the shutdown script to send a removeInstance event

TOPIC=instance-group-changes
instance_id=`curl -s http://metadata.google.internal/0.1/meta-data/instance-id`
gcloud pubsub topics publish "$TOPIC" \
   --attribute 'event=removeInstance' \
   --message "instance_id=$instance_id"

Testing

Create the subscription

gcloud pubsub subscriptions create sub-instance-group-changes --topic=instance-group-changes

Pull from the subscription

gcloud pubsub subscriptions pull --limit  5 sub-instance-group-changes
┌─────────────────────────────────┬─────────────────┬──────────────────────┬────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│               DATA              │    MESSAGE_ID   │      ATTRIBUTES      │                                                                               ACK_ID                                                                               │
├─────────────────────────────────┼─────────────────┼──────────────────────┼────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│
│ instance_id=5396233750823583338 │ 407816607936940 │ event=addInstance    │ XkASTD4HRElTK0MLKlgRTgQhIT4wPkVTRFAGFixdRkhRNxkIaFEOT14jPzUgKEUaC1MTUVx1Hk4Qb1gzdQdRDRlze2hxO1kaAFMTUHRdURsfWVx-SgNRChFze2d1bVMQBwtBU1b55f_L9q0zZhs9XBJLLD5-NTJFQQ │
│ instance_id=5396233750823583338 │ 407816742842477 │ event=removeInstance │ XkASTD4HRElTK0MLKlgRTgQhIT4wPkVTRFAGFixdRkhRNxkIaFEOT14jPzUgKEUaC1MTUVx1Hk4Qb1gzdQdRDRlze2hxO1kaAFMTUHRcURsfWVx-SgNRChFze2ZxaFIXAwZCVFb55f_L9q0zZhs9XBJLLD5-NTJFQQ │

Upvotes: 0

John Hanley
John Hanley

Reputation: 81416

To the best of my knowledge there is no method to subscribe to a list of instances in an managed instance group.

You will need to poll the managed instance group manually to determine the list of current instances.

gcloud compute instance-groups managed list <NAME>

This is a task that could be done very easily in Cloud Functions. At fixed intervals scan the group and email you the list in Json for example. The possibilities are endless.

Upvotes: 2

Related Questions