Terraform:错误:Kubernetes集群无法访问:配置无效。

24

使用"terraform destroy"删除Kubernetes集群后,我无法再次创建它。

"terraform apply"返回以下错误消息:

Error: Kubernetes cluster unreachable: invalid configuration: no configuration has been provided, try setting KUBERNETES_MASTER environment variable

这是terraform的配置:

terraform {
  backend "s3" {
    bucket = "skyglass-msur"
    key    = "terraform/backend"
    region = "us-east-1"
  }
}

locals {
  env_name         = "staging"
  aws_region       = "us-east-1"
  k8s_cluster_name = "ms-cluster"
}

variable "mysql_password" {
  type        = string
  description = "Expected to be retrieved from environment variable TF_VAR_mysql_password"
}

provider "aws" {
  region = local.aws_region
}

data "aws_eks_cluster" "msur" {
  name = module.aws-kubernetes-cluster.eks_cluster_id
}

module "aws-network" {
  source = "github.com/skyglass-microservices/module-aws-network"

  env_name              = local.env_name
  vpc_name              = "msur-VPC"
  cluster_name          = local.k8s_cluster_name
  aws_region            = local.aws_region
  main_vpc_cidr         = "10.10.0.0/16"
  public_subnet_a_cidr  = "10.10.0.0/18"
  public_subnet_b_cidr  = "10.10.64.0/18"
  private_subnet_a_cidr = "10.10.128.0/18"
  private_subnet_b_cidr = "10.10.192.0/18"
}

module "aws-kubernetes-cluster" {
  source = "github.com/skyglass-microservices/module-aws-kubernetes"

  ms_namespace       = "microservices"
  env_name           = local.env_name
  aws_region         = local.aws_region
  cluster_name       = local.k8s_cluster_name
  vpc_id             = module.aws-network.vpc_id
  cluster_subnet_ids = module.aws-network.subnet_ids

  nodegroup_subnet_ids     = module.aws-network.private_subnet_ids
  nodegroup_disk_size      = "20"
  nodegroup_instance_types = ["t3.medium"]
  nodegroup_desired_size   = 1
  nodegroup_min_size       = 1
  nodegroup_max_size       = 5
}

# Create namespace
# Use kubernetes provider to work with the kubernetes cluster API
provider "kubernetes" {
  # load_config_file       = false
  cluster_ca_certificate = base64decode(data.aws_eks_cluster.msur.certificate_authority.0.data)
  host                   = data.aws_eks_cluster.msur.endpoint
  exec {
    api_version = "client.authentication.k8s.io/v1alpha1"
    command     = "aws-iam-authenticator"
    args        = ["token", "-i", "${data.aws_eks_cluster.msur.name}"]
  }
}

# Create a namespace for microservice pods
resource "kubernetes_namespace" "ms-namespace" {
  metadata {
    name = "microservices"
  }
}

附注:似乎terraform kubernetes提供程序0.14.7存在问题。

在此版本中,我无法使用“load_config_file”=false,因此不得不将其注释掉,这似乎是问题的原因。

另外可能也是过时的cluster_ca_certificate的问题,terraform试图使用它:删除此证书可能就足够了,尽管我不确定它存储在哪里。


1
这个问题能不能在不添加export KUBE_CONFIG_PATH=/path/to/.kube/config的情况下解决?我的意思是,比如在AWS codepipeline的情况下,它可能无法工作。 - undefined
1
@fireman777 试试这个命令: aws eks --region ${var.region} update-kubeconfig --name ${var.cluster_name}确保你在AWS codepipeline中有访问你的AWS账户的权限。确保在运行脚本时你的AWS EKS集群已经存在。 用你自己的值替换var.region和var.cluster_name。 - undefined
谢谢,@Mykhailo Skliar的帮助。顺便说一下,我注意到一个奇怪的行为:升级到Terraform 0.15.5之后,我的问题突然消失了。 - undefined
9个回答

45

在直接操作状态之类的激进行为之前,请尝试设置KUBE_CONFIG_PATH变量:

export KUBE_CONFIG_PATH=/path/to/.kube/config

在此之后重新运行planapply命令。 这为我解决了问题。


2
由于某些原因,我的环境中设置了 KUBECONFIG,这似乎可以工作 - 直到最近。KUBE_CONFIG_PATH 对我有效。太棒了! - jitter

10

我遇到了同样的问题。我甚至手动删除了EKS集群,这导致terraform状态混乱。

然而,在浪费了几个小时之后,我发现有一个非常简单的解决方案。

您可以运行:

terraform state rm <resource_type>.<resource_name>

我刚刚执行了
terraform state rm `terraform state list | grep eks`

以安全的方式从状态文件中删除特定服务的所有条目。


谢谢,我通过以下命令解决了我的问题: terraform state rm terraform state list | grep eks terraform state rm terraform state list | grep kubectl terraform state rm terraform state list | grep helm - Shivam Anand

2
当我需要更新集群以删除一些资源时,我遇到了这个问题。您也可以尝试运行terraform apply -refresh=false,并让它自动销毁资源。

1
我使用官方的helm提供者代替kubernetes来解决了这个问题。
首先,我们列出所需的提供者:
terraform {
  backend "s3" {
    bucket  = "..."
    key     = "..."
    region  = "..."
    profile = "..."
  }

  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.49"
    }

    kubernetes = {
      source  = "hashicorp/kubernetes"
      version = "~> 2.16.1"
    }

    helm = {
      source  = "hashicorp/helm"
      version = "~> 2.8.0"
    }
  }
}

然后,我们配置提供者:
data "aws_eks_cluster" "cluster" {
  name = var.cluster_name
}

provider "helm" {
  kubernetes {
    host                   = data.aws_eks_cluster.cluster.endpoint
    cluster_ca_certificate = base64decode(data.aws_eks_cluster.cluster.certificate_authority.0.data)
    exec {
      api_version = "client.authentication.k8s.io/v1"
      command = "aws"
      args = [
        "eks",
        "get-token",
        "--cluster-name",
        data.aws_eks_cluster.cluster.name,
        "--profile",
        var.profile
      ]
    }
  }
}

最后,我们通过helm_release资源添加图表:
resource "helm_release" "foo" {
  name             = "foo"
  chart            = "foo"
  repository       = "https://foo.bar/chart"
  namespace        = "foo"
  create_namespace = true
  values           = [templatefile("${path.module}/chart/values.yaml", {})]
}

1
在我的情况下,当我尝试使用“tf destroy”销毁资源时,出现了这个错误。
对于我来说,逻辑解决方案是执行以下操作:
  1. 在你引导K8S集群的terraform状态上运行“tf apply -refresh=true”。这是输出K8S凭证(k8s_cluster_access_token)的工作空间。
  2. 在使用上述K8S凭证创建K8S资源的terraform状态上运行“tf apply -refresh=true”。
  3. 运行“tf destroy”(成功完成)。

1
删除 AWS 上的 terraform 状态 S3 存储桶解决了该问题。

0

在运行“terraform”命令的文件夹中删除“.terraform”子文件夹也应该解决问题。

我没有尝试过这个确切的情况,但今天我遇到了类似的问题,所以我决定分享另一个解决方案。它似乎比删除S3存储桶更温和。


0
这个问题的原因是通常情况下,我的kubeconfig中有一些内容,但由于某种原因它变为空了。虽然解决方法有点随机,但当我重新初始化配置(我也在使用Minikube,所以我启动了minikube),Terraform就能正常工作了。
我很好奇如果你尝试使用aws命令行来更新kubeconfig是否会起作用。https://docs.aws.amazon.com/cli/latest/reference/eks/update-kubeconfig.html

0
如果你不知道你的 KUBECONFIG 路径,可以使用以下命令: aws eks --region ${var.region} update-kubeconfig --name ${var.cluster_name}
  • 确保你在 terraform(或任何其他脚本)中可以访问你的 AWS 账户。
  • 确保在运行脚本时,你的 AWS EKS 集群已经存在。
  • 用你自己的值替换 var.regionvar.cluster_name
你甚至可以创建 terraform 的 null_resource,它会自动更新你的 KUBECONFIG 路径。
resource "null_resource" "update_kubeconfig" {
  provisioner "local-exec" {
    command = "aws eks --region ${var.region} update-kubeconfig --name ${var.cluster_name}"
  }
  depends_on = [your_kubernetes_cluster_resource]
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接