摘要:私有網(wǎng)絡(luò)下批量部署多臺云主機(jī)本篇目錄摘要摘要拓?fù)鋱D拓?fù)鋱D操作步驟操作步驟參考文獻(xiàn)參考文獻(xiàn)關(guān)鍵詞摘要云主機(jī)是構(gòu)建在云環(huán)境的彈性計(jì)算資源,是最為核心的服務(wù)。
本篇目錄
關(guān)鍵詞:UHost, VPC, Subnet
云主機(jī)是構(gòu)建在云環(huán)境的彈性計(jì)算資源,是 UCloud 最為核心的服務(wù)。有些服務(wù),如彈性 IP、鏡像、云硬盤等必須與云主機(jī)結(jié)合后使用,另一些服務(wù),如數(shù)據(jù)庫、緩存、對象存儲等可以和云主機(jī)結(jié)合共同構(gòu)建 IT 環(huán)境。
此案例使用 Terraform 并行批量創(chuàng)建多臺云主機(jī),并在每一臺云主機(jī)上綁定 VPC, Subnet 用于網(wǎng)絡(luò)隔離。
UCloud 是國內(nèi)最早采用 SDN 技術(shù)的云計(jì)算服務(wù)商,VPC 基于 SDN 技術(shù)構(gòu)建,是屬于用戶的、邏輯隔離的網(wǎng)絡(luò)環(huán)境。在私有網(wǎng)絡(luò)中,可以創(chuàng)建指定網(wǎng)段的 VPC,并在 VPC 中創(chuàng)建子網(wǎng)、自主管理云資源,同時(shí)可通過網(wǎng)絡(luò) ACL 實(shí)現(xiàn)安全防護(hù)。
使用 Terraform 來創(chuàng)建云主機(jī)除了享有由基礎(chǔ)設(shè)施既代碼 (IaC) 帶來的便利外,還可以利用并行資源編排帶來的性能提升,當(dāng)基礎(chǔ)設(shè)施十分龐大和復(fù)雜時(shí),已定義的資源會自動(dòng)被抽象為有向無環(huán)圖 (DAG), 尋找盡可能的并行編排路徑,以達(dá)到較優(yōu)的編排性能。
此案例需要一個(gè)可用的 UCloud 帳號,以及確保目標(biāo)可用區(qū)有足夠的權(quán)限和配額可以創(chuàng)建云主機(jī),VPC 和防火墻??梢栽谙路?span style="-webkit-font-smoothing: antialiased;-webkit-tap-highlight-color: rgba(0, 0, 0, 0);text-size-adjust: none;box-sizing: border-box;color: rgb(44, 62, 80);font-weight: 600">操作步驟中拷貝使用,或克隆 官方倉庫 以獲取完整的 案例演示代碼.
首先創(chuàng)建基礎(chǔ)設(shè)施代碼文件,可從 官方樣例 中獲取全部源碼文件。
一個(gè) variables.tf 文件,用于定義輸入?yún)?shù),代碼詳情如下:
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
一個(gè) main.tf 文件,用于建立一個(gè)從云資源到代碼的映射,代碼詳情如下:
# 指定 UCloud Provider 和配置信息 provider "ucloud" { region = var.region } # 查詢默認(rèn)可用區(qū)中的主機(jī)鏡像 data "ucloud_images" "default" { availability_zone = var.zone name_regex = "^CentOS 7.[1-2] 64" image_type = "base" } # 創(chuàng)建 VPC resource "ucloud_vpc" "default" { name = "tf-example-intranet-cluster" tag = "tf-example" # vpc network cidr_blocks = ["192.168.0.0/16"] } # 創(chuàng)建 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 } # 創(chuàng)建內(nèi)網(wǎng)集群 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
在當(dāng)前目錄下執(zhí)行 terraform plan 命令,查看編排計(jì)劃:
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
可以看到即將創(chuàng)建三臺云主機(jī)、一個(gè) VPC,一個(gè) Subnet。
執(zhí)行 terraform apply 命令并確認(rèn),執(zhí)行編排計(jì)劃:
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
可通過 控制臺 確認(rèn)資源已創(chuàng)建完成。
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/126509.html