Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to create Azure AKS Cluster using existing VNET and Subnets

I'm trying to build an AKS cluster in Azure using Terraform. However, I do not want AKS deployed into its own VNET and Subnet, I already have built a subnet within a vnet that I want it to use. When trying to just give it the subnet ID, I get an overlapping CIDER issue. My networking is:

VNET: 10.0.0.0/16 Subnets: 10.0.1.0/24, 10.0.2.0/24, and 10.0.3.0/24. I need AKS to use the 10.0.1.0./24 subnet within this VNET. However, my Terraform config is trying to use a CIDR of 10.0.0.0/16, which is an obviouis conflict. I don't know how to fix this issue inside of Terraform, with the portal I can just choose the vnet/subnet for AKS. Below is my Terraform configuration which generates the error:

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "=2.46.0"
    }
  }
}

# Configure the Microsoft Azure Provider
provider "azurerm" {
  features {}

  subscription_id = "####"
  tenant_id       = "####"
}

locals {
  azure_location = "East US"
  azure_location_short = "eastus"
}

resource "azurerm_resource_group" "primary_vnet_resource_group" {
  name     = "vnet-prod-002-eastus-001"
  location = local.azure_location
}

resource "azurerm_virtual_network" "primary_vnet_virtual_network" {
  name                = "vnet_primary_eastus-001"
  location            = local.azure_location
  resource_group_name = azurerm_resource_group.primary_vnet_resource_group.name
  address_space       = ["10.0.0.0/16"]
}

resource "azurerm_subnet" "aks-subnet" {
  name           = "snet-aks-prod-002-eastus-001"
#  location = local.azure_location
  virtual_network_name = azurerm_virtual_network.primary_vnet_virtual_network.name
  resource_group_name = azurerm_resource_group.primary_vnet_resource_group.name
  address_prefixes = ["10.0.1.0/24"]
 }

output "aks_subnet_id" {
  value = azurerm_subnet.aks-subnet.id
}

resource "azurerm_subnet" "application-subnet" {
  name           = "snet-app-prod-002-eastus-001"
#  location            = local.azure_location
  virtual_network_name = azurerm_virtual_network.primary_vnet_virtual_network.name
  resource_group_name = azurerm_resource_group.primary_vnet_resource_group.name
  address_prefixes = ["10.0.2.0/24"]
 }

resource "azurerm_subnet" "postgres-subnet" {
  name           = "snet-postgres-prod-002-eastus-001"
#  location            = local.azure_location
  virtual_network_name = azurerm_virtual_network.primary_vnet_virtual_network.name
  resource_group_name = azurerm_resource_group.primary_vnet_resource_group.name
  address_prefixes = ["10.0.3.0/24"]
 }

output "postgres_subnet_id" {
  value = azurerm_subnet.postgres-subnet.id
}

resource "azurerm_kubernetes_cluster" "aks-prod-002-eastus-001" {
  name                = "aks-prod-002-eastus-001"
  location            = local.azure_location
  resource_group_name = azurerm_resource_group.primary_vnet_resource_group.name
  dns_prefix          = "aks-prod-002-eastus-001"


  default_node_pool {
    name       = "default"
    node_count = 1
    vm_size    = "Standard_DS2_v2"
    vnet_subnet_id = azurerm_subnet.aks-subnet.id

  }

  network_profile {
    network_plugin = "azure"
  }

  identity {
    type = "SystemAssigned"
  }

  addon_profile {
    aci_connector_linux {
      enabled = false
    }

    azure_policy {
      enabled = false
    }

    http_application_routing {
      enabled = false
    }

    oms_agent {
      enabled = false
    }
  }
}

I'm not a Terraform expert and really need a hand with this if anyone knows how to accomplish this. I've been up and down the documentation and I can find a way to specify the subnet id but that's about all I can do. If I don't specify the subnet id then everything is built, but there is a new vnet created which is what I don't want.

Thanks in advance

like image 609
Richard Wolford Avatar asked Oct 15 '25 14:10

Richard Wolford


1 Answers

All the following properties need to be set under network_profile as following:

network_profile {
    network_plugin = "azure"
    network_policy = "azure"
    service_cidr = "10.0.4.0/24"
    dns_service_ip = "10.0.4.10"
    docker_bridge_cidr = "172.17.0.1/16"
  }

These were missed, I hope this helps anyone who is having problems similar to mine.

More info about this block can be found here: https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/kubernetes_cluster#network_plugin

like image 58
Richard Wolford Avatar answered Oct 17 '25 15:10

Richard Wolford



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!