如何使用 Terraform 'error dial tcp 127.0.0.1:80: connect: connection refused' 修复 Azure Kubernetes 服务?

How to fix Azure Kubernetes Services with Terraform 'error dial tcp 127.0.0.1:80: connect: connection refused'?

'terraform apply' 在我的本地计算机上出现以下错误的原因是什么?在构建服务器上似乎 运行 没问题。

我也查看了相关的Whosebug消息:

Plan: 3 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

kubernetes_namespace.azurevotefront-namespace: Creating...
kubernetes_service.azurevotefront-metadata: Creating...
kubernetes_deployment.azurevotefront-namespace: Creating...
╷
│ Error: Post "http://localhost/api/v1/namespaces": dial tcp 127.0.0.1:80: connect: connection refused   
│
│   with kubernetes_namespace.azurevotefront-namespace,
│   on kubernetes.tf line 1, in resource "kubernetes_namespace" "azurevotefront-namespace":
│    1: resource "kubernetes_namespace" "azurevotefront-namespace" {
│
╵
╷
│ Error: Failed to create deployment: Post "http://localhost/apis/apps/v1/namespaces/azurevotefront-namespace/deployments": dial tcp 127.0.0.1:80: connect: connection refused
│
│   with kubernetes_deployment.azurevotefront-namespace,
│   on main.tf line 1, in resource "kubernetes_deployment" "azurevotefront-namespace":
│    1: resource "kubernetes_deployment" "azurevotefront-namespace" {
│
╵
╷
│ Error: Post "http://localhost/api/v1/namespaces/azurevotefront-namespace/services": dial tcp 127.0.0.1:80: connect: connection refused
│
│   with kubernetes_service.azurevotefront-metadata,
│   on main.tf line 47, in resource "kubernetes_service" "azurevotefront-metadata":
│   47: resource "kubernetes_service" "azurevotefront-metadata" {

Kubernetes.tf

resource "kubernetes_namespace" "azurevotefront-namespace" {
  metadata {
    annotations = {
      name = "azurevotefront-annotation"
    }

    labels = {
      mylabel = "azurevotefront-value"
    }

    name = "azurevotefront-namespace"
  }
}

Provider.tf

terraform {
  backend "azurerm" {
    key = "terraform.tfstate"
    resource_group_name = "MASKED"
    storage_account_name = "MASKED"
    access_key = "MASKED"
    container_name = "MASKED"
  }
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~>2.68"
    }
    kubernetes = {
      source  = "hashicorp/kubernetes"
      version = "~> 2.4"
    }
  }
}

provider "azurerm" {
  tenant_id = "MASKED"
  subscription_id = "MASKED"
  client_id = "MASKED"
  client_secret = "MASKED"
  features {}
}

如评论中所述,您缺少 kubernetes 提供程序配置:

provider "kubernetes" {
  host                   = azurerm_kubernetes_cluster.aks.kube_admin_config.0.host
  client_certificate     = base64decode(azurerm_kubernetes_cluster.aks.kube_admin_config.0.client_certificate)
  client_key             = base64decode(azurerm_kubernetes_cluster.aks.kube_admin_config.0.client_key)
  cluster_ca_certificate = base64decode(azurerm_kubernetes_cluster.aks.kube_admin_config.0.cluster_ca_certificate)
}