-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.tf
218 lines (197 loc) · 6.65 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
terraform {
required_providers {
crusoe = {
source = "registry.terraform.io/crusoecloud/crusoe"
}
}
}
locals {
my_ssh_privkey_path="</path/to/priv.key>"
my_ssh_pubkey="<pub-key>"
worker_instance_type = "h100-80gb-sxm-ib.8x"
worker_image = "ubuntu22.04-nvidia-sxm-docker:latest"
ib_partition_id = "<ib_partition_id>"
count_workers = 2
headnode_instance_type="c1a.8x"
deploy_location = "us-east1-a"
haproxy_local = <<-EOT
global
log /dev/log local0
log /dev/log local1 notice
chroot /var/lib/haproxy
stats socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners
stats timeout 30s
user haproxy
group haproxy
daemon
defaults
log global
mode tcp
timeout connect 5000
timeout client 50000
timeout server 50000
frontend k3s_cluster
bind *:6443
mode tcp
default_backend k3s_nodes
backend k3s_nodes
mode tcp
balance roundrobin
server k3s_node1 ${crusoe_compute_instance.k3s_0.network_interfaces[0].private_ipv4.address}:6443 check
server k3s_node2 ${crusoe_compute_instance.k3s_1.network_interfaces[0].private_ipv4.address}:6443 check
server k3s_node3 ${crusoe_compute_instance.k3s_2.network_interfaces[0].private_ipv4.address}:6443 check
EOT
}
resource "local_file" "haproxy_config" {
filename = "haproxy.cfg"
content = local.haproxy_local
}
resource "crusoe_compute_instance" "k3s_lb" {
depends_on = [local_file.haproxy_config]
name = "crusoe-k3s-lb"
type = local.headnode_instance_type
ssh_key = local.my_ssh_pubkey
location = local.deploy_location
image = "ubuntu22.04:latest"
startup_script = file("k3haproxy-install.sh")
provisioner "local-exec" {
command = "crusoe compute vms get ${self.name} -f json > /tmp/metadata.${self.name}.json"
}
provisioner "file" {
source = "haproxy.cfg"
destination = "/tmp/haproxy.cfg"
connection {
type = "ssh"
user = "root"
host = "${self.network_interfaces[0].public_ipv4.address}"
private_key = file("${local.my_ssh_privkey_path}")
}
}
}
resource "crusoe_compute_instance" "k3s_0" {
name = "crusoe-k3s-0"
type = local.headnode_instance_type
ssh_key = local.my_ssh_pubkey
location = local.deploy_location
image = "ubuntu22.04:latest"
startup_script = file("k3install-main.sh")
provisioner "local-exec" {
command = "crusoe compute vms get ${self.name} -f json > /tmp/metadata.${self.name}.json"
}
provisioner "file" {
source = "k3-0-serve-token.py"
destination = "/opt/k3-0-serve-token.py"
connection {
type = "ssh"
user = "root"
host = "${self.network_interfaces[0].public_ipv4.address}"
private_key = file("${local.my_ssh_privkey_path}")
}
}
provisioner "file" {
source = "/tmp/metadata.${crusoe_compute_instance.k3s_0.name}.json"
destination = "/root/k3-0-main.json"
connection {
type = "ssh"
user = "root"
host = "${self.network_interfaces[0].public_ipv4.address}"
private_key = file("${local.my_ssh_privkey_path}")
}
}
}
resource "crusoe_compute_instance" "k3s_1" {
depends_on = [crusoe_compute_instance.k3s_0]
name = "crusoe-k3s-1"
type = local.headnode_instance_type
ssh_key = local.my_ssh_pubkey
location = local.deploy_location
image = "ubuntu22.04:latest"
startup_script = file("k3install-child.sh")
provisioner "file" {
source = "/tmp/metadata.${crusoe_compute_instance.k3s_0.name}.json"
destination = "/root/k3-0-main.json"
connection {
type = "ssh"
user = "root"
host = "${self.network_interfaces[0].public_ipv4.address}"
private_key = file("${local.my_ssh_privkey_path}")
}
}
}
resource "crusoe_compute_instance" "k3s_2" {
depends_on = [crusoe_compute_instance.k3s_0]
name = "crusoe-k3s-2"
type = local.headnode_instance_type
ssh_key = local.my_ssh_pubkey
location = local.deploy_location
image = "ubuntu22.04:latest"
startup_script = file("k3install-child.sh")
provisioner "file" {
source = "/tmp/metadata.${crusoe_compute_instance.k3s_0.name}.json"
destination = "/root/k3-0-main.json"
connection {
type = "ssh"
user = "root"
host = "${self.network_interfaces[0].public_ipv4.address}"
private_key = file("${local.my_ssh_privkey_path}")
}
}
}
resource "null_resource" "copy-lb-file" {
depends_on = [crusoe_compute_instance.k3s_lb]
provisioner "file" {
source = "/tmp/metadata.${crusoe_compute_instance.k3s_lb.name}.json"
destination = "/root/k3-lb-main.json"
connection {
type = "ssh"
user = "root"
host = "${crusoe_compute_instance.k3s_0.network_interfaces[0].public_ipv4.address}"
private_key = file("${local.my_ssh_privkey_path}")
}
}
}
resource "crusoe_compute_instance" "workers" {
depends_on = [crusoe_compute_instance.k3s_lb]
count = local.count_workers
name = "crusoe-k3s-worker-${count.index}"
type = local.worker_instance_type
ssh_key = local.my_ssh_pubkey
location = local.deploy_location
image = local.worker_image
startup_script = file("k3install-worker.sh")
host_channel_adapters = [{ib_partition_id = local.ib_partition_id}]
provisioner "file" {
source = "/tmp/metadata.${crusoe_compute_instance.k3s_0.name}.json"
destination = "/root/k3-0-main.json"
connection {
type = "ssh"
user = "root"
host = "${self.network_interfaces[0].public_ipv4.address}"
private_key = file("${local.my_ssh_privkey_path}")
}
}
provisioner "file" {
source = "/tmp/metadata.${crusoe_compute_instance.k3s_lb.name}.json"
destination = "/root/k3-lb-main.json"
connection {
type = "ssh"
user = "root"
host = "${self.network_interfaces[0].public_ipv4.address}"
private_key = file("${local.my_ssh_privkey_path}")
}
}
}
resource "crusoe_vpc_firewall_rule" "k3_rule" {
network = crusoe_compute_instance.k3s_lb.network_interfaces[0].network
name = "k3s-pub-access"
action = "allow"
direction = "ingress"
protocols = "tcp"
source = "0.0.0.0/0"
source_ports = "1-65535"
destination = crusoe_compute_instance.k3s_lb.network_interfaces[0].private_ipv4.address
destination_ports = "6443"
}
output "k30-instance_public_ip" {
value = crusoe_compute_instance.k3s_0.network_interfaces[0].public_ipv4.address
}