0

我有一台服务器用于 consul 集群的自我修复和自动缩放。它通过由 consul 监视和运行状况检查运行的 terraform 脚本来做到这一点。

我想为故障转移添加一个额外的备份 terraform 服务器。为此,我必须在我的服务器之间共享terraform.tfstateterraform.tfstate.backup,以便它们可以在相同的资源上运行 terraform。我想使用Terraform "COMMAND: REMOTE CONFIG"共享这些文件,但对我来说,我不清楚如何开始共享。

基本上我希望terraform.tfstateterraform.tfstate.backup文件在两台服务器上不断同步。这是我设置它的尝试。请注意,两个 terraform 服务器都在运行连接到我的 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“命令:远程配置”而不是像 glusterfs 之类的普通文件共享系统同步。

如何以这种方式正确同步我的 terraform 文件?

4

1 回答 1

1

所以是的@Martin Atkins做对了,我只需要使用 sudo运行文档中的示例。使用terraform remote config.tfstate 文件将存储在.terraform/包含 terraform 脚本的目录中的隐藏目录中。

工作原理terraform remote config是它在 consul 中创建一个包含 tfstate 文件详细信息的键值。

答案非常接近文档中列出的内容。在实践中使用terraform remote config是一个 3 步过程。

在运行 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
于 2016-07-08T07:49:45.970 回答