2016-07-07 57 views
0

我有一台服务器用于自我修复和自动缩放consul集群。它通过由consul手表和健康检查运行的terraform脚本完成此操作。如何与领事设置Terraform“COMMAND:REMOTE CONFIG”

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

基本上我希望terraform.tfstateterraform.tfstate.backup文件在两台服务器上不断同步。这是我设置这个的尝试。注意,这两个terraform服务器运行连接到我的领事群集的其余部分领事客户:

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" 

然而,这显然是错误的语法。当试图运行提供链接的文档的领事例子,我收到了以下的输出:

[email protected]:/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文件?

+0

该错误消息表明您g远程配置命令足够正确,Terraform试图启用远程状态,但无论您正在运行'terraform'的用户都没有写权限来创建或更新本地文件系统中的'terraform.tfstate.backup'文件。 –

回答

0

所以是的@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文件出来以改变关键值:

terraform remote config \ 
-backend=consul \ 
-backend-config="address=localhost:8500" \ 
-backend-config="path=tfstate" \ 
push