Initial attempt is working.

new-vpc
Curtis Wilson 4 years ago
parent b07deec786
commit 14166ca8be

38
.gitignore vendored

@ -0,0 +1,38 @@
################################################################################
# Pulled from github/gitignore 2021-11-10 commit 1a84870
################################################################################
# Local .terraform directories
**/.terraform/*
# .tfstate files
*.tfstate
*.tfstate.*
# Crash log files
crash.log
# Exclude all .tfvars files, which are likely to contain sentitive data, such as
# password, private keys, and other secrets. These should not be part of version
# control as they are data points which are potentially sensitive and subject
# to change depending on the environment.
#
*.tfvars
# Ignore override files as they are usually used to override resources locally and so
# are not checked in
override.tf
override.tf.json
*_override.tf
*_override.tf.json
# Include override files you do wish to add to version control using negated pattern
#
# !example_override.tf
# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
# example: *tfplan*
# Ignore CLI configuration files
.terraformrc
terraform.rc

@ -0,0 +1,10 @@
Dependencies
----------------------------------------
TODO REM add libvirt provider
libvirt provider depends on mkisofs
security_driver = none for ubuntu host, link github issue.
https://github.com/dmacvicar/terraform-provider-libvirt/issues/546

@ -0,0 +1,17 @@
#!/bin/sh
# Use eval $(./get-vm-ips.sh) to set env vars for ips.
terraform refresh > /dev/null
IPS_JSON="$(terraform show -json | jq '.values.root_module.resources[] | select(.type == "libvirt_domain") | {name: .values.name, ip: .values.network_interface[0].addresses[0]}')"
echo $IPS_JSON | \
jq 'select(.name | contains("master")) | .ip' | \
xargs -I% echo export MASTER=%
echo $IPS_JSON | \
jq 'select(.name | contains("worker")) | .ip' | \
nl -v 0 | \
awk '{print "export WORKER" $1 "=" $2}' | \
sed 's/"//g'

@ -0,0 +1,127 @@
terraform {
required_providers {
libvirt = {
source = "dmacvicar/libvirt"
}
}
}
provider "libvirt" {
uri = "qemu+ssh://gert@gertie/system"
}
resource "libvirt_pool" "images" {
name = "k8s-tf-images"
type = "dir"
path = var.disk-image-dir
}
resource "libvirt_volume" "master-image" {
name = "k8s-tf-master"
pool = libvirt_pool.images.name
source = var.ubuntu-image
format = "qcow2"
}
resource "libvirt_volume" "worker-volumes" {
name = "k8s-tf-worker-${count.index}"
pool = libvirt_pool.images.name
source = var.ubuntu-image
format = "qcow2"
count = var.worker-nodes
}
data "template_file" "user_data" {
template = file("${path.module}/cloud_init.cfg")
}
data "template_file" "network_config" {
template = file("${path.module}/network_config.cfg")
}
# for more info about paramater check this out
# https://github.com/dmacvicar/terraform-provider-libvirt/blob/master/website/docs/r/cloudinit.html.markdown
# Use CloudInit to add our ssh-key to the instance
# you can add also meta_data field
resource "libvirt_cloudinit_disk" "commoninit" {
name = "commoninit.images"
user_data = data.template_file.user_data.rendered
network_config = data.template_file.network_config.rendered
pool = libvirt_pool.images.name
}
# Create the machine
resource "libvirt_domain" "master-domain" {
name = "k8s-tf-master"
memory = var.node-memory
vcpu = var.node-vcpus
cloudinit = libvirt_cloudinit_disk.commoninit.id
network_interface {
network_name = "default"
}
# IMPORTANT: this is a known bug on cloud images, since they expect a console
# we need to pass it
# https://bugs.launchpad.net/cloud-images/+bug/1573095
console {
type = "pty"
target_port = "0"
target_type = "serial"
}
console {
type = "pty"
target_type = "virtio"
target_port = "1"
}
disk {
volume_id = libvirt_volume.master-image.id
}
graphics {
type = "spice"
listen_type = "address"
autoport = true
}
}
resource "libvirt_domain" "worker-domains" {
count = var.worker-nodes
name = "k8s-tf-worker-${count.index}"
memory = var.node-memory
vcpu = var.node-vcpus
cloudinit = libvirt_cloudinit_disk.commoninit.id
network_interface {
network_name = "default"
}
# IMPORTANT: this is a known bug on cloud images, since they expect a console
# we need to pass it
# https://bugs.launchpad.net/cloud-images/+bug/1573095
console {
type = "pty"
target_port = "0"
target_type = "serial"
}
console {
type = "pty"
target_type = "virtio"
target_port = "1"
}
disk {
volume_id = element(libvirt_volume.worker-volumes.*.id, count.index)
}
graphics {
type = "spice"
listen_type = "address"
autoport = true
}
}

@ -0,0 +1,26 @@
variable "disk-image-dir" {
description = "This is the location on the KVM hypervisor host where all the disk images will be kept."
}
variable "node-memory" {
default = "2048"
description = "The amount of memory to be used for all the nodes."
type = number
}
variable "node-vcpus" {
default = "2"
description = "The amount of vcpus to be used for all the nodes."
type = number
}
variable "worker-nodes" {
default = "2"
description = "The number of worker nodes to create."
type = number
}
variable "ubuntu-image" {
default = "https://cloud-images.ubuntu.com/releases/focal/release/ubuntu-20.04-server-cloudimg-amd64-disk-kvm.img"
}
Loading…
Cancel
Save