附註:
- 此教學課程需要存取 Oracle Cloud。若要註冊免費帳戶,請參閱開始使用 Oracle Cloud Infrastructure Free Tier 。
- 它使用 Oracle Cloud Infrastructure 憑證、租用戶及區間的範例值。完成實驗室時,請將這些值取代為您雲端環境特有的值。
設定 CD3 Toolkit 與 CLI 以建立及匯出 Oracle Cloud Infrastructure 資源
簡介
雲端部署設計交付項目 (CD3) 自動化工具套件可讓您輕鬆將 Excel 範本轉換為功能完整的 Terraform 模組,輕鬆建置、匯出及管理 Oracle Cloud Infrastructure (OCI) 資源。
此工具程式也支援使用 OCI DevOps Git 服務和 Jenkins 管線的無縫資源管理。
目標
-
只要按一下即可啟動 CD3 容器資源管理程式堆疊,並使用 CD3 CLI 建立 OCI Networking 和 OCI Compute 資源。
-
使用 CD3 CLI 匯出 OCI Networking 和 OCI Compute 資源。
必要條件
-
Oracle Cloud Infrastructure Identity and Access Management (OCI IAM) 原則,可讓使用者或執行處理主體管理使用工具組建立或匯出所需的服務。
-
部署堆疊的使用者應該可以存取啟動 OCI 資源管理程式堆疊、OCI Compute 執行處理和 OCI 網路資源。
作業 1:設定工具套件容器
-
按一下部署至 Oracle Cloud 以啟動建立 CD3 workVM 的 OCI 資源管理程式堆疊。
-
接受條款與條件。輸入 Network 、 Compartment 、 Virtual Machine (VM) Name 、 Shape 以及 workVM 的其他詳細資訊。
注意:若要維護安全的環境,請提供特定來源 CIDR 範圍以存取 VM。請勿使用
0.0.0.0/0
。 -
核取執行套用段落,然後按一下建立。
-
套用工作成功之後,按一下工作並向下捲動至日誌結尾。
尋找所建立 VM 的詳細資訊,以及登入工具程式容器所要執行的命令。下列影像顯示範例輸出。
登入容器之後,請依照工作 2 並連線至 OCI 租用戶以執行 OCI API。
作業 2:將容器連線至 OCI 租用戶
-
在容器中,瀏覽至
cd /cd3user/oci_tools/cd3_automation_toolkit/user-scripts/
並開啟tenancyconfig.properties
檔案。 -
在必要參數和認證詳細資訊參數段落中新增必要的組態值。
注意:在本教學課程中,我們將使用 API 金鑰認證。將
auth_mechanism
保留為其預設值。 -
前往 OCI 主控台的使用者設定值下,將公開金鑰上傳至 APIkeys 。將私密金鑰放在容器內。複製必要的組態值,並貼到
tenancyconfig.properties
檔案中對應的參數底下。 -
保留
outdir_structure_file
參數的預設值。這會將每個服務在其服務特定資料夾下產生的auto.tfvars
檔案分組。 -
對於 IaC 工具,可選取 terraform 或 tofu 。預設值為 terraform。若要使用 OpenTofu,請指定 tofu 。
注意:請保留 DevOps 的進階參數段落,因為 Jenkins 設定不在本教學課程的範圍內。若要使用 Jenkins 執行工具程式,請參閱使用 Jenkins 設定 CD3 以建立及匯出 Oracle Cloud Infrastructure 資源。
-
儲存檔案並執行
createTenancyConfig.py
以初始化環境,然後開始使用 CD3。python createTenancyConfig.py tenancyconfig.properties
-
驗證輸出。
接著,若要在 OCI 中建立資源,請依照任務 3 和匯出資源,然後依照任務 4 進行操作。
作業 3:建立資源
工作 3.1:準備 Excel 和變數檔案
-
從此處下載預先填入的 Excel 範本: cd3quickstart.xlsx 。根據您的環境修改工作表中的區域和區間值,然後切換回容器。
-
使用下方 scp 命令,在
/cd3user/tenancies/cd3_demo_cli/
的容器內新增 Excel 檔案。注意:CD3 容器中的租用戶資料夾會對應至運算 VM 中的
/cd3user/mount_path
資料夾。使用cd3user
登入以避免權限問題。scp -i ~/.ssh/id_rsa /Users/xyz/cd3quickstart.xlsx cd3user@<workvm_ip>:/cd3user/mount_path/cd3_demo_cli/cd3quickstart.xlsx
-
從容器開啟
/cd3user/tenancies/cd3_demo_cli/terraform_files/<region>/compute/variables_<region>.tf
。在instance_ssh_keys
變數下,新增 Excel 工作表ssh_public_key
中指定之 SSH 金鑰的變數名稱及其對應值 (索引鍵內容)。 -
在
instance_source_ocids
變數下,包含來源影像myimageocid
之 Excel 工作表中指定的變數名稱,並將其 OCID 指派為對應的值。
工作 3.2:執行 setUpOCI.py
命令檔以產生資源 Terraform 檔案
-
開啟
/cd3user/tenancies/cd3_demo_cli/cd3_demo_cli_setUpOCI.properties
檔案,並在cd3file
變數下新增 CD3 excel 檔案路徑。將 workflow_type 變數設定為create_resources
並儲存檔案。 -
將目錄變更為
cd /cd3user/oci_tools/cd3_automation_toolkit/
,然後執行setUpOCI.py
程序檔。python setUpOCI.py /cd3user/tenancies/cd3_demo_cli/cd3_demo_cli_setUpOCI.properties
注意:如果您是第一次執行命令檔,請為執行命令檔以將區間 OCID 擷取至變數檔案提示選取 y 。
-
從輸出功能表選取網路和運算選項,然後選取子選項:建立網路以建立 VCN 和子網路,新增 / 修改 / 刪除執行處理 / 開機備份原則以佈建執行處理。
-
將目錄變更為
/cd3user/tenancies/cd3_demo_cli/terraform_files/<region>/network/
。執行 Terraform 命令以佈建網路資源。terraform init terraform plan -out tfplan terraform apply tfplan
-
建立網路會在 VCN 中建立一些尚未出現在 CD3 Excel 工作表中的規則 (因為這些詳細資訊最初是從子網路頁籤取得)。若要將它們同步至 Excel 檔案,請使用與上述相同的 Excel 工作表再次執行
setUpOCI
指令碼。確定工作流程在cd3_demo_cli_setUpOCI.properties
檔案中設為建立資源。選取主選項下的 [ 網路 ],再選取下方的子選項。Security Rules ---> Export Security Rules (From OCI into SecRulesinOCI sheet), Add/Modify/Delete Security Rules (Reads SecRulesinOCI sheet) Route Rules ---> Export Route Rules (From OCI into RouteRulesinOCI sheet), Add/Modify/Delete Route Rules (Reads RouteRulesinOCI sheet) DRG Route Rules ---> Export DRG Route Rules (From OCI into DRGRouteRulesinOCI sheet), Add/Modify/Delete DRG Route Rules (Reads DRGRouteRulesinOCI sheet)
-
成功完成之後,Excel 範本中的
DRGRouteRulesinOCI
、RouteRulesinOCI
及SecRulesinOCI
頁會填入對應的資料,而資料夾/cd3user/tenancies/cd3_demo_cli/terraform_files/<region_dir>/<service_dir>
下將會產生cd3_demo_cli_seclists.auto.tfvars
、cd3_demo_cli_routetables.auto.tfvars
及cd3_demo_cli_drg-routetables.auto.tfvars
檔案。 -
驗證
terraform plan
和terraform apply
。執行 terraform 套用會完成從 OCI 匯出安全規則、路由規則以及 DRG 路由規則。現在,地形狀態應與 OCI 同步。 -
若要啟動設定運算資源,請將目錄變更為
/cd3user/tenancies/cd3_demo_cli/terraform_files/<region>/compute/
,然後執行 Terraform 命令。terraform init terraform plan -out tfplan terraform apply tfplan
在 OCI 主控台中驗證佈建的資源。
作業 4:匯出資源
作業 4.1:新增 Excel 樣版至貨櫃
-
下載 CD3-Blank-template ,然後將它新增至位於
/cd3user/tenancies/cd3_demo_cli/
的容器。注意:cd3 容器中的租用戶資料夾會對應至運算 VM 中的
/cd3user/mount_path
資料夾。使用cd3user
登入以避免權限問題。 -
導覽至
/cd3user/tenancies/cd3_demo_cli/
資料夾並開啟cd3_demo_cli_setUpOCI.properties
檔案。 -
在變數
cd3file
下新增 CD3 Excel 檔案路徑。 -
將 workflow_type 參數設為
export_resources
並儲存檔案。
工作 4.2:執行 setUpOCI.py
命令檔
-
將目錄變更為
cd /cd3user/oci_tools/cd3_automation_toolkit/
,然後執行setUpOCI.py
程序檔。python setUpOCI.py /cd3user/tenancies/cd3_demo_cli/cd3_demo_cli_setUpOCI.properties
注意:如果您是第一次執行命令檔,請為執行命令檔以將區間 OCID 擷取至變數檔案提示選取 y 。
-
如果您必須從多個區域匯出資源,請提供以逗號分隔的
region
值。如果未提供區域值,工具程式將會從所有訂閱的區域匯出資源。 -
選取要匯出的資源選項 ( 網路和運算 )。您可以選擇只從特定區間或所有區間匯出資源。區間名稱若不是唯一的,應依據階層以下列格式指定。
Parent Compartment::Child Compartment1::Child Compartment2
-
若只要匯出特定資源,請在提示時輸入必要的篩選值。
注意:如需有關匯出 OCI 網路資源和 OCI Compute 資源的詳細資訊,請參閱匯出 OCI 網路資源和匯出 OCI Compute 資源。
-
成功完成後,匯出的資源資料會植入輸入 Excel 檔案中。更新的 Excel 工作表位於容器內的
/cd3user/tenancies/cd3_demo_cli
資料夾下,需要使用 scp 命令手動複製到本機系統。scp -i ~/.ssh/id_rsa cd3user@<workvm_ip>:/cd3user/mount_path/cd3_demo_cli/CD3-Blank-template.xlsx /Users/xyz/CD3-export.xlsx
-
含有 terraform 匯入命令的服務特定
.tfvars
和.sh
檔案會在區域資料夾底下個別的服務資料夾內產生:-
/cd3user/tenancies/cd3_demo_cli/terraform_files/<region_dir>/network/
-
/cd3user/tenancies/cd3_demo_cli/terraform_files/<region_dir>/compute/
若為 OCI Compute,關聯的 ssh 公用金鑰會置於
instance_ssh_keys
變數下的variables_<region>.tf
中,而來源 OCID 則會置於instance_source_ocid
變數底下。 -
作業 4.3:執行產生的 .sh
檔案
-
從 network 與 compute 服務資料夾中,執行每個產生的
.sh
檔案,其中包含 Terraform 匯入指令。注意:對於 OCI Networking,在執行其他
.sh
命令檔之前,請確定為 major_objects 執行.sh
命令檔:sh import_commands_network_major-objects.sh
-
順利完成
.sh
命令檔之後,就會產生 Terraform 狀態檔,並將 terraform 計畫更新為最新版本。
此產生的 Terraform 代碼可與 OCI 資源管理程式搭配使用,或由組織 CI/CD 處理作業運用以進一步管理。
若要新增資源或修改現有資源,請先將資源匯出至 CD3,在匯出的 Excel 範本中進行必要變更,然後在 /cd3user/tenancies/cd3_demo_cli/cd3_demo_cli_setUpOCI.properties
檔案中,將 workflow_type 修改為 create_resources
並執行工具程式。
相關連結
認可
- 作者 - Lasya Vadavalli (資深雲端工程師)
其他學習資源
瀏覽 docs.oracle.com/learn 的其他實驗室,或前往 Oracle Learning YouTube 頻道存取更多免費學習內容。此外,請造訪 education.oracle.com/learning-explorer 以成為 Oracle Learning Explorer。
如需產品文件,請造訪 Oracle Help Center 。
Configure CD3 Toolkit with CLI to Create and Export Oracle Cloud Infrastructure Resources
F87018-04
September 2024