Skip to content

Instantly share code, notes, and snippets.

@yogesh174
Created June 6, 2021 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yogesh174/e6936ea1589b9c0a4eeced3562dcaa52 to your computer and use it in GitHub Desktop.
Save yogesh174/e6936ea1589b9c0a4eeced3562dcaa52 to your computer and use it in GitHub Desktop.
#########################################
######## Create k8s worker nodes ########
#########################################
resource "aws_instance" "k8s_worker" {
count = var.aws_nodes
ami = data.aws_ami.amazon_linux_2.image_id
instance_type = var.aws_instance_type
# Created after and destroyed before master node
depends_on = [
aws_instance.k8s_master,
]
tags = {
Name = "aws-k8s-worker-${count.index}"
}
key_name = aws_key_pair.terraform_k8s.key_name
provisioner "local-exec" {
when = destroy
command = "ansible-playbook ansible/destroy-k8s-worker.yaml -i ansible-inventory -e 'node=${self.private_dns}'"
}
}
#########################################
######## Create k8s worker nodes ########
#########################################
resource "google_compute_instance" "k8s_worker" {
count = var.gcp_nodes
name = "gcp-k8s-worker-${count.index}"
machine_type = var.gcp_machine_type
zone = var.gcp_zone
boot_disk {
initialize_params {
image = "debian-cloud/debian-9"
}
}
network_interface {
network = "default"
access_config {}
}
# Created after and destroyed before master node
depends_on = [
aws_instance.k8s_master,
]
provisioner "local-exec" {
when = destroy
command = "ansible-playbook ansible/destroy-k8s-worker.yaml -i ansible-inventory -e 'node=${self.name}'"
}
}
########################################
######## Create virtual machine ########
########################################
resource "azurerm_linux_virtual_machine" "k8s_worker" {
count = var.azure_nodes
name = "azure-k8s-worker-${count.index}"
resource_group_name = azurerm_resource_group.k8s.name
location = azurerm_resource_group.k8s.location
size = var.azure_vm_size
admin_username = "adminuser"
network_interface_ids = [
azurerm_network_interface.k8s[count.index].id,
]
admin_ssh_key {
username = "adminuser"
public_key = file(var.public_key_path)
}
os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}
source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "16.04-LTS"
version = "latest"
}
# Created after and destroyed before master node
depends_on = [
aws_instance.k8s_master,
]
provisioner "local-exec" {
when = destroy
command = "ansible-playbook ansible/destroy-k8s-worker.yaml -i ansible-inventory -e 'node=${self.name}'"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment