From 6e7acfb6955371b90b3045dbaa8fc64369b01f50 Mon Sep 17 00:00:00 2001 From: Tom Archer Date: Mon, 1 Jul 2024 13:19:54 -0700 Subject: [PATCH 1/8] Create a Windows-based Azure Kubernetes Service (AKS) cluster --- quickstart/101-aks-cluster-windows/README.md | 18 +++++ quickstart/101-aks-cluster-windows/main.tf | 69 +++++++++++++++++++ quickstart/101-aks-cluster-windows/outputs.tf | 3 + .../101-aks-cluster-windows/providers.tf | 19 +++++ .../101-aks-cluster-windows/variables.tf | 23 +++++++ 5 files changed, 132 insertions(+) create mode 100644 quickstart/101-aks-cluster-windows/README.md create mode 100644 quickstart/101-aks-cluster-windows/main.tf create mode 100644 quickstart/101-aks-cluster-windows/outputs.tf create mode 100644 quickstart/101-aks-cluster-windows/providers.tf create mode 100644 quickstart/101-aks-cluster-windows/variables.tf diff --git a/quickstart/101-aks-cluster-windows/README.md b/quickstart/101-aks-cluster-windows/README.md new file mode 100644 index 000000000..e257e7377 --- /dev/null +++ b/quickstart/101-aks-cluster-windows/README.md @@ -0,0 +1,18 @@ +# Windows-based Azure Kubernetes Service (AKS) cluster + +This template deploys an AKS cluster with Windows nodes. + +## 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) + +## 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 + diff --git a/quickstart/101-aks-cluster-windows/main.tf b/quickstart/101-aks-cluster-windows/main.tf new file mode 100644 index 000000000..62b212301 --- /dev/null +++ b/quickstart/101-aks-cluster-windows/main.tf @@ -0,0 +1,69 @@ +# 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 "random_pet" "azurerm_kubernetes_cluster_node_pool" { + prefix = "pool" +} + +resource "azurerm_virtual_network" "vnet" { + name = "myvnet" + location = azurerm_resource_group.rg.location + resource_group_name = azurerm_resource_group.rg.name + address_space = ["10.1.0.0/16"] + + subnet { + name = "subnet1" + address_prefix = "10.1.1.0/24" + } +} + +resource "azurerm_kubernetes_cluster" "aks" { + name = random_pet.azurerm_kubernetes_cluster_name.id + location = azurerm_resource_group.rg.location + 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 + vnet_subnet_id = element(tolist(azurerm_virtual_network.vnet.subnet), 0).id + } + + windows_profile { + admin_username = "azureadmin" + admin_password = "Password0123!" + } + + network_profile { + network_plugin = "kubenet" + load_balancer_sku = "standard" + } +} + +resource "azurerm_kubernetes_cluster_node_pool" "win" { + name = random_pet.azurerm_kubernetes_cluster_node_pool.id + kubernetes_cluster_id = azurerm_kubernetes_cluster.aks.id + vm_size = "Standard_D4s_v3" + node_count = var.node_count_windows + os_type = "Windows" +} \ No newline at end of file diff --git a/quickstart/101-aks-cluster-windows/outputs.tf b/quickstart/101-aks-cluster-windows/outputs.tf new file mode 100644 index 000000000..62816bd73 --- /dev/null +++ b/quickstart/101-aks-cluster-windows/outputs.tf @@ -0,0 +1,3 @@ +output "resource_group_name" { + value = azurerm_resource_group.rg.name +} diff --git a/quickstart/101-aks-cluster-windows/providers.tf b/quickstart/101-aks-cluster-windows/providers.tf new file mode 100644 index 000000000..a0155e168 --- /dev/null +++ b/quickstart/101-aks-cluster-windows/providers.tf @@ -0,0 +1,19 @@ +terraform { + required_version = ">= 1.0" + + required_providers { + azurerm = { + source = "hashicorp/azurerm" + version = "~>3.0" + } + random = { + source = "hashicorp/random" + version = "~>3.0" + } + } +} + +provider "azurerm" { + features { + } +} diff --git a/quickstart/101-aks-cluster-windows/variables.tf b/quickstart/101-aks-cluster-windows/variables.tf new file mode 100644 index 000000000..51cecde0d --- /dev/null +++ b/quickstart/101-aks-cluster-windows/variables.tf @@ -0,0 +1,23 @@ +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_linux" { + type = number + description = "The initial quantity of Linux nodes for the node pool." + default = 1 +} + +variable "node_count_windows" { + type = number + description = "The initial quantity of Windows nodes for the node pool." + default = 1 +} \ No newline at end of file From 5182c37a0f1e99ef4000791d72bccc8ebde45170 Mon Sep 17 00:00:00 2001 From: Tom Archer Date: Mon, 1 Jul 2024 15:57:55 -0700 Subject: [PATCH 2/8] fixes --- quickstart/101-aks-cluster-windows/main.tf | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/quickstart/101-aks-cluster-windows/main.tf b/quickstart/101-aks-cluster-windows/main.tf index 62b212301..5ba71a46d 100644 --- a/quickstart/101-aks-cluster-windows/main.tf +++ b/quickstart/101-aks-cluster-windows/main.tf @@ -16,10 +16,6 @@ resource "random_pet" "azurerm_kubernetes_cluster_dns_prefix" { prefix = "dns" } -resource "random_pet" "azurerm_kubernetes_cluster_node_pool" { - prefix = "pool" -} - resource "azurerm_virtual_network" "vnet" { name = "myvnet" location = azurerm_resource_group.rg.location @@ -59,11 +55,3 @@ resource "azurerm_kubernetes_cluster" "aks" { load_balancer_sku = "standard" } } - -resource "azurerm_kubernetes_cluster_node_pool" "win" { - name = random_pet.azurerm_kubernetes_cluster_node_pool.id - kubernetes_cluster_id = azurerm_kubernetes_cluster.aks.id - vm_size = "Standard_D4s_v3" - node_count = var.node_count_windows - os_type = "Windows" -} \ No newline at end of file From da5efce1779d9920d292a55bc1b88c8dcf4e04f4 Mon Sep 17 00:00:00 2001 From: Tom Archer Date: Mon, 1 Jul 2024 16:48:54 -0700 Subject: [PATCH 3/8] fixes 2 --- quickstart/101-aks-cluster-windows/article | 0 quickstart/101-aks-cluster-windows/main.tf | 14 +++++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 quickstart/101-aks-cluster-windows/article diff --git a/quickstart/101-aks-cluster-windows/article b/quickstart/101-aks-cluster-windows/article new file mode 100644 index 000000000..e69de29bb diff --git a/quickstart/101-aks-cluster-windows/main.tf b/quickstart/101-aks-cluster-windows/main.tf index 5ba71a46d..7bb953f38 100644 --- a/quickstart/101-aks-cluster-windows/main.tf +++ b/quickstart/101-aks-cluster-windows/main.tf @@ -16,6 +16,10 @@ resource "random_pet" "azurerm_kubernetes_cluster_dns_prefix" { prefix = "dns" } +resource "random_pet" "azurerm_kubernetes_cluster_node_pool" { + prefix = "nodepool" +} + resource "azurerm_virtual_network" "vnet" { name = "myvnet" location = azurerm_resource_group.rg.location @@ -51,7 +55,15 @@ resource "azurerm_kubernetes_cluster" "aks" { } network_profile { - network_plugin = "kubenet" + network_plugin = "azure" load_balancer_sku = "standard" } } + +resource "azurerm_kubernetes_cluster_node_pool" "win" { + name = random_pet.azurerm_kubernetes_cluster_node_pool.id + kubernetes_cluster_id = azurerm_kubernetes_cluster.aks.id + vm_size = "Standard_D4s_v3" + node_count = var.node_count_windows + os_type = "Windows" +} From 1bd62d28a8a1ed5284c19446f8902e989ffdd8c6 Mon Sep 17 00:00:00 2001 From: Tom Archer Date: Mon, 1 Jul 2024 17:00:31 -0700 Subject: [PATCH 4/8] declared user & pwd as vars --- quickstart/101-aks-cluster-windows/main.tf | 4 ++-- quickstart/101-aks-cluster-windows/variables.tf | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/quickstart/101-aks-cluster-windows/main.tf b/quickstart/101-aks-cluster-windows/main.tf index 7bb953f38..46513070f 100644 --- a/quickstart/101-aks-cluster-windows/main.tf +++ b/quickstart/101-aks-cluster-windows/main.tf @@ -50,8 +50,8 @@ resource "azurerm_kubernetes_cluster" "aks" { } windows_profile { - admin_username = "azureadmin" - admin_password = "Password0123!" + admin_username = var.admin_username + admin_password = var.admin_password } network_profile { diff --git a/quickstart/101-aks-cluster-windows/variables.tf b/quickstart/101-aks-cluster-windows/variables.tf index 51cecde0d..ce823cfb2 100644 --- a/quickstart/101-aks-cluster-windows/variables.tf +++ b/quickstart/101-aks-cluster-windows/variables.tf @@ -20,4 +20,16 @@ variable "node_count_windows" { type = number description = "The initial quantity of Windows nodes for the node pool." default = 1 +} + +variable "admin_username" { + type = string + description = "The admin username for the Windows node pool." + default = "azureuser" +} + +variable "admin_password" { + type = string + description = "The admin password for the Windows node pool." + default = "Passw0rd1234Us!" } \ No newline at end of file From 068cf2da3fc76f357808a49e6c67e5fcc897a743 Mon Sep 17 00:00:00 2001 From: Tom Archer Date: Mon, 1 Jul 2024 17:18:04 -0700 Subject: [PATCH 5/8] changed params of terraform init --- quickstart/101-aks-cluster-windows/main.tf | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/quickstart/101-aks-cluster-windows/main.tf b/quickstart/101-aks-cluster-windows/main.tf index 46513070f..b3910baec 100644 --- a/quickstart/101-aks-cluster-windows/main.tf +++ b/quickstart/101-aks-cluster-windows/main.tf @@ -16,8 +16,12 @@ resource "random_pet" "azurerm_kubernetes_cluster_dns_prefix" { prefix = "dns" } -resource "random_pet" "azurerm_kubernetes_cluster_node_pool" { - prefix = "nodepool" +resource "random_string" "azurerm_kubernetes_cluster_node_pool" { + length = 12 + special = false + numeric = false + lower = true + upper = false } resource "azurerm_virtual_network" "vnet" { From bee854b03b46eb329162fa6660209239493ec25a Mon Sep 17 00:00:00 2001 From: Tom Archer Date: Mon, 1 Jul 2024 17:19:12 -0700 Subject: [PATCH 6/8] changed params of node pool name --- quickstart/101-aks-cluster-windows/main.tf | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/quickstart/101-aks-cluster-windows/main.tf b/quickstart/101-aks-cluster-windows/main.tf index b3910baec..c8e1f4479 100644 --- a/quickstart/101-aks-cluster-windows/main.tf +++ b/quickstart/101-aks-cluster-windows/main.tf @@ -20,7 +20,7 @@ resource "random_string" "azurerm_kubernetes_cluster_node_pool" { length = 12 special = false numeric = false - lower = true + lower = true upper = false } @@ -65,7 +65,7 @@ resource "azurerm_kubernetes_cluster" "aks" { } resource "azurerm_kubernetes_cluster_node_pool" "win" { - name = random_pet.azurerm_kubernetes_cluster_node_pool.id + name = random_string.azurerm_kubernetes_cluster_node_pool.result kubernetes_cluster_id = azurerm_kubernetes_cluster.aks.id vm_size = "Standard_D4s_v3" node_count = var.node_count_windows From 7c29b17ca6c0afd82587e8a6c27df5f50691afe8 Mon Sep 17 00:00:00 2001 From: Tom Archer Date: Mon, 1 Jul 2024 19:01:53 -0700 Subject: [PATCH 7/8] fixed node pool name len --- quickstart/101-aks-cluster-windows/main.tf | 4 ++-- quickstart/101-aks-cluster-windows/variables.tf | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/quickstart/101-aks-cluster-windows/main.tf b/quickstart/101-aks-cluster-windows/main.tf index c8e1f4479..3795214c6 100644 --- a/quickstart/101-aks-cluster-windows/main.tf +++ b/quickstart/101-aks-cluster-windows/main.tf @@ -17,10 +17,10 @@ resource "random_pet" "azurerm_kubernetes_cluster_dns_prefix" { } resource "random_string" "azurerm_kubernetes_cluster_node_pool" { - length = 12 + length = 6 special = false numeric = false - lower = true + lower = true upper = false } diff --git a/quickstart/101-aks-cluster-windows/variables.tf b/quickstart/101-aks-cluster-windows/variables.tf index ce823cfb2..2cf992b98 100644 --- a/quickstart/101-aks-cluster-windows/variables.tf +++ b/quickstart/101-aks-cluster-windows/variables.tf @@ -25,11 +25,11 @@ variable "node_count_windows" { variable "admin_username" { type = string description = "The admin username for the Windows node pool." - default = "azureuser" + default = "azureuser" } variable "admin_password" { type = string description = "The admin password for the Windows node pool." - default = "Passw0rd1234Us!" + default = "Passw0rd1234Us!" } \ No newline at end of file From bf2f1f59398a15399eb4ff9e6a8b58de5d66d12c Mon Sep 17 00:00:00 2001 From: Tom Archer Date: Mon, 1 Jul 2024 19:21:33 -0700 Subject: [PATCH 8/8] completed readme --- quickstart/101-aks-cluster-windows/README.md | 9 ++++++++- quickstart/101-aks-cluster-windows/article | 0 quickstart/101-aks-cluster-windows/outputs.tf | 17 +++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) delete mode 100644 quickstart/101-aks-cluster-windows/article diff --git a/quickstart/101-aks-cluster-windows/README.md b/quickstart/101-aks-cluster-windows/README.md index e257e7377..071782ad1 100644 --- a/quickstart/101-aks-cluster-windows/README.md +++ b/quickstart/101-aks-cluster-windows/README.md @@ -6,6 +6,10 @@ This template deploys an AKS cluster with Windows nodes. - [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_virtual_network](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/virtual_network) +- [azurerm_kubernetes_cluster](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/kubernetes_cluster) +- [azurerm_kubernetes_cluster_node_pool](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/kubernetes_cluster_node_pool) ## Variables @@ -13,6 +17,9 @@ This template deploys an AKS cluster with Windows nodes. |-|-|-| | `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 | +| `node_count_linux` | Initial quantity of Linux nodes for the node pool. | 1 | +| `node_count_windows` | Initial quantity of Windows nodes for the node pool. | 1 | +| `admin_username` | Admin username for the Windows node pool. | azureuser | +| `admin_password` | Admin password for the Windows node pool. | Passw0rd1234Us! | ## Example - diff --git a/quickstart/101-aks-cluster-windows/article b/quickstart/101-aks-cluster-windows/article deleted file mode 100644 index e69de29bb..000000000 diff --git a/quickstart/101-aks-cluster-windows/outputs.tf b/quickstart/101-aks-cluster-windows/outputs.tf index 62816bd73..7067a4304 100644 --- a/quickstart/101-aks-cluster-windows/outputs.tf +++ b/quickstart/101-aks-cluster-windows/outputs.tf @@ -1,3 +1,20 @@ output "resource_group_name" { value = azurerm_resource_group.rg.name } + +output "kubernetes_cluster_name" { + value = azurerm_kubernetes_cluster.aks.name +} + +output "kubernetes_cluster_dns_prefix" { + value = azurerm_kubernetes_cluster.aks.dns_prefix +} + +output "kubernetes_cluster_node_pool_name" { + value = azurerm_kubernetes_cluster_node_pool.win.name +} + +output "kubernetes_cluster_kube_config_raw" { + value = azurerm_kubernetes_cluster.aks.kube_config_raw + sensitive = true +} \ No newline at end of file