3 Commits
8 changed files with 100 additions and 85 deletions
+22 -4
View File
@@ -1,4 +1,4 @@
vm-name-prefix = "docker-ansible-test" vm-name-prefix = "k8s-tf"
# A CIDR block ending in '/32' equates to a single IP address, '0.0.0.0/0' # A CIDR block ending in '/32' equates to a single IP address, '0.0.0.0/0'
# equates to any ip address. # equates to any ip address.
@@ -7,12 +7,13 @@ admin-ips = [ "8.8.8.8/32", "0.0.0.0/0" ]
disk-image-dir = "/path/to/disk/pool/" disk-image-dir = "/path/to/disk/pool/"
libvirt-connection-url = "qemu+ssh://<user>@<host>/system" libvirt-connection-url = "qemu+ssh://<user>@<host>/system"
master-nodes = 1
worker-nodes = 2
node-memory = 2048 node-memory = 2048
node-vcpus = 2 node-vcpus = 2
################################################################################
# AWS EC2 instance types
################################################################################
# 1 GiB, 1 vcpu, only one that is free. # 1 GiB, 1 vcpu, only one that is free.
# This one won't work with k8s because it requires at least 2 vcpus. # This one won't work with k8s because it requires at least 2 vcpus.
aws-ec2-instance-type = "t2.micro" aws-ec2-instance-type = "t2.micro"
@@ -20,6 +21,19 @@ aws-ec2-instance-type = "t2.micro"
# 4 GiB, 2 vcpus # 4 GiB, 2 vcpus
# aws-ec2-instnce-type = "t2.medium" # aws-ec2-instnce-type = "t2.medium"
################################################################################
nodes-config = {
"master" = {
base-image = "ami-0dd0ccab7e2801812"
num = 1
},
"worker" = {
base-image = "ami-0dd0ccab7e2801812"
num = 2
}
}
################################################################################ ################################################################################
# AWS images (AMIs) # AWS images (AMIs)
################################################################################ ################################################################################
@@ -53,6 +67,10 @@ base-image = "ami-0dd0ccab7e2801812"
# From https://cloud.centos.org/centos/7/images/ from 2020-11-12 06:52 # From https://cloud.centos.org/centos/7/images/ from 2020-11-12 06:52
# base-image = "https://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud-2009.qcow2" # base-image = "https://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud-2009.qcow2"
################################################################################
# Keys/Passwords
################################################################################
# Password hash created with: # Password hash created with:
# python3 -c 'import crypt; print(crypt.crypt("linux", crypt.mksalt(crypt.METHOD_SHA512)))' # python3 -c 'import crypt; print(crypt.crypt("linux", crypt.mksalt(crypt.METHOD_SHA512)))'
# where "linux" is the password. # where "linux" is the password.
+4 -4
View File
@@ -22,7 +22,7 @@ IPS_JSON="$(
# An array of all node "types" # An array of all node "types"
NODE_TYPE_ARRAY="$( NODE_TYPE_ARRAY="$(
echo $IPS_JSON | \ echo $IPS_JSON | \
jq '.key' | \ jq '.value.value | to_entries | .[] | .key' | \
sed 's/"//g' | \ sed 's/"//g' | \
sed -z 's/\n/ /g;s/ $/\n/g')" sed -z 's/\n/ /g;s/ $/\n/g')"
@@ -32,12 +32,12 @@ VM_IP_EXPORTS="$(
# Convert type, converts "master-ips" to "MASTER" # Convert type, converts "master-ips" to "MASTER"
TYPE_UPPER="$(echo ${TYPE^^} | sed s/_.*$//g)" TYPE_UPPER="$(echo ${TYPE^^} | sed s/_.*$//g)"
echo "$OUTPUTS_JSON" | \ echo "$IPS_JSON" | \
jq '.'"$TYPE"'.value[]' | \ jq '.value.value.'"$TYPE"'[]' | \
# Add line numbers starting with 0. # Add line numbers starting with 0.
nl -v 0 | \ nl -v 0 | \
# Print an export string with a type placeholder "__TYPE__". # Print an export string with a type placeholder "__TYPE__".
awk '{print "export __TYPE__" $1 "=" $2}' | \ awk '{print "export __TYPE___" $1 "=" $2}' | \
sed s/__TYPE__/$TYPE_UPPER/g sed s/__TYPE__/$TYPE_UPPER/g
done)" done)"
+30 -68
View File
@@ -1,5 +1,5 @@
terraform { terraform {
required_version = ">= 0.13" required_version = ">= 1.0.8"
required_providers { required_providers {
libvirt = { libvirt = {
source = "dmacvicar/libvirt" source = "dmacvicar/libvirt"
@@ -12,24 +12,14 @@ terraform {
# cloud-init # cloud-init
################################################################################ ################################################################################
data "template_file" "master-node-user-datas" { module "cloud-init-config" {
template = file("${path.module}/cloud_init.cfg") for_each = var.nodes-config
vars = { source = "./modules/cloud-init-config"
admin-passwd = "${var.root-admin-passwd}" cloud-init-template = "${path.module}/cloud_init.cfg"
admin-pub-key = "${var.root-admin-pub-key}" hostname-prefix = "${var.vm-name-prefix}-${each.key}"
hostname = "${var.vm-name-prefix}-master-${count.index}" num = each.value.num
} root-admin-passwd = var.root-admin-passwd
count = var.master-nodes root-admin-pub-key = var.root-admin-pub-key
}
data "template_file" "worker-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.vm-name-prefix}-worker-${count.index}"
}
count = var.worker-nodes
} }
################################################################################ ################################################################################
@@ -42,9 +32,13 @@ provider "aws" {
region = "us-east-2" region = "us-east-2"
} }
module "aws-amis" { # This module will grab the latest ami for a variety of distros.
source = "./modules/aws-amis" # module "aws-amis" {
} # source = "./modules/aws-amis"
# }
# output "amis" {
# value = module.aws-amis.amis
# }
module "aws-network" { module "aws-network" {
source = "./modules/aws-network" source = "./modules/aws-network"
@@ -64,30 +58,16 @@ resource "aws_key_pair" "key" {
} }
} }
module "master-nodes" { module "nodes" {
for_each = var.nodes-config
source = "./modules/aws-nodes" source = "./modules/aws-nodes"
ami = var.base-image ami = each.value.base-image
ec2-instance-type = var.aws-ec2-instance-type ec2-instance-type = var.aws-ec2-instance-type
subnet-id = module.aws-network.subnet.id subnet-id = module.aws-network.subnet.id
security-group-ids = [module.aws-network.default-security-group.id] security-group-ids = [module.aws-network.default-security-group.id]
user-datas = data.template_file.master-node-user-datas user-datas = lookup(module.cloud-init-config, each.key, null).user-datas
num-nodes = var.master-nodes num-nodes = each.value.num
name-prefix = "${var.vm-name-prefix}-master" name-prefix = "${var.vm-name-prefix}-${each.key}"
}
module "worker-nodes" {
source = "./modules/aws-nodes"
ami = var.base-image
ec2-instance-type = var.aws-ec2-instance-type
subnet-id = module.aws-network.subnet.id
security-group-ids = [module.aws-network.default-security-group.id]
user-datas = data.template_file.worker-node-user-datas
num-nodes = var.worker-nodes
name-prefix = "${var.vm-name-prefix}-worker"
}
output "amis" {
value = module.aws-amis.amis
} }
################################################################################ ################################################################################
@@ -104,32 +84,19 @@ output "amis" {
# uri = var.libvirt-connection-url # uri = var.libvirt-connection-url
# } # }
# #
# module "master-nodes" { # module "nodes" {
# for_each = var.nodes-config
# source = "./modules/libvirt-nodes" # source = "./modules/libvirt-nodes"
# pool-name = libvirt_pool.images.name # pool-name = libvirt_pool.images.name
# name-prefix = "${var.vm-name-prefix}-master" # name-prefix = "${var.vm-name-prefix}-${each.key}"
# num-nodes = var.master-nodes # num-nodes = each.value.num
# node-memory = var.node-memory # node-memory = var.node-memory
# node-vcpus = var.node-vcpus # node-vcpus = var.node-vcpus
# base-image = var.base-image # base-image = each.value.base-image
# root-admin-passwd = var.root-admin-passwd # root-admin-passwd = var.root-admin-passwd
# root-admin-pub-key = var.root-admin-pub-key # root-admin-pub-key = var.root-admin-pub-key
# libvirt-connection-url = var.libvirt-connection-url # libvirt-connection-url = var.libvirt-connection-url
# user-datas = data.template_file.master-node-user-datas # user-datas = lookup(module.cloud-init-config, each.key, null).user-datas
# }
#
# module "worker-nodes" {
# source = "./modules/libvirt-nodes"
# pool-name = libvirt_pool.images.name
# name-prefix = "${var.vm-name-prefix}-worker"
# num-nodes = var.worker-nodes
# node-memory = var.node-memory
# node-vcpus = var.node-vcpus
# base-image = var.base-image
# root-admin-passwd = var.root-admin-passwd
# root-admin-pub-key = var.root-admin-pub-key
# libvirt-connection-url = var.libvirt-connection-url
# user-datas = data.template_file.worker-node-user-datas
# } # }
# #
# resource "libvirt_pool" "images" { # resource "libvirt_pool" "images" {
@@ -142,11 +109,6 @@ output "amis" {
# end libvirt # end libvirt
################################################################################ ################################################################################
# TODO REM move to other file? output "ips" {
output "master-ips" { value = { for type, node in module.nodes : type => node.ips }
value = module.master-nodes.ips
}
output "worker-ips" {
value = module.worker-nodes.ips
} }
+9
View File
@@ -0,0 +1,9 @@
data "template_file" "user-datas" {
template = file("${var.cloud-init-template}")
vars = {
admin-passwd = "${var.root-admin-passwd}"
admin-pub-key = "${var.root-admin-pub-key}"
hostname = "${var.hostname-prefix}-${count.index}"
}
count = var.num
}
+3
View File
@@ -0,0 +1,3 @@
output "user-datas" {
value = data.template_file.user-datas
}
+22
View File
@@ -0,0 +1,22 @@
variable "cloud-init-template" {
default = "../../cloud_init.cfg"
description = "The path to the cloud-init config template."
type = string
}
variable "hostname-prefix" {
description = "This prefix wil be applied as a prefix for the hostnames."
}
variable "num" {
description = "The number of user-datas to create with these parameters."
}
variable "root-admin-passwd" {
description = "This value will be substituted for any occurence of 'admin-password' in the cloud-init config template."
}
variable "root-admin-pub-key" {
description = "This value will be substituted for any occurence of 'admin-pub-key' in the cloud-init config template."
}
-4
View File
@@ -8,10 +8,6 @@ terraform {
} }
} }
provider "libvirt" {
uri = var.libvirt-connection-url
}
resource "libvirt_volume" "node-images" { resource "libvirt_volume" "node-images" {
name = "${var.name-prefix}-${count.index}" name = "${var.name-prefix}-${count.index}"
pool = var.pool-name pool = var.pool-name
+5
View File
@@ -43,6 +43,11 @@ variable "node-vcpus" {
type = number type = number
} }
variable "nodes-config" {
description = "A config that declares how many nodes of each type you want created."
type = map(object({base-image=string,num=number}))
}
variable "root-admin-passwd" { 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." 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."
} }