|
| 1 | +terraform { |
| 2 | + required_providers { |
| 3 | + mgc = { |
| 4 | + source = "magalucloud/mgc" |
| 5 | + version = "0.29.2" |
| 6 | + } |
| 7 | + } |
| 8 | +} |
| 9 | + |
| 10 | +module "network" { |
| 11 | + source = "./modules/network" |
| 12 | + |
| 13 | + api_key = var.api_key |
| 14 | + |
| 15 | + project_name = var.project_name |
| 16 | + allowed_tcp_ports = var.allowed_tcp_ports |
| 17 | + allowed_udp_ports = var.allowed_udp_ports |
| 18 | + lb_tcp_ports = var.lb_tcp_ports |
| 19 | + lb_udp_ports = var.lb_udp_ports |
| 20 | +} |
| 21 | + |
| 22 | +resource "mgc_ssh_keys" "ssh_keys" { |
| 23 | + name = "${var.project_name}-ssh-key" |
| 24 | + key = var.ssh_key |
| 25 | +} |
| 26 | + |
| 27 | +locals { |
| 28 | + cluster_majority = floor(0.5 + (var.cluster_size + 1) / 2) |
| 29 | + cluster_minority = var.cluster_size - local.cluster_majority |
| 30 | +} |
| 31 | + |
| 32 | +resource "mgc_virtual_machine_instances" "cluster_lb" { |
| 33 | + name = "${var.project_name}-lb" |
| 34 | + machine_type = { |
| 35 | + name = var.lb_machine_type |
| 36 | + } |
| 37 | + image = { |
| 38 | + name = "cloud-debian-12 LTS" |
| 39 | + } |
| 40 | + network = { |
| 41 | + # vpc = { |
| 42 | + # id = module.network.vpc_id |
| 43 | + # } |
| 44 | + associate_public_ip = true |
| 45 | + delete_public_ip = true |
| 46 | + interface = { |
| 47 | + security_groups = [ |
| 48 | + { id = module.network.lb_sec_group_id }, |
| 49 | + { id = module.network.ssh_sec_group_id }, |
| 50 | + ] |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + ssh_key_name = mgc_ssh_keys.ssh_keys.name |
| 55 | +} |
| 56 | + |
| 57 | +resource "mgc_virtual_machine_instances" "node_instances" { |
| 58 | + count = var.cluster_size |
| 59 | + name = "${var.project_name}-node-${count.index}" |
| 60 | + machine_type = { |
| 61 | + name = var.nodes_machine_type |
| 62 | + } |
| 63 | + image = { |
| 64 | + name = "cloud-debian-12 LTS" |
| 65 | + } |
| 66 | + network = { |
| 67 | + # vpc = { |
| 68 | + # id = module.network.vpc_id |
| 69 | + # } |
| 70 | + associate_public_ip = false |
| 71 | + delete_public_ip = false |
| 72 | + interface = { |
| 73 | + security_groups = [ |
| 74 | + { id = module.network.nodes_sec_group_id }, |
| 75 | + { id = module.network.lb_sec_group_id }, |
| 76 | + { id = module.network.ssh_sec_group_id }, |
| 77 | + ] |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + ssh_key_name = mgc_ssh_keys.ssh_keys.name |
| 82 | +} |
| 83 | + |
| 84 | +resource "null_resource" "provision_lb" { |
| 85 | + provisioner "remote-exec" { |
| 86 | + inline = [ |
| 87 | + "sudo apt-get update", |
| 88 | + "sudo apt-get install haproxy -y", |
| 89 | + "sudo bash -c 'echo \"ENABLED=1\" >> /etc/default/haproxy'" |
| 90 | + ] |
| 91 | + } |
| 92 | + |
| 93 | + provisioner "file" { |
| 94 | + content = <<-EOT |
| 95 | +%{for port in var.lb_tcp_ports[*]~} |
| 96 | +frontend tcp_${port}_in |
| 97 | + bind *:${port} |
| 98 | + default_backend tcp_${port}_out |
| 99 | +%{endfor~} |
| 100 | +
|
| 101 | +%{for port in var.lb_udp_ports[*]~} |
| 102 | +frontend udp_${port}_in |
| 103 | + bind *:${port} |
| 104 | + default_backend udp_${port}_out |
| 105 | +%{endfor~} |
| 106 | +
|
| 107 | +%{for port in var.lb_tcp_ports[*]~} |
| 108 | +backend tcp_${port}_out |
| 109 | + mode tcp |
| 110 | + %{for node in mgc_virtual_machine_instances.node_instances[*]~} |
| 111 | + server ${node.name} ${node.network.private_address}:${port} check |
| 112 | + %{endfor~} |
| 113 | +
|
| 114 | +%{endfor~} |
| 115 | +
|
| 116 | +%{for port in var.lb_udp_ports[*]~} |
| 117 | +backend udp_${port}_out |
| 118 | + mode udp |
| 119 | + %{for node in mgc_virtual_machine_instances.node_instances[*]~} |
| 120 | + server ${node.name} ${node.network.private_address}:${port} check |
| 121 | + %{endfor~} |
| 122 | +
|
| 123 | +%{endfor~} |
| 124 | +EOT |
| 125 | + # destination = "/etc/haproxy/haproxy.cfg" |
| 126 | + destination = "/home/debian/haproxy.cfg" |
| 127 | + } |
| 128 | + |
| 129 | + provisioner "remote-exec" { |
| 130 | + inline = [ |
| 131 | + "sudo mv /home/debian/haproxy.cfg /etc/haproxy/haproxy.cfg", |
| 132 | + "sudo service haproxy restart", |
| 133 | + ] |
| 134 | + } |
| 135 | + |
| 136 | + connection { |
| 137 | + type = "ssh" |
| 138 | + user = "debian" |
| 139 | + private_key = file(var.ssh_private_key_path) |
| 140 | + host = mgc_virtual_machine_instances.cluster_lb.network.public_address |
| 141 | + } |
| 142 | +} |
0 commit comments