Walrus 入門教程:如何創(chuàng)建模板以沉淀可復(fù)用的團(tuán)隊(duì)最佳實(shí)踐
模板是 Walrus 的核心功能之一,模板創(chuàng)建完成后用戶可以重復(fù)使用,并在使用過程中逐漸沉淀研發(fā)和運(yùn)維團(tuán)隊(duì)的最佳實(shí)踐,進(jìn)一步簡(jiǎn)化服務(wù)及資源的部署。用戶可以使用 HCL 語言自定義創(chuàng)建模板,也可以一鍵復(fù)用 Terraform 社區(qū)中上萬個(gè)成熟的 Module。在本文中,我們將以阿里云 EC2 為例,介紹如何創(chuàng)建 Walrus 阿里云 EC2 模板并使用它在阿里云上創(chuàng)建 ECS 實(shí)例服務(wù)。 ?上創(chuàng)建倉庫?
在 GitHub 上創(chuàng)建你自己的新倉庫,這里我們使用倉庫?demo
將倉庫克隆到你的本機(jī)。
git clone git@github.com:walrus-catalog-demo/demo.git
創(chuàng)建模板文件?
進(jìn)入克隆的倉庫目錄。
cd demo
在目錄中創(chuàng)建如下文件: - demo
- main.tf
- outputs.tf
- variables.tf
- README.md
main.tf?文件定義了要?jiǎng)?chuàng)建的資源,這里我們?yōu)槟0宥x了一個(gè)阿里云 ECS 實(shí)例的資源。 resource "alicloud_instance" "example" {
instance_name = "demo-instance"
instance_type = var.instance_type
image_id = var.image_id
system_disk_category = var.system_disk_category
system_disk_size = var.system_disk_size
internet_charge_type = var.internet_charge_type
internet_max_bandwidth_out = var.internet_max_bandwidth_out
vswitch_id = data.alicloud_vswitches.default.vswitches.0.id
host_name = var.hostname
key_name = "seal-demo"
security_groups = [
data.alicloud_security_groups.default.groups.0.id
]
}
data "alicloud_vpcs" "default" {
name_regex = "default"
}
data "alicloud_vswitches" "default" {
vpc_id = data.alicloud_vpcs.default.vpcs.0.id
}
data "alicloud_security_groups" "default" {
name_regex = "default"
}
resource "null_resource" "health_check" {
depends_on = [
alicloud_instance.example,
]
}
variables.tf?文件定義了模板中使用的變量,Walrus 將使用這些變量為用戶生成模板填寫表單。 Walrus 通過?@label?和?@group?注釋來定義變量的標(biāo)簽和組。 可選的?@options?注釋用于定義變量的下拉選項(xiàng), 如果沒有定義 @options 注釋,則該變量將在表單中顯示為文本框。 關(guān)于模板變量注釋的更多詳細(xì)信息可以在這里查看。 在這個(gè)例子中,我們定義了兩個(gè)組:?基礎(chǔ)?和?高級(jí),它們將在創(chuàng)建服務(wù)的模板表單中以兩個(gè)選項(xiàng)卡的形式顯示。
# @label "實(shí)例規(guī)格"
# @group "基礎(chǔ)"
variable "instance_type" {
description = "The instance type of the ECS instance"
default = "ecs.s6-c1m2.small"
}
# @label "VM鏡像id"
# @group "基礎(chǔ)"
variable "image_id" {
description = "The ID of the image used to launch the ECS instance"
default = "ubuntu_18_04_x64_20G_alibase_20230208.vhd"
}
# @label "系統(tǒng)磁盤類型"
# @group "基礎(chǔ)"
# @options ["ephemeral_ssd", "cloud_efficiency", "cloud_ssd", "cloud_essd", "cloud", "cloud_auto"]
variable "system_disk_category" {
description = "The category of the system disk"
default = "cloud_efficiency"
}
# @label "系統(tǒng)盤大小"
# @group "基礎(chǔ)"
variable "system_disk_size" {
description = "The size of the system disk, value range: [20, 500]"
default = 40
}
# @label "主機(jī)名"
# @group "基礎(chǔ)"
variable "hostname" {
type = string
description = "The hostname of the ECS instance"
default = ""
}
# @label "網(wǎng)絡(luò)計(jì)費(fèi)類型"
# @group "高級(jí)"
# @options ["PayByTraffic", "PayByBandwidth"]
variable "internet_charge_type" {
description = "The billing method of the public network bandwidth"
default = "PayByTraffic"
}
# @label "最大出口帶寬(MB)"
# @group "高級(jí)"
variable "internet_max_bandwidth_out" {
description = "The maximum outbound bandwidth of the public network"
default = 5
}
outputs.tf?文件定義了模板的輸出,它們將在服務(wù)創(chuàng)建后作為服務(wù)的輸出參數(shù)查看,并且可以被其他服務(wù)引用。 output "public_ip" {
value = alicloud_instance.example.public_ip
}
output "primary_ip_address" {
value = alicloud_instance.example.primary_ip_address
}
README.md?文件是模板的描述。在使用此模板創(chuàng)建服務(wù)時(shí),我們可以在模板詳情頁面查看該模板的具體功能參數(shù)及定義。這里我們可以使用工具?terraform-docs來生成模板的描述。 terraform-docs markdown . > README.md
生成的?README.md?文件如下:
## Requirements
No requirements.
## Providers
| Name | Version |
|------|---------|
#provider\_alicloud) | n/a |
#provider\_null) | n/a |
## Modules
No modules.
## Resources
| Name | Type |
|------|------|
| [alicloud_instance.example](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/instance) | resource |
| [null_resource.health_check](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
| [alicloud_security_groups.default](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/data-sources/security_groups) | data source |
| [alicloud_vpcs.default](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/data-sources/vpcs) | data source |
| [alicloud_vswitches.default](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/data-sources/vswitches) | data source |
## Inputs
| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
#input\_hostname) | The hostname of the ECS instance | `string` | `""` | no |
#input\_image\_id) | The ID of the image used to launch the ECS instance | `string` | `"ubuntu_18_04_x64_20G_alibase_20230208.vhd"` | no |
#input\_instance\_type) | The instance type of the ECS instance | `string` | `"ecs.s6-c1m2.small"` | no |
#input\_internet\_charge\_type) | The billing method of the public network bandwidth | `string` | `"PayByTraffic"` | no |
| [internet\_max\_bandwidth\_out](
#input\_internet\_max\_bandwidth\_out) | The maximum outbound bandwidth of the public network | `number` | `5` | no |
#input\_system\_disk\_category) | The category of the system disk | `string` | `"cloud_efficiency"` | no |
#input\_system\_disk\_size) | The size of the system disk, value range: [20, 500] | `number` | `40` | no |
## Outputs
| Name | Description |
|------|-------------|
#output\_primary\_ip\_address) | n/a |
#output\_public\_ip) | n/a |
提交模板版本?
git add .
git commit -m "add template files"
git push -u origin main
為模板創(chuàng)建一個(gè)標(biāo)簽作為版本。 git tag v0.0.1
git push --tags
在 Walrus 上創(chuàng)建模板?
在瀏覽器中打開 Walrus 并登錄。
轉(zhuǎn)到?運(yùn)維中心?下的模板管理,使用我們剛剛創(chuàng)建的模板新建一個(gè)模板,這里我們將模板命名為?aliyun-ec2。??
導(dǎo)入任務(wù)完成后,模板將顯示在模板列表中,我們可以看到模板版本為剛剛創(chuàng)建的版本v0.0.1。?
在?運(yùn)維中心?的?連接器下添加阿里云連接器。
配置阿里云連接器到環(huán)境.
使用模板?aliyun-ec2?創(chuàng)建一個(gè)服務(wù), 表單組和標(biāo)簽的渲染是根據(jù)上述模板中定義的變量注釋自動(dòng)生成的。?
服務(wù)創(chuàng)建后,我們可以看到服務(wù)的詳情和模板的輸出??
在阿里云控制臺(tái)檢查 ECS 實(shí)例,我們可以看到 ECS 實(shí)例已成功創(chuàng)建。?