【问题标题】:Can't create secrets in kubernetes with terraform cloud无法使用 terraform cloud 在 Kubernetes 中创建秘密
【发布时间】:2020-06-08 05:44:54
【问题描述】:

我正在尝试在我的 kubernetes 集群和 terraform 云中创建一个秘密。 我可以毫无问题地创建集群,但是当我尝试在集群中注入秘密时会出现问题。 这是我的 terraform 清单的简化版本:

terraform {
  backend "remote" {
    organization = "my-org"

    // Workspaces separate deployment envs (like prod, stage, or UK, Italy)
    workspaces {
      name = "my-workspace-name"
    }
  }
}
resource "google_container_cluster" "demo-k8s-cluster" {
  name = "demo-cluster"
  location = var.region
  initial_node_count = 1
  project = var.project-id


  master_auth {
    username = ""
    password = ""

    client_certificate_config {
      issue_client_certificate = false
    }
  }

  node_config {
    oauth_scopes = [
      "https://www.googleapis.com/auth/logging.write",
      "https://www.googleapis.com/auth/monitoring",
    ]

//    service_account = var.service-account

    metadata = {
      disable-legacy-endpoints = "true"
    }
  }

  timeouts {
    create = "30m"
    update = "40m"
  }
}

provider "kubernetes" {
  host     = google_container_cluster.demo-k8s-cluster.endpoint
  username = google_container_cluster.demo-k8s-cluster.master_auth.0.username
  password = google_container_cluster.demo-k8s-cluster.master_auth.0.password
  client_certificate     = base64decode(google_container_cluster.demo-k8s-cluster.master_auth.0.client_certificate)
  client_key             = base64decode(google_container_cluster.demo-k8s-cluster.master_auth.0.client_key)
  cluster_ca_certificate = base64decode(google_container_cluster.demo-k8s-cluster.master_auth.0.cluster_ca_certificate)
  load_config_file = "false"
}

resource "kubernetes_secret" "cloudsql-db-credentials" {
   metadata {
       name = "cloudsql-instance-credentials-test"
   }

  data = {
    "stack-creds.json" = var.service-account
  }

}

计划运行良好,我在Apply 阶段收到以下错误:

Error: secrets is forbidden: User "system:anonymous" cannot create resource "secrets" in API group "" in the namespace "default"

  on infrastructure.tf line 149, in resource "kubernetes_secret" "cloudsql-db-credentials":
 149: resource "kubernetes_secret" "cloudsql-db-credentials" {

【问题讨论】:

  • 用户authentication 似乎有问题。您能否分享有关您的.kube/config 的任何详细信息?您可以从运行 terraform 的节点手动运行 kubectl apply -f secret.yaml 吗?结果如何?
  • 请同时查看this 部分。
  • 我不认为 kubernetes auth 配置有问题,因为我可以从命令行手动创建秘密没有问题。问题是我无法让它与 terraform cloud 一起使用。
  • “用户身份验证问题”并不一定意味着您的.kube/config 有问题。请注意,直接运行命令时,您不会以用户 system:anonymous 的身份连接到 kubernetes api。但是,当您通过 terraform cloud 连接到它时,API 服务器出于某种原因将您识别为“system:anonymous”用户,该用户无权在您的 kubernetes 集群上执行所需的操作。
  • 看起来身份验证也可以通过简单地提供 config_path 来处理,因此 terraform 可以在连接到您的 时使用您的 kubeconfig 文件中已经存在的数据Kubernetes 集群.

标签: kubernetes terraform terraform-cloud


【解决方案1】:

根据@mario 的评论,事实证明 terraform cloud 无法获得正确的身份,也无法连接到集群以注入秘密。我没有使用 terraform cloud,而是选择使用 GCS 后端并设法让它工作。以下配置有效:

terraform {
  backend "gcs" {
    bucket = "infrastructure-state-bucket"
    prefix = "test/so_simple2"
  }
}

// The project-id variable contains project id to use.
variable "project-id" {
  type = string
}

variable "region" {
  type = string
}

variable "cluster-name" {
  type = string
}

provider "google" {
  project = var.project-id
  region = var.region
}

provider "random" {}

resource "random_id" "id" {
  byte_length = 4
  prefix      = "${var.cluster-name}-"
}


resource "google_container_cluster" "cluster" {
  name = random_id.id.hex
  location = var.region
  initial_node_count = 1
  project = var.project-id
}

provider "kubernetes" {
  host     = google_container_cluster.cluster.endpoint
  username = google_container_cluster.cluster.master_auth.0.username
  password = google_container_cluster.cluster.master_auth.0.password
  client_certificate     = base64decode(google_container_cluster.cluster.master_auth.0.client_certificate)
  client_key             = base64decode(google_container_cluster.cluster.master_auth.0.client_key)
  cluster_ca_certificate = base64decode(google_container_cluster.cluster.master_auth.0.cluster_ca_certificate)
// This is a deal breaker, if is set to false I get same error.
//  load_config_file = "false"
}

resource "kubernetes_secret" "example" {
  metadata {
    name = "basic-auth"
  }

  data = {
    username = "admin"
    password = "P4ssw0rd"
  }

  type = "kubernetes.io/basic-auth"
}

【讨论】:

    猜你喜欢
    • 2019-05-07
    • 2019-08-16
    • 2021-10-26
    • 2020-12-27
    • 2020-12-04
    • 1970-01-01
    • 2022-01-02
    • 2023-01-20
    • 2022-11-02
    相关资源
    最近更新 更多