无法使用现有 vnet 创建 ak

Failed to create aks using existing vnet

我正在尝试使用 terraform 创建 aks;问题是我已经创建了 vnet 和子网,我需要在该网络中创建集群。

执行此代码时出现错误:

locals {
  environment     = "prod"
  resource_group =  "hnk_rg_poc"
  vnet_subnet_cidr = ["10.3.1.0/24"]
}

#Existing vnet with address space "10.3.1.0/24"
data "azurerm_virtual_network" "existing-vnet" {
  name                = "${var.vnet}"
  resource_group_name = local.resource_group
}

#subnets
resource "azurerm_subnet" "vnet_subnet_id" {
  name                 = "${var.vnet_subnet_id}"
  resource_group_name  = local.resource_group
  address_prefixes     = local.vnet_subnet_cidr
  virtual_network_name = data.azurerm_virtual_network.existing-vnet.name
}

vnet_subnet_id       =  data.azurerm_subnet.vnet_subnet_id.id

由于您已经拥有供 AKS 群集使用的现有 Vnet 和子网,因此您必须为子网使用数据块而不是资源块。

您可以使用以下方法使用现有的 Vnet 和子网创建基本的 aks 集群:

provider "azurerm" {
  features {}
}

#local vars

locals {
  environment     = "test"
  resource_group = "resource_group_name"
  name_prefix     = "name-aks"
}


#Existing vnet with address space 
data "azurerm_virtual_network" "base" {
  name                = "existing-vnet"
  resource_group_name = local.resource_group
}

#existing subnet to be used by aks

data "azurerm_subnet" "aks" {
  name                 = "existing-subnet"
  resource_group_name  = local.resource_group
  virtual_network_name = data.azurerm_virtual_network.base.name
}


#kubernetes_cluster

resource "azurerm_kubernetes_cluster" "base" {
  name                    = "${local.name_prefix}-${local.environment}"
  location                = data.azurerm_virtual_network.base.location
  resource_group_name     = data.azurerm_virtual_network.base.resource_group_name
  dns_prefix              = "dns-${local.name_prefix}-${local.environment}"

  network_profile {
    network_plugin = "azure"
  }


  default_node_pool {
    name           = "default"
    node_count     = 1
    vm_size        = "Standard_D2_v2"
    vnet_subnet_id = data.azurerm_subnet.aks.id
  }

  identity {
    type = "SystemAssigned"
  }
}

输出: (Terraform 计划)