← azuredeploy

Configure AKS cluster DNS prefix and resolve related issues

e3bca565867bbcb2b305fc487b523eb83a5af25a · ovidiucs · Apr 3, 2024 22:05

- Define `prefix` variable in `variables.tf` with a default value that meets DNS prefix criteria
- Define `cluster_log_analytics_workspace_name` with a default value for
  the log analytics workspace.
- Provide option to override `prefix` and
  `cluster_log_analytics_workspace_name` variable in `terraform.tfvars`
- Pass `prefix` variable to `aks` module in `main.tf` for `dns_prefix` argument
- Disabled rbac_aad for now until Entra is configured.
- Create a resource group before AKS cluster and made AKS be dependet on
  the resource group using depends_on. RG defines the location of the
  cluster.
- Added an SSH public key for the system nodes

+80 −95 · 5 files changed

  • terraform/main.tf +29 −11
  • terraform/modules/terraforn-azurerm-aks/main.tf +1 −1
  • terraform/terraform.tfvars +10 −6
  • terraform/variables.tf +40 −1
  • terraform/vm/vm.tf +0 −76
diff --git a/terraform/main.tf b/terraform/main.tf
index f1e45d7..1ea0c2a 100644
--- a/terraform/main.tf
+++ b/terraform/main.tf
@@ -2,18 +2,36 @@ provider "azurerm" {
   features {}
 }
 
+resource "azurerm_resource_group" "az-rg-lbl" {
+  name     = var.resource_group_name
+  location = var.location
+}
+
 module "aks" {
-  source                    = "./modules/terraform-azurerm-aks"
-  resource_group_name       = var.resource_group_name
-  kubernetes_cluster_name   = var.cluster_name
-  kubernetes_version        = var.kubernetes_version
-  agents_count              = var.node_count
-  agents_max_count          = var.agents_max_count
-  agents_min_count          = var.agents_min_count
-  agents_size               = var.vm_size
-  client_id                 = var.client_id
-  client_secret             = var.client_secret
-  # Add any other required variables from the module
+  source  = "github.com/Azure/terraform-azurerm-aks"
+
+  resource_group_name = azurerm_resource_group.az-rg-lbl.name
+  location            = azurerm_resource_group.az-rg-lbl.location
+  cluster_name        = var.cluster_name
+  kubernetes_version  = var.kubernetes_version
+  cluster_log_analytics_workspace_name = var.cluster_log_analytics_workspace_name
+  prefix          = var.dns_prefix
+  rbac_aad = false
+  
+  node_pools = {
+    node_pool_a = {
+      name                = var.default_node_pool_name
+      node_count          = var.node_count
+      vm_size             = var.vm_size
+    }
+  }
+
+  admin_username = var.admin_username
+  public_ssh_key = file(var.ssh_public_key)
+
+  tags = var.tags
+
+  depends_on = [azurerm_resource_group.az-rg-lbl]
 }
 
 module "vm" {
diff --git a/terraform/modules/terraforn-azurerm-aks/main.tf b/terraform/modules/terraforn-azurerm-aks/main.tf
index 3f24e44..738e939 100644
--- a/terraform/modules/terraforn-azurerm-aks/main.tf
+++ b/terraform/modules/terraforn-azurerm-aks/main.tf
@@ -4,7 +4,7 @@ provider "azurerm" {
 }
 
 module "aks" {
-  source  = "github.com/Azure/terraform-azurerm-aks?ref=main"
+  source  = "https://github.com/Azure/terraform-azurerm-aks?ref=main"
   version = "1.0.0" # Use the appropriate version
 
   resource_group_name = var.resource_group_name
diff --git a/terraform/terraform.tfvars b/terraform/terraform.tfvars
index fa186da..a9a367b 100644
--- a/terraform/terraform.tfvars
+++ b/terraform/terraform.tfvars
@@ -1,9 +1,13 @@
-resource_group_name = "your-resource-group-name"
-cluster_name        = "your-aks-cluster-name"
-kubernetes_version  = "1.24.6"
+resource_group_name = "AZ-AKS-RG"
+cluster_name        = "AZ-AKS001"
+kubernetes_version  = "1.29.0"
 node_count          = 3
 agents_max_count    = 5
 agents_min_count    = 1
-vm_size             = "Standard_D2s_v3"
-client_id           = "your-service-principal-client-id"
-client_secret       = "your-service-principal-client-secret"x
+vm_size             = "Standard_B1s"
+client_id           = ""
+client_secret       = ""
+ssh_public_key      = "~/.ssh/azure_test1.pub"
+location            = "westeurope"
+cluster_log_analytics_workspace_name = "AZ-LOG-AN-AKS-A001"
+dns_prefix              = "aks"
diff --git a/terraform/variables.tf b/terraform/variables.tf
index d953537..f69de52 100644
--- a/terraform/variables.tf
+++ b/terraform/variables.tf
@@ -10,6 +10,17 @@ variable "name" {
   default     = "app-failover-and-traffic"
 }
 
+variable "admin_username" {
+  description = "The name of the project"
+  type        = string
+  default     = "admin"
+}
+
+variable "ssh_public_key" {
+  description = "The path to the SSH Key File"
+  type        = string
+}
+
 variable "project" {
   description = "The name of the project"
   type        = string
@@ -25,7 +36,7 @@ variable "client" {
 variable "owner" {
   description = "The owner of the project"
   type        = string
-  default     = "Custom Text"
+  default     = "Ovidiu"
 }
 
 variable "environment" {
@@ -79,3 +90,31 @@ variable "client_secret" {
   type        = string
 }
 
+variable "location" {
+  description = "The location where the resources will be created."
+  type        = string
+}
+
+variable "default_node_pool_name" {
+  description = "The name of the default node pool."
+  type        = string
+  default     = "nodepool1"
+}
+
+variable "tags" {
+  description = "A map of tags to add to all resources."
+  type        = map(string)
+  default     = {}
+}
+
+variable "cluster_log_analytics_workspace_name" {
+     description = "The name of the existing Log Analytics workspace to use for AKS monitoring."
+     type        = string
+     default     = null
+}
+
+variable "dns_prefix" {
+     description = "The prefix to use for naming resources."
+     type        = string
+     default     = null
+}
diff --git a/terraform/vm/vm.tf b/terraform/vm/vm.tf
deleted file mode 100644
index c983a17..0000000
--- a/terraform/vm/vm.tf
+++ /dev/null
@@ -1,76 +0,0 @@
-provider "azurerm" {
-  features {}
-}
-
-resource "azurerm_resource_group" "example" {
-  name     = "example-resources"
-  location = "East US"
-}
-
-resource "azurerm_virtual_network" "example" {
-  name                = "example-network"
-  address_space       = ["10.0.0.0/16"]
-  location            = azurerm_resource_group.example.location
-  resource_group_name = azurerm_resource_group.example.name
-}
-
-resource "azurerm_subnet" "example" {
-  name                 = "example-subnet"
-  resource_group_name  = azurerm_resource_group.example.name
-  virtual_network_name = azurerm_virtual_network.example.name
-  address_prefixes     = ["10.0.2.0/24"]
-}
-
-resource "azurerm_public_ip" "example" {
-  name                = "example-publicip"
-  location            = azurerm_resource_group.example.location
-  resource_group_name = azurerm_resource_group.example.name
-  allocation_method   = "Dynamic"
-}
-
-resource "azurerm_network_security_group" "example" {
-  name                = "example-nsg"
-  location            = azurerm_resource_group.example.location
-  resource_group_name = azurerm_resource_group.example.name
-}
-
-resource "azurerm_network_interface" "example" {
-  name                = "example-nic"
-  location            = azurerm_resource_group.example.location
-  resource_group_name = azurerm_resource_group.example.name
-
-  ip_configuration {
-    name                          = "internal"
-    subnet_id                     = azurerm_subnet.example.id
-    private_ip_address_allocation = "Dynamic"
-    public_ip_address_id          = azurerm_public_ip.example.id
-  }
-}
-
-resource "azurerm_linux_virtual_machine" "example" {
-  name                = "example-vm"
-  resource_group_name = azurerm_resource_group.example.name
-  location            = azurerm_resource_group.example.location
-  size                = "Standard_F2"
-  admin_username      = "adminuser"
-  network_interface_ids = [
-    azurerm_network_interface.example.id,
-  ]
-
-  os_disk {
-    caching              = "ReadWrite"
-    storage_account_type = "Standard_LRS"
-  }
-
-  source_image_reference {
-    publisher = "Canonical"
-    offer     = "UbuntuServer"
-    sku       = "18.04-LTS"
-    version   = "latest"
-  }
-
-  admin_ssh_key {
-    username   = "adminuser"
-    public_key = file("~/.ssh/id_rsa.pub")
-  }
-}