From 795c1b540aee94744deedd39007f054a6ddfd404 Mon Sep 17 00:00:00 2001 From: Tom Archer Date: Wed, 10 Jul 2024 06:31:48 -0700 Subject: [PATCH 1/8] Initial put --- .../101-aks-standard-lb-and-vmss/main.tf | 45 +++++++++++++++++++ .../101-aks-standard-lb-and-vmss/outputs.tf | 42 +++++++++++++++++ .../101-aks-standard-lb-and-vmss/providers.tf | 26 +++++++++++ .../variablles.tf | 29 ++++++++++++ 4 files changed, 142 insertions(+) create mode 100644 quickstart/101-aks-standard-lb-and-vmss/main.tf create mode 100644 quickstart/101-aks-standard-lb-and-vmss/outputs.tf create mode 100644 quickstart/101-aks-standard-lb-and-vmss/providers.tf create mode 100644 quickstart/101-aks-standard-lb-and-vmss/variablles.tf diff --git a/quickstart/101-aks-standard-lb-and-vmss/main.tf b/quickstart/101-aks-standard-lb-and-vmss/main.tf new file mode 100644 index 000000000..4c06f732e --- /dev/null +++ b/quickstart/101-aks-standard-lb-and-vmss/main.tf @@ -0,0 +1,45 @@ +# Generate random resource group name +resource "random_pet" "rg_name" { + prefix = var.resource_group_name_prefix +} + +resource "azurerm_resource_group" "rg" { + location = var.resource_group_location + name = random_pet.rg_name.id +} + +resource "random_pet" "azurerm_kubernetes_cluster_name" { + prefix = "cluster" +} + +resource "random_pet" "azurerm_kubernetes_cluster_dns_prefix" { + prefix = "dns" +} + +resource "azurerm_kubernetes_cluster" "k8s" { + location = azurerm_resource_group.rg.location + name = random_pet.azurerm_kubernetes_cluster_name.id + resource_group_name = azurerm_resource_group.rg.name + dns_prefix = random_pet.azurerm_kubernetes_cluster_dns_prefix.id + + identity { + type = "SystemAssigned" + } + + default_node_pool { + name = "agentpool" + vm_size = "Standard_D2_v2" + node_count = var.node_count + } + linux_profile { + admin_username = var.username + + ssh_key { + key_data = azapi_resource_action.ssh_public_key_gen.output.publicKey + } + } + network_profile { + network_plugin = "kubenet" + load_balancer_sku = "standard" + } +} \ No newline at end of file diff --git a/quickstart/101-aks-standard-lb-and-vmss/outputs.tf b/quickstart/101-aks-standard-lb-and-vmss/outputs.tf new file mode 100644 index 000000000..0d82c4463 --- /dev/null +++ b/quickstart/101-aks-standard-lb-and-vmss/outputs.tf @@ -0,0 +1,42 @@ +output "resource_group_name" { + value = azurerm_resource_group.rg.name +} + +output "kubernetes_cluster_name" { + value = azurerm_kubernetes_cluster.k8s.name +} + +output "client_certificate" { + value = azurerm_kubernetes_cluster.k8s.kube_config[0].client_certificate + sensitive = true +} + +output "client_key" { + value = azurerm_kubernetes_cluster.k8s.kube_config[0].client_key + sensitive = true +} + +output "cluster_ca_certificate" { + value = azurerm_kubernetes_cluster.k8s.kube_config[0].cluster_ca_certificate + sensitive = true +} + +output "cluster_password" { + value = azurerm_kubernetes_cluster.k8s.kube_config[0].password + sensitive = true +} + +output "cluster_username" { + value = azurerm_kubernetes_cluster.k8s.kube_config[0].username + sensitive = true +} + +output "host" { + value = azurerm_kubernetes_cluster.k8s.kube_config[0].host + sensitive = true +} + +output "kube_config" { + value = azurerm_kubernetes_cluster.k8s.kube_config_raw + sensitive = true +} \ No newline at end of file diff --git a/quickstart/101-aks-standard-lb-and-vmss/providers.tf b/quickstart/101-aks-standard-lb-and-vmss/providers.tf new file mode 100644 index 000000000..7eff01791 --- /dev/null +++ b/quickstart/101-aks-standard-lb-and-vmss/providers.tf @@ -0,0 +1,26 @@ +terraform { + required_version = ">=1.0" + + required_providers { + azapi = { + source = "azure/azapi" + version = "~>1.5" + } + azurerm = { + source = "hashicorp/azurerm" + version = "~>3.0" + } + random = { + source = "hashicorp/random" + version = "~>3.0" + } + time = { + source = "hashicorp/time" + version = "0.9.1" + } + } +} + +provider "azurerm" { + features {} +} \ No newline at end of file diff --git a/quickstart/101-aks-standard-lb-and-vmss/variablles.tf b/quickstart/101-aks-standard-lb-and-vmss/variablles.tf new file mode 100644 index 000000000..1eada683a --- /dev/null +++ b/quickstart/101-aks-standard-lb-and-vmss/variablles.tf @@ -0,0 +1,29 @@ +variable "resource_group_location" { + type = string + default = "eastus" + description = "Location of the resource group." +} + +variable "resource_group_name_prefix" { + type = string + default = "rg" + description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription." +} + +variable "node_count" { + type = number + description = "The initial quantity of nodes for the node pool." + default = 3 +} + +variable "msi_id" { + type = string + description = "The Managed Service Identity ID. Set this value if you're running this example using Managed Identity as the authentication method." + default = null +} + +variable "username" { + type = string + description = "The admin username for the new cluster." + default = "azureadmin" +} \ No newline at end of file From 65acca1e0cc67e0ee90c3f82d8738bc61fffa453 Mon Sep 17 00:00:00 2001 From: Tom Archer Date: Wed, 10 Jul 2024 07:26:49 -0700 Subject: [PATCH 2/8] Fixed file name --- .../101-aks-standard-lb-and-vmss/{variablles.tf => variables.tf} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename quickstart/101-aks-standard-lb-and-vmss/{variablles.tf => variables.tf} (100%) diff --git a/quickstart/101-aks-standard-lb-and-vmss/variablles.tf b/quickstart/101-aks-standard-lb-and-vmss/variables.tf similarity index 100% rename from quickstart/101-aks-standard-lb-and-vmss/variablles.tf rename to quickstart/101-aks-standard-lb-and-vmss/variables.tf From 84022f9adfcba6538c870f484d04b521c4b81154 Mon Sep 17 00:00:00 2001 From: Tom Archer Date: Wed, 10 Jul 2024 08:56:57 -0700 Subject: [PATCH 3/8] Fixing code --- .../101-aks-standard-lb-and-vmss/main.tf | 44 +++++++++---------- .../101-aks-standard-lb-and-vmss/outputs.tf | 39 ---------------- .../101-aks-standard-lb-and-vmss/providers.tf | 10 ----- .../101-aks-standard-lb-and-vmss/variables.tf | 18 -------- 4 files changed, 20 insertions(+), 91 deletions(-) diff --git a/quickstart/101-aks-standard-lb-and-vmss/main.tf b/quickstart/101-aks-standard-lb-and-vmss/main.tf index 4c06f732e..9b06db3c4 100644 --- a/quickstart/101-aks-standard-lb-and-vmss/main.tf +++ b/quickstart/101-aks-standard-lb-and-vmss/main.tf @@ -1,4 +1,3 @@ -# Generate random resource group name resource "random_pet" "rg_name" { prefix = var.resource_group_name_prefix } @@ -8,38 +7,35 @@ resource "azurerm_resource_group" "rg" { name = random_pet.rg_name.id } -resource "random_pet" "azurerm_kubernetes_cluster_name" { - prefix = "cluster" +resource "random_string" "aks_cluster_name" { + length = 12 + special = false } -resource "random_pet" "azurerm_kubernetes_cluster_dns_prefix" { - prefix = "dns" -} - -resource "azurerm_kubernetes_cluster" "k8s" { +resource "azurerm_kubernetes_cluster" "aks_cluster" { + name = random_string.aks_cluster_name.result location = azurerm_resource_group.rg.location - name = random_pet.azurerm_kubernetes_cluster_name.id resource_group_name = azurerm_resource_group.rg.name - dns_prefix = random_pet.azurerm_kubernetes_cluster_dns_prefix.id + dns_prefix = "myakscluster" - identity { - type = "SystemAssigned" + default_node_pool { + type = "VirtualMachineScaleSets" + name = "default" + node_count = 1 + vm_size = "Standard_DS2_v2" + enable_auto_scaling = true } - default_node_pool { - name = "agentpool" - vm_size = "Standard_D2_v2" - node_count = var.node_count + network_profile { + network_plugin = "azure" + load_balancer_sku = "Standard" } - linux_profile { - admin_username = var.username - ssh_key { - key_data = azapi_resource_action.ssh_public_key_gen.output.publicKey - } + identity { + type = "SystemAssigned" } - network_profile { - network_plugin = "kubenet" - load_balancer_sku = "standard" + + tags = { + Environment = "Production" } } \ No newline at end of file diff --git a/quickstart/101-aks-standard-lb-and-vmss/outputs.tf b/quickstart/101-aks-standard-lb-and-vmss/outputs.tf index 0d82c4463..62816bd73 100644 --- a/quickstart/101-aks-standard-lb-and-vmss/outputs.tf +++ b/quickstart/101-aks-standard-lb-and-vmss/outputs.tf @@ -1,42 +1,3 @@ output "resource_group_name" { value = azurerm_resource_group.rg.name } - -output "kubernetes_cluster_name" { - value = azurerm_kubernetes_cluster.k8s.name -} - -output "client_certificate" { - value = azurerm_kubernetes_cluster.k8s.kube_config[0].client_certificate - sensitive = true -} - -output "client_key" { - value = azurerm_kubernetes_cluster.k8s.kube_config[0].client_key - sensitive = true -} - -output "cluster_ca_certificate" { - value = azurerm_kubernetes_cluster.k8s.kube_config[0].cluster_ca_certificate - sensitive = true -} - -output "cluster_password" { - value = azurerm_kubernetes_cluster.k8s.kube_config[0].password - sensitive = true -} - -output "cluster_username" { - value = azurerm_kubernetes_cluster.k8s.kube_config[0].username - sensitive = true -} - -output "host" { - value = azurerm_kubernetes_cluster.k8s.kube_config[0].host - sensitive = true -} - -output "kube_config" { - value = azurerm_kubernetes_cluster.k8s.kube_config_raw - sensitive = true -} \ No newline at end of file diff --git a/quickstart/101-aks-standard-lb-and-vmss/providers.tf b/quickstart/101-aks-standard-lb-and-vmss/providers.tf index 7eff01791..7261b1fb4 100644 --- a/quickstart/101-aks-standard-lb-and-vmss/providers.tf +++ b/quickstart/101-aks-standard-lb-and-vmss/providers.tf @@ -1,11 +1,5 @@ terraform { - required_version = ">=1.0" - required_providers { - azapi = { - source = "azure/azapi" - version = "~>1.5" - } azurerm = { source = "hashicorp/azurerm" version = "~>3.0" @@ -14,10 +8,6 @@ terraform { source = "hashicorp/random" version = "~>3.0" } - time = { - source = "hashicorp/time" - version = "0.9.1" - } } } diff --git a/quickstart/101-aks-standard-lb-and-vmss/variables.tf b/quickstart/101-aks-standard-lb-and-vmss/variables.tf index 1eada683a..1a8c6abba 100644 --- a/quickstart/101-aks-standard-lb-and-vmss/variables.tf +++ b/quickstart/101-aks-standard-lb-and-vmss/variables.tf @@ -8,22 +8,4 @@ variable "resource_group_name_prefix" { type = string default = "rg" description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription." -} - -variable "node_count" { - type = number - description = "The initial quantity of nodes for the node pool." - default = 3 -} - -variable "msi_id" { - type = string - description = "The Managed Service Identity ID. Set this value if you're running this example using Managed Identity as the authentication method." - default = null -} - -variable "username" { - type = string - description = "The admin username for the new cluster." - default = "azureadmin" } \ No newline at end of file From e442bda05b30a8a89cdf70346c39b0dd16ccc270 Mon Sep 17 00:00:00 2001 From: Tom Archer Date: Wed, 10 Jul 2024 09:20:14 -0700 Subject: [PATCH 4/8] Added output --- quickstart/101-aks-standard-lb-and-vmss/outputs.tf | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/quickstart/101-aks-standard-lb-and-vmss/outputs.tf b/quickstart/101-aks-standard-lb-and-vmss/outputs.tf index 62816bd73..fab9f07ae 100644 --- a/quickstart/101-aks-standard-lb-and-vmss/outputs.tf +++ b/quickstart/101-aks-standard-lb-and-vmss/outputs.tf @@ -1,3 +1,7 @@ output "resource_group_name" { value = azurerm_resource_group.rg.name } + +output "aks_cluster_name" { + value = azurerm_kubernetes_cluster.aks.name +} \ No newline at end of file From 15a19838ad287c7467d75d466a8f73de762ad191 Mon Sep 17 00:00:00 2001 From: Tom Archer Date: Thu, 11 Jul 2024 05:21:16 -0700 Subject: [PATCH 5/8] Fixes --- quickstart/101-aks-standard-lb-and-vmss/main.tf | 2 +- quickstart/101-aks-standard-lb-and-vmss/outputs.tf | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/quickstart/101-aks-standard-lb-and-vmss/main.tf b/quickstart/101-aks-standard-lb-and-vmss/main.tf index 9b06db3c4..46a82e8fb 100644 --- a/quickstart/101-aks-standard-lb-and-vmss/main.tf +++ b/quickstart/101-aks-standard-lb-and-vmss/main.tf @@ -28,7 +28,7 @@ resource "azurerm_kubernetes_cluster" "aks_cluster" { network_profile { network_plugin = "azure" - load_balancer_sku = "Standard" + load_balancer_sku = "standard" } identity { diff --git a/quickstart/101-aks-standard-lb-and-vmss/outputs.tf b/quickstart/101-aks-standard-lb-and-vmss/outputs.tf index fab9f07ae..720116722 100644 --- a/quickstart/101-aks-standard-lb-and-vmss/outputs.tf +++ b/quickstart/101-aks-standard-lb-and-vmss/outputs.tf @@ -3,5 +3,5 @@ output "resource_group_name" { } output "aks_cluster_name" { - value = azurerm_kubernetes_cluster.aks.name + value = azurerm_kubernetes_cluster.aks_cluster.name } \ No newline at end of file From c20885dabc9fc1ec2bb76983d67e8a7ecf3cb125 Mon Sep 17 00:00:00 2001 From: Tom Archer Date: Thu, 11 Jul 2024 05:39:38 -0700 Subject: [PATCH 6/8] Added required fields to default_node_pool block --- quickstart/101-aks-standard-lb-and-vmss/main.tf | 2 ++ 1 file changed, 2 insertions(+) diff --git a/quickstart/101-aks-standard-lb-and-vmss/main.tf b/quickstart/101-aks-standard-lb-and-vmss/main.tf index 46a82e8fb..9c9764110 100644 --- a/quickstart/101-aks-standard-lb-and-vmss/main.tf +++ b/quickstart/101-aks-standard-lb-and-vmss/main.tf @@ -22,6 +22,8 @@ resource "azurerm_kubernetes_cluster" "aks_cluster" { type = "VirtualMachineScaleSets" name = "default" node_count = 1 + max_count = 3 + min_count = 1 vm_size = "Standard_DS2_v2" enable_auto_scaling = true } From e9dfde8f8847b81bcad6b09185f4ee2ab3f34b4b Mon Sep 17 00:00:00 2001 From: Tom Archer Date: Thu, 11 Jul 2024 09:23:27 -0700 Subject: [PATCH 7/8] terraform fmt --- quickstart/101-aks-standard-lb-and-vmss/main.tf | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/quickstart/101-aks-standard-lb-and-vmss/main.tf b/quickstart/101-aks-standard-lb-and-vmss/main.tf index 9c9764110..831d3c48f 100644 --- a/quickstart/101-aks-standard-lb-and-vmss/main.tf +++ b/quickstart/101-aks-standard-lb-and-vmss/main.tf @@ -16,20 +16,20 @@ resource "azurerm_kubernetes_cluster" "aks_cluster" { name = random_string.aks_cluster_name.result location = azurerm_resource_group.rg.location resource_group_name = azurerm_resource_group.rg.name - dns_prefix = "myakscluster" + dns_prefix = "myakscluster" default_node_pool { - type = "VirtualMachineScaleSets" - name = "default" - node_count = 1 - max_count = 3 - min_count = 1 - vm_size = "Standard_DS2_v2" + type = "VirtualMachineScaleSets" + name = "default" + node_count = 1 + max_count = 3 + min_count = 1 + vm_size = "Standard_DS2_v2" enable_auto_scaling = true } network_profile { - network_plugin = "azure" + network_plugin = "azure" load_balancer_sku = "standard" } From c1bd8b8a27a236ed71c8076e6c093df23bdf555f Mon Sep 17 00:00:00 2001 From: Tom Archer Date: Mon, 15 Jul 2024 12:04:32 -0700 Subject: [PATCH 8/8] Added README.md --- .../101-aks-standard-lb-and-vmss/README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 quickstart/101-aks-standard-lb-and-vmss/README.md diff --git a/quickstart/101-aks-standard-lb-and-vmss/README.md b/quickstart/101-aks-standard-lb-and-vmss/README.md new file mode 100644 index 000000000..7a331e6de --- /dev/null +++ b/quickstart/101-aks-standard-lb-and-vmss/README.md @@ -0,0 +1,19 @@ +# Aure Kubernetes Service (AKS) with standard load balancer and Virtual Machine Scale Sets (VMSS) + +This template deploys an AKS cluster a standard load balancer and Virtual Machine Scale Sets (VMSS) + +## Terraform resource types + +- [random_pet](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet) +- [azurerm_resource_group](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group) +- [random_string](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/string) +- [azurerm_kubernetes_cluster](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/kubernetes_cluster) + +## Variables + +| Name | Description | Default | +|-|-|-| +| `resource_group_name_prefix` | Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription. | rg | +| `resource_group_location` | Location of the resource group. | eastus | + +## Example