Search code examples
google-compute-enginegcloudgce-instance-group

How to add managed instance group to target pool?


How can an autoscaling managed instance group be added to a target pool?

It's easy enough to add existing instances to a target pool via

$ gcloud compute target-pools create mypool --region us-central1
$ gcloud compute target-pools add-instances mypool \
   --instances existing-instance1 existing-instance2 --zone us-central1-b

However, I want all the instances that appear in my autoscaling group to automatically be added to my target pool.


Solution

  • There are four different types of resources in your setup:

    • instance is a virtual machine
    • a target pool is a pool of instances used only for the purpose of L3 (e.g. IP) level network load balancing
    • managed instance group is a group of instances, used among others as a target for your autoscaler
    • autoscaler looks at a managed instance group and adds/deletes instances in this group as appropriate according to load (and your policy)

    To make sure that all the instances in your managed instance group (that is all the instances in your autoscaling group) are automatically in your target pool, you need to tell the managed instance group about the target pool. As @Faizan correctly mentioned, the command to do it is:

    gcloud compute instance-groups managed set-target-pools instance-group-name --target-pools your-target-pool
    

    The help page for this command seems more useful than the online documentation:

    gcloud compute instance-groups managed set-target-pools --help
    

    Please note that this help page seems to be out of date though. Setting a new target pool now DOES apply to existing instances in the group (when using the API version v1 or later). It was not the case in the beta versions (v1beta2).