|
| 1 | +############################################################## |
| 2 | +# Example: Load Balancer with L7 Policy (Redirect to Pool) |
| 3 | +# |
| 4 | +# This example creates the following topology: |
| 5 | +# |
| 6 | +# - 1 Load Balancer |
| 7 | +# - 1 HTTP listener on port 80 |
| 8 | +# - 2 backend pools: |
| 9 | +# - app_default -> frontend instances |
| 10 | +# - app_admin -> admin instances |
| 11 | +# (each pool includes an HTTP health monitor) |
| 12 | +# |
| 13 | +# - 1 L7 policy applied to the listener: |
| 14 | +# - Requests with PATH starting with "/admin" |
| 15 | +# are redirected to pool "app_admin". |
| 16 | +# - All other requests go to pool "app_default". |
| 17 | +# |
| 18 | +# Traffic flow: |
| 19 | +# curl http://<VIP>/ -> app_default pool |
| 20 | +# curl http://<VIP>/admin/... -> app_admin pool |
| 21 | +############################################################## |
| 22 | + |
| 23 | +############# |
| 24 | +### Image ### |
| 25 | +############# |
| 26 | +data "openstack_images_image_v2" "distro" { |
| 27 | + most_recent = true |
| 28 | + tag = "ubuntu-jammy" |
| 29 | +} |
| 30 | + |
| 31 | +############################## |
| 32 | +### Instance(s): front-end ### |
| 33 | +############################## |
| 34 | +resource "openstack_compute_instance_v2" "frontend" { |
| 35 | + count = 2 |
| 36 | + |
| 37 | + name = format("example-l7-frontend-%02d", count.index + 1) |
| 38 | + image_id = data.openstack_images_image_v2.distro.id |
| 39 | + flavor_name = var.flavor_name |
| 40 | + key_pair = var.keypair_name |
| 41 | + security_groups = [openstack_networking_secgroup_v2.my_lb.name] |
| 42 | + |
| 43 | + network { |
| 44 | + uuid = var.network_id |
| 45 | + } |
| 46 | + |
| 47 | + user_data = <<-EOF |
| 48 | +#cloud-config |
| 49 | +runcmd: |
| 50 | + - apt update |
| 51 | + - apt -y install apache2 |
| 52 | + - systemctl enable apache2 |
| 53 | + - systemctl start apache2 |
| 54 | + - echo "FRONTEND - $(hostname; echo ; ip a)" > /var/www/html/index.html |
| 55 | +EOF |
| 56 | +} |
| 57 | + |
| 58 | +########################## |
| 59 | +### Instance(s): admin ### |
| 60 | +########################## |
| 61 | +resource "openstack_compute_instance_v2" "admin" { |
| 62 | + count = 2 |
| 63 | + |
| 64 | + name = format("example-l7-admin-%02d", count.index + 1) |
| 65 | + image_id = data.openstack_images_image_v2.distro.id |
| 66 | + flavor_name = var.flavor_name |
| 67 | + key_pair = var.keypair_name |
| 68 | + security_groups = [openstack_networking_secgroup_v2.my_lb.name] |
| 69 | + |
| 70 | + network { |
| 71 | + uuid = var.network_id |
| 72 | + } |
| 73 | + |
| 74 | + user_data = <<-EOF |
| 75 | +#cloud-config |
| 76 | +runcmd: |
| 77 | + - apt update |
| 78 | + - apt -y install apache2 |
| 79 | + - systemctl enable apache2 |
| 80 | + - systemctl start apache2 |
| 81 | + - mkdir /var/www/html/admin |
| 82 | + - echo "ADMIN - $(hostname; echo ; ip a)" > /var/www/html/admin/index.html |
| 83 | +EOF |
| 84 | +} |
| 85 | + |
| 86 | +#################### |
| 87 | +### LB L7 Policy ### |
| 88 | +#################### |
| 89 | +module "openstack-lb" { |
| 90 | + source = "git::https://github.com/thobiast/terraform-openstack-loadbalancer.git" |
| 91 | + |
| 92 | + lb_name = "example-l7-policy" |
| 93 | + lb_vip_subnet_id = var.subnet_id |
| 94 | + |
| 95 | + ################# |
| 96 | + # HTTP listener # |
| 97 | + ################# |
| 98 | + listeners = { |
| 99 | + # The map key "my_http_listener" is the listener key |
| 100 | + # This same key will be used under "l7policies" map to attach L7 policies |
| 101 | + my_http_listener = { |
| 102 | + protocol = "HTTP" |
| 103 | + protocol_port = 80 |
| 104 | + # Must match a pool key from the "pools" map below. |
| 105 | + default_pool_key = "app_default" |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + ############################### |
| 110 | + # Two pools (default + admin) # |
| 111 | + ############################### |
| 112 | + pools = { |
| 113 | + # The map key "app_default" is the pool key |
| 114 | + # This is the default pool for normal traffic (non-/admin) |
| 115 | + app_default = { |
| 116 | + protocol = "HTTP" |
| 117 | + monitor = { type = "HTTP", delay = 5, timeout = 3, max_retries = 3 } |
| 118 | + members = { |
| 119 | + # One member per frontend instance |
| 120 | + for inst in openstack_compute_instance_v2.frontend : |
| 121 | + inst.name => { |
| 122 | + address = inst.network[0].fixed_ip_v4 |
| 123 | + protocol_port = 80 |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + # The map key "app_admin" is the pool key |
| 128 | + # This pool only receives traffic that matches the L7 /admin rule |
| 129 | + app_admin = { |
| 130 | + protocol = "HTTP" |
| 131 | + monitor = { type = "HTTP", delay = 5, timeout = 3, max_retries = 3 } |
| 132 | + members = { |
| 133 | + # One member per admin instance |
| 134 | + for inst in openstack_compute_instance_v2.admin : |
| 135 | + inst.name => { |
| 136 | + address = inst.network[0].fixed_ip_v4 |
| 137 | + protocol_port = 80 |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + ################################################################ |
| 144 | + # L7 Policy: redirect /admin* to app_admin pool # |
| 145 | + # Example: curl http://<vip>/admin/ # goes to app_admin pool # |
| 146 | + ################################################################ |
| 147 | + l7policies = { |
| 148 | + # This map key MUST match the listener key in the "listeners" map |
| 149 | + # In this case "my_http_listener" |
| 150 | + my_http_listener = { |
| 151 | + path_to_admin = { |
| 152 | + action = "REDIRECT_TO_POOL" |
| 153 | + position = 1 |
| 154 | + # Redirect to the pool whose key is "app_admin" in "pools" map |
| 155 | + redirect_pool_key = "app_admin" |
| 156 | + rules = { |
| 157 | + path_admin = { type = "PATH", compare_type = "STARTS_WITH", value = "/admin" } |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +##################### |
| 165 | +### LB VIP Output ### |
| 166 | +##################### |
| 167 | +output "vip_address" { |
| 168 | + description = "Allocated VIP address" |
| 169 | + value = module.openstack-lb.vip_address |
| 170 | +} |
0 commit comments