Azure Terraform(五)利用Azure DevOps 實現自動化部署基礎資源

Grant_Allen發表於2021-01-18

一,引言

  上一篇我們結合學習 Azure Traffic Manger 的內容,做了一個負載均衡的基礎設施架構。通過 Terraform 部署執行計劃,將整個 Azure Traffic Manager 結合 Azure Web App 的架構快速部署到雲上。然後再將我們的示例專案程式碼部署到對應的不同區域的Azure Web 應用程式上。最後Azure Traffic Manager 將不同地理位置的使用者的訪問請求轉發到後端的 Azure Web 應用上。

這時,又有人提問了,現在都流行 DevOps ,整個應用層面的專案程式碼都可以實現 CI/CD 整個過程,那這些基礎設施程式碼可以實現 CI/CD 嗎?

答案是肯定的,今天要演示的正如文章標題那樣,利用 Azure DevOps 快速實現自動化部署基礎設施資源。

開始內容之前,我們先看看整個 pipeline 過程

--------------------Azure Terraform 系列--------------------

1,Azure Terraform(一)入門簡介

2,Azure Terraform(二)語法詳解

3,Azure Terraform(三)部署 Web 應用程式

4,Azure Terraform(四)狀態檔案儲存

5,Azure Terraform(五)利用Azure DevOps 實現自動化部署基礎資源

二,正文

1,Azure DevOps 建立專案

Azure DevOps 上建立 "CnBateBlogWeb_Infrastructure" 專案,Azure DevOps 地址:dev.azure.com

2,配置Azure DevOps Pipeline 環境

選擇 “Pipelines =》Releases”,並且點選 “New pipeline” 建立新的 pipeline

選擇模板頁面,我們先選擇 “Empty job” 

修改 "Stage name",並且點選 “x” 進行關閉此頁面

接下來,我們需要給當前 "pipeline" 新增 “artifact”

選擇 “GitHub”,點選 “Service” 旁邊的 “+” ,新增新的 “github connection”

輸入 “Connection name” :“github_connection_yunqian44”,並且點選 “Authorize using OAuth” 進行登陸 github 認證

 

等待驗證完成後,我們需要選擇 terraform 對應的程式碼倉庫,點選圖中的箭頭指向的位置

輸入相應引數

Source(repository): "Terraform_Cnbate_Traffic_Manager"

Default branch(預設倉庫):“remote_stats”

Default version:“Lastest from the default branch”

Source alias 選擇預設後,點選 “Add” 進行新增

接下來就需要我們新增 “task” 了,點選圖中箭頭指向

點選 “Agent Job” 旁白 “+”,並在右邊的輸入框中輸入 “Azure Key Vault”,選中圖中的“Azure Key Vault”。點選 “Add”,新增 task。

修改相關引數

Display name:“Azure Key Vault:Get Storage Access Secret”

Azure subscription 選擇當前自己的訂閱

Key vault 選擇:“cnbate-terraform-kv”

Secrets filter(機密過濾器):“terraform-stste-storage-key”,如果選擇預設 “*”,則下載選定金鑰庫的所有機密

然後新增新的 task,搜尋 “Terraform”,選擇 “Terraform tool install”

修改 Terraform 版本 “0.14.3”

接下來再新增 Terraform 新的 task,選擇 “Terraform”,點選 “Add”

修改相應引數:

Display name:“Terraform:Init”

Command 選擇:“init”

Addition command arguments:”-backend-config="access_key=$(terraform-stste-storage-key)"“ (tf 程式碼中沒有access_key 的配置資訊,所以我們需要在 terraform init 過程中傳遞此引數)

AzureRM backend configuration:

Azure subscription:選擇當前自己的訂閱

Resource group:”Web_Test_TF_RG“

Storage account:”cnbateterraformstorage“

Container:”terraform-state“

Key:”cnbate.terraform.stats“  

設定完 terraform init 相應引數後,我們需要修改 terraform init 的工作目錄,選擇完成後,點選 ”Add“ 進行新增操作

再次新增 ”Terraform“ task,用來配置 生成 terraform 部署計劃

Display name:”Terraform:plan“

Command 選擇:”plan“

Configuration directory: 選擇 terraform 程式碼所在目錄

Azure subscription 選擇當前自己的Azure訂閱

最後一步,再次新增 Terraform 新的 task,選擇 “Terraform”,點選 “Add”

修改相應引數:

Display name:”Terraform:auto-apply“

Command:”validate and apply“

Configuration directory:選擇 terraform 程式碼的工作目錄

Additional command arguments:“-auto-approve”

Azure subscription:選擇當前自己訂閱

修改當前 pipeline 名稱,點選 “Save” 進行儲存

最後,我們需要設定 pipeline 的觸發條件

開啟持續部署觸發,每次在所選儲存庫中發生Git推送時觸發pipeline,接下來新增分支篩選條件

Type:Include,Branch:“remote_stats”,也就是說每當 “remote_stats” 發生git 推送的時候,觸發此 pipeline

設定完畢後,點選 “Save” 進行儲存

 

3,對Service principal 設定Key Vault 訪問策略

具體操作步驟,我就不再演示了,大家可以參考:Azure Kay Vault(一).NET Core Console App 獲取金鑰保管庫中的機密資訊

4,測試 Terraform 自動化部署

回到 terraform 程式碼上,我們提交併且推送新的程式碼到 “remote_stats” 遠端分支上

這個時候,我們回到Azure DevOps 上我們就可以看到,當前 pipeline 已被觸發。

等待稍許時間,我們可以看到當前 pipeline 執行日誌,部署成功

同樣,我們需要登陸Azure Portal 上確認 使用 terraform 管理的資源是否建立成功

檢視Storage Account 的 Bolb 中的 terraform 狀態檔案

 

Bingo,今天的內容就到此結束,下期我們繼續/

最後,大家試驗完,記得刪除剛剛使用 terraform 部署的資源,防止 Auzre 扣費

三,結尾

  今天的內容比較多,基本上全都是需要在Azure DevOps 上進行操作的,大家要多加練習,熟能生巧。本文所分享的內容也存在著很多我自己的一些理解,有理解不到位的,還希望多多包涵,並且指出不足之處。

參考資料:Terraform 官方azurerm 文件

Terraform_Cnbate_Traffic_Manager github:https://github.com/yunqian44/Terraform_Cnbate_Traffic_Manager

作者:Allen 

版權:轉載請在文章明顯位置註明作者及出處。如發現錯誤,歡迎批評指正。

相關文章