【问题标题】:How to set up Terraform "COMMAND: REMOTE CONFIG" with consul如何使用 consul 设置 Terraform "COMMAND: REMOTE CONFIG"
【发布时间】:2016-11-10 08:55:11
【问题描述】:

我有一台服务器,用于对 consul 集群进行自我修复和自动扩展。它通过 consul watch 和运行状况检查运行的 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 “COMMAND: REMOTE CONFIG”而不是像 glusterfs 之类的普通文件共享系统同步。

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

【问题讨论】:

  • 该错误消息表明您的远程配置命令足够正确,以至于 Terraform 尝试启用远程状态,但是无论您正在运行 terraform 的任何用户都没有创建或更新terraform.tfstate.backup 本地文件系统中的文件。

标签: file-sharing consul terraform


【解决方案1】:

所以是的 @Martin Atkins 做对了,我只需要使用 sudo 运行 documentation 中的示例。使用 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-06-29
    • 2021-12-12
    • 2018-12-23
    • 1970-01-01
    • 2016-02-18
    • 1970-01-01
    • 2019-12-23
    • 1970-01-01
    • 2023-01-14
    相关资源
    最近更新 更多