diff --git a/aws/jumpserver/.terraform.lock.hcl b/aws/jumpserver/.terraform.lock.hcl new file mode 100644 index 0000000..a88891d --- /dev/null +++ b/aws/jumpserver/.terraform.lock.hcl @@ -0,0 +1,24 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/hashicorp/aws" { + version = "5.48.0" + hashes = [ + "h1:sdlIu2yp0/x9nyEgJcKOYDE95hMd6SFvcO8aWzJFAqE=", + "zh:0876d94be46be905d1f6c149461979cd6e9bec80d5ffad43fd6267fe7c3a924d", + "zh:3a853f887e6f61c2ba383c46e71bcec97ecd31d25a78dab08958f43bbbaecb86", + "zh:43235595e26dd131f00704b5b64a65c4e7c4984a559b30d4272170e1b78e99b7", + "zh:6866f7535ec2ef8fe6ed16eecee2e31418a2bd86cec73e1d18e47bd3bb87f68e", + "zh:756a4ed97f30ea6e8871c16446b24ce55601143a715e067b7f9ebdae8349da34", + "zh:793e8414962934be9805186874f207ca1dc8d162b6665e4938893ad827a545c6", + "zh:79b2f886507f21ff1b752ff140ed95ed551f389abf0c8177c7b5f5bbbd95da8e", + "zh:8653b1bc6f7e62404e02f940d962d0c2ba0c4dd4c28bd595945454cf348c2697", + "zh:9b12af85486a96aedd8d7984b0ff811a4b42e3d88dad1a3fb4c0b580d04fa425", + "zh:9bd612f013c075685c129e5d0ec9243572cd51359599b7218459babe0e9b6ac7", + "zh:aabafe758ee8392f56d2c894017203de4dae38c1e3e0d274c54e194b9b8fccff", + "zh:aded7d6034115ca512b79ca17da65ebb8906e3b8def78dcbc0640142b0c05ca0", + "zh:ce383ee19b37666aba60db6b01cbe7a1fcbb40c6dd54d0cb36b2ba114ee5ae62", + "zh:ceaf1b998b9ced4b63f35da386358e4c7ad6def582438987c91bceffefb9e258", + "zh:d79225a9ae6a7391c33aa2d794bf9b167db66398c4f054f94d557615b051a40d", + ] +} diff --git a/aws/jumpserver/main.tf b/aws/jumpserver/main.tf new file mode 100644 index 0000000..c2b3787 --- /dev/null +++ b/aws/jumpserver/main.tf @@ -0,0 +1,81 @@ +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + } + } +} +provider "aws" { + region = var.region + access_key = var.AWS_ACCESS_KEY + secret_key = var.AWS_SECRET_KEY +} + +resource "aws_vpc" "jumpservervpc" { + cidr_block = "10.0.0.0/16" +} +# Create a public subnet +resource "aws_subnet" "public_subnet" { + vpc_id = aws_vpc.jumpservervpc.id + cidr_block = "10.0.1.0/24" + map_public_ip_on_launch = true +} + +# Create an internet gateway +resource "aws_internet_gateway" "jumpserver_igw" { + vpc_id = aws_vpc.jumpservervpc.id +} + +resource "aws_route_table" "jumpserverrt" { + vpc_id = aws_vpc.jumpservervpc.id + + route { + cidr_block = "0.0.0.0/0" + gateway_id = aws_internet_gateway.jumpserver_igw.id + } + + tags = { + Name = "jumpserverrt" + } +} + +# Associate the route table with the public subnet +resource "aws_route_table_association" "public_subnet_association" { + subnet_id = aws_subnet.public_subnet.id + route_table_id = aws_route_table.jumpserverrt.id +} + +# Create a security group allowing SSH only from your IP +resource "aws_security_group" "ssh_sg" { + vpc_id = aws_vpc.jumpservervpc.id + + ingress { + from_port = 22 + to_port = 22 + protocol = "tcp" + cidr_blocks = ["${var.localserverip}/32"] # Replace your_ip with your actual IP address + } +} + +# Create an EC2 instance +resource "aws_instance" "my_instance" { + ami = "ami-0a283ac1aafe112d5" # Replace with your AMI ID + instance_type = "t2.micro" + subnet_id = aws_subnet.public_subnet.id + key_name = aws_key_pair.jumpserverkey.key_name + security_groups = [aws_security_group.ssh_sg.name] + + tags = { + Name = "talosJumpserver" + } +} + +# Generate SSH key pair +resource "aws_key_pair" "jumpserverkey" { + key_name = "jumpserverkey" + public_key = file("~/.ssh/id_rsa.pub") +} + +output "ssh_key" { + value = aws_key_pair.jumpserverkey.key_name +} \ No newline at end of file diff --git a/aws/jumpserver/vars.tf b/aws/jumpserver/vars.tf new file mode 100644 index 0000000..44c0dac --- /dev/null +++ b/aws/jumpserver/vars.tf @@ -0,0 +1,15 @@ +variable "AWS_ACCESS_KEY" { + description = "AWS Access key" +} + +variable "AWS_SECRET_KEY" { + description = "AWS Secret key" +} + +variable "region" { + description = "AWS Region to deploy the resources" +} + +variable "localserverip" { + description = "AWS Region to deploy the resources" +} \ No newline at end of file diff --git a/aws/talos/main.tf b/aws/talos/main.tf index 91d5ac7..7b53d4b 100755 --- a/aws/talos/main.tf +++ b/aws/talos/main.tf @@ -125,68 +125,126 @@ data "local_file" "workerfile" { depends_on = [ null_resource.createtalosconfig ] } -resource "aws_instance" talos_master_instance { +resource "aws_placement_group" "talosplacemnentgroup" { + name = "talosplacemnentgroup" + strategy = "cluster" +} - count = var.mastercount +resource "aws_launch_configuration" "talosmaster" { + name = "talos-master" + image_id = data.aws_ami.talos.id + instance_type = var.instance_type +} - ami = data.aws_ami.talos.id - instance_type = var.instance_type - monitoring = var.nodemonitoringenabled - vpc_security_group_ids = [ module.security_group.security_group_id ] - subnet_id = "${element(module.vpc.private_subnets, 0)}" +resource "aws_lb" "talosapi" { + name = "talosapi" + internal = false + load_balancer_type = "network" + subnets = [for subnet in aws_subnet.public : subnet.id] - user_data = data.local_file.controllerfile.content - associate_public_ip_address = true + enable_deletion_protection = true - root_block_device { - volume_size = 200 - } +} - depends_on = [ data.local_file.controllerfile ] +resource "aws_autoscaling_group" "talosmaster-static" { + name = "talosmaster-static" + max_size = 20 + min_size = var.mastercount + health_check_grace_period = 300 + health_check_type = "ELB" + desired_capacity = 4 + force_delete = true + placement_group = aws_placement_group.talosplacemnentgroup.id + launch_configuration = aws_launch_configuration.talosmaster.name + load_balancers = aws_lb.talosapi.arn + + + timeouts { + delete = "15m" + } - tags = { - Name = "talosmaster" - } +} +resource "aws_autoscaling_group" "talosmaster-scalable" { + name = "talosmaster-scalable" + max_size = 20 + min_size = var.mastercount + health_check_grace_period = 300 + health_check_type = "ELB" + desired_capacity = 4 + force_delete = true + placement_group = aws_placement_group.talosplacemnentgroup.id + launch_configuration = aws_launch_configuration.talosmaster.name + + + timeouts { + delete = "15m" + } } -resource "aws_instance" talos_worker_instance { + +# resource "aws_instance" talos_master_instance { + +# count = var.mastercount + +# ami = data.aws_ami.talos.id +# instance_type = var.instance_type +# monitoring = var.nodemonitoringenabled +# vpc_security_group_ids = [ module.security_group.security_group_id ] +# subnet_id = "${element(module.vpc.private_subnets, 0)}" + +# user_data = data.local_file.controllerfile.content +# associate_public_ip_address = true + +# root_block_device { +# volume_size = 200 +# } + +# depends_on = [ data.local_file.controllerfile ] + +# tags = { +# Name = "talosmaster" +# } + + +# } +# resource "aws_instance" talos_worker_instance { - count = var.workercount +# count = var.workercount - ami = data.aws_ami.talos.id - instance_type = var.instance_type - monitoring = var.nodemonitoringenabled - vpc_security_group_ids = [ module.security_group.security_group_id ] - subnet_id = "${element(module.vpc.private_subnets, 0)}" +# ami = data.aws_ami.talos.id +# instance_type = var.instance_type +# monitoring = var.nodemonitoringenabled +# vpc_security_group_ids = [ module.security_group.security_group_id ] +# subnet_id = "${element(module.vpc.private_subnets, 0)}" - user_data = data.local_file.workerfile.content - associate_public_ip_address = true +# user_data = data.local_file.workerfile.content +# associate_public_ip_address = true - depends_on = [ data.local_file.workerfile ] +# depends_on = [ data.local_file.workerfile ] - root_block_device { - volume_size = 200 - } - tags = { - Name = "talosworker" - } +# root_block_device { +# volume_size = 200 +# } +# tags = { +# Name = "talosworker" +# } -} +# } -resource "aws_ebs_volume" "ebs_volume" { - count = "${var.workercount}" - availability_zone = "${element(aws_instance.talos_master_instance.*.availability_zone, count.index)}" - size = "200" -} +# resource "aws_ebs_volume" "ebs_volume" { +# count = "${var.workercount}" +# availability_zone = "${element(aws_instance.talos_master_instance.*.availability_zone, count.index)}" +# size = "200" +# } -resource "aws_volume_attachment" "volume_attachement" { - count = "${var.workercount}" - volume_id = "${aws_ebs_volume.ebs_volume.*.id[count.index]}" - device_name = "/dev/sdd" - instance_id = "${element(aws_instance.talos_worker_instance.*.id, count.index)}" -} +# resource "aws_volume_attachment" "volume_attachement" { +# count = "${var.workercount}" +# volume_id = "${aws_ebs_volume.ebs_volume.*.id[count.index]}" +# device_name = "/dev/sdd" +# instance_id = "${element(aws_instance.talos_worker_instance.*.id, count.index)}" +# } resource "aws_lb_target_group" "talos-tg" { name = var.talostg @@ -197,15 +255,24 @@ resource "aws_lb_target_group" "talos-tg" { } -resource "aws_lb_target_group" "traefik-tg-80" { - name = var.traefik_tg_80_name - port = var.traefikhttpport +resource "aws_lb_target_group" "talos-api" { + name = "talosapi" + port = 500000 protocol = "TCP" target_type = "ip" vpc_id = module.vpc.vpc_id } +# resource "aws_lb_target_group" "traefik-tg-80" { +# name = var.traefik_tg_80_name +# port = var.traefikhttpport +# protocol = "TCP" +# target_type = "ip" +# vpc_id = module.vpc.vpc_id + +# } + resource "aws_lb_target_group" "traefik-tg-443" { name = var.traefik_tg_443_name port = var.traefikhttpsport @@ -234,14 +301,22 @@ resource "aws_lb_target_group_attachment" "registertarget" { } -resource "aws_lb_target_group_attachment" "registertarget-traefik-80" { +resource "aws_lb_target_group_attachment" "talosapi" { - count = var.workercount - target_group_arn = aws_lb_target_group.traefik-tg-80.arn - target_id = "${element(split(",", join(",", aws_instance.talos_worker_instance.*.private_ip)), count.index)}" - depends_on = [ aws_instance.talos_worker_instance ] + count = var.mastercount + target_group_arn = aws_lb_target_group.talos-tg.arn + target_id = "${element(split(",", join(",", aws_instance.talos_master_instance.*.private_ip)), count.index)}" + depends_on = [ aws_instance.talos_master_instance ] } +# resource "aws_lb_target_group_attachment" "registertarget-traefik-80" { + +# count = var.workercount +# target_group_arn = aws_lb_target_group.traefik-tg-80.arn +# target_id = "${element(split(",", join(",", aws_instance.talos_worker_instance.*.private_ip)), count.index)}" +# depends_on = [ aws_instance.talos_worker_instance ] + +# } resource "aws_lb_target_group_attachment" "registertarget-traefik-443" { @@ -301,7 +376,7 @@ resource "aws_alb_listener" "traefik-listener-443" { } } - + resource "aws_alb_listener" "nats-listener-4222" { load_balancer_arn = aws_lb.traefik.arn port = 4222 @@ -313,16 +388,6 @@ resource "aws_alb_listener" "nats-listener-4222" { } -resource "aws_alb_listener" "traefik-listener-80" { - load_balancer_arn = aws_lb.traefik.arn - port = 80 - protocol = "TCP" - default_action { - type = "forward" - target_group_arn = aws_lb_target_group.traefik-tg-80.arn - } - -} resource "null_resource" "bootstrap_etcd" { provisioner "local-exec" { diff --git a/azure/jumpserver/main.tf b/azure/jumpserver/main.tf new file mode 100644 index 0000000..f437607 --- /dev/null +++ b/azure/jumpserver/main.tf @@ -0,0 +1,107 @@ +terraform { + required_providers { + azurerm = { + source = "hashicorp/azurerm" + version = ">=3.2.1" + } + } + +} + +provider "azurerm" { + features {} +} + +# Create a resource group +resource "azurerm_resource_group" "jumpserverrg" { + name = "jumpserverrg" + location = var.region +} + +# Create virtual network +resource "azurerm_virtual_network" "jumpservervnet" { + name = "jumpservervnet" + resource_group_name = azurerm_resource_group.jumpserverrg.name + location = azurerm_resource_group.jumpserverrg.location + address_space = ["10.0.0.0/16"] +} + +# Create subnet +resource "azurerm_subnet" "jumpserversubnet" { + name = "jumpserversubnet" + resource_group_name = azurerm_resource_group.jumpserverrg.name + virtual_network_name = azurerm_virtual_network.jumpservervnet.name + address_prefixes = ["10.0.1.0/24"] +} + +# Create public IP address +resource "azurerm_public_ip" "jumpserverpublicip" { + name = "jumpserverpublicip" + location = azurerm_resource_group.jumpserverrg.location + resource_group_name = azurerm_resource_group.jumpserverrg.name + allocation_method = "Static" +} + +# Create network security group +resource "azurerm_network_security_group" "jumpserversg" { + name = "jumpserversg" + location = azurerm_resource_group.jumpserverrg.location + resource_group_name = azurerm_resource_group.jumpserverrg.name + + security_rule { + name = "SSH" + priority = 1001 + direction = "Inbound" + access = "Allow" + protocol = "Tcp" + source_port_range = "*" + destination_port_range = "22" + source_address_prefix = "${var.localserverip}/32" # Replace with your actual IP + destination_address_prefix = "*" + } +} + +# Create network interface with public IP and attach NSG +resource "azurerm_network_interface" "jumpserver-nic" { + name = "jumpserver-nic" + location = azurerm_resource_group.jumpserverrg.location + resource_group_name = azurerm_resource_group.jumpserverrg.name + + ip_configuration { + name = "internal" + subnet_id = azurerm_subnet.jumpserversubnet.id + private_ip_address_allocation = "Dynamic" + public_ip_address_id = azurerm_public_ip.jumpserverpublicip.id + } + + + #network_security_group_id = azurerm_network_security_group.jumpserversg.id +} + +# Create virtual machine +resource "azurerm_linux_virtual_machine" "jumpserver" { + name = "talos-jumpserver" + location = azurerm_resource_group.jumpserverrg.location + resource_group_name = azurerm_resource_group.jumpserverrg.name + network_interface_ids = [azurerm_network_interface.jumpserver-nic.id] + size = "Standard_DS1_v2" + admin_username = "adminuser" + disable_password_authentication = true + + admin_ssh_key { + username = "adminuser" + public_key = file("~/.ssh/id_rsa.pub") # Path to your SSH public key + } + + os_disk { + caching = "ReadWrite" + storage_account_type = "Premium_LRS" + } + + source_image_reference { + publisher = "Canonical" + offer = "UbuntuServer" + sku = "16.04-LTS" + version = "latest" + } +} diff --git a/azure/jumpserver/vars.tf b/azure/jumpserver/vars.tf new file mode 100644 index 0000000..8c26a70 --- /dev/null +++ b/azure/jumpserver/vars.tf @@ -0,0 +1,7 @@ +variable "region" { + description = "Azure Region to deploy the resources" +} + +variable "localserverip" { + description = "Localip of your laptop to allow ssh" +} \ No newline at end of file diff --git a/azure/talos/main.tf b/azure/talos/main.tf index 412318d..917e3b8 100644 --- a/azure/talos/main.tf +++ b/azure/talos/main.tf @@ -2,9 +2,11 @@ terraform { required_providers { azurerm = { source = "hashicorp/azurerm" - version = "=3.0.0" + version = ">=3.2.1" } } + backend "azurerm" {} + } provider "azurerm" { @@ -199,15 +201,15 @@ resource "azurerm_network_security_rule" "nats" { } -resource "azurerm_public_ip" "talos-public-ip" { - count = length(var.publicipname) - name = "${var.talos_cluster_name}-publicip-${count.index}" - resource_group_name = azurerm_resource_group.talosrg.name - allocation_method = "Static" - location = var.region - sku = "Standard" +# resource "azurerm_public_ip" "talos-public-ip" { +# count = length(var.publicipname) +# name = "${var.talos_cluster_name}-publicip-${count.index}" +# resource_group_name = azurerm_resource_group.talosrg.name +# allocation_method = "Static" +# location = var.region +# sku = "Standard" -} +# } resource "azurerm_public_ip" "talos-public-ip-lb" { name = "${var.talos_cluster_name}-talos-public-ip-lb" @@ -274,21 +276,21 @@ resource "azurerm_lb_backend_address_pool" "traefikbe" { depends_on = [ azurerm_lb.traefiklb ] } -resource "azurerm_network_interface_backend_address_pool_association" "lbbackendassociation" { - count = length(var.nics) - network_interface_id = element( azurerm_network_interface.nics[*].id, count.index % (length(var.nics) + 1) ) - ip_configuration_name = "${var.talos_cluster_name}-config-${count.index}" - backend_address_pool_id = azurerm_lb_backend_address_pool.talosbe.id +# resource "azurerm_network_interface_backend_address_pool_association" "lbbackendassociation" { +# count = length(var.nics) +# network_interface_id = element( azurerm_network_interface.nics[*].id, count.index % (length(var.nics) + 1) ) +# ip_configuration_name = "${var.talos_cluster_name}-config-${count.index}" +# backend_address_pool_id = azurerm_lb_backend_address_pool.talosbe.id -} +# } -resource "azurerm_network_interface_backend_address_pool_association" "traefikbeassociation" { - count = length(var.workernics) - network_interface_id = element( azurerm_network_interface.workernics[*].id, count.index % (length(var.workernics) + 1) ) - ip_configuration_name = "${var.talos_cluster_name}-workerconfig-${count.index}" - backend_address_pool_id = azurerm_lb_backend_address_pool.traefikbe.id +# resource "azurerm_network_interface_backend_address_pool_association" "traefikbeassociation" { +# count = length(var.workernics) +# network_interface_id = element( azurerm_network_interface.workernics[*].id, count.index % (length(var.workernics) + 1) ) +# ip_configuration_name = "${var.talos_cluster_name}-workerconfig-${count.index}" +# backend_address_pool_id = azurerm_lb_backend_address_pool.traefikbe. -} +# } resource "azurerm_lb_probe" "talos-lb-health" { loadbalancer_id = azurerm_lb.taloslb.id @@ -297,6 +299,13 @@ resource "azurerm_lb_probe" "talos-lb-health" { protocol = "Tcp" } +resource "azurerm_lb_probe" "talos-api-lb-health" { + loadbalancer_id = azurerm_lb.taloslb.id + name = "${var.talos_cluster_name}-talos-api-lb-health" + port = 50000 + protocol = "Tcp" +} + resource "azurerm_lb_probe" "traefik-443-health" { loadbalancer_id = azurerm_lb.traefiklb.id name = "${var.talos_cluster_name}-traefik-443-health" @@ -304,12 +313,12 @@ resource "azurerm_lb_probe" "traefik-443-health" { protocol = "Tcp" } -resource "azurerm_lb_probe" "traefik-80-health" { - loadbalancer_id = azurerm_lb.traefiklb.id - name = "${var.talos_cluster_name}-traefik-80-health" - port = var.traefikhttpport - protocol = "Tcp" -} +# resource "azurerm_lb_probe" "traefik-80-health" { +# loadbalancer_id = azurerm_lb.traefiklb.id +# name = "${var.talos_cluster_name}-traefik-80-health" +# port = var.traefikhttpport +# protocol = "Tcp" +# } resource "azurerm_lb_probe" "nats-4222-health" { loadbalancer_id = azurerm_lb.traefiklb.id @@ -329,6 +338,17 @@ resource "azurerm_lb_rule" "talos-6443" { probe_id = azurerm_lb_probe.talos-lb-health.id } +resource "azurerm_lb_rule" "talos-50000" { + loadbalancer_id = azurerm_lb.taloslb.id + name = "${var.talos_cluster_name}-talos-50000" + protocol = "Tcp" + frontend_port = 50000 + backend_port = 50000 + frontend_ip_configuration_name = "${var.talos_cluster_name}-talosfe" + backend_address_pool_ids = [ azurerm_lb_backend_address_pool.talosbe.id ] + probe_id = azurerm_lb_probe.talos-api-lb-health.id +} + resource "azurerm_lb_rule" "traefik-443" { loadbalancer_id = azurerm_lb.traefiklb.id name = "${var.talos_cluster_name}-traefik-443" @@ -342,18 +362,18 @@ resource "azurerm_lb_rule" "traefik-443" { probe_id = azurerm_lb_probe.traefik-443-health.id } -resource "azurerm_lb_rule" "traefik-80" { - loadbalancer_id = azurerm_lb.traefiklb.id - name = "${var.talos_cluster_name}-traefik-80" - protocol = "Tcp" - frontend_port = 80 - backend_port = var.traefikhttpport - load_distribution = "SourceIPProtocol" - frontend_ip_configuration_name = "${var.talos_cluster_name}-traefikfe" - backend_address_pool_ids = [ azurerm_lb_backend_address_pool.traefikbe.id ] +# resource "azurerm_lb_rule" "traefik-80" { +# loadbalancer_id = azurerm_lb.traefiklb.id +# name = "${var.talos_cluster_name}-traefik-80" +# protocol = "Tcp" +# frontend_port = 80 +# backend_port = var.traefikhttpport +# load_distribution = "SourceIPProtocol" +# frontend_ip_configuration_name = "${var.talos_cluster_name}-traefikfe" +# backend_address_pool_ids = [ azurerm_lb_backend_address_pool.traefikbe.id ] - probe_id = azurerm_lb_probe.traefik-80-health.id -} +# probe_id = azurerm_lb_probe.traefik-80-health.id +# } resource "azurerm_lb_rule" "nats-4222" { loadbalancer_id = azurerm_lb.traefiklb.id @@ -367,38 +387,38 @@ resource "azurerm_lb_rule" "nats-4222" { probe_id = azurerm_lb_probe.nats-4222-health.id } -resource "azurerm_network_interface" "nics" { - count = length(var.nics) - name = "${var.talos_cluster_name}-nic-${count.index}" - location = var.region - resource_group_name = azurerm_resource_group.talosrg.name +# resource "azurerm_network_interface" "nics" { +# count = length(var.nics) +# name = "${var.talos_cluster_name}-nic-${count.index}" +# location = var.region +# resource_group_name = azurerm_resource_group.talosrg.name - ip_configuration { - private_ip_address_allocation = "Dynamic" - name = "${var.talos_cluster_name}-config-${count.index}" - subnet_id = azurerm_subnet.talossubnet.id - private_ip_address = element(var.nics, count.index) - public_ip_address_id = element(azurerm_public_ip.talos-public-ip[*].id, count.index % ( length(var.nics) + 1 ) ) +# ip_configuration { +# private_ip_address_allocation = "Dynamic" +# name = "${var.talos_cluster_name}-config-${count.index}" +# subnet_id = azurerm_subnet.talossubnet.id +# private_ip_address = element(var.nics, count.index) +# public_ip_address_id = element(azurerm_public_ip.talos-public-ip[*].id, count.index % ( length(var.nics) + 1 ) ) - } -} +# } +# } -resource "azurerm_network_interface" "workernics" { - count = length(var.workernics) - name = "${var.talos_cluster_name}-workernic-${count.index}" - location = var.region - resource_group_name = azurerm_resource_group.talosrg.name +# resource "azurerm_network_interface" "workernics" { +# count = length(var.workernics) +# name = "${var.talos_cluster_name}-workernic-${count.index}" +# location = var.region +# resource_group_name = azurerm_resource_group.talosrg.name - ip_configuration { - private_ip_address_allocation = "Dynamic" - name = "${var.talos_cluster_name}-workerconfig-${count.index}" - subnet_id = azurerm_subnet.talossubnet.id - private_ip_address = element(var.nics, count.index) +# ip_configuration { +# private_ip_address_allocation = "Dynamic" +# name = "${var.talos_cluster_name}-workerconfig-${count.index}" +# subnet_id = azurerm_subnet.talossubnet.id +# private_ip_address = element(var.nics, count.index) - } -} +# } +# } resource "azurerm_availability_set" "talosas" { name = "${var.talos_cluster_name}-talosas" @@ -408,17 +428,17 @@ resource "azurerm_availability_set" "talosas" { } -resource "azurerm_network_interface_security_group_association" "networkinterface_sg_association" { - count = length(var.nics) - network_interface_id = element( azurerm_network_interface.nics[*].id, count.index % (length(var.nics) + 1) ) - network_security_group_id = azurerm_network_security_group.talossg.id -} +# resource "azurerm_network_interface_security_group_association" "networkinterface_sg_association" { +# count = length(var.nics) +# network_interface_id = element( azurerm_network_interface.nics[*].id, count.index % (length(var.nics) + 1) ) +# network_security_group_id = azurerm_network_security_group.talossg.id +# } -resource "azurerm_network_interface_security_group_association" "networkinterface_worker_sg_association" { - count = length(var.workernics) - network_interface_id = element( azurerm_network_interface.workernics[*].id, count.index % (length(var.workernics)+ 1) ) - network_security_group_id = azurerm_network_security_group.talossg.id -} +# resource "azurerm_network_interface_security_group_association" "networkinterface_worker_sg_association" { +# count = length(var.workernics) +# network_interface_id = element( azurerm_network_interface.workernics[*].id, count.index % (length(var.workernics)+ 1) ) +# network_security_group_id = azurerm_network_security_group.talossg.id +# } resource "azurerm_nat_gateway" "talosnat" { name = "${var.talos_cluster_name}-worker-node-nat" @@ -457,119 +477,220 @@ data "local_file" "workerfile" { depends_on = [ null_resource.createtalosconfig ] } -resource "azurerm_virtual_machine" "talosmaster" { - count = length(var.mastercount) - name = "${var.talos_cluster_name}-talosmaster-${count.index}" - resource_group_name = azurerm_resource_group.talosrg.name - location = var.region - vm_size = var.instancetype - boot_diagnostics { - enabled = true - storage_uri = "https://${azurerm_storage_account.talosimagesa.name}.blob.core.windows.net" - } - os_profile { - computer_name = "${var.talos_cluster_name}-talosmaster-${count.index}" - admin_username = "talos" - admin_password = "Talos@1234" - custom_data = data.local_file.controllerfile.content - } +resource "azurerm_orchestrated_virtual_machine_scale_set" "talosmaster-scalable" { + name = var.masterscalesetname + location = var.region + resource_group_name = azurerm_resource_group.talosrg.name + instances = 1 + platform_fault_domain_count = 1 + sku_name = var.instancetype -# storage_data_disk { -# name = "talosstoragedata" -# disk_size_gb = "100" -# lun = "1" -# create_option = "Empty" -# } - storage_image_reference { - id = "/subscriptions/7bccafd3-c548-4b45-837d-fb7dc81167b6/resourceGroups/talos-image/providers/Microsoft.Compute/images/talos" - } - storage_os_disk { - name = "${var.talos_cluster_name}-talosmaster-${count.index}" - caching = "ReadWrite" - create_option = "FromImage" - managed_disk_type = "StandardSSD_LRS" - disk_size_gb = "100" + os_profile { + custom_data = data.local_file.controllerfile.content_base64 + linux_configuration { + admin_username = "talos" + admin_password = "Talos@123" + disable_password_authentication = false } - - os_profile_linux_config { - disable_password_authentication = false + } + network_interface { + name = "${var.masterscalesetname}-network-interface" + primary = true + network_security_group_id = azurerm_network_security_group.talossg.id + + ip_configuration { + name = "${var.masterscalesetname}-ip-master" + primary = true + subnet_id = azurerm_subnet.talossubnet.id + public_ip_address { + name = "t${var.masterscalesetname}-public-ip-master" + } } + } + + os_disk { + caching = "ReadWrite" + storage_account_type = "StandardSSD_LRS" + } - network_interface_ids = [ element( azurerm_network_interface.nics[*].id, count.index ) ] - availability_set_id = azurerm_availability_set.talosas.id - delete_os_disk_on_termination = true - - depends_on = [ azurerm_availability_set.talosas ] - + data_disk { + lun = 1 + caching = "ReadWrite" + create_option = "Empty" + disk_size_gb = 100 + storage_account_type = "StandardSSD_LRS" + } + boot_diagnostics { + storage_account_uri = "https://${azurerm_storage_account.talosimagesa.name}.blob.core.windows.net" + } - + source_image_id = "/subscriptions/7bccafd3-c548-4b45-837d-fb7dc81167b6/resourceGroups/talos-image/providers/Microsoft.Compute/images/talos" } -resource "azurerm_virtual_machine" "talosworker" { - count = length(var.workercount) - name = "${var.talos_cluster_name}-talosworker-${count.index}" - resource_group_name = azurerm_resource_group.talosrg.name - location = var.region - vm_size = var.instancetype - boot_diagnostics { - enabled = true - storage_uri = "https://${azurerm_storage_account.talosimagesa.name}.blob.core.windows.net" +resource "azurerm_orchestrated_virtual_machine_scale_set" "talosmaster-static" { + name = var.masterstaticname + location = var.region + resource_group_name = azurerm_resource_group.talosrg.name + instances = var.staticmasternodecount + platform_fault_domain_count = 1 + sku_name = var.instancetype + + network_interface { + name = "${var.masterstaticname}-network-Interface" + primary = true + network_security_group_id = azurerm_network_security_group.talossg.id + + ip_configuration { + name = "${var.masterstaticname}-ip-conf" + primary = true + subnet_id = azurerm_subnet.talossubnet.id + public_ip_address { + name = "${var.masterstaticname}-public-ip" + } + load_balancer_backend_address_pool_ids = [ "/subscriptions/${var.subscription_id}/resourceGroups/${var.talosrgname}/providers/Microsoft.Network/loadBalancers/${azurerm_lb.taloslb.name}/backendAddressPools/${azurerm_lb_backend_address_pool.talosbe.name}" ] } - - os_profile { - computer_name = "${var.talos_cluster_name}-talosworker-${count.index}" + } + + os_profile { + custom_data = data.local_file.controllerfile.content_base64 + linux_configuration { admin_username = "talos" - admin_password = "Talos@1234" - custom_data = data.local_file.workerfile.content + admin_password = "Talos@123" + disable_password_authentication = false } + } + os_disk { + caching = "ReadWrite" + storage_account_type = "StandardSSD_LRS" + } + + data_disk { + lun = 1 + caching = "ReadWrite" + create_option = "Empty" + disk_size_gb = 100 + storage_account_type = "StandardSSD_LRS" + } + boot_diagnostics { + storage_account_uri = "https://${azurerm_storage_account.talosimagesa.name}.blob.core.windows.net" + } + + source_image_id = "/subscriptions/7bccafd3-c548-4b45-837d-fb7dc81167b6/resourceGroups/talos-image/providers/Microsoft.Compute/images/talos" +} - storage_data_disk { - name = "datadisk-talosworker${count.index}" - disk_size_gb = "200" - lun = "1" - create_option = "Empty" - managed_disk_type = "StandardSSD_LRS" +resource "azurerm_orchestrated_virtual_machine_scale_set" "talosworker-static" { + name = var.workerstaticname + location = var.region + resource_group_name = azurerm_resource_group.talosrg.name + instances = var.staticworkernodecount + platform_fault_domain_count = 1 + sku_name = var.instancetype + + network_interface { + name = "${var.workerstaticname}-network-interface" + primary = true + network_security_group_id = azurerm_network_security_group.talossg.id + + ip_configuration { + name = "${var.workerstaticname}-ip-conf" + primary = true + subnet_id = azurerm_subnet.talossubnet.id } - storage_image_reference { - id = "/subscriptions/7bccafd3-c548-4b45-837d-fb7dc81167b6/resourceGroups/talos-image/providers/Microsoft.Compute/images/talos" + } + + os_profile { + custom_data = data.local_file.workerfile.content_base64 + linux_configuration { + admin_username = "talos" + admin_password = "Talos@123" + disable_password_authentication = false } + } + os_disk { + caching = "ReadWrite" + storage_account_type = "StandardSSD_LRS" + } - storage_os_disk { - name = "${var.talos_cluster_name}-talosworker-${count.index}" - caching = "ReadWrite" - create_option = "FromImage" - managed_disk_type = "StandardSSD_LRS" - disk_size_gb = "100" - } - availability_set_id = azurerm_availability_set.talosas.id - network_interface_ids = [ element(azurerm_network_interface.workernics[*].id, count.index ) ] + data_disk { + lun = 1 + caching = "ReadWrite" + create_option = "Empty" + disk_size_gb = 100 + storage_account_type = "StandardSSD_LRS" + } + boot_diagnostics { + storage_account_uri = "https://${azurerm_storage_account.talosimagesa.name}.blob.core.windows.net" + } + + source_image_id = "/subscriptions/7bccafd3-c548-4b45-837d-fb7dc81167b6/resourceGroups/talos-image/providers/Microsoft.Compute/images/talos" +} - os_profile_linux_config { - disable_password_authentication = false +resource "azurerm_orchestrated_virtual_machine_scale_set" "talosworker-scalable" { + name = var.wokerscalesetname + location = var.region + resource_group_name = azurerm_resource_group.talosrg.name + instances = 1 + platform_fault_domain_count = 1 + sku_name = var.instancetype + + network_interface { + name = "${var.wokerscalesetname}-network-interface" + primary = true + network_security_group_id = azurerm_network_security_group.talossg.id + + ip_configuration { + name = "${var.wokerscalesetname}-ip-conf" + primary = true + subnet_id = azurerm_subnet.talossubnet.id + load_balancer_backend_address_pool_ids = [ "/subscriptions/${var.subscription_id}/resourceGroups/${var.talosrgname}/providers/Microsoft.Network/loadBalancers/${azurerm_lb.traefiklb.name}/backendAddressPools/${azurerm_lb_backend_address_pool.traefikbe.name}" ] } - delete_os_disk_on_termination = true - - depends_on = [ azurerm_availability_set.talosas ] + } + os_profile { + custom_data = data.local_file.workerfile.content_base64 + linux_configuration { + admin_username = "talos" + admin_password = "Talos@123" + disable_password_authentication = false + } + } + os_disk { + caching = "ReadWrite" + storage_account_type = "StandardSSD_LRS" + } + + data_disk { + lun = 1 + caching = "ReadWrite" + create_option = "Empty" + disk_size_gb = 100 + storage_account_type = "StandardSSD_LRS" + } + boot_diagnostics { + storage_account_uri = "https://${azurerm_storage_account.talosimagesa.name}.blob.core.windows.net" + } + + source_image_id = "/subscriptions/7bccafd3-c548-4b45-837d-fb7dc81167b6/resourceGroups/talos-image/providers/Microsoft.Compute/images/talos" } + resource "null_resource" "bootstrap_etcd" { provisioner "local-exec" { - command = "/bin/bash scripts/bootstrapetcd.sh ${azurerm_public_ip.talos-public-ip[0].ip_address} ${var.talosctlfolderpath}" + command = "/bin/bash scripts/bootstrapetcd.sh ${azurerm_public_ip.talos-public-ip-lb.ip_address} ${var.talosctlfolderpath}" } provisioner "local-exec" { - command = "${var.talosctlfolderpath}/talosctl --talosconfig scripts/talosconfig kubeconfig ${var.configfolderpath} --nodes ${azurerm_public_ip.talos-public-ip[0].ip_address}" + command = "${var.talosctlfolderpath}/talosctl --talosconfig scripts/talosconfig kubeconfig ${var.configfolderpath} --nodes ${azurerm_public_ip.talos-public-ip-lb.ip_address}" } provisioner "local-exec" { command = "echo 'LoadBalancerHost: \"${azurerm_public_ip.talos-public-ip-traefik.ip_address}\"' > ${var.configfolderpath}/capten-lb-endpoint.yaml" } - depends_on = [ azurerm_virtual_machine.talosmaster ] - + depends_on = [ azurerm_orchestrated_virtual_machine_scale_set.talosmaster-static ] + } diff --git a/azure/talos/scripts/talosconfiggen.sh b/azure/talos/scripts/talosconfiggen.sh index f87c01a..30c9cfe 100644 --- a/azure/talos/scripts/talosconfiggen.sh +++ b/azure/talos/scripts/talosconfiggen.sh @@ -46,7 +46,7 @@ echo ${dnsname} echo ${port} echo ${talosctlpath} -${talosctlpath}/talosctl gen config talosconfig-userdata https://${dnsname}:${port} --with-examples=false --with-docs=false --output-dir scripts/ --config-patch @scripts/patch.yaml --force +${talosctlpath}/talosctl gen config talosconfig-userdata https://${dnsname}:${port} --with-examples=false --with-docs=false --output-dir scripts/ --config-patch @scripts/patch.yaml --config-patch '[{"op": "add", "path": "/machine/certSANs", "value": ['${dnsname}']}]' --force ${talosctlpath}/talosctl validate --config scripts/controlplane.yaml --mode cloud if [ $? -eq 1 ] then diff --git a/azure/talos/vars.tf b/azure/talos/vars.tf index 3c908b2..03fa499 100644 --- a/azure/talos/vars.tf +++ b/azure/talos/vars.tf @@ -2,12 +2,12 @@ variable "region" { description = "Azure Region to deploy the resources" } -variable "mastercount" { +variable "staticmasternodecount" { description = "talos master node count" default = 3 } -variable "workercount" { +variable "staticworkernodecount" { description = "talos worker node count" default = 3 } @@ -69,3 +69,36 @@ variable "talos_imagecont_name" { variable "talos_cluster_name" { description = "talosvnet name" } + +variable "wokerscalesetname" { + description = "talosworkerscaleset name" +} + +variable "masterstaticname" { + description = "talos master static name" +} + +variable "workerstaticname" { + description = "talos worker static name" +} + +variable "masterscalesetname" { + description = "talos master scaleset name" +} + +variable "subscription_id" { + description = "subscription id" +} + +variable "tfstatergname" { + description = "resourcegroup where tfstate to be stored" +} + +variable "tfstatesaname" { + description = "storage account name of tfstate" + +} + +variable "tfstatecontname" { + description = "tfstate storage account container name" +} \ No newline at end of file