diff --git a/Makefile b/Makefile index f091dea..32e253a 100644 --- a/Makefile +++ b/Makefile @@ -41,3 +41,7 @@ stack_init: -backend-config="cacert_path=$$HOME/virtualization/kubernetes-the-hard-way/certs/ca.pem" \ -backend-config="cert_path=$$HOME/virtualization/kubernetes-the-hard-way/certs/kubernetes.pem" \ -backend-config="key_path=$$HOME/virtualization/kubernetes-the-hard-way/certs/kubernetes-key.pem" + +stack_test: + cd applications/ && \ + ansible -m debug -i stack_address.toml all -a "msg=test" diff --git a/applications/stack_address.toml b/applications/stack_address.toml index 5078dbf..0c98fcf 100755 --- a/applications/stack_address.toml +++ b/applications/stack_address.toml @@ -1,11 +1,11 @@ -# two first domain is controller domain +# controller domain [controller] -compute-0 ansible_host=100.64.0.146 index=0 -compute-1 ansible_host=100.64.0.214 index=1 -# next domain is worker domain +compute-0 ansible_host=100.64.0.216 index=0 +# worker domain [worker] -compute-2 ansible_host=100.64.0.79 index=0 -compute-3 ansible_host=100.64.0.86 index=1 +compute-1 ansible_host=100.64.0.14 index=0 +compute-2 ansible_host=100.64.0.26 index=1 +compute-3 ansible_host=100.64.0.34 index=2 [all:children] controller diff --git a/stack/cloud_init.tf b/stack/cloud_init.tf index 6823c78..8e8fd78 100644 --- a/stack/cloud_init.tf +++ b/stack/cloud_init.tf @@ -3,7 +3,7 @@ data "template_file" "user_data" { vars = { hostname = format("%s-%s", var.hostname, count.index) } - count = var.number_domain + count = local.number_domain } data "template_file" "meta_data" { @@ -11,7 +11,7 @@ data "template_file" "meta_data" { vars = { instance_id = count.index } - count = var.number_domain + count = local.number_domain } # Use CloudInit to add the instance @@ -21,5 +21,5 @@ resource "libvirt_cloudinit_disk" "commoninit" { user_data = data.template_file.user_data[count.index].rendered meta_data = data.template_file.meta_data[count.index].rendered - count = var.number_domain + count = local.number_domain } diff --git a/stack/computes.tf b/stack/computes.tf index e3ada81..086a3e0 100644 --- a/stack/computes.tf +++ b/stack/computes.tf @@ -2,14 +2,14 @@ # Define KVM domain to create -resource "libvirt_domain" "domains" { +resource "libvirt_domain" "controllers" { name = format("db%s", count.index) - memory = "1024" - vcpu = 1 + memory = "2048" + vcpu = 2 running = "true" autostart = "true" - count = var.number_domain + count = var.number_controller qemu_agent = true boot_device { @@ -43,3 +43,45 @@ resource "libvirt_domain" "domains" { autoport = "true" } } + +resource "libvirt_domain" "workers" { + name = format("db%s", var.number_controller + count.index) + memory = "1024" + vcpu = 1 + running = "true" + autostart = "true" + + count = var.number_worker + qemu_agent = true + + boot_device { + dev = ["hd", "network"] + } + + network_interface { + network_id = libvirt_network.private_network.id + hostname = format("compute-%s", var.number_controller + count.index) + } + + disk { + volume_id = libvirt_volume.root_debian[var.number_controller + count.index].id + } + + disk { + volume_id = libvirt_volume.external_disk[var.number_controller + count.index].id + } + + cloudinit = libvirt_cloudinit_disk.commoninit[var.number_controller + count.index].id + + console { + type = "pty" + target_type = "serial" + target_port = "0" + } + + graphics { + type = "vnc" + listen_type = "address" + autoport = "true" + } +} diff --git a/stack/libvirtd.tf b/stack/libvirtd.tf index 2e95ad1..6bcb320 100644 --- a/stack/libvirtd.tf +++ b/stack/libvirtd.tf @@ -33,37 +33,45 @@ provider "libvirt" { uri = "qemu+tcp://dx30.localdomain/system" } -resource "null_resource" "delay_10s" { +resource "null_resource" "delay_controllers_10s" { provisioner "local-exec" { - command = "sleep 120" + command = "sleep 60" } triggers = { - # trigger after last domain created - "after" = libvirt_domain.domains[var.number_domain - 1].id + # trigger after last domain controller and laster domain worker created + "after" = libvirt_domain.controllers[var.number_controller - 1].id + } +} +resource "null_resource" "delay_workers_10s" { + provisioner "local-exec" { + command = "sleep 60" + } + triggers = { + # trigger after last domain controller and laster domain worker created + "after" = libvirt_domain.workers[var.number_worker - 1].id } } # Output Server IP output "ip" { - value = libvirt_domain.domains.*.network_interface - depends_on = [null_resource.delay_10s] + value = concat(libvirt_domain.controllers.*.network_interface, libvirt_domain.workers.*.network_interface) + depends_on = [null_resource.delay_controllers_10s, null_resource.delay_workers_10s] } resource "local_file" "write_address" { - content = <<-EOT -# two first domain is controller domain +# controller domain [controller] -%{ for idx, s in slice(libvirt_domain.domains, 0, 2) ~} -%{ if length(s.network_interface.0.addresses) > 0 ~} -${s.network_interface.0.hostname} ansible_host=${s.network_interface.0.addresses.0} index=${idx} +%{ for idx, s in libvirt_domain.controllers.*.network_interface.0 ~} +%{ if length(s.addresses) > 0 ~} +${s.hostname} ansible_host=${s.addresses.0} index=${idx} %{ endif ~} %{ endfor ~} -# next domain is worker domain +# worker domain [worker] -%{ for idx, s in slice(libvirt_domain.domains, 2, length(libvirt_domain.domains)) ~} -%{ if length(s.network_interface.0.addresses) > 0 ~} -${s.network_interface.0.hostname} ansible_host=${s.network_interface.0.addresses.0} index=${idx} +%{ for idx, s in libvirt_domain.workers.*.network_interface.0 ~} +%{ if length(s.addresses) > 0 ~} +${s.hostname} ansible_host=${s.addresses.0} index=${idx} %{ endif ~} %{ endfor ~} diff --git a/stack/variable.tf b/stack/variable.tf index c502f2a..bf5eac2 100644 --- a/stack/variable.tf +++ b/stack/variable.tf @@ -4,10 +4,16 @@ variable "ip" { description = "fixed ip address for compute" } -variable "number_domain" { - type = string - default = "4" - description = "number of domain" +variable "number_controller" { + type = number + default = 1 + description = "number of controller domain" +} + +variable "number_worker" { + type = number + default = 3 + description = "number of worker domain" } variable "hostname" { @@ -29,6 +35,7 @@ variable "pool_1" { } locals { + number_domain = var.number_controller + var.number_worker debian_buster_qcow2 = "debian-10.7.0-with-docker.qcow2" centos7_qcow2 = "centos7.qcow2" } diff --git a/stack/volumes.tf b/stack/volumes.tf index 7e37565..5c4bf36 100644 --- a/stack/volumes.tf +++ b/stack/volumes.tf @@ -14,7 +14,7 @@ resource "libvirt_volume" "root_debian" { name = format("root-debian-%s", count.index) pool = var.pool_1 base_volume_name = local.debian_buster_qcow2 - count = var.number_domain + count = local.number_domain } resource "libvirt_volume" "external_disk" { @@ -22,5 +22,5 @@ resource "libvirt_volume" "external_disk" { # 10Gb size = 10737418240 pool = var.pool_1 - count = var.number_domain + count = local.number_domain }