-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.tf
241 lines (201 loc) · 6.52 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
variable "hostname" { default = "kubenode" }
variable "domain" { default = "kubenode.com" }
variable "m_memoryMB" { default = 1024*1 }
variable "m_cpu" { default = 1 }
variable "w_memoryMB" { default = 1024*1 }
variable "w_cpu" { default = 1 }
variable "e_memoryMB" { default = 1024*1 }
variable "e_cpu" { default = 1 }
variable "masters" { default = 0 }
variable "workers" { default = 1 }
variable "etcd" { default = 0 }
variable "etcd_gb" { default = 10 }
variable "base_gb" { default = 10 }
variable "data_gb" { default = 10 }
variable "cluster_volume_pool_dir" { default = "/var/lib/libvirt/images-kubec" }
variable "detached_etcd_volume_pool_dir" { default = "/var/lib/libvirt/images-detcd" }
terraform {
required_version = ">= 0.13"
required_providers {
libvirt = {
source = "dmacvicar/libvirt"
version = "0.6.11"
}
}
}
provider "libvirt" {
uri = "qemu:///system"
}
# Cloud-init configuration
data "template_file" "user_data" {
template = file("${path.module}/cloud-init.cfg")
vars = {
hostname = var.hostname
fqdn = "${var.hostname}.${var.domain}"
}
}
data "template_file" "network_data" {
template = file("${path.module}/network-conf.cfg")
}
# Cluster storage pool
resource "libvirt_pool" "kubernetes_pool" {
name = "kubernetes_pool"
type = "dir"
path = "${var.cluster_volume_pool_dir}"
}
# Detached etcd storage pool
resource "libvirt_pool" "etcd_pool" {
name = "etcd_pool"
type = "dir"
path = "${var.detached_etcd_volume_pool_dir}"
}
# Cloudinit inject volumes
resource "libvirt_cloudinit_disk" "commoninit" {
name = "commoninit.iso"
pool = libvirt_pool.kubernetes_pool.name
user_data = data.template_file.user_data.rendered
network_config = data.template_file.network_data.rendered
}
# Ubuntu volume
resource "libvirt_volume" "os_image" {
name = "os_image"
pool = libvirt_pool.kubernetes_pool.name
source = "https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img"
}
# VM Volumes
# Volume specified for Ubuntu install (2GiB) and Kubernetes overhead (3-4 GiB at least)
resource "libvirt_volume" "volume-master" {
name = "volume-master-${count.index}"
pool = libvirt_pool.kubernetes_pool.name
size = var.base_gb * 1024 * 1024 * 1024 # bytes to GiB (2^3 -> 2^33)
base_volume_id = libvirt_volume.os_image.id
format = "qcow2"
count = var.masters
}
# Volume specified for Ubuntu install (2GiB) and Kubernetes overhead (3-4 GiB at least)
resource "libvirt_volume" "volume-worker" {
name = "volume-worker-${count.index}"
pool = libvirt_pool.kubernetes_pool.name
size = var.data_gb * 1024 * 1024 * 1024 # bytes to GiB (2^3 -> 2^33)
base_volume_id = libvirt_volume.os_image.id
format = "qcow2"
count = var.workers
}
# Volume specified for Ubuntu install (2GiB) and etcd
resource "libvirt_volume" "volume-etcd" {
name = "volume-etcd-${count.index}"
pool = libvirt_pool.etcd_pool.name
size = var.data_gb * 1024 * 1024 * 1024 # bytes to GiB (2^3 -> 2^33)
base_volume_id = libvirt_volume.os_image.id
format = "qcow2"
count = var.etcd
}
# Network configuration
resource "libvirt_network" "kube_network" {
name = "kube"
mode = "nat"
domain = "k8s.local"
addresses = ["200.0.5.0/24"]
autostart = true
dns {
enabled = true
local_only = false # Forward to upstream if local doesn't respond
}
}
# Domain configuration
resource "libvirt_domain" "kubemaster" {
name = "kube-master-${count.index}"
memory = var.m_memoryMB
vcpu = var.m_cpu
count = var.masters
qemu_agent = true
cloudinit = libvirt_cloudinit_disk.commoninit.id
# IMPORTANT
# Ubuntu can hang is a isa-serial is not present at boot time.
# If you find your cpu 100% and never is available this is why
console {
type = "pty"
target_port = "0"
target_type = "serial"
}
graphics {
type = "spice"
listen_type = "address"
autoport = true
}
disk {
volume_id = element(libvirt_volume.volume-master.*.id, count.index)
}
network_interface {
network_id = libvirt_network.kube_network.id
hostname = "master-${count.index}"
wait_for_lease = true
}
}
resource "libvirt_domain" "kubeworker" {
name = "kube-worker-${count.index}"
memory = var.w_memoryMB
vcpu = var.w_cpu
count = var.workers
qemu_agent = true
cloudinit = libvirt_cloudinit_disk.commoninit.id
# IMPORTANT
# Ubuntu can hang is a isa-serial is not present at boot time.
# If you find your cpu 100% and never is available this is why
console {
type = "pty"
target_port = "0"
target_type = "serial"
}
graphics {
type = "spice"
listen_type = "address"
autoport = true
}
disk {
volume_id = element(libvirt_volume.volume-worker.*.id, count.index)
}
network_interface {
network_id = libvirt_network.kube_network.id
hostname = "worker-${count.index}"
wait_for_lease = true
}
}
resource "libvirt_domain" "etcd" {
name = "kube-etcd-${count.index}"
memory = var.e_memoryMB
vcpu = var.e_cpu
count = var.etcd
qemu_agent = true
cloudinit = libvirt_cloudinit_disk.commoninit.id
# IMPORTANT
# Ubuntu can hang is a isa-serial is not present at boot time.
# If you find your cpu 100% and never is available this is why
console {
type = "pty"
target_port = "0"
target_type = "serial"
}
graphics {
type = "spice"
listen_type = "address"
autoport = true
}
disk {
volume_id = element(libvirt_volume.volume-etcd.*.id, count.index)
}
network_interface {
network_id = libvirt_network.kube_network.id
hostname = "etcd-${count.index}"
wait_for_lease = true
}
}
output "master_ips" {
value = libvirt_domain.kubemaster.*.network_interface.0.addresses
}
output "worker_ips" {
value = libvirt_domain.kubeworker.*.network_interface.0.addresses
}
output "etcd_ips" {
value = libvirt_domain.etcd.*.network_interface.0.addresses
}