在我的 terraform 配置文件中,我在 GKE 上创建了一个 Kubernetes 集群,创建后,设置一个 Kubernetes 提供程序来访问所述集群并执行各种操作,例如设置命名空间。
问题是在没有 terraform 的集群中创建了一些新的命名空间,现在我尝试将这些命名空间导入我的状态似乎由于无法连接到集群而失败,我认为这是由于以下原因(取自 Terraform 的官方文档)导入命令):
The only limitation Terraform has when reading the configuration files is that the import provider configurations must not depend on non-variable inputs. For example, a provider configuration cannot depend on a data source.
我用来导入命名空间的命令非常简单:
terraform import kubernetes_namespace.my_new_namespace my_new_namespace
我也尝试使用
-provdier=""
和 -config=""
但无济于事。我的 Kubernetes 提供程序配置是这样的:
provider "kubernetes" {
version = "~> 1.8"
host = module.gke.endpoint
token = data.google_client_config.current.access_token
cluster_ca_certificate = base64decode(module.gke.cluster_ca_certificate)
}
我试图导入的命名空间资源的一个例子是:
resource "kubernetes_namespace" "my_new_namespace" {
metadata {
name = "my_new_namespace"
}
}
导入命令的结果如下:
Error: Get http://localhost/api/v1/namespaces/my_new_namespace: dial tcp [::1]:80: connect: connection refused
很明显它注定要失败,因为它试图到达
localhost
而不是实际的集群 IP 和配置。这个用例有什么解决方法吗?
提前致谢。
最佳答案
(1) 在您的 kubeconfig 文件中为您的 GKE 集群创建一个条目。
gcloud container clusters get-credentials cluster-name
见:https://cloud.google.com/kubernetes-engine/docs/how-to/cluster-access-for-kubectl#generate_kubeconfig_entry(2) 将 terraform Kubernetes provider 指向您的 kubeconfig:
provider "kubernetes" {
config_path = "~/.kube/config"
}
关于kubernetes - 如何在 terraform 中导入生成的 Kubernetes 集群的命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57865646/