2016-07-07 59 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