Eri*_*oen 4 terraform terraform-provider-gcp
我知道 Terraform google 提供商的 Github 上也有类似的问题,涉及 google_container_cluster 的幂等性;然而,似乎没有一个与我的简单例子相符。任何应用 Terraform 计划的尝试都会破坏并重新创建我的集群,这是一个 6 分钟以上的操作。
集群没有明显的变化,但是 terraform 状态表明集群的 id 是集群的名称,但是新的 id 是计算出来的;因此,必须重新创建集群。我可以阻止这种情况吗?
我按照建议的示例设置集群:使用remove_initial_node_pool=true和定义集群initial_node_count=1,然后创建显式节点池作为依赖资源。我还尝试使用初始节点池创建默认集群。我没有指定其他人与幂等性问题相关的任何其他属性(例如 master_ipv4_cidr_block)。
这是基本的 Terraform 设置。我正在使用 Terraform v0.11.13 和provider.google v2.6.0。
provider "google" {
project = "${var.google_project}"
region = "${var.google_region}"
zone = "${var.google_zone}"
}
resource "google_container_cluster" "cluster" {
project = "${var.google_project}"
name = "${var.cluster_name}"
location = "${var.google_region}"
remove_default_node_pool = true
initial_node_count = 1
master_auth {
username = ""
password = ""
}
timeouts {
create = "20m"
update = "15m"
delete = "15m"
}
}
resource "google_container_node_pool" "cluster_nodes" {
name = "${var.cluster_name}-node-pool"
cluster = "${google_container_cluster.cluster.name}"
node_count = "${var.cluster_node_count}"
node_config {
preemptible = "${var.preemptible}"
disk_size_gb = "${var.disk_size_gb}"
disk_type = "${var.disk_type}"
machine_type = "${var.machine_type}"
oauth_scopes = [
"https://www.googleapis.com/auth/devstorage.read_only",
"https://www.googleapis.com/auth/logging.write",
"https://www.googleapis.com/auth/monitoring",
"https://www.googleapis.com/auth/service.management.readonly",
"https://www.googleapis.com/auth/servicecontrol",
"https://www.googleapis.com/auth/trace.append",
"https://www.googleapis.com/auth/compute",
"https://www.googleapis.com/auth/cloud-platform",
]
}
timeouts {
create = "20m"
update = "15m"
delete = "15m"
}
}
output "cluster_ca_certificate" {
value = "${google_container_cluster.cluster.master_auth.0.cluster_ca_certificate}"
}
output "host" {
value = "${google_container_cluster.cluster.endpoint}"
}
provider "kubernetes" {
host = "${google_container_cluster.cluster.endpoint}"
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)}"
}
Run Code Online (Sandbox Code Playgroud)
等等。未显示用于启用 Helm 服务帐户的服务帐户和集群角色绑定以及 Helm 版本。我认为这些在这里并不重要。
如果我执行terraform apply两次,第二次调用会销毁并创建一个新集群。一切都没有改变,所以这不应该发生。
通常情况下,这没什么问题,只是我经常会看到 terraform 提供者出现很多超时,并且必须重新申请,这对我毫无帮助,因为重新申请会导致集群被销毁并重新创建。
输出terraform apply看起来像这样:
terraform-gke$ terraform apply
data.template_file.gke_values: Refreshing state...
google_container_cluster.cluster: Refreshing state... (ID: test-eric)
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
+ create
-/+ destroy and then create replacement
Terraform will perform the following actions:
-/+ google_container_cluster.cluster (new resource required)
id: "test-eric" => <computed> (forces new resource)
additional_zones.#: "3" => <computed>
addons_config.#: "1" => <computed>
cluster_autoscaling.#: "0" => <computed>
cluster_ipv4_cidr: "10.20.0.0/14" => <computed>
enable_binary_authorization: "" => <computed>
enable_kubernetes_alpha: "false" => "false"
enable_legacy_abac: "false" => "false"
enable_tpu: "" => <computed>
endpoint: "34.66.113.0" => <computed>
initial_node_count: "1" => "1"
instance_group_urls.#: "0" => <computed>
ip_allocation_policy.#: "0" => <computed>
location: "us-central1" => "us-central1"
logging_service: "logging.googleapis.com" => <computed>
master_auth.#: "1" => "1"
master_auth.0.client_certificate: "" => <computed>
master_auth.0.client_certificate_config.#: "1" => "0" (forces new resource)
master_auth.0.client_key: <sensitive> => <computed> (attribute changed)
Run Code Online (Sandbox Code Playgroud)
因此,归根结底,这是一个提供程序错误,但它是由 1.11.x 和 1.12.x 版本之间的 kubernetes 主服务行为变化引起的,Google 最近刚刚推出该服务作为 GKE 节点的默认设置。它在 Terraform Google 提供商的 Github 问题中被捕获为#3369。
解决方法是告诉 Terraform 忽略master_auth和中的更改network:
resource google_container_cluster cluster {
master_auth {
username = ""
password = ""
}
# Workaround for issue 3369 (until provider version 3.0.0?)
# This is necessary when using GKE node version 1.12.x or later.
# It is possible to make GKE use node version 1.11.x as an
# alternative workaround.
lifecycle {
ignore_changes = [ "master_auth", "network" ]
}
}
Run Code Online (Sandbox Code Playgroud)
注意,也许是为了帮助其他人解决同样的问题...很难在网络和 Github 等地方搜索此类问题的相关答案,因为作者使用许多不同的术语来描述 Terraform 所表现出的行为。此问题有时也被描述为 Terraform 幂等性和 Terraform 更改的问题。
| 归档时间: |
|
| 查看次数: |
3512 次 |
| 最近记录: |