将卷添加到 Azure 上的 Terraform AKS 群集时出错 'no such host'

Error 'no such host' when adding a volume to Terraform AKS cluster on Azure

我正在使用以下 Terraform 文件创建 AKS 集群:

resource "random_pet" "prefix" {}

resource "kubernetes_persistent_volume" "example" {
  metadata {
    name = "example"
  }
  spec {
    capacity = {
      storage = "1Gi"
    }
    access_modes = ["ReadWriteOnce"]
    persistent_volume_source {
      azure_disk {
        caching_mode  = "None"
        data_disk_uri = azurerm_managed_disk.example.id
        disk_name     = "example"
        kind          = "Managed"
      }
    }
  }
}

resource "azurerm_kubernetes_cluster" "example" {
  name                = "${random_pet.prefix.id}-aks"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  dns_prefix          = "${random_pet.prefix.id}-k8s"

  default_node_pool {
    name            = "example"
    node_count      = 2
    vm_size         = "Standard_D2_v2"
    os_disk_size_gb = 30
  }

  identity {
    type = "SystemAssigned"
  }

  role_based_access_control {
    enabled = true
  }

  addon_profile {
    kube_dashboard {
      enabled = true
    }
  }

  tags = {
    environment = "Demo"
  }
}

provider "azurerm" {
  version = ">=2.20.0"
  features {}
}

resource "azurerm_resource_group" "example" {
  name     = "${random_pet.prefix.id}-rg"
  location = "westus2"
}


resource "azurerm_managed_disk" "example" {
  name                 = "example"
  location             = azurerm_resource_group.example.location
  resource_group_name  = azurerm_resource_group.example.name
  storage_account_type = "Standard_LRS"
  create_option        = "Empty"
  disk_size_gb         = "1"
  tags = {
    environment = azurerm_resource_group.example.name
  }
}

我从 Terraform 关于设置 AKS 集群的教程中导出了上述文件:https://learn.hashicorp.com/tutorials/terraform/aks

我在这里使用了 Terraform 的设置 Azure 托管磁盘和 k8s 卷的示例:https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/persistent_volume

当我尝试 运行 使用 Terraform 进行上述配置时,出现以下错误:

Error: Post "https://pumped-llama-k8s-419df981.hcp.westus2.azmk8s.io:443/api/v1/persistentvolumes": dial tcp: lookup pumped-llama-k8s-419df981.hcp.westus2.azmk8s.io on 192.168.1.1:53: no such host

  on main.tf line 3, in resource "kubernetes_persistent_volume" "example":
   3: resource "kubernetes_persistent_volume" "example" {

每当我尝试使用任何非azurerm Terraform 资源时,我都会遇到同样的错误。例如。尝试配置角色和角色绑定时:resource "kubernetes_role"

我假设错误消息的 url - 尝试连接到 HashiCorp 云平台,我认为这是默认设置 - 我需要明确告诉这些非 azurerm 资源我正在连接到 Azure 托管的 Kubernetes;但是我不知道该怎么做。

原来我需要在 Terraform 文件中定义 kubernetes 提供程序 - 考虑到我正在与它的资源进行交互,我不会因为不包含它而收到某种警告,这让我很惊讶。

这是我修复它的方法:

outputs.tf:

output "host" {
  value = azurerm_kubernetes_cluster.default.kube_config.0.host
}

output "client_key" {
  value = azurerm_kubernetes_cluster.default.kube_config.0.client_key
}

output "client_certificate" {
  value = azurerm_kubernetes_cluster.default.kube_config.0.client_certificate
}

output "kube_config" {
  value = azurerm_kubernetes_cluster.default.kube_config_raw
}

output "cluster_ca_certificate" {
  value = azurerm_kubernetes_cluster.default.kube_config.0.cluster_ca_certificate
}

main.tf:

...
provider "kubernetes" {
  version = "=1.13.2"
  load_config_file = "false"

  host = azurerm_kubernetes_cluster.default.kube_config.0.host
  
  client_certificate     = "${base64decode(azurerm_kubernetes_cluster.default.kube_config.0.client_certificate)}"
  client_key             = "${base64decode(azurerm_kubernetes_cluster.default.kube_config.0.client_key)}"
  cluster_ca_certificate = "${base64decode(azurerm_kubernetes_cluster.default.kube_config.0.cluster_ca_certificate)}"
}
...