文件共享 - 如何与领事一起设置 Terraform "COMMAND: REMOTE CONFIG"



我有一台服务器用于 consul 集群的自我修复和自动扩展。 它通过使用由领事监视和运行状况检查运行的地形脚本来实现此目的。

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

基本上,我希望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 之类的普通文件共享系统。

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

所以

是的,@Martin阿特金斯做对了,我只需要使用 sudo 运行文档中的示例。使用该terraform remote config会将 .tfstate 文件存储在隐藏目录中.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

最新更新