From a75f8750263403256d9227a6ce825105104b3a77 Mon Sep 17 00:00:00 2001 From: Antoine Date: Sat, 23 Jan 2021 21:45:22 +0100 Subject: [PATCH] Add count to generate multiple domain --- Makefile | 6 +++--- base/libvirtd.tf | 10 +++++++++- docker-kvm-qemu-libvirt | 2 +- stack/cloud_init.tf | 15 +++++++++------ stack/cloud_init_meta_data.yml | 8 +++----- stack/computes.tf | 12 +++++++----- stack/libvirtd.tf | 13 +++++++++---- stack/networks.tf | 7 +++++++ stack/variable.tf | 14 +++++++------- stack/volumes.tf | 16 +++++++++------- 10 files changed, 64 insertions(+), 39 deletions(-) diff --git a/Makefile b/Makefile index 928d562..adbad4a 100644 --- a/Makefile +++ b/Makefile @@ -4,8 +4,8 @@ console: @virsh console --domain db1 ssh: - @IP=$(shell virsh domifaddr --domain db1 | tail -2 | awk -F ' ' '{ print $$4 }' | sed 's|/.*||') && \ - ssh -o ProxyCommand="ssh -W %h:%p antoine@dx30.localdomain" -i ~/.ssh/pri/id_rsa_bis antoine@"$$IP" + IP=$(shell virsh domifaddr --domain db1 | tail -2 | awk -F ' ' '{ print $$4 }' | sed 's|/.*||') && \ + ssh -o ProxyCommand="ssh -W %h:%p antoine@dx30_remote.localdomain" -i ~/.ssh/pri/id_rsa_bis antoine@"$$IP" dhcp-lease: @virsh net-dhcp-leases --network private @@ -14,4 +14,4 @@ get-ip: @virsh domifaddr --domain db1 destroy_vm: - @terraform destroy -auto-approve -target=libvirt_domain.db1 \ No newline at end of file + @terraform destroy -auto-approve -target=libvirt_domain.db1 diff --git a/base/libvirtd.tf b/base/libvirtd.tf index 2cad44e..b8f96ab 100644 --- a/base/libvirtd.tf +++ b/base/libvirtd.tf @@ -47,8 +47,16 @@ resource "libvirt_volume" "centos7_qcow2" { } resource "libvirt_volume" "debian_buster_qcow2" { - name = "debian-buster.qcow2" + name = "debian-openstack-buster.qcow2" pool = libvirt_pool.pool_1.name source = "./images/debian-10-openstack-amd64.qcow2" format = "qcow2" } + +resource "libvirt_volume" "debian_with_docker_qcow2" { + name = "debian-10.7.0-with-docker.qcow2" + pool = libvirt_pool.pool_1.name + source = "./images/debian-10.7.0_packer.qcow2" + format = "qcow2" +} + diff --git a/docker-kvm-qemu-libvirt b/docker-kvm-qemu-libvirt index 6e6f8cb..ebd073a 160000 --- a/docker-kvm-qemu-libvirt +++ b/docker-kvm-qemu-libvirt @@ -1 +1 @@ -Subproject commit 6e6f8cb6470380c8b4c07910d98f744bad8852a3 +Subproject commit ebd073a56c23ea79efd65426563182c92855725f diff --git a/stack/cloud_init.tf b/stack/cloud_init.tf index a13a813..608e9cd 100644 --- a/stack/cloud_init.tf +++ b/stack/cloud_init.tf @@ -1,22 +1,25 @@ data "template_file" "user_data" { template = "${file("${path.module}/cloud_init_user_data.yml")}" vars = { - hostname = var.hostname + hostname = format("%s-%s", var.hostname, count.index) } + count = var.number_domain } data "template_file" "meta_data" { template = "${file("${path.module}/cloud_init_meta_data.yml")}" vars = { - dns_address = var.dns_address - dns_domain = var.dns_domain + instance_id = count.index } + count = var.number_domain } # Use CloudInit to add the instance resource "libvirt_cloudinit_disk" "commoninit" { - name = "commoninit.iso" + name = format("commoninit-%s.iso", count.index) pool = var.pool_1 - user_data = data.template_file.user_data.rendered - meta_data = data.template_file.meta_data.rendered + user_data = data.template_file.user_data[count.index].rendered + meta_data = data.template_file.meta_data[count.index].rendered + + count = var.number_domain } diff --git a/stack/cloud_init_meta_data.yml b/stack/cloud_init_meta_data.yml index 4818b83..ddc409e 100644 --- a/stack/cloud_init_meta_data.yml +++ b/stack/cloud_init_meta_data.yml @@ -1,8 +1,6 @@ #cloud-config # vim: syntax=yaml -network-interfaces: | - auto eth0 - iface eth0 inet dhcp - dns-nameservers ${dns_address} - dns-search ${dns_domain} +instance-id: ${instance_id} +network: + config: disabled \ No newline at end of file diff --git a/stack/computes.tf b/stack/computes.tf index 7a85d8f..cc995f4 100644 --- a/stack/computes.tf +++ b/stack/computes.tf @@ -2,13 +2,15 @@ # Define KVM domain to create -resource "libvirt_domain" "db1" { - name = "db1" +resource "libvirt_domain" "domains" { + name = format("db%s", count.index) memory = "1024" vcpu = 1 running = "true" autostart = "true" + count = var.number_domain + boot_device { dev = ["hd", "network"] } @@ -18,14 +20,14 @@ resource "libvirt_domain" "db1" { } disk { - volume_id = libvirt_volume.my_root_debian.id + volume_id = element(libvirt_volume.root_debian.*.id, count.index) } disk { - volume_id = libvirt_volume.external_disk_1.id + volume_id = element(libvirt_volume.external_disk.*.id, count.index) } - cloudinit = libvirt_cloudinit_disk.commoninit.id + cloudinit = libvirt_cloudinit_disk.commoninit[count.index].id console { type = "pty" diff --git a/stack/libvirtd.tf b/stack/libvirtd.tf index a19af1f..6e91ab6 100644 --- a/stack/libvirtd.tf +++ b/stack/libvirtd.tf @@ -13,9 +13,13 @@ terraform { version = "~> 2.1" } libvirt = { - source = "dmacvicar/libvirt" + source = "dmacvicar/libvirt" version = ">= 0.6.3" } + docker = { + source = "kreuzwerker/docker" + version = "2.11.0" + } } backend "etcdv3" { @@ -35,15 +39,16 @@ provider "libvirt" { resource "null_resource" "delay_10s" { provisioner "local-exec" { - command = "sleep 10" + command = "sleep 120" } triggers = { - "before" = libvirt_domain.db1.id + // trigger after last domain created + "after" = libvirt_domain.domains[var.number_domain - 1].id } } # Output Server IP output "ip" { - value = libvirt_domain.db1.network_interface + value = libvirt_domain.domains.*.network_interface depends_on = [null_resource.delay_10s] } diff --git a/stack/networks.tf b/stack/networks.tf index d72bab1..6973dcf 100644 --- a/stack/networks.tf +++ b/stack/networks.tf @@ -9,4 +9,11 @@ resource "libvirt_network" "private_network" { dhcp { enabled = true } + dns { + enabled = true + local_only = true + forwarders { + address = var.dns_address + } + } } diff --git a/stack/variable.tf b/stack/variable.tf index e41d8e9..276727d 100644 --- a/stack/variable.tf +++ b/stack/variable.tf @@ -4,16 +4,16 @@ variable "ip" { description = "fixed ip address for compute" } -variable "hostname" { +variable "number_domain" { type = string - default = "compute-1" - description = "compute hostname" + default = "3" + description = "number of domain" } -variable "dns_domain" { +variable "hostname" { type = string - default = "localdomain" - description = "dns domain name" + default = "compute" + description = "compute hostname" } variable "dns_address" { @@ -30,7 +30,7 @@ variable "pool_1" { variable "debian_buster_qcow2" { type = string - default = "debian-buster.qcow2" + default = "debian-10.7.0-with-docker.qcow2" description = "already created debian vol, set with variable because libvirt provider dont' wotk with data resource" } diff --git a/stack/volumes.tf b/stack/volumes.tf index 329efd0..490e3c8 100644 --- a/stack/volumes.tf +++ b/stack/volumes.tf @@ -4,21 +4,23 @@ # ~> Tip: when provisioning multiple domains using the same base image, create a libvirt_volume for the base image and then define # the domain specific ones as based on it. This way the image will not be modified and no extra disk space is going to be used for the base image. -resource "libvirt_volume" "my_root_centos" { +resource "libvirt_volume" "root_centos" { name = "my-root-centos" pool = var.pool_1 base_volume_name = var.centos7_qcow2 } -resource "libvirt_volume" "my_root_debian" { - name = "my-root-debian" +resource "libvirt_volume" "root_debian" { + name = format("root-debian-%s", count.index) pool = var.pool_1 base_volume_name = var.debian_buster_qcow2 + count = var.number_domain } -resource "libvirt_volume" "external_disk_1" { - name = "external-disk-1" +resource "libvirt_volume" "external_disk" { + name = format("external-disk-%s", count.index) # 10Gb - size = 10737418240 - pool = var.pool_1 + size = 10737418240 + pool = var.pool_1 + count = var.number_domain }