Terraform kubectl提供程序错误:无法创建用于读取资源的kubernetes rest客户端

jpfvwuh4  于 12个月前  发布在  Kubernetes
关注(0)|答案(2)|浏览(103)

我有一个Terraform配置,(以及其他资源)在Google Cloud上创建了一个Google Kubernetes Engine集群。我使用kubectl提供程序为ManagedCertificate和FrontendConfig添加YAML清单,因为这些不是kubernetes或google提供程序的一部分。当从本地机器应用Terraform配置时,这与预期一样有效,但当我尝试在CI管道中执行时,对于两个kubectl_manifest资源,我都得到以下错误:

Error: failed to create kubernetes rest client for read of resource: Get "http://localhost/api?timeout=32s": dial tcp 127.0.0.1:80: connect: connection refused

字符串
由于我只在CI期间遇到这个问题,我的第一个猜测是服务帐户缺少正确的作用域,但据我所知,所有作用域都存在。非常感谢您的任何建议和想法!

sxissh06

sxissh061#

通过将load_config_file = false添加到kubectl提供程序配置修复了该问题。我的提供程序配置现在看起来像这样:

data "google_client_config" "default" {}

provider "kubernetes" {
  host                   = "https://${endpoint from GKE}"
  token                  = data.google_client_config.default.access_token
  cluster_ca_certificate = base64decode(CA certificate from GKE)
}

provider "kubectl" {
  host                   = "https://${endpoint from GKE}"
  token                  = data.google_client_config.default.access_token
  cluster_ca_certificate = base64decode(CA certificate from GKE)
  load_config_file       = false
}

字符串

7hiiyaii

7hiiyaii2#

提供程序试图连接localhost,这意味着您需要提供适当的kube-config文件或在terraform中动态设置它。
虽然你没有提到如何设置auth,但这里有两种方法
糟糕的方式

resource "null_resource" "deploy-app" {
  provisioner "local-exec" {
    interpreter = ["/bin/bash", "-c"]
    command     = <<EOT
    kubectl apply -f myapp.yaml ./temp/kube-config.yaml;
    EOT
  }
 # will run always, its bad
  triggers = {
    always_run = "${timestamp()}"
  }
  depends_on = [
    local_file.kube_config
  ]
}

resource "local_file" "kube_config" {
  content  = var.my_kube_config # pass the config file from ci variable
  filename = "${path.module}/temp/kube-config.yaml"
}

字符串

正确的方式

data "google_container_cluster" "cluster" {
  name = "your_cluster_name"
}
data "google_client_config" "current" {
}
  provider "kubernetes" {
    host  = data.google_container_cluster.cluster.endpoint
    token = data.google_client_config.current.access_token
    cluster_ca_certificate = base64decode(
      data.google_container_cluster.cluster.master_auth[0].cluster_ca_certificate
    )
  }

data "kubectl_file_documents" "app_yaml" {
  content = file("myapp.yaml")
}

resource "kubectl_manifest" "app_installer" {
  for_each  = data.kubectl_file_documents.app_yaml.manifests
  yaml_body = each.value
}


如果群集在同一模块中,则提供程序应

provider "kubernetes" {
  load_config_file = "false"
  host     = google_container_cluster.my_cluster.endpoint
  client_certificate     = google_container_cluster.my_cluster.master_auth.0.client_certificate
  client_key             = google_container_cluster.my_cluster.master_auth.0.client_key
  cluster_ca_certificate = google_container_cluster.my_cluster.master_auth.0.cluster_ca_certificate
}

相关问题