摘要:私有網絡下批量部署多臺云主機本篇目錄摘要摘要拓撲圖拓撲圖操作步驟操作步驟參考文獻參考文獻關鍵詞摘要云主機是構建在云環境的彈性計算資源,是最為核心的服務。
本篇目錄
關鍵詞:UHost, VPC, Subnet
云主機是構建在云環境的彈性計算資源,是 UCloud 最為核心的服務。有些服務,如彈性 IP、鏡像、云硬盤等必須與云主機結合后使用,另一些服務,如數據庫、緩存、對象存儲等可以和云主機結合共同構建 IT 環境。
此案例使用 Terraform 并行批量創建多臺云主機,并在每一臺云主機上綁定 VPC, Subnet 用于網絡隔離。
UCloud 是國內最早采用 SDN 技術的云計算服務商,VPC 基于 SDN 技術構建,是屬于用戶的、邏輯隔離的網絡環境。在私有網絡中,可以創建指定網段的 VPC,并在 VPC 中創建子網、自主管理云資源,同時可通過網絡 ACL 實現安全防護。
使用 Terraform 來創建云主機除了享有由基礎設施既代碼 (IaC) 帶來的便利外,還可以利用并行資源編排帶來的性能提升,當基礎設施十分龐大和復雜時,已定義的資源會自動被抽象為有向無環圖 (DAG), 尋找盡可能的并行編排路徑,以達到較優的編排性能。
此案例需要一個可用的 UCloud 帳號,以及確保目標可用區有足夠的權限和配額可以創建云主機,VPC 和防火墻。可以在下方操作步驟中拷貝使用,或克隆 官方倉庫 以獲取完整的 案例演示代碼.
首先創建基礎設施代碼文件,可從 官方樣例 中獲取全部源碼文件。
一個 variables.tf 文件,用于定義輸入參數,代碼詳情如下:
variable "region" { default = "cn-bj2" } variable "zone" { default = "cn-bj2-05" } variable "instance_password" { default = "ucloud_2020" } variable "instance_count" { default = 3 } variable "count_format" { default = "%02d" }CopyErrorSuccess
一個 main.tf 文件,用于建立一個從云資源到代碼的映射,代碼詳情如下:
# 指定 UCloud Provider 和配置信息 provider "ucloud" { region = var.region } # 查詢默認可用區中的主機鏡像 data "ucloud_images" "default" { availability_zone = var.zone name_regex = "^CentOS 7.[1-2] 64" image_type = "base" } # 創建 VPC resource "ucloud_vpc" "default" { name = "tf-example-intranet-cluster" tag = "tf-example" # vpc network cidr_blocks = ["192.168.0.0/16"] } # 創建 Subnet 到 VPC 下 resource "ucloud_subnet" "default" { name = "tf-example-intranet-cluster" tag = "tf-example" # subnet's network must be contained by vpc network # and a subnet must have least 8 ip addresses in it (netmask < 30). cidr_block = "192.168.1.0/24" vpc_id = ucloud_vpc.default.id } # 創建內網集群 resource "ucloud_instance" "intranet" { count = "${var.instance_count}" availability_zone = var.zone image_id = data.ucloud_images.default.images[0].id instance_type = "n-basic-2" root_password = var.instance_password boot_disk_type = "cloud_ssd" # we will put all the instances into same vpc and subnet, # so they can communicate with each other. vpc_id = ucloud_vpc.default.id subnet_id = ucloud_subnet.default.id name = "tf-example-intranet-cluster-${format(var.count_format, count.index + 1)}" tag = "tf-example" }CopyErrorSuccess
在當前目錄下執行 terraform plan 命令,查看編排計劃:
Refreshing Terraform state in-memory prior to plan... The refreshed state will be used to calculate this plan, but will not be persisted to local or remote state storage. data.ucloud_zones.default: Refreshing state... data.ucloud_images.default: Refreshing state... ------------------------------------------------------------------------ An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: + ucloud_instance.intranet[0] id: <computed> auto_renew: <computed> availability_zone: "cn-bj2-02" boot_disk_size: <computed> boot_disk_type: <computed> charge_type: "month" cpu: <computed> create_time: <computed> data_disk_size: <computed> data_disk_type: <computed> disk_set.#: <computed> expire_time: <computed> image_id: "uimage-f1chxn" instance_type: "n-basic-2" ip_set.#: <computed> memory: <computed> name: "tf-example-intranet-cluster-01" private_ip: <computed> remark: <computed> root_password: <sensitive> security_group: <computed> status: <computed> subnet_id: "${ucloud_subnet.default.id}" tag: "tf-example" vpc_id: "${ucloud_vpc.default.id}" + ucloud_instance.intranet[1] id: <computed> auto_renew: <computed> availability_zone: "cn-bj2-02" boot_disk_size: <computed> boot_disk_type: <computed> charge_type: "month" cpu: <computed> create_time: <computed> data_disk_size: <computed> data_disk_type: <computed> disk_set.#: <computed> expire_time: <computed> image_id: "uimage-f1chxn" instance_type: "n-basic-2" ip_set.#: <computed> memory: <computed> name: "tf-example-intranet-cluster-02" private_ip: <computed> remark: <computed> root_password: <sensitive> security_group: <computed> status: <computed> subnet_id: "${ucloud_subnet.default.id}" tag: "tf-example" vpc_id: "${ucloud_vpc.default.id}" + ucloud_instance.intranet[2] id: <computed> auto_renew: <computed> availability_zone: "cn-bj2-02" boot_disk_size: <computed> boot_disk_type: <computed> charge_type: "month" cpu: <computed> create_time: <computed> data_disk_size: <computed> data_disk_type: <computed> disk_set.#: <computed> expire_time: <computed> image_id: "uimage-f1chxn" instance_type: "n-basic-2" ip_set.#: <computed> memory: <computed> name: "tf-example-intranet-cluster-03" private_ip: <computed> remark: <computed> root_password: <sensitive> security_group: <computed> status: <computed> subnet_id: "${ucloud_subnet.default.id}" tag: "tf-example" vpc_id: "${ucloud_vpc.default.id}" + ucloud_subnet.default id: <computed> cidr_block: "192.168.1.0/24" create_time: <computed> name: "tf-example-intranet-cluster" remark: <computed> tag: "tf-example" vpc_id: "${ucloud_vpc.default.id}" + ucloud_vpc.default id: <computed> cidr_blocks.#: "1" cidr_blocks.3901788224: "192.168.0.0/16" create_time: <computed> name: "tf-example-intranet-cluster" network_info.#: <computed> remark: <computed> tag: "tf-example" update_time: <computed> Plan: 5 to add, 0 to change, 0 to destroy. ------------------------------------------------------------------------ Note: You didn't specify an "-out" parameter to save this plan, so Terraform can't guarantee that exactly these actions will be performed if "terraform apply" is subsequently run.CopyErrorSuccess
可以看到即將創建三臺云主機、一個 VPC,一個 Subnet。
執行 terraform apply 命令并確認,執行編排計劃:
Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value: yesCopyErrorSuccess
可通過 控制臺 確認資源已創建完成。
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/126509.html