已经成功使用Terraform大约一个月了。使用terraform apply
在Azure上创建了许多资源(即azurerm_kubernetes_cluster
,kubernetes_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)
}
...
型
1条答案
按热度按时间5jdjgkvh1#
字符串
发生错误的原因是
Terraform plan
表示计划更换AKS
群集。因此,当计划过程达到Kubernetes provider
配置时,没有已知的AKS cluster endpoint
,导致提供程序默认连接到localhost。要解决此问题并连接到
AKS cluster
,您可以在provider "kubernetes
中使用以下配置。型
config_path
指定Kubernetes configuration
文件(~/.kube/config
)的路径。该文件包含访问AKS cluster
所需的所有配置详细信息,包括群集的API server URL, client certificate, and client key
。如果在
Kubernetes provider
中指定host
、client_certificate
、client_key
和cluster_ca_certificate
信息,则必须使用如下所示的目标执行terraform plan
。此配置使您能够连接到AKS cluster
而不是local host
。terraform plan -target *name of the your AKS cluster*
型
适用Terraform
的数据
一旦运行了terraform代码,部署就已经创建了。
的数据
参考:dial tcp [::1]:80: connect: connection refused by
apparentlymart