如何使用 consul 设置 Terraform "COMMAND: REMOTE CONFIG"
How to set up Terraform "COMMAND: REMOTE CONFIG" with consul
我有一台服务器,用于 consul 集群的自我修复和自动缩放。它通过 terraform 脚本执行此操作,这些脚本由 consul watches 和健康检查运行
我想添加一个额外的备份 terraform 服务器用于故障转移。为此,我必须在我的服务器之间共享 terraform.tfstate
和 terraform.tfstate.backup
,以便它们可以在相同的资源上 运行 terraform。我想使用 Terraform "COMMAND: REMOTE CONFIG" 共享这些文件,但我不清楚如何开始共享。
基本上我希望 terraform.tfstate
和 terraform.tfstate.backup
文件在两台服务器上始终保持同步。这是我尝试进行设置的尝试。请注意,两个 terraform 服务器都是 运行ning consul 客户端连接到我的其余 consul 集群:
terraform remote config \
-backend=consul \
-backend-config="address=localhost:8500" \
-backend-config="path=/consul-scripts/terr/terraform.tfstate" \
-backend-config="backup=/consul-scripts/terr/terraform.tfstate.backup" \
-path="/consul-scripts/terr/terraform.tfstate" \
-backup="/consul-scripts/terr/terraform.tfstate.backup" \
-address="localhost:8500"
然而,这显然是错误的语法。当尝试 运行 链接文档中提供的领事示例时,我收到以下输出:
username@hostname:/consul-scripts/terr$ terraform remote config \
> -backend=consul \
> -backend-config="address=localhost:8500" \
> -backend-config="path=/consul-scripts/terr/terraform.tfstate"
Error writing backup state file: open terraform.tfstate.backup: permission denied
我想让我的 Terraform 服务器通过 Terraform "COMMAND: REMOTE CONFIG" 同步,而不是像 glusterfs 之类的普通文件共享系统。
如何以这种方式正确同步我的 Terraform 文件?
是的 @Martin Atkins got it right I just had to run the example in the documentation 使用 sudo。使用 terraform remote config
会将 .tfstate 文件存储在包含 terraform 脚本的目录中的隐藏目录 .terraform/
中。
terraform remote config
的工作原理是它在 consul 中创建一个键值,其中包含 tfstate 文件的详细信息。
答案与文档中列出的内容非常接近。在实践中使用 terraform remote config
是一个 3 步过程。
在 运行ning terraform 之前,以下应该是 运行 拉取当前的 tfstate 文件:
#this will pull the current tfstate file
#if none exists it will create the tfstate key value
terraform remote config \
-backend=consul \
-backend-config="address=localhost:8500" \
-backend-config="path=tfstate" \
pull
然后运行:
terraform apply
完成后运行以下将更新的 tfstate 文件推送到 consul 以更改键值:
terraform remote config \
-backend=consul \
-backend-config="address=localhost:8500" \
-backend-config="path=tfstate" \
push
我有一台服务器,用于 consul 集群的自我修复和自动缩放。它通过 terraform 脚本执行此操作,这些脚本由 consul watches 和健康检查运行
我想添加一个额外的备份 terraform 服务器用于故障转移。为此,我必须在我的服务器之间共享 terraform.tfstate
和 terraform.tfstate.backup
,以便它们可以在相同的资源上 运行 terraform。我想使用 Terraform "COMMAND: REMOTE CONFIG" 共享这些文件,但我不清楚如何开始共享。
基本上我希望 terraform.tfstate
和 terraform.tfstate.backup
文件在两台服务器上始终保持同步。这是我尝试进行设置的尝试。请注意,两个 terraform 服务器都是 运行ning consul 客户端连接到我的其余 consul 集群:
terraform remote config \
-backend=consul \
-backend-config="address=localhost:8500" \
-backend-config="path=/consul-scripts/terr/terraform.tfstate" \
-backend-config="backup=/consul-scripts/terr/terraform.tfstate.backup" \
-path="/consul-scripts/terr/terraform.tfstate" \
-backup="/consul-scripts/terr/terraform.tfstate.backup" \
-address="localhost:8500"
然而,这显然是错误的语法。当尝试 运行 链接文档中提供的领事示例时,我收到以下输出:
username@hostname:/consul-scripts/terr$ terraform remote config \
> -backend=consul \
> -backend-config="address=localhost:8500" \
> -backend-config="path=/consul-scripts/terr/terraform.tfstate"
Error writing backup state file: open terraform.tfstate.backup: permission denied
我想让我的 Terraform 服务器通过 Terraform "COMMAND: REMOTE CONFIG" 同步,而不是像 glusterfs 之类的普通文件共享系统。
如何以这种方式正确同步我的 Terraform 文件?
是的 @Martin Atkins got it right I just had to run the example in the documentation 使用 sudo。使用 terraform remote config
会将 .tfstate 文件存储在包含 terraform 脚本的目录中的隐藏目录 .terraform/
中。
terraform remote config
的工作原理是它在 consul 中创建一个键值,其中包含 tfstate 文件的详细信息。
答案与文档中列出的内容非常接近。在实践中使用 terraform remote config
是一个 3 步过程。
在 运行ning terraform 之前,以下应该是 运行 拉取当前的 tfstate 文件:
#this will pull the current tfstate file
#if none exists it will create the tfstate key value
terraform remote config \
-backend=consul \
-backend-config="address=localhost:8500" \
-backend-config="path=tfstate" \
pull
然后运行:
terraform apply
完成后运行以下将更新的 tfstate 文件推送到 consul 以更改键值:
terraform remote config \
-backend=consul \
-backend-config="address=localhost:8500" \
-backend-config="path=tfstate" \
push