Moved everything from k8s-nodes to the top-level.
parent
2e96171329
commit
d4922cbcb9
@ -1,29 +0,0 @@
|
|||||||
A Terraform script to create k8s nodes. This script has modules for creating the
|
|
||||||
nodes on a KVM/QEMU (libvirt) hypervisor or creating the nodes via AWS.
|
|
||||||
|
|
||||||
The modules allow you create N VMs of a specific type. So you could create 1
|
|
||||||
master node and 3 worker nodes or you could create 3 Ubuntu VMs and 5 CentOS
|
|
||||||
VMs, or whatever fits your needs.
|
|
||||||
|
|
||||||
Cloud-Init
|
|
||||||
----------------------------------------
|
|
||||||
|
|
||||||
Both the libvirt and aws modules use cloud-init for initial configuration of the
|
|
||||||
VMs.
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
Other
|
|
||||||
----------------------------------------
|
|
||||||
|
|
||||||
Create a password hash.
|
|
||||||
```shell
|
|
||||||
python3 -c 'import crypt; print(crypt.crypt("test", crypt.mksalt(crypt.METHOD_SHA512)))'
|
|
||||||
```
|
|
||||||
@ -1,42 +0,0 @@
|
|||||||
#cloud-config
|
|
||||||
# vim: syntax=yaml
|
|
||||||
|
|
||||||
users:
|
|
||||||
- name: admin
|
|
||||||
# If we don't supress the user group then cloud init will fail because there
|
|
||||||
# is allready an admin group in the ubuntu base image.
|
|
||||||
no_user_group: true
|
|
||||||
groups: users, admin, sudo
|
|
||||||
shell: /usr/bin/bash
|
|
||||||
sudo: ALL=(ALL) NOPASSWD:ALL
|
|
||||||
ssh_authorized_keys:
|
|
||||||
- ${admin-pub-key}
|
|
||||||
- name: root
|
|
||||||
ssh_authorized_keys:
|
|
||||||
- ${admin-pub-key}
|
|
||||||
|
|
||||||
ssh_pwauth: true
|
|
||||||
disable_root: false
|
|
||||||
chpasswd:
|
|
||||||
list:
|
|
||||||
- root:${admin-passwd}
|
|
||||||
- admin:${admin-passwd}
|
|
||||||
expire: false
|
|
||||||
|
|
||||||
hostname: ${hostname}
|
|
||||||
fqdn: ${hostname}
|
|
||||||
|
|
||||||
%{ if install-qemu-agent }
|
|
||||||
packages:
|
|
||||||
# This are only necessary for libvirt.
|
|
||||||
- qemu-guest-agent
|
|
||||||
runcmd:
|
|
||||||
# TODO At some point revisit this, this was added because it seemed like
|
|
||||||
# apparmor was causing dhclient to not get an IP address for ubuntu. This
|
|
||||||
# should be double checked.
|
|
||||||
- echo "/proc/*/task/*/comm wr," | tee -a /etc/apparmor.d/local/sbin.dhclient
|
|
||||||
# These are only necessary for libvirt.
|
|
||||||
- systemctl enable qemu-guest-agent
|
|
||||||
- systemctl start qemu-guest-agent
|
|
||||||
- systemctl status qemu-guest-agent
|
|
||||||
%{ endif }
|
|
||||||
@ -1,85 +0,0 @@
|
|||||||
vm-name-prefix = "ansible-test"
|
|
||||||
|
|
||||||
# A CIDR block ending in '/32' equates to a single IP address, '0.0.0.0/0'
|
|
||||||
# equates to any ip address.
|
|
||||||
admin-ips = [ "8.8.8.8/32", "0.0.0.0/0" ]
|
|
||||||
|
|
||||||
disk-image-dir = "/path/to/disk/pool/"
|
|
||||||
libvirt-connection-url = "qemu+ssh://<user>@<host>/system"
|
|
||||||
|
|
||||||
node-memory = 2048
|
|
||||||
node-vcpus = 2
|
|
||||||
|
|
||||||
## libvirt disk size
|
|
||||||
# 1 GiB = 1073741824
|
|
||||||
# 4 GiB
|
|
||||||
# libvirt-node-disk-size = "${4 * 1073741824}"
|
|
||||||
# 8 GiB
|
|
||||||
# libvirt-node-disk-size = "${8 * 1073741824}"
|
|
||||||
# 12 GiB
|
|
||||||
# libvirt-node-disk-size = "${12 * 1073741824}"
|
|
||||||
|
|
||||||
################################################################################
|
|
||||||
# AWS EC2 instance types
|
|
||||||
################################################################################
|
|
||||||
|
|
||||||
# 1 GiB, 1 vcpu, only one that is free.
|
|
||||||
# This one won't work with k8s because it requires at least 2 vcpus.
|
|
||||||
aws-ec2-instance-type = "t2.micro"
|
|
||||||
|
|
||||||
# 4 GiB, 2 vcpus
|
|
||||||
# aws-ec2-instnce-type = "t2.medium"
|
|
||||||
|
|
||||||
################################################################################
|
|
||||||
# AWS images (AMIs)
|
|
||||||
################################################################################
|
|
||||||
|
|
||||||
## Amazon Linux 2
|
|
||||||
# AWS Amazon Linux 2 AMI (HVM), SSD Volume Type - Oregon - 2021.11.11 - free
|
|
||||||
# base-image = "ami-00be885d550dcee43"
|
|
||||||
# AWS Amazon Linux 2 AMI (HVM), SSD Volume Type - us-east-2 - 2021.11.12 - free
|
|
||||||
base-image = "ami-0dd0ccab7e2801812"
|
|
||||||
|
|
||||||
## CentOS
|
|
||||||
# CentOS 7.9.2009 x86_64 - us-east-2 - 2021-11-15
|
|
||||||
# base-image = "ami-00f8e2c955f7ffa9b"
|
|
||||||
# CentOS 8.4.2105 x86_64 - us-east-2 - 2021-11015
|
|
||||||
# base-image = "ami-057cacbfbbb471bb3"
|
|
||||||
|
|
||||||
## Ubuntu
|
|
||||||
# Ubuntu Server 20.04 LTS (HVM), SSD Volume Type
|
|
||||||
# us-east-2 - (64-bit x86) - 2021.11.12 - free
|
|
||||||
# base-image = "ami-0629230e074c580f2"
|
|
||||||
|
|
||||||
## Arch linux
|
|
||||||
# arch-linux-lts-hvm-2021.06.02.x86_64-ebs - us-east-2
|
|
||||||
# base-image = "ami-02653f06de985e3ba"
|
|
||||||
|
|
||||||
################################################################################
|
|
||||||
# libvirt images
|
|
||||||
################################################################################
|
|
||||||
|
|
||||||
# base-image = "https://cloud-images.ubuntu.com/releases/focal/release/ubuntu-20.04-server-cloudimg-amd64-disk-kvm.img"
|
|
||||||
# From https://cloud.centos.org/centos/7/images/ from 2020-12-06
|
|
||||||
# base-image = "https://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud-2111.qcow2"
|
|
||||||
|
|
||||||
## Arch
|
|
||||||
# From https://gitlab.archlinux.org/archlinux/arch-boxes/-/jobs/40102/artifacts/browse/output
|
|
||||||
# on 2021-11-28
|
|
||||||
# base-image = "/media/nas/software/isos/Arch-Linux-x86_64-libvirt-20211128.40102.box"
|
|
||||||
|
|
||||||
## Arch
|
|
||||||
# From https://gitlab.archlinux.org/archlinux/arch-boxes/-/jobs/40102/artifacts/browse/output
|
|
||||||
# on 2021-11-28
|
|
||||||
# base-image = "/media/nas/software/isos/Arch-Linux-x86_64-libvirt-20211128.40102.box"
|
|
||||||
|
|
||||||
################################################################################
|
|
||||||
# Keys/Passwords
|
|
||||||
################################################################################
|
|
||||||
|
|
||||||
# Password hash created with:
|
|
||||||
# python3 -c 'import crypt; print(crypt.crypt("linux", crypt.mksalt(crypt.METHOD_SHA512)))'
|
|
||||||
# where "linux" is the password.
|
|
||||||
root-admin-passwd = "$6$fiLRWvGQkdK.MnZA$Co9NkA5ruuBUA389JzmKJiC8gKRohmyM09AFnVBOD7ErZnxK4RHMUlKvYg1HSgwaCXTl7H/q1svoeQeUfgc6f0"
|
|
||||||
|
|
||||||
root-admin-pub-key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDfDcjMFmWd6qy9KIlnIHNbEfeNLHC885UUH3jGwESmMTpFfPUn01t9hq5GGaFDrBR55VgdKebAv2JSVl209+r3tE5XxUX5/s2Pu3o2283PiZhA+D18skL7fzaolygOY8mxi9CZSDFia//lLbqT/OE45VGahVBRtda4gmjrade0XRKqjJUCkIo6huG9Ub6yP4gFtFU/C1rRvQo0hqT/imsMYU0Q5XzrKVWv3CpzA7EIQq8llU0fRGMuXWYYOXznPeqqf5BTbWhMWUXVS0o7Cz+zvbxwq1dOR1qHbJ8Vrkt30Cz5QEd159dIM3LHCtOHnveeOpkFo0RqkhQdpZM+2cKzESvivGNGP9h+PrSjcveADxVwDHcxguumUyM012M3yR8cK9KY+GqW5jPdAs13yXGTG4OWiQKeKEgX910l/FndhQi0tSpSEhIlfcEpa3k3P8RrhKJbwiRgR7Qvus4R/KU+lx4OiOr4RKyPQJobC0i0/bvqkw+UHWp4U0Hqivjsb6k= admin"
|
|
||||||
@ -1,4 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
|
|
||||||
aws ec2 describe-instance-status | \
|
|
||||||
jq '.InstanceStatuses[] | {id: .InstanceId, instance_status: .InstanceStatus.Status, system_status: .SystemStatus.Status}'
|
|
||||||
@ -1,81 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
# This script will grab the IPs for libvirt VMs. This script is only needed when
|
|
||||||
# using a bridge as the network for the VMs. This should only be needed while
|
|
||||||
# https://github.com/dmacvicar/terraform-provider-libvirt/issues/891 is
|
|
||||||
# unresolved.
|
|
||||||
|
|
||||||
# These are the network interfaces that this script will attempt to get the IP
|
|
||||||
# address for.
|
|
||||||
# Ubuntu 20.04 ens3
|
|
||||||
# Centos 7 & 8 eth0
|
|
||||||
NET_INTERFACES="eth0 ens3"
|
|
||||||
|
|
||||||
LIBVIRT_CONNECTION_URL="libvirt-connection-url"
|
|
||||||
VM_NAME_PREFIX="vm-name-prefix"
|
|
||||||
|
|
||||||
INV_GROUPS="$( \
|
|
||||||
cat terraform.tfstate | \
|
|
||||||
jq '.resources[] | select(.type=="libvirt_domain") | .module' | \
|
|
||||||
sed 's/".*\[\\"\(.*\)\\.*$/\1/g' )"
|
|
||||||
|
|
||||||
# Grab the connection URL and the vm name prefix. We do this by greping all
|
|
||||||
# *.tfvars files making sure to cat terraform.tfvars last. Then we just grab the
|
|
||||||
# last grep result, this way we make sure any value in terraform.tfvars will
|
|
||||||
# take priority.
|
|
||||||
CONN_URLS="$( \
|
|
||||||
find . -name "*.tfvars" -exec grep "$LIBVIRT_CONNECTION_URL" {} \; && \
|
|
||||||
grep "$LIBVIRT_CONNECTION_URL" terraform.tfvars)"
|
|
||||||
|
|
||||||
CONN_URL="$(echo "$CONN_URLS" | tail -n 1 | sed 's/^.*=\s*"\(.*\)"/\1/g')"
|
|
||||||
|
|
||||||
NAME_PREFIXES="$( \
|
|
||||||
find . -name "*.tfvars" -exec grep "$VM_NAME_PREFIX" {} \; && \
|
|
||||||
grep "$VM_NAME_PREFIX" terraform.tfvars)"
|
|
||||||
|
|
||||||
NAME_PREFIX="$(echo "$NAME_PREFIXES" | tail -n 1 | sed 's/^.*=\s*"\(.*\)"/\1/g')"
|
|
||||||
|
|
||||||
# These can be used for debugging.
|
|
||||||
# echo "Using connection URL: $CONN_URL"
|
|
||||||
# echo "Using prefix: $NAME_PREFIX"
|
|
||||||
|
|
||||||
# Get the names of our VMs from libvirt.
|
|
||||||
VMS="$(virsh -c $CONN_URL list --all | grep $NAME_PREFIX | awk '{print $2}')"
|
|
||||||
|
|
||||||
# Convert the lines of VM names to an array.
|
|
||||||
OLD_IFS=$IFS
|
|
||||||
IFS=$'\n'
|
|
||||||
VMS=($VMS)
|
|
||||||
IFS=$OLD_IFS
|
|
||||||
|
|
||||||
# Loop over our VM array and grab the ipv4 IP address from libvirt. Then add the
|
|
||||||
# result to VM_IP_PAIRS as <vm-name>:<ipv4-address>.
|
|
||||||
VM_IP_PAIRS=""
|
|
||||||
for VM in "${VMS[@]}"; do
|
|
||||||
for INTERFACE in $NET_INTERFACES; do
|
|
||||||
IP="$( \
|
|
||||||
virsh -c $CONN_URL qemu-agent-command $VM '{"execute": "guest-network-get-interfaces"}' | \
|
|
||||||
jq '.return[] | select(.name=="'"$INTERFACE"'") | ."ip-addresses"[] | select(."ip-address-type"=="ipv4") | ."ip-address"' | \
|
|
||||||
sed 's/"//g')"
|
|
||||||
# Add the VM:IP pair if IP is not empty.
|
|
||||||
if [ ! -z "$IP" ]; then
|
|
||||||
VM_IP_PAIRS="$VM_IP_PAIRS"$'\n'"$VM:$IP"
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
done
|
|
||||||
|
|
||||||
# Write inventory
|
|
||||||
cat /dev/null > inventory
|
|
||||||
for GROUP in $INV_GROUPS; do
|
|
||||||
echo "[$GROUP]" >> inventory
|
|
||||||
echo "$VM_IP_PAIRS" | \
|
|
||||||
grep $GROUP | \
|
|
||||||
sed 's/^\(.*\):\(.*\)$/\1 ansible_host=\2/g' >> inventory
|
|
||||||
done
|
|
||||||
|
|
||||||
# Print vars
|
|
||||||
echo "$VM_IP_PAIRS" | \
|
|
||||||
sed 's/^\(.*\):\(.*\)$/\1=\2/g' | \
|
|
||||||
sed s/$NAME_PREFIX-//g | \
|
|
||||||
sed 's/-/_/g' | \
|
|
||||||
awk '{print toupper($1)}'
|
|
||||||
@ -1,92 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
# This script will create environment variables for all of the output IPs. An
|
|
||||||
# anisble inventory file is created as well.
|
|
||||||
#
|
|
||||||
# Use eval $(./get-vm-ips.sh) to set env vars for ips.
|
|
||||||
|
|
||||||
terraform refresh > /dev/null
|
|
||||||
|
|
||||||
# The file to write the inventory to. This file will be completely overridden.
|
|
||||||
INVENTORY_FILE="inventory"
|
|
||||||
|
|
||||||
# Grab the the vm name prefix. We do this by greping all *.tfvars files making
|
|
||||||
# sure to cat terraform.tfvars last. Then we just grab the last grep result,
|
|
||||||
# this way we make sure any value in terraform.tfvars will take priority.
|
|
||||||
VM_NAME_PREFIX_VAR="vm-name-prefix"
|
|
||||||
VM_NAME_PREFIXES="$( \
|
|
||||||
find . -name "*.tfvars" -exec grep "$VM_NAME_PREFIX_VAR" {} \; && \
|
|
||||||
grep "$VM_NAME_PREFIX_VAR" terraform.tfvars)"
|
|
||||||
VM_NAME_PREFIX="$(
|
|
||||||
echo "$VM_NAME_PREFIXES" | \
|
|
||||||
tail -n 1 | \
|
|
||||||
sed 's/^.*=\s*"\(.*\)"/\1/g')"
|
|
||||||
|
|
||||||
# This command stores the output data in the format below.
|
|
||||||
# [
|
|
||||||
# {
|
|
||||||
# "group": "master",
|
|
||||||
# "vms": [
|
|
||||||
# {
|
|
||||||
# "hostname": "ansible-test-master-0",
|
|
||||||
# "ip": "52.14.114.48"
|
|
||||||
# }
|
|
||||||
# ]
|
|
||||||
# },
|
|
||||||
# {
|
|
||||||
# "group": "worker",
|
|
||||||
# "vms": [
|
|
||||||
# {
|
|
||||||
# "hostname": "ansible-test-worker-0",
|
|
||||||
# "ip": "3.145.121.159"
|
|
||||||
# },
|
|
||||||
# {
|
|
||||||
# "hostname": "ansible-test-worker-1",
|
|
||||||
# "ip": "18.217.112.176"
|
|
||||||
# }
|
|
||||||
# ]
|
|
||||||
# }
|
|
||||||
# ]
|
|
||||||
DATA="$(terraform show -json | \
|
|
||||||
jq '.values.outputs.groups_hostnames_ips.value | to_entries |
|
|
||||||
map({group: .key, vms:.value | to_entries |
|
|
||||||
map({hostname:.key,ip:.value})})')"
|
|
||||||
|
|
||||||
# Pull out the groups from $DATA. The format is a single string with the groups
|
|
||||||
# separated by spaces, ie. "group1 group2 group3".
|
|
||||||
ANS_GROUPS="$(
|
|
||||||
echo $DATA | \
|
|
||||||
jq '.[] | .group' | \
|
|
||||||
sed 's/"//g' | \
|
|
||||||
tr '\n' ' '
|
|
||||||
)"
|
|
||||||
|
|
||||||
# Clear the inventory file.
|
|
||||||
cat /dev/null > $INVENTORY_FILE
|
|
||||||
|
|
||||||
# For each group, write the VM info to $INVENTORY_FILE and also print a variable
|
|
||||||
# expression to stdout.
|
|
||||||
for GROUP in $ANS_GROUPS; do
|
|
||||||
|
|
||||||
# Write the inventory file to $INVENTORY_FILE.
|
|
||||||
echo "[$GROUP]" >> $INVENTORY_FILE
|
|
||||||
echo $DATA | \
|
|
||||||
jq '.[] | select(.group=="'"$GROUP"'") | .vms[] |
|
|
||||||
"\(.hostname) ansible_host=\(.ip)"' | \
|
|
||||||
sed 's/"//g' \
|
|
||||||
>> $INVENTORY_FILE
|
|
||||||
|
|
||||||
# For this group, collect expressions into VARS. The format is:
|
|
||||||
# HOSTNAME1=0.0.0.0
|
|
||||||
# HOSTNAME2=0.0.0.0
|
|
||||||
VARS="$(
|
|
||||||
echo $DATA | \
|
|
||||||
jq '.[] | select(.group=="'"$GROUP"'") | .vms[] |
|
|
||||||
"\(.hostname)=\(.ip)"' | \
|
|
||||||
sed 's/"//g' | \
|
|
||||||
sed "s/$VM_NAME_PREFIX-//g" | \
|
|
||||||
sed 's/-/_/g'
|
|
||||||
)"
|
|
||||||
# Print the contents of $VARS converted to uppercase.
|
|
||||||
echo "${VARS^^}"
|
|
||||||
done
|
|
||||||
@ -1,134 +0,0 @@
|
|||||||
|
|
||||||
locals {
|
|
||||||
nodes-config = {
|
|
||||||
"master" = {
|
|
||||||
base-image = var.centos8-ami
|
|
||||||
num = 1
|
|
||||||
},
|
|
||||||
"worker" = {
|
|
||||||
base-image = var.centos8-ami
|
|
||||||
num = 2
|
|
||||||
}
|
|
||||||
}
|
|
||||||
install-qemu-agent = false
|
|
||||||
}
|
|
||||||
|
|
||||||
################################################################################
|
|
||||||
# cloud-init
|
|
||||||
################################################################################
|
|
||||||
|
|
||||||
module "cloud-init-config" {
|
|
||||||
for_each = local.nodes-config
|
|
||||||
source = "./modules/cloud-init-config"
|
|
||||||
cloud-init-template = "${path.module}/cloud_init.cfg"
|
|
||||||
hostname-prefix = "${var.vm-name-prefix}-${each.key}"
|
|
||||||
num = each.value.num
|
|
||||||
root-admin-passwd = var.root-admin-passwd
|
|
||||||
root-admin-pub-key = var.root-admin-pub-key
|
|
||||||
install-qemu-agent = local.install-qemu-agent
|
|
||||||
}
|
|
||||||
|
|
||||||
################################################################################
|
|
||||||
# aws
|
|
||||||
# To use the aws module, uncomment the aws modules/resources and comment out the
|
|
||||||
# libvirt modules/resources.
|
|
||||||
################################################################################
|
|
||||||
|
|
||||||
# This module will grab the latest ami for a variety of distros. Uncomment to
|
|
||||||
# get a list of the latest AMIs for our supported distros.
|
|
||||||
# module "aws-amis" {
|
|
||||||
# source = "./modules/aws-amis"
|
|
||||||
# }
|
|
||||||
# output "amis" {
|
|
||||||
# value = module.aws-amis.amis
|
|
||||||
# }
|
|
||||||
|
|
||||||
################################################################################
|
|
||||||
# AWS Networking
|
|
||||||
# Use of the 2 modules below to create resources for the AWS network.
|
|
||||||
# aws-network-from-scratch will build the AWS network from scratch.
|
|
||||||
# aws-network-existing will query AWS for an existing VPC.
|
|
||||||
################################################################################
|
|
||||||
|
|
||||||
# module "aws-network-from-scratch" {
|
|
||||||
# source = "./modules/aws-network-from-scratch"
|
|
||||||
# name-prefix = var.vm-name-prefix
|
|
||||||
# vpc-cidr-block = var.aws-vpc-cidr-block
|
|
||||||
# subnet-cidr-block = var.aws-subnet-cidr-block
|
|
||||||
# admin-ips = var.admin-ips
|
|
||||||
# }
|
|
||||||
|
|
||||||
module "aws-network-existing" {
|
|
||||||
source = "./modules/aws-network-existing"
|
|
||||||
}
|
|
||||||
|
|
||||||
################################################################################
|
|
||||||
|
|
||||||
# This key pair is not actually used. Keys are added to the nodes via cloud-init
|
|
||||||
# instead. We just add this here that this key will show up in the AWS console."
|
|
||||||
resource "aws_key_pair" "key" {
|
|
||||||
key_name = "${var.vm-name-prefix}-key}"
|
|
||||||
public_key = var.root-admin-pub-key
|
|
||||||
tags = {
|
|
||||||
Name = "${var.vm-name-prefix}-key"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module "nodes" {
|
|
||||||
for_each = local.nodes-config
|
|
||||||
source = "./modules/aws-nodes"
|
|
||||||
ami = each.value.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 = lookup(module.cloud-init-config, each.key, null).user-datas
|
|
||||||
num-nodes = each.value.num
|
|
||||||
name-prefix = "${var.vm-name-prefix}-${each.key}"
|
|
||||||
}
|
|
||||||
|
|
||||||
################################################################################
|
|
||||||
# end aws
|
|
||||||
################################################################################
|
|
||||||
|
|
||||||
################################################################################
|
|
||||||
# libvirt
|
|
||||||
# To use the libvirt module, uncomment the libvirt modules/resources and comment
|
|
||||||
# out the aws modules/resources.
|
|
||||||
################################################################################
|
|
||||||
|
|
||||||
# provider "libvirt" {
|
|
||||||
# uri = var.libvirt-connection-url
|
|
||||||
# }
|
|
||||||
#
|
|
||||||
# module "nodes" {
|
|
||||||
# for_each = local.nodes-config
|
|
||||||
# source = "./modules/libvirt-nodes"
|
|
||||||
# pool-name = libvirt_pool.images.name
|
|
||||||
# name-prefix = "${var.vm-name-prefix}-${each.key}"
|
|
||||||
# num-nodes = each.value.num
|
|
||||||
# node-memory = var.node-memory
|
|
||||||
# node-vcpus = var.node-vcpus
|
|
||||||
# node-disk-size = var.libvirt-node-disk-size
|
|
||||||
# base-image = each.value.base-image
|
|
||||||
# network-name = var.libvirt-network-name
|
|
||||||
# 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 = lookup(module.cloud-init-config, each.key, null).user-datas
|
|
||||||
# }
|
|
||||||
#
|
|
||||||
# resource "libvirt_pool" "images" {
|
|
||||||
# name = var.disk-image-pool-name
|
|
||||||
# type = "dir"
|
|
||||||
# path = var.disk-image-dir
|
|
||||||
# }
|
|
||||||
|
|
||||||
################################################################################
|
|
||||||
# end libvirt
|
|
||||||
################################################################################
|
|
||||||
|
|
||||||
# This will outpus a map of group => [{hostname, ip}].
|
|
||||||
# TODO A 'names' output needs to be added to libvirt.
|
|
||||||
output "groups_hostnames_ips" {
|
|
||||||
value = { for type, node in module.nodes : type => zipmap(node.names, node.ips) }
|
|
||||||
}
|
|
||||||
@ -1,58 +0,0 @@
|
|||||||
locals {
|
|
||||||
amis = {
|
|
||||||
amzn2 = {
|
|
||||||
owner-id = "137112412989"
|
|
||||||
name = "amzn2-ami-hvm-2*x86_64-gp2"
|
|
||||||
},
|
|
||||||
ubuntu = {
|
|
||||||
owner-id = "099720109477"
|
|
||||||
name = "ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"
|
|
||||||
},
|
|
||||||
centos7 = {
|
|
||||||
owner-id = "125523088429"
|
|
||||||
name = "CentOS 7.*x86_64"
|
|
||||||
},
|
|
||||||
centos8 = {
|
|
||||||
owner-id = "125523088429"
|
|
||||||
name = "CentOS 8.*x86_64"
|
|
||||||
},
|
|
||||||
arch = {
|
|
||||||
owner-id = "093273469852"
|
|
||||||
name = "arch-linux-lts-hvm*x86_64-ebs"
|
|
||||||
},
|
|
||||||
rhel7 = {
|
|
||||||
owner-id = "309956199498"
|
|
||||||
name = "RHEL-7.*HVM*x86_64*GP2"
|
|
||||||
},
|
|
||||||
rhel8 = {
|
|
||||||
owner-id = "309956199498"
|
|
||||||
name = "RHEL-8.*HVM*x86_64*GP2"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
data "aws_ami" "amis" {
|
|
||||||
for_each = local.amis
|
|
||||||
most_recent = true
|
|
||||||
owners = [each.value.owner-id]
|
|
||||||
|
|
||||||
filter {
|
|
||||||
name = "name"
|
|
||||||
values = [each.value.name]
|
|
||||||
}
|
|
||||||
|
|
||||||
filter {
|
|
||||||
name = "virtualization-type"
|
|
||||||
values = ["hvm"]
|
|
||||||
}
|
|
||||||
|
|
||||||
filter {
|
|
||||||
name = "architecture"
|
|
||||||
values = ["x86_64"]
|
|
||||||
}
|
|
||||||
|
|
||||||
filter {
|
|
||||||
name = "root-device-type"
|
|
||||||
values = ["ebs"]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,3 +0,0 @@
|
|||||||
output "amis" {
|
|
||||||
value = tomap({ for type, ami in data.aws_ami.amis : type => ami.id })
|
|
||||||
}
|
|
||||||
@ -1,15 +0,0 @@
|
|||||||
resource "aws_instance" "nodes" {
|
|
||||||
ami = var.ami
|
|
||||||
instance_type = var.ec2-instance-type
|
|
||||||
# TODO REM double check this key.
|
|
||||||
# key_name = aws_key_pair.debug1.key_name
|
|
||||||
associate_public_ip_address = true
|
|
||||||
subnet_id = var.subnet-id
|
|
||||||
vpc_security_group_ids = var.security-group-ids
|
|
||||||
user_data = element(var.user-datas.*.rendered, count.index)
|
|
||||||
count = var.num-nodes
|
|
||||||
|
|
||||||
tags = {
|
|
||||||
Name = "${var.name-prefix}-${count.index}"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,7 +0,0 @@
|
|||||||
output "ips" {
|
|
||||||
value = aws_instance.nodes.*.public_ip
|
|
||||||
}
|
|
||||||
|
|
||||||
output "names" {
|
|
||||||
value = aws_instance.nodes.*.tags.Name
|
|
||||||
}
|
|
||||||
@ -1,36 +0,0 @@
|
|||||||
variable "ami" {
|
|
||||||
description = "The AWS AMI to be used for all the nodes"
|
|
||||||
type = string
|
|
||||||
}
|
|
||||||
|
|
||||||
variable "ec2-instance-type" {
|
|
||||||
default = "t2.micro"
|
|
||||||
description = "The AWS instance type to use for all nodes."
|
|
||||||
type = string
|
|
||||||
}
|
|
||||||
|
|
||||||
variable "name-prefix" {
|
|
||||||
default = "tf-node"
|
|
||||||
description = "This prefix will be applied to all names created by this module."
|
|
||||||
type = string
|
|
||||||
}
|
|
||||||
|
|
||||||
variable "num-nodes" {
|
|
||||||
default = 1
|
|
||||||
description = "The number of nodes to create from the given input parameters."
|
|
||||||
type = number
|
|
||||||
}
|
|
||||||
|
|
||||||
variable "user-datas" {
|
|
||||||
description = "A list of cloud-init configs that get applied to their corresponding node."
|
|
||||||
}
|
|
||||||
|
|
||||||
variable "subnet-id" {
|
|
||||||
description = "The ID of the subnet that all the nodes will be added to."
|
|
||||||
type = string
|
|
||||||
}
|
|
||||||
|
|
||||||
variable "security-group-ids" {
|
|
||||||
description = "A list of security group IDs to be applied to all the nodes."
|
|
||||||
type = list(string)
|
|
||||||
}
|
|
||||||
@ -1,10 +0,0 @@
|
|||||||
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}"
|
|
||||||
install-qemu-agent = var.install-qemu-agent
|
|
||||||
}
|
|
||||||
count = var.num
|
|
||||||
}
|
|
||||||
@ -1,3 +0,0 @@
|
|||||||
output "user-datas" {
|
|
||||||
value = data.template_file.user-datas
|
|
||||||
}
|
|
||||||
@ -1,28 +0,0 @@
|
|||||||
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 "install-qemu-agent" {
|
|
||||||
default = false
|
|
||||||
description = "This flag determines whether or not qemu-agent is installed."
|
|
||||||
type = bool
|
|
||||||
}
|
|
||||||
|
|
||||||
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."
|
|
||||||
}
|
|
||||||
|
|
||||||
@ -1,77 +0,0 @@
|
|||||||
terraform {
|
|
||||||
required_version = ">= 0.13"
|
|
||||||
required_providers {
|
|
||||||
libvirt = {
|
|
||||||
source = "dmacvicar/libvirt"
|
|
||||||
version = "0.6.11"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
resource "libvirt_volume" "node-images" {
|
|
||||||
name = "${var.name-prefix}-base"
|
|
||||||
pool = var.pool-name
|
|
||||||
source = var.base-image
|
|
||||||
format = "qcow2"
|
|
||||||
}
|
|
||||||
|
|
||||||
resource "libvirt_volume" "node-images-resized" {
|
|
||||||
name = "${var.name-prefix}-${count.index}-resized"
|
|
||||||
pool = var.pool-name
|
|
||||||
base_volume_id = libvirt_volume.node-images.id
|
|
||||||
count = var.num-nodes
|
|
||||||
size = var.node-disk-size
|
|
||||||
}
|
|
||||||
|
|
||||||
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(var.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 = var.network-name
|
|
||||||
hostname = "${var.name-prefix}-${count.index}"
|
|
||||||
# wait_for_lease = true
|
|
||||||
}
|
|
||||||
|
|
||||||
# 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-resized.*.id, count.index)
|
|
||||||
}
|
|
||||||
|
|
||||||
graphics {
|
|
||||||
type = "spice"
|
|
||||||
listen_type = "address"
|
|
||||||
autoport = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@ -1,4 +0,0 @@
|
|||||||
version: 2
|
|
||||||
ethernets:
|
|
||||||
ens3:
|
|
||||||
dhcp4: true
|
|
||||||
@ -1,6 +0,0 @@
|
|||||||
|
|
||||||
# This only works on the default network. They will not work using the bridged
|
|
||||||
# network.
|
|
||||||
# output "ips" {
|
|
||||||
# value = libvirt_domain.nodes.*.network_interface.0.addresses.0
|
|
||||||
# }
|
|
||||||
@ -1,56 +0,0 @@
|
|||||||
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 "network-name" {
|
|
||||||
default = "default"
|
|
||||||
description = "The name of a pre-existing virtual-network."
|
|
||||||
}
|
|
||||||
|
|
||||||
variable "node-disk-size" {
|
|
||||||
default = 4294967296
|
|
||||||
description = "The size of the disk to be used for libvirt nodes. (in bytes)"
|
|
||||||
}
|
|
||||||
|
|
||||||
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 "user-datas" {
|
|
||||||
description = "A list of cloud-init configs that get applied to their corresponding node."
|
|
||||||
}
|
|
||||||
|
|
||||||
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."
|
|
||||||
}
|
|
||||||
@ -1,146 +0,0 @@
|
|||||||
variable "admin-ips" {
|
|
||||||
description = "A list of ips or cidr blocks that are allowed to connect to the nodes."
|
|
||||||
type = list(string)
|
|
||||||
}
|
|
||||||
|
|
||||||
variable "aws-ec2-instance-type" {
|
|
||||||
default = "t2.micro"
|
|
||||||
description = "The AWS instance type to use for all nodes."
|
|
||||||
}
|
|
||||||
|
|
||||||
variable "aws-region" {
|
|
||||||
default = "us-east-1"
|
|
||||||
description = "The AWS region to use."
|
|
||||||
}
|
|
||||||
|
|
||||||
variable "aws-subnet-cidr-block" {
|
|
||||||
default = "10.0.1.0/24"
|
|
||||||
description = "The address space to be used for this subnet."
|
|
||||||
}
|
|
||||||
|
|
||||||
variable "aws-vpc-cidr-block" {
|
|
||||||
default = "10.0.0.0/16"
|
|
||||||
description = "The address space to be used for the VPC that all the AWS nodes will be in."
|
|
||||||
}
|
|
||||||
|
|
||||||
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 "libvirt-network-name" {
|
|
||||||
default = "default"
|
|
||||||
description = "The name of a pre-existing libvirt virtual-network."
|
|
||||||
}
|
|
||||||
|
|
||||||
variable "libvirt-node-disk-size" {
|
|
||||||
default = 4294967296
|
|
||||||
description = "The size of the disk to be used for libvirt nodes. (in bytes)"
|
|
||||||
}
|
|
||||||
|
|
||||||
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 "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."
|
|
||||||
}
|
|
||||||
|
|
||||||
variable "master-nodes" {
|
|
||||||
default = 1
|
|
||||||
description = "The number of master nodes to create."
|
|
||||||
type = number
|
|
||||||
}
|
|
||||||
|
|
||||||
variable "worker-nodes" {
|
|
||||||
default = 2
|
|
||||||
description = "The number of worker nodes to create."
|
|
||||||
type = number
|
|
||||||
}
|
|
||||||
|
|
||||||
variable "base-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."
|
|
||||||
}
|
|
||||||
|
|
||||||
################################################################################
|
|
||||||
# AWS AMI vars
|
|
||||||
# These variables are really mor like constants. Using variables improves
|
|
||||||
# readability. The defaults are manually updated. Use the aws-amis module to get
|
|
||||||
# the latest for each distro.
|
|
||||||
################################################################################
|
|
||||||
|
|
||||||
variable "amzn2-ami" {
|
|
||||||
default = "ami-0dd0ccab7e2801812"
|
|
||||||
description = "The AMI to use for Amazon Linux 2."
|
|
||||||
}
|
|
||||||
variable "ubuntu-ami" {
|
|
||||||
default = "ami-06c7d6c0987eaa46c"
|
|
||||||
description = "The AMI to use for Ubuntu."
|
|
||||||
}
|
|
||||||
variable "centos7-ami" {
|
|
||||||
default = "ami-00f8e2c955f7ffa9b"
|
|
||||||
description = "The AMI to use for CentOS 7."
|
|
||||||
}
|
|
||||||
variable "centos8-ami" {
|
|
||||||
default = "ami-057cacbfbbb471bb3"
|
|
||||||
description = "The AMI to use for CentOS 8."
|
|
||||||
}
|
|
||||||
variable "arch-ami" {
|
|
||||||
default = "ami-02653f06de985e3ba"
|
|
||||||
description = "The AMI to use for Arch Linux."
|
|
||||||
}
|
|
||||||
variable "rhel7-ami" {
|
|
||||||
default = "ami-0a509b3c2a4d05b3f"
|
|
||||||
description = "The AMI to use for RHEL 7."
|
|
||||||
}
|
|
||||||
variable "rhel8-ami" {
|
|
||||||
default = "ami-0d871ca8a77af2948"
|
|
||||||
description = "The AMI to use for RHEL 8."
|
|
||||||
}
|
|
||||||
|
|
||||||
################################################################################
|
|
||||||
# Libvirt Images
|
|
||||||
# These variables are really mor like constants. Using variables improves
|
|
||||||
# readability. The defaults are manually updated.
|
|
||||||
################################################################################
|
|
||||||
|
|
||||||
variable "ubuntu-img" {
|
|
||||||
default = "https://cloud-images.ubuntu.com/releases/focal/release/ubuntu-20.04-server-cloudimg-amd64-disk-kvm.img"
|
|
||||||
description = "The libvirt image tp use for Ubuntu."
|
|
||||||
}
|
|
||||||
|
|
||||||
variable "centos7-img" {
|
|
||||||
# Latest as of 2021-12-06.
|
|
||||||
default = "https://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud-2111.qcow2"
|
|
||||||
description = "The libvirt image tp use for CentOS 7."
|
|
||||||
}
|
|
||||||
|
|
||||||
variable "centos8-img" {
|
|
||||||
default = "https://cloud.centos.org/centos/8/x86_64/images/CentOS-8-GenericCloud-8.4.2105-20210603.0.x86_64.qcow2"
|
|
||||||
description = "The libvirt image tp use for CentOS 8."
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue