kubernetes Terraform错误连接拒绝:错误地使用本地主机

iecba09b  于 2023-11-17  发布在  Kubernetes
关注(0)|答案(1)|浏览(139)

已经成功使用Terraform大约一个月了。使用terraform apply在Azure上创建了许多资源(即azurerm_kubernetes_clusterkubernetes_service)。
我突然收到下面关于我的kubernetes服务的错误。

╷
│ Error: Get "http://localhost/api/v1/namespaces/default/services/<service name>": dial tcp [::1]:80: connect: connection refused
│ 
│   with kubernetes_service.<service name>,
│   on main.tf line 132, in resource "kubernetes_service" "<service name>":
│  132: resource "kubernetes_service" "<service name>" {
│ 
╵

字符串

我不知道为什么这个URL突然引用localhost,应该是Azure。我不确定是什么改变了这一点。

  • 我正在使用正确的Kubectl上下文。
  • kubectl config view返回正确的群集详细信息
  • kubectl cluster-info返回正确的Azure端点
  • 已验证服务在Azure门户中可用
  • 更新terraform至最新版本
  • 已运行apply with debug:
2023-11-05T16:14:31.939-0500 [DEBUG] provider.terraform-provider-kubernetes_v2.23.0_x5: 2023/11/05 16:14:31 [INFO] Checking service <service name>
2023-11-05T16:14:31.940-0500 [DEBUG] provider.terraform-provider-kubernetes_v2.23.0_x5: 2023/11/05 16:14:31 [DEBUG] Kubernetes API Request Details:
2023-11-05T16:14:31.940-0500 [DEBUG] provider.terraform-provider-kubernetes_v2.23.0_x5: ---[ REQUEST ]---------------------------------------
2023-11-05T16:14:31.940-0500 [DEBUG] provider.terraform-provider-kubernetes_v2.23.0_x5: GET /api/v1/namespaces/default/services/<service name> HTTP/1.1
2023-11-05T16:14:31.940-0500 [DEBUG] provider.terraform-provider-kubernetes_v2.23.0_x5: Host: localhost
2023-11-05T16:14:31.940-0500 [DEBUG] provider.terraform-provider-kubernetes_v2.23.0_x5: User-Agent: HashiCorp/1.0 Terraform/1.6.3
2023-11-05T16:14:31.940-0500 [DEBUG] provider.terraform-provider-kubernetes_v2.23.0_x5: Accept: application/json, */*
2023-11-05T16:14:31.940-0500 [DEBUG] provider.terraform-provider-kubernetes_v2.23.0_x5: Accept-Encoding: gzip
2023-11-05T16:14:31.940-0500 [DEBUG] provider.terraform-provider-kubernetes_v2.23.0_x5
2023-11-05T16:14:31.940-0500 [DEBUG] provider.terraform-provider-kubernetes_v2.23.0_x5
2023-11-05T16:14:31.940-0500 [DEBUG] provider.terraform-provider-kubernetes_v2.23.0_x5: -----------------------------------------------------
2023-11-05T16:14:31.942-0500 [DEBUG] provider.terraform-provider-kubernetes_v2.23.0_x5: 2023/11/05 16:14:31 [DEBUG] Received error: &url.Error{Op:"Get", URL:"http://localhost/api/v1/namespaces/default/services/<service name>", Err:(*net.OpError)(0x14001036a50)}
2023-11-05T16:14:31.944-0500 [ERROR] provider.terraform-provider-kubernetes_v2.23.0_x5: Response contains error diagnostic: diagnostic_summary="Get \"http://localhost/api/v1/namespaces/default/services/<service name>\": dial tcp [::1]:80: connect: connection refused" tf_req_id=ab63a5a7-5bab-fc9a-c4f2-c7b102614920 tf_resource_type=kubernetes_service @caller=github.com/hashicorp/[email protected]/tfprotov5/internal/diag/diagnostics.go:55 tf_rpc=ReadResource diagnostic_detail="" tf_proto_version=5.3 tf_provider_addr=registry.terraform.io/hashicorp/kubernetes @module=sdk.proto diagnostic_severity=ERROR timestamp=2023-11-05T16:14:31.943-0500
2023-11-05T16:14:31.944-0500 [ERROR] vertex "kubernetes_service.<service name>" error: Get "http://localhost/api/v1/namespaces/default/services/<service name>": dial tcp [::1]:80: connect: connection refused
2023-11-05T16:14:31.944-0500 [ERROR] vertex "kubernetes_service.<service name> (expand)" error: Get "http://localhost/api/v1/namespaces/default/services/<service name>": dial tcp [::1]:80: connect: connection refused


terraform文件中的提供程序

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "3.72.0"
    }
    kubernetes = {
      source  = "hashicorp/kubernetes"
      version = "2.23.0"
    }
    cloudflare = {
      source  = "cloudflare/cloudflare"
      version = "4.15.0"
    }
    random = {
      source = "hashicorp/random"
    }
  }
}

provider "azurerm" {
  features {}
}

provider "cloudflare" {
  api_token = var.CLOUDFLARE_API_TOKEN
}

...

provider "kubernetes" {
  host                   = data.azurerm_kubernetes_cluster.cluster.kube_config.0.host
  client_certificate     = base64decode(data.azurerm_kubernetes_cluster.cluster.kube_config.0.client_certificate)
  client_key             = base64decode(data.azurerm_kubernetes_cluster.cluster.kube_config.0.client_key)
  cluster_ca_certificate = base64decode(data.azurerm_kubernetes_cluster.cluster.kube_config.0.cluster_ca_certificate)
}

...

5jdjgkvh

5jdjgkvh1#

Error: Get "http://localhost/api/v1/namespaces/default/services/<service name>": dial tcp [::1]:80: connect: connection refused

字符串
发生错误的原因是Terraform plan表示计划更换AKS群集。因此,当计划过程达到Kubernetes provider配置时,没有已知的AKS cluster endpoint,导致提供程序默认连接到localhost
要解决此问题并连接到AKS cluster,您可以在provider "kubernetes中使用以下配置。

provider "kubernetes" {
  config_path = "~/.kube/config"
}


config_path指定Kubernetes configuration文件(~/.kube/config)的路径。该文件包含访问AKS cluster所需的所有配置详细信息,包括群集的API server URL, client certificate, and client key
如果在Kubernetes provider中指定hostclient_certificateclient_keycluster_ca_certificate信息,则必须使用如下所示的目标执行terraform plan。此配置使您能够连接到AKS cluster而不是local host
terraform plan -target *name of the your AKS cluster*

terraform {
      required_providers {
        azurerm = {
          source  = "hashicorp/azurerm"
          version = "3.0.2"
        }
        kubernetes = {
          source  = "hashicorp/kubernetes"
          version = ">= 2.0.1"
        }
      }
    }
    
    provider "azurerm" {
      features {}
    }
    
    
    data "azurerm_kubernetes_cluster" "example" {
      name                = "Venkat-aks"
      resource_group_name = "existing-RG"
    }
    
    provider "kubernetes" {
      config_path = "~/.kube/config"
    }
    
    resource "kubernetes_deployment" "nginx" {
      metadata {
        name = "venkat-nginx"
        labels = {
          App = "venkatNginx"
        }
      }
    
      spec {
        replicas = 2
        selector {
          match_labels = {
            App = "VenkatNginx"
          }
        }
        template {
          metadata {
            labels = {
              App = "VenkatNginx"
            }
          }
          spec {
            container {
              image = "nginx:1.7.8"
              name  = "venkat"
    
              port {
                container_port = 80
              }
    
              resources {
                limits = {
                  cpu    = "0.5"
                  memory = "512Mi"
                }
                requests = {
                  cpu    = "250m"
                  memory = "50Mi"
                }
              }
            }
          }
        }
      }
    }
    
    resource "kubernetes_service" "nginx" {
      metadata {
        name = "nginx-venkat"
      }
      spec {
        selector = {
          App = kubernetes_deployment.nginx.spec.0.template.0.metadata[0].labels.App
        }
        port {
          port        = 80
          target_port = 80
        }
    
        type = "LoadBalancer"
      }
    }

适用Terraform


的数据
一旦运行了terraform代码,部署就已经创建了。


的数据
参考:dial tcp [::1]:80: connect: connection refused by apparentlymart

相关问题