Skip to content

Commit 3b37f2c

Browse files
authored
Merge pull request #10 from thobiast/dev
Dev
2 parents feb995f + 8a6d49e commit 3b37f2c

File tree

12 files changed

+1194
-372
lines changed

12 files changed

+1194
-372
lines changed

.github/workflows/terraform.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ jobs:
1111
run:
1212
shell: bash
1313
steps:
14-
- uses: actions/checkout@v2
14+
- uses: actions/checkout@v4
1515
- name: Setup Terraform
16-
uses: hashicorp/setup-terraform@v1
16+
uses: hashicorp/setup-terraform@v3
1717
with:
1818
terraform_wrapper: false
19-
terraform_version: 0.13.x
19+
terraform_version: ">=1.3.0"
2020
- name: Terraform version
2121
run: terraform --version
2222
- name: Terraform init
2323
run: terraform init
2424
- name: Terraform format
25-
run: terraform fmt -check
25+
run: terraform fmt -diff -check
2626
- name: Terraform validate
2727
run: terraform validate

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,6 @@ tags
5353
[._]*.un~
5454

5555
# End of https://www.gitignore.io/api/vim,terraform
56+
57+
# tests
58+
.envrc

README.md

Lines changed: 211 additions & 87 deletions
Large diffs are not rendered by default.

examples/basic_http_lb.tf

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
###############################################
2+
# Example: Basic HTTP Load Balancer
3+
#
4+
# This example creates the following topology:
5+
#
6+
# - 1 Load Balancer
7+
# - 1 Listener:
8+
# - my_http -> port 80 -> my_pool
9+
#
10+
# - 1 Pool (HTTP) with health monitor:
11+
# - my_pool
12+
# - HTTP monitor
13+
# - Backend members: http instances
14+
#
15+
# Traffic flow:
16+
# VIP:80 -> my_http listener -> my_pool pool -> http instances
17+
###############################################
18+
19+
#############
20+
### Image ###
21+
#############
22+
data "openstack_images_image_v2" "image" {
23+
most_recent = true
24+
tag = "ubuntu-jammy"
25+
}
26+
27+
###################
28+
### Instance(s) ###
29+
###################
30+
resource "openstack_compute_instance_v2" "http" {
31+
count = 2
32+
33+
name = format("test-basic-http-%02d", count.index + 1)
34+
image_id = data.openstack_images_image_v2.image.id
35+
flavor_name = var.flavor_name
36+
key_pair = var.keypair_name
37+
security_groups = [var.secgroup_name]
38+
39+
network {
40+
uuid = var.network_id
41+
}
42+
43+
# Cloud-init user_data: install Apache and expose basic info on "/"
44+
user_data = <<-EOF
45+
#cloud-config
46+
runcmd:
47+
- apt update
48+
- apt -y install apache2
49+
- systemctl enable apache2
50+
- systemctl start apache2
51+
- echo "$(hostname; echo ; ip a)" > /var/www/html/index.html
52+
EOF
53+
}
54+
55+
#####################
56+
### Basic http LB ###
57+
#####################
58+
module "openstack-lb" {
59+
source = "git::https://github.com/thobiast/terraform-openstack-loadbalancer.git"
60+
61+
# Logical name for the load balancer
62+
lb_name = "example-basic-http"
63+
64+
# Subnet where the LB VIP will be allocated
65+
lb_vip_subnet_id = var.subnet_id
66+
67+
#################
68+
# HTTP listener #
69+
#################
70+
listeners = {
71+
# The map key "my_http" is the listener key
72+
my_http = {
73+
protocol = "HTTP"
74+
protocol_port = 80
75+
76+
# default_pool_key MUST match a pool key from the "pools" map below
77+
# Traffic arriving on this listener will be sent to pool "my_pool"
78+
default_pool_key = "my_pool"
79+
}
80+
}
81+
82+
############
83+
# One Pool #
84+
############
85+
pools = {
86+
# The map key "my_pool" is the pool key
87+
my_pool = {
88+
protocol = "HTTP"
89+
monitor = { type = "HTTP", delay = 5, timeout = 3, max_retries = 3 }
90+
91+
# Build members map. One entry per instance
92+
# Example:
93+
# {
94+
# "test-basic-http-01" = { address = "10.x.x.x", protocol_port = 80 }
95+
# "test-basic-http-02" = { address = "10.x.x.y", protocol_port = 80 }
96+
# }
97+
#
98+
# - key: instance name
99+
# - value: backend address and port
100+
members = {
101+
for inst in openstack_compute_instance_v2.http :
102+
inst.name => {
103+
address = inst.network[0].fixed_ip_v4
104+
protocol_port = 80
105+
}
106+
}
107+
}
108+
}
109+
}
110+
111+
#####################
112+
### LB VIP Output ###
113+
#####################
114+
output "vip_address" {
115+
description = "Allocated VIP address"
116+
value = module.openstack-lb.vip_address
117+
}

examples/l7_policy.tf

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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

Comments
 (0)