摘要:此案例使用創建一臺服務器基礎設施,通過創建一臺云主機并在云主機上綁定云硬盤和外網彈性,同時使用外網防火墻來保護云主機的網絡安全性。
本篇目錄
關鍵詞:UHost, EIP, UDisk
云主機是構建在云環境的彈性計算資源,是 UCloud 最為核心的服務。有些服務,如彈性 IP、鏡像、云硬盤等必須與云主機結合后使用,另一些服務,如數據庫、緩存、對象存儲等可以和云主機結合共同構建 IT 環境。
此案例使用 Terraform 創建一臺 web 服務器基礎設施,通過創建一臺云主機并在云主機上綁定云硬盤和外網彈性IP,同時使用外網防火墻來保護云主機的網絡安全性。
使用 Terraform 來創建云主機可以享有由基礎設施即代碼 (IaC) 帶來的便利。通過編寫 HCL 文件,可以快速構建包含基礎設施定義和它們之間關聯的拓撲,并借助于代碼版本管理工具,將基礎設施的變更納入版本控制中。
此案例需要一個可用的 UCloud 帳號,以及確保目標可用區有足夠的權限和配額可以創建云主機,EIP 和 UDisk。可以在下方 操作步驟拷貝使用,或克隆 官方倉庫 以獲取完整的 案例演示代碼.
首先創建基礎設施代碼文件。
該樣例中包含:
一個 variables.tf 文件,用于定義輸入參數,代碼詳情如下:
variable "region" { default = "cn-bj2" } variable "zone" { default = "cn-bj2-05" } variable "instance_password" { default = "ucloud_2020" }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" } # 查詢默認推薦 web 外網防火墻 data "ucloud_security_groups" "default" { type = "recommend_web" } # 創建一臺 web 服務器 resource "ucloud_instance" "web" { availability_zone = var.zone image_id = data.ucloud_images.default.images[0].id instance_type = "n-basic-2" root_password = var.instance_password name = "tf-example-web-server" tag = "tf-example" boot_disk_type = "cloud_ssd" # the default Web Security Group that UCloud recommend to users security_group = data.ucloud_security_groups.default.security_groups[0].id # create cloud data disk attached to instance data_disks { size = 20 type = "cloud_ssd" } delete_disks_with_instance = true } # 創建外網彈性 EIP resource "ucloud_eip" "default" { bandwidth = 2 charge_mode = "bandwidth" name = "tf-example-web-server" tag = "tf-example" internet_type = "bgp" } # EIP 綁定到主機 resource "ucloud_eip_association" "default" { resource_id = ucloud_instance.web.id eip_id = ucloud_eip.default.id }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_images.default: Refreshing state... data.ucloud_security_groups.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_eip.default will be created + resource "ucloud_eip" "default" { + bandwidth = 2 + charge_mode = "bandwidth" + charge_type = (known after apply) + create_time = (known after apply) + expire_time = (known after apply) + id = (known after apply) + internet_type = "bgp" + ip_set = (known after apply) + name = "tf-example-web-server" + public_ip = (known after apply) + remark = (known after apply) + resource = (known after apply) + status = (known after apply) + tag = "tf-example" } # ucloud_eip_association.default will be created + resource "ucloud_eip_association" "default" { + eip_id = (known after apply) + id = (known after apply) + resource_id = (known after apply) + resource_type = (known after apply) } # ucloud_instance.web will be created + resource "ucloud_instance" "web" { + auto_renew = (known after apply) + availability_zone = "cn-bj2-05" + boot_disk_size = (known after apply) + boot_disk_type = "cloud_ssd" + charge_type = (known after apply) + cpu = (known after apply) + cpu_platform = (known after apply) + create_time = (known after apply) + data_disk_size = (known after apply) + data_disk_type = (known after apply) + delete_disks_with_instance = true + disk_set = (known after apply) + expire_time = (known after apply) + id = (known after apply) + image_id = "uimage-ohveag" + instance_type = "n-basic-2" + ip_set = (known after apply) + isolation_group = (known after apply) + memory = (known after apply) + name = "tf-example-web-server" + private_ip = (known after apply) + remark = (known after apply) + root_password = (sensitive value) + security_group = "firewall-h55aem" + status = (known after apply) + subnet_id = (known after apply) + tag = "tf-example" + vpc_id = (known after apply) + data_disks { + size = 20 + type = "cloud_ssd" } } Plan: 3 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
可以看到即將創建一臺云主機、一塊云硬盤、一個彈性 EIP、一個主機和 EIP 之間的綁定關系,以及一個主機與云硬盤之間的掛載關系。
執行 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/126508.html