资讯专栏INFORMATION COLUMN

资源编排工具-UCloud Terraform-搭建一台 web 服务器

ernest.wang / 674人阅读

摘要:此案例使用创建一台服务器基础设施,通过创建一台云主机并在云主机上绑定云硬盘和外网弹性,同时使用外网防火墙来保护云主机的网络安全性。

搭建一台 web 服务器

本篇目录

关键词UHostEIPUDisk

摘要

云主机是构建在云环境的弹性计算资源,是 UCloud 最为核心的服务。有些服务,如弹性 IP、镜像、云硬盘等必须与云主机结合后使用,另一些服务,如数据库、缓存、对象存储等可以和云主机结合共同构建 IT 环境。

此案例使用 Terraform 创建一台 web 服务器基础设施,通过创建一台云主机并在云主机上绑定云硬盘和外网弹性IP,同时使用外网防火墙来保护云主机的网络安全性。

使用 Terraform 来创建云主机可以享有由基础设施即代码 (IaC) 带来的便利。通过编写 HCL 文件,可以快速构建包含基础设施定义和它们之间关联的拓扑,并借助于代码版本管理工具,将基础设施的变更纳入版本控制中。

此案例需要一个可用的 UCloud 帐号,以及确保目标可用区有足够的权限和配额可以创建云主机,EIP 和 UDisk。可以在下方 操作步骤拷贝使用,或克隆 官方仓库 以获取完整的 案例演示代码.

拓扑图

avatar

操作步骤

定义资源

首先创建基础设施代码文件。

该样例中包含:

一个 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

可通过控制台确认资源已创建完成。


文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/126508.html

相关文章

  • IaC 自动化配置与编排神器 - Terraform 深度解析

    摘要:而对于依赖关系的抽象,业界最通行的做法即使用有向无环图,来描述事务间的依赖关系。图表并行遍历,执行资源动作从根节点开始,并行地去编排整个资源拓扑,遍历整个有向无环图,直到所有资源都被成功编排,并执行清理操作。前言Terraform 是 Hashicorp 公司开源的一种多云资源编排工具。使用者通过一种特定的配置语言(HCL Hashicorp Configuration Language)来...

    Tecode 评论0 收藏0
  • 多云资源编排工具-UCloud Terraform

    摘要:是基于公司开源的实现的多云资源编排工具,用户可以通过编写规格文件,实现对基础设施的自动化管理。资源编排工具将资源的状态描述为一个状态的集合,并支持若干种不同类型的状态存储。UCloud Terraform 是基于 Hashicorp 公司开源的Terraform实现的多云资源编排工具,用户可以通过编写 HCL(Hashicorp Configuration Language) 规格文件,实现...

    ernest.wang 评论0 收藏0
  • UCloud 资源编排工具与Chef,Puppet,Ansible对比

    摘要:使用资源编排工具的功能,可以与配置管理工具有机地结合在一起。资源编排工具基于公司开源的工具,使用简单且统一的语法,几乎可以管理任何资源而无需学习新的工具。与其它工具的对比本篇目录配置管理工具(如 Chef,Puppet,Ansible 等)友商的资源编排系统,如 AWS CloudFormation,阿里 ROS基于 API/SDK 自行研发配置管理工具(如 Chef,Puppet,Ansi...

    ernest.wang 评论0 收藏0
  • 多云资源编排工具-API GW 是否可以抗住 Terraform 高并发的调用场景?

    摘要:多云资源编排工具是否可以抗住高并发的调用场景目前默认的最大并发数是,不会由单个用户同时发起过多的并发连接,所以降低了业务间锁竞争的风险,可以支持更多资源同时编排。多云资源编排工具-APIGW是否可以抗住Terraform高并发的调用场景?目前Terraform默认的最大并发数是10,不会由单个用户同时发起过多的并发连接,所以降低了业务间锁竞争的风险,可以支持更多资源同时编排。是否所有的可用区...

    ernest.wang 评论0 收藏0
  • 云命令行(CloudShell)-什么是云命令行

    摘要:产品概述云命令行是版的命令行工具,目的是为了方便用户管理云服务。不同账户的虚拟机相互隔离,保障安全。预装命令工具链产品即开即用,无须配置账户信息。编程语言常用命令等使用限制每个账户同时能打开的会话不超过个。产品概述云命令行(CloudShell)是Web版的命令行工具,目的是为了方便用户管理UCloud云服务。 打开CloudShell产品页面,后台会自动分配一台虚拟机供您免费使用,虚拟机内...

    ernest.wang 评论0 收藏0

发表评论

0条评论

ernest.wang

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<