Loadbalancer terraform config
This commit is contained in:
parent
4b7591ffed
commit
7763e347a7
@ -4,12 +4,16 @@ module "private_server_group" {
|
|||||||
network_zone = var.network_zone
|
network_zone = var.network_zone
|
||||||
server_count = var.server_count
|
server_count = var.server_count
|
||||||
server_subnetwork_ip_range = var.subnetwork_ip_range
|
server_subnetwork_ip_range = var.subnetwork_ip_range
|
||||||
|
ssh_keys = var.ssh_keys
|
||||||
}
|
}
|
||||||
|
|
||||||
module "loadbalancer" {
|
module "loadbalancer" {
|
||||||
source = "./modules/loadbalancer"
|
source = "./modules/loadbalancer"
|
||||||
location = var.location
|
location = var.location
|
||||||
subnet_id = module.private_server_group.subnetwork_id
|
subnet_id = module.private_server_group.subnetwork_id
|
||||||
|
network_id = module.private_server_group.network_id
|
||||||
lb_internal_ip = var.lb_internal_ip
|
lb_internal_ip = var.lb_internal_ip
|
||||||
lb_external_ip = var.lb_external_ip
|
lb_external_ip = var.lb_external_ip
|
||||||
|
lb_service_id = var.lb_service_id
|
||||||
|
ssh_key_ids = module.private_server_group.ssh_key_ids
|
||||||
}
|
}
|
||||||
|
@ -1,26 +1,39 @@
|
|||||||
resource "hcloud_load_balancer" "main" {
|
resource "hcloud_server" "lb" {
|
||||||
name = "main"
|
name = "LB"
|
||||||
load_balancer_type = var.lb_type
|
image = "ubuntu-22.04"
|
||||||
|
server_type = "cx21"
|
||||||
location = var.location
|
location = var.location
|
||||||
algorithm {
|
labels = {
|
||||||
type = var.lb_algorithm
|
app = "lb"
|
||||||
}
|
}
|
||||||
|
ssh_keys = var.ssh_key_ids
|
||||||
|
|
||||||
|
public_net {
|
||||||
|
ipv4_enabled = true
|
||||||
|
ipv6_enabled = false
|
||||||
}
|
}
|
||||||
|
|
||||||
resource "hcloud_load_balancer_network" "main" {
|
network {
|
||||||
load_balancer_id = hcloud_load_balancer.main.id
|
network_id = var.network_id
|
||||||
subnet_id = var.subnet_id
|
|
||||||
ip = var.lb_internal_ip
|
ip = var.lb_internal_ip
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
resource "hcloud_load_balancer_target" "main" {
|
resource "hcloud_load_balancer_target" "main" {
|
||||||
type = "label_selector"
|
type = "server"
|
||||||
load_balancer_id = hcloud_load_balancer.main.id
|
load_balancer_id = var.lb_service_id
|
||||||
label_selector = "app=web"
|
|
||||||
use_private_ip = true
|
use_private_ip = true
|
||||||
|
server_id = hcloud_server.lb.id
|
||||||
}
|
}
|
||||||
|
|
||||||
resource "hcloud_load_balancer_service" "main" {
|
resource "hcloud_load_balancer_network" "main" {
|
||||||
load_balancer_id = hcloud_load_balancer.main.id
|
load_balancer_id = var.lb_service_id
|
||||||
protocol = "http"
|
network_id = var.network_id
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "hcloud_load_balancer_service" "load_balancer_service" {
|
||||||
|
load_balancer_id = var.lb_service_id
|
||||||
|
protocol = "tcp"
|
||||||
|
listen_port = 443
|
||||||
|
destination_port = 443
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,3 @@
|
|||||||
|
output "lb_public_ip" {
|
||||||
|
value = hcloud_server.lb.ipv4_address
|
||||||
|
}
|
@ -34,7 +34,22 @@ variable "lb_internal_ip" {
|
|||||||
type = string
|
type = string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
variable "lb_service_id" {
|
||||||
|
description = "ID of the loadbalancer service to attach to"
|
||||||
|
type = number
|
||||||
|
}
|
||||||
|
|
||||||
variable "subnet_id" {
|
variable "subnet_id" {
|
||||||
description = "ID of the subnetwork to attach the loadbalancer to"
|
description = "ID of the subnetwork to attach the loadbalancer to"
|
||||||
type = string
|
type = string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
variable "network_id" {
|
||||||
|
description = "ID of the network to attach the loadbalancer to"
|
||||||
|
type = string
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "ssh_key_ids" {
|
||||||
|
description = "SSH key IDs to add to the loadbalancer server"
|
||||||
|
type = list(number)
|
||||||
|
}
|
||||||
|
@ -8,14 +8,16 @@ resource "hcloud_server" "private_node" {
|
|||||||
app = "web"
|
app = "web"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ssh_keys = hcloud_ssh_key.main[*].id
|
||||||
|
|
||||||
public_net {
|
public_net {
|
||||||
ipv4_enabled = false
|
ipv4_enabled = true
|
||||||
ipv6_enabled = false
|
ipv6_enabled = false
|
||||||
}
|
}
|
||||||
|
|
||||||
network {
|
network {
|
||||||
network_id = hcloud_network.main.id
|
network_id = hcloud_network.main.id
|
||||||
ip = cidrhost(hcloud_network_subnet.main.ip_range, count.index + 2)
|
ip = cidrhost(hcloud_network_subnet.main.ip_range, count.index + 3)
|
||||||
}
|
}
|
||||||
depends_on = [
|
depends_on = [
|
||||||
hcloud_network_subnet.main
|
hcloud_network_subnet.main
|
||||||
@ -33,3 +35,9 @@ resource "hcloud_network_subnet" "main" {
|
|||||||
network_zone = var.network_zone
|
network_zone = var.network_zone
|
||||||
ip_range = var.server_subnetwork_ip_range
|
ip_range = var.server_subnetwork_ip_range
|
||||||
}
|
}
|
||||||
|
|
||||||
|
resource "hcloud_ssh_key" "main" {
|
||||||
|
count = length(var.ssh_keys)
|
||||||
|
name = "ssh-key-${count.index}"
|
||||||
|
public_key = file(var.ssh_keys[count.index])
|
||||||
|
}
|
||||||
|
@ -13,3 +13,7 @@ output "server_name" {
|
|||||||
output "server_network" {
|
output "server_network" {
|
||||||
value = hcloud_server.private_node[*].network
|
value = hcloud_server.private_node[*].network
|
||||||
}
|
}
|
||||||
|
|
||||||
|
output "ssh_key_ids" {
|
||||||
|
value = hcloud_ssh_key.main[*].id
|
||||||
|
}
|
||||||
|
@ -42,3 +42,8 @@ variable "server_subnetwork_ip_range" {
|
|||||||
type = string
|
type = string
|
||||||
default = "10.0.0.0/24"
|
default = "10.0.0.0/24"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
variable "ssh_keys" {
|
||||||
|
description = "SSH keys to add to the servers"
|
||||||
|
type = list(string)
|
||||||
|
}
|
||||||
|
8
terraform/prod.tfvars
Normal file
8
terraform/prod.tfvars
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
location = "nbg1"
|
||||||
|
network_zone = "eu-central"
|
||||||
|
server_count = 1
|
||||||
|
subnetwork_ip_range = "10.0.0.0/24"
|
||||||
|
lb_internal_ip = "10.0.0.100"
|
||||||
|
lb_external_ip = "167.235.105.161"
|
||||||
|
lb_service_id = 1399502
|
||||||
|
ssh_keys = ["rothe.pub"]
|
@ -33,3 +33,13 @@ variable "lb_internal_ip" {
|
|||||||
description = "Internal IP address of the loadbalancer"
|
description = "Internal IP address of the loadbalancer"
|
||||||
type = string
|
type = string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
variable "lb_service_id" {
|
||||||
|
description = "ID of the loadbalancer service to attach to"
|
||||||
|
type = number
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "ssh_keys" {
|
||||||
|
description = "SSH keys to add to servers"
|
||||||
|
type = list(string)
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user