How to properyly set up target groups for an application load balancer (ALB)?

314 views
Skip to first unread message

Tim Stewart

unread,
Mar 28, 2018, 6:50:01 PM3/28/18
to Ansible Project
Hi,

Background:

We are developing a system using a Microservices approach and deploying it with Ansible 2.5.0.

I'm writing a site.yml playbook that will provision two Ubuntu 16.04 servers for each of eight different microservices.  We're provisioning two of each microservice for HA and we may increase that number in the future.  For example, here are the microservice instances I want to create grouped by subnet (note this is not YAML):

SubNet-a:
    MicroService-1-a
    MicroService-2-a
    :
    :
    MicroService-8-a

SubNet-b:
    MicroService-1-b
    MicroService-2-b
    :
    :
    MicroService-8-b

Now I want to create a Target Group for each kind of microservice (e.g. one for MicroService-1, one for Microservice-2, etc.).  For example, here are the target groups and their targets:

MicroService-1-TargetGroup:
   MicroService-1-a
   MicroService-1-b
   
MicroService-2-TargetGroup:
   MicroService-2-a
   MicroService-2-b

:
:

MicroService-8-TargetGroup:
   MicroService-8-a
   MicroService-8-b

I then want to set up my ALB (using the elb_application_lb module) so that it has a listener with eight rules that route requests to the various Target groups based on a path_pattern condition.

Here is an example elb_target_group with hard-coded targets (based on Ansible's documentation on elb_target_groups):
- elb_target_group:
    name: Microservice-1
    protocol: http
    port: 81
    vpc_id: vpc-01234567
    health_check_path: /
    successful_response_codes: "200"
    targets:
      - Id: i-01234567
        Port: 80
      - Id: i-98765432
        Port: 80
    state: present
    wait_timeout: 200
    wait: True

But since my site.yml playbook creates the EC2 instances, I won't have access to the instance_ids referred to in the Id parameter above.

What is a good way of specifying the targets property of the elb_target_group (above) such that it only includes all of the EC2 instances of a particular kind of microservice?

For example:
- elb_target_group:
    name: Microservice-1
    protocol: http
    port: 81
    vpc_id: vpc-01234567
    health_check_path: /
    successful_response_codes: "200,250-260"
    targets:
      - Id: < Microservice-1-a's instance_id >
        Port: 80
      - Id: < Microservice-2-a's instance_id >
        Port: 80
    state: present
    wait_timeout: 200
    wait: True

Thanks!

Tim Stewart

unread,
Mar 29, 2018, 11:30:45 AM3/29/18
to Ansible Project
One more thing.   The reason I did not provide any of my ansible files is that I would rather find the right way of doing this and have to go and change all my Ansible files than have someone try to come up with a solution that works with the potentially bad approach I've started from.  Thanks!

Tim Stewart

unread,
Mar 29, 2018, 4:28:50 PM3/29/18
to Ansible Project
Here is the solution that I came up with through a good deal of trial and error:

vars:

    # The list of service names we will be deploying.
    services:
      - name: microservice-1
        port: 8080
      - name: microservice-2
        port: 8080
      - name: microservice-3
        port: 8080
     :
     :
      - name: microservice-8
        port: 8080

tasks:

    # some tasks omitted

    - name: Create Target Groups
      elb_target_group:
        region: "{{ region }}"
        name: "{{ item.name }}-tg"
        protocol: http
        port: 80
        vpc_id: "{{ vpc_id }}"
        health_check_path: /health
        health_check_interval: 60
        successful_response_codes: "200"
        targets: "{{ ec2_instances | json_query(query) | map('combine', {'Port': item.port}) | list }}"
        tags:
          service: "{{ item.name }}"
        state: present
      register: target_groups
      with_items: "{{ services }}"
      vars:
        query: "results[].tagged_instances[?tags.service=='{{item.name}}'][].{Id: id}"


On Wednesday, March 28, 2018 at 4:50:01 PM UTC-6, Tim Stewart wrote:

Tim Stewart

unread,
Mar 30, 2018, 11:07:35 AM3/30/18
to Ansible Project
BTW, the ec2_instances variable in my solution is the registered result of the ec2 task run with 'with_items'.
Reply all
Reply to author
Forward
0 new messages