`node` module is working.

new-vpc
Curtis Wilson 4 years ago
parent 99d7ba1c43
commit f4889f0428

@ -9,144 +9,33 @@ terraform {
}
provider "libvirt" {
uri = "qemu+ssh://gert@gertie/system"
uri = var.libvirt-connection-url
}
resource "libvirt_pool" "images" {
name = "k8s-tf-images"
type = "dir"
path = var.disk-image-dir
}
# Add 'size' when we need more space. It must be used in conjuction with
# 'growpart' in cloud-init as well.
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" "master-user-data" {
template = file("${path.module}/cloud_init.cfg")
vars = {
admin-passwd = "${var.root-admin-passwd}"
admin-pub-key = "${var.root-admin-pub-key}"
hostname = "k8s-tf-master"
}
}
data "template_file" "worker-user-data" {
template = file("${path.module}/cloud_init.cfg")
vars = {
admin-passwd = "${var.root-admin-passwd}"
admin-pub-key = "${var.root-admin-pub-key}"
hostname = "k8s-tf-worker-${count.index}"
}
count = var.worker-nodes
}
data "template_file" "network_config" {
template = file("${path.module}/network_config.cfg")
}
resource "libvirt_cloudinit_disk" "master-init" {
name = "k8s-tf-master-init"
user_data = data.template_file.master-user-data.rendered
network_config = data.template_file.network_config.rendered
pool = libvirt_pool.images.name
module "master-nodes" {
source = "./modules/node"
pool-name = libvirt_pool.images.name
name-prefix = "${var.vm-name-prefix}-master"
num-nodes = var.master-nodes
base-image = var.ubuntu-image
root-admin-passwd = var.root-admin-passwd
root-admin-pub-key = var.root-admin-pub-key
libvirt-connection-url = var.libvirt-connection-url
}
resource "libvirt_cloudinit_disk" "worker-init" {
name = "k8s-tf-worker-${count.index}-init"
user_data = element(data.template_file.worker-user-data.*.rendered, count.index)
network_config = data.template_file.network_config.rendered
pool = libvirt_pool.images.name
count = var.worker-nodes
}
# Create the machine
resource "libvirt_domain" "master-domain" {
name = "k8s-tf-master"
memory = var.node-memory
vcpu = var.node-vcpus
cloudinit = libvirt_cloudinit_disk.master-init.id
network_interface {
network_name = "default"
hostname = "k8s-tf-master"
}
# 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
}
module "worker-nodes" {
source = "./modules/node"
pool-name = libvirt_pool.images.name
name-prefix = "${var.vm-name-prefix}-worker"
num-nodes = var.worker-nodes
base-image = var.ubuntu-image
root-admin-passwd = var.root-admin-passwd
root-admin-pub-key = var.root-admin-pub-key
libvirt-connection-url = var.libvirt-connection-url
}
resource "libvirt_domain" "worker-domains" {
count = var.worker-nodes
name = "k8s-tf-worker-${count.index}"
memory = var.node-memory
vcpu = var.node-vcpus
cloudinit = element(libvirt_cloudinit_disk.worker-init.*.id, count.index)
network_interface {
network_name = "default"
hostname = "k8s-tf-worker-${count.index}"
}
# 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
}
resource "libvirt_pool" "images" {
name = var.disk-image-pool-name
type = "dir"
path = var.disk-image-dir
}

@ -0,0 +1,83 @@
terraform {
required_version = ">= 0.13"
required_providers {
libvirt = {
source = "dmacvicar/libvirt"
version = "0.6.11"
}
}
}
provider "libvirt" {
uri = var.libvirt-connection-url
}
resource "libvirt_volume" "node-images" {
name = "${var.name-prefix}-${count.index}"
pool = var.pool-name
source = var.base-image
count = var.num-nodes
format = "qcow2"
}
data "template_file" "node-user-datas" {
template = file("${path.module}/cloud_init.cfg")
vars = {
admin-passwd = "${var.root-admin-passwd}"
admin-pub-key = "${var.root-admin-pub-key}"
hostname = "${var.name-prefix}-${count.index}"
}
count = var.num-nodes
}
data "template_file" "network-config" {
template = file("${path.module}/network_config.cfg")
}
resource "libvirt_cloudinit_disk" "node-inits" {
name = "${var.name-prefix}-${count.index}-init"
user_data = element(data.template_file.node-user-datas.*.rendered, count.index)
network_config = data.template_file.network-config.rendered
pool = var.pool-name
count = var.num-nodes
}
resource "libvirt_domain" "nodes" {
count = var.num-nodes
name = "${var.name-prefix}-${count.index}"
memory = var.node-memory
vcpu = var.node-vcpus
cloudinit = element(libvirt_cloudinit_disk.node-inits.*.id, count.index)
network_interface {
network_name = "default"
hostname = "${var.name-prefix}-${count.index}"
}
# 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.node-images.*.id, count.index)
}
graphics {
type = "spice"
listen_type = "address"
autoport = true
}
}

@ -0,0 +1,42 @@
variable "base-image" {
default = "https://cloud-images.ubuntu.com/releases/focal/release/ubuntu-20.04-server-cloudimg-amd64-disk-kvm.img"
description = "The base image to be used for all nodes."
}
variable "libvirt-connection-url" {
description = "The libvirt connection URI, ie. qemu+ssh://<user>@<host>/system"
}
variable "name-prefix" {
default = "k8s-node"
description = "This will be a prefix for all resource names, ie. domains will be created suck as \"k8s-node-2\"."
}
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 "num-nodes" {
description = "The number of nodes to create with this config."
}
variable "pool-name" {
default = "default"
description = "The name of the pool to put all disk images in."
}
variable "root-admin-passwd" {
description = "This will be the password for the root and admin user. The format of this can by any format accepted by cloud-init's chpasswd module."
}
variable "root-admin-pub-key" {
description = "The public key to be added to authorized_keys for the root and admin accounts."
}

@ -3,6 +3,15 @@ variable "disk-image-dir" {
description = "This is the location on the KVM hypervisor host where all the disk images will be kept."
}
variable "disk-image-pool-name" {
default = "k8s-tf-images"
description = "The name of the disk pool where all the images will be kept."
}
variable "libvirt-connection-url" {
description = "The libvirt connection URI, ie. qemu+ssh://<user>@<host>/system"
}
variable "node-memory" {
default = "2048"
description = "The amount of memory to be used for all the nodes."
@ -23,8 +32,14 @@ variable "root-admin-pub-key" {
description = "The public key to be added to authorized_keys for the root and admin accounts."
}
variable "master-nodes" {
default = 1
description = "The number of master nodes to create."
type = number
}
variable "worker-nodes" {
default = "2"
default = 2
description = "The number of worker nodes to create."
type = number
}
@ -32,3 +47,8 @@ variable "worker-nodes" {
variable "ubuntu-image" {
default = "https://cloud-images.ubuntu.com/releases/focal/release/ubuntu-20.04-server-cloudimg-amd64-disk-kvm.img"
}
variable "vm-name-prefix" {
default = "k8s-tf"
description = "This prefix will appear before all VM names and hostnames, ie. k8s-tf-master-0."
}

Loading…
Cancel
Save