云生态下的基础架构资源管理利器Terraform

本文涉及的产品
云服务器 ECS,每月免费额度200元 3个月
云服务器ECS,u1 2核4GB 1个月
简介: ## About Terraform Terraform (https://www.terraform.io/)是HashiCorp旗下的一款开源(Go语言开发)的DevOps 基础架构资源管理运维工具,可以看下对应的DevOps工具链: ![image.

About Terraform

Terraform (https://www.terraform.io/) 是HashiCorp旗下的一款开源(Go语言开发)的DevOps 基础架构资源管理运维工具,可以看下对应的DevOps工具链:
image.png

Terraform可以安全高效的构建、更改和合并多个云厂商的各种服务资源,当前支持有阿里云、AWS、微软Azure、Vmware、Google Cloud Platform等多个云厂商云产品的资源创建。
image.png

Write, Plan, and Create Infrastructure as Code

Terraform通过模板配置文件定义所有资源类型(有如主机,OS,存储类型,中间件,网络VPC,SLB,DB,Cache等)和资源的数量、规格类型、资源创建依赖关系,基于资源厂商的OpenAPI快速创建一键创建定义的资源列表,同时也支持资源的一键销毁。

通过模板配置化的方式,我们可以得知整个资源关系,也可以做相应的资源版本化管理,甚至可以把该资源模板直接复制建立多可用区的服务,包括资源的弹性伸缩,可以说在提高运维自动化生产力上是一个绝好的利器。

这里引用@箫竹 一次分享中提供的一个创建阿里云资源的Terraform产品介绍图 :
image.png
最左边栏通过一个main.tf文件(只需要是.tf 文件)定义了ECS(镜像、实例类型)、VPC(CIDR、VPC Name)、OSS资源(ACL、实例Name)信息,通过Terraform 对资源配置参数做解析,调用阿里cloud OpenAPI 进行资源校验于创建,同时把整个资源创建状态化到一个.tf.state文件中,基于该文件则可以得知资源创建的所有信息,包括资源数量调整,规格调整,实例变更都依赖这种非常重要的文件。 具体后续综合一些案例,比如Terraform 实现WordPress的一键自动化搭建,可以更好的理解该产品的执行过程。

下面先讲述下Terraform的安装,模板配置文件的编写,后续再专门写几个案例作为分享。

Install Terraform

Terraform 可以随意部署在任意的C端上,只要可以连通公网即可(Initializing provider plugins and Call Cloud OpenAPI)
通过 https://www.terraform.io/downloads.html 下载对应平台的可执行二进制包,解压出来Executable Binary 放指定目录,配置好相应的PATH则可使用terraform 指令。

wangzhipengs-MacBook-Pro:~ wangzhipeng$ which terraform
/usr/local/bin/terraform
wangzhipengs-MacBook-Pro:~ wangzhipeng$ terraform 
Usage: terraform [--version] [--help] <command> [args]

The available commands for execution are listed below.
The most common, useful commands are shown first, followed by
less common or more advanced commands. If you're just getting
started with Terraform, stick with the common commands. For the
other commands, please read the help and docs before usage.

Common commands:
    apply              Builds or changes infrastructure
    console            Interactive console for Terraform interpolations
    destroy            Destroy Terraform-managed infrastructure
    env                Workspace management
    fmt                Rewrites config files to canonical format
    get                Download and install modules for the configuration
    graph              Create a visual graph of Terraform resources
    import             Import existing infrastructure into Terraform
    init               Initialize a Terraform working directory
    output             Read an output from a state file
    plan               Generate and show an execution plan
    providers          Prints a tree of the providers used in the configuration
    push               Upload this Terraform module to Atlas to run
    refresh            Update local state file against real resources
    show               Inspect Terraform state or plan
    taint              Manually mark a resource for recreation
    untaint            Manually unmark a resource as tainted
    validate           Validates the Terraform files
    version            Prints the Terraform version
    workspace          Workspace management

All other commands:
    debug              Debug output management (experimental)
    force-unlock       Manually unlock the terraform state
    state              Advanced state management

这里需要特别说明一下的是目前Terraform官网的版本只支持阿里cloud比较有效的几个大产品的resource,因此如果要创建阿里cloud多个resource需要引入阿里cloud的provider:
https://github.com/alibaba/terraform-provider/releases
通过下载alicloud的provider binary (bin/terraform-provider-alicloud) 放置到Terraform的安装目录下。

Build Infrastructure

整个架构资源创建的过程,可以分成如下四部分:
image.png
通过一个*.tf 后缀的模板文件,描述指定厂商的所有资源配置信息,比如说创建一个alicloud ECS的模板配置:

# Configure the Alicloud Provider
provider "alicloud" {
  access_key = "${var.access_key}"
  secret_key = "${var.secret_key}"
  region     = "${var.region}"
}

# Create a web server
resource "alicloud_instance" "web" {
  # cn-beijing
  provider          = "alicloud"
  availability_zone = "cn-beijing-a"
  image_id          = "ubuntu_140405_32_40G_cloudinit_20161115.vhd"

  internet_charge_type  = "PayByBandwidth"

  instance_type        = "ecs.n1.medium"
  io_optimized         = "optimized"
  system_disk_category = "cloud_efficiency"
  security_groups      = ["${alicloud_security_group.default.id}"]
  instance_name        = "web"
}

# Create security group
resource "alicloud_security_group" "default" {
  name        = "default"
  provider    = "alicloud"
  description = "default"
}

# Output message
output "ecs instance name" {
  value = "${alicloud_instance.web.instance_name}"
}

output "ecs private ip" {
  value = "${alicloud_instance.web.private_ip}"
}

如上access_key,secret_key 填写创建资源账号的AK,该Config File涵盖了Region,可用区,ECS镜像,带宽计费方式,实例规格,IO类型,存储类型,安全组,实例名称多个信息。
完成如上配置信息后,需要通过terraform init 做cloud provider plugins的初始化操作:

wangzhipengs-MacBook-Pro:terraform_dir wangzhipeng$ terraform init

Initializing provider plugins...
- Checking for available provider plugins on https://releases.hashicorp.com...
- Downloading plugin for provider "alicloud" (0.1.0)...

The following providers do not have any version constraints in configuration,
so the latest version was installed.

To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = "..." constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.

* provider.alicloud: version = "~> 0.1"

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

此时会在当前目录下创建一个.terraform目录,保存有该平台下的provider可执行文件。
上面信息也做了提示,我们可以running "terraform plan" 查看将要创建的资源信息:

wangzhipengs-MacBook-Pro:terraform_dir wangzhipeng$ 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.


------------------------------------------------------------------------

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:

  + alicloud_instance.web
      id:                   <computed>
      allocate_public_ip:   "false"
      availability_zone:    "cn-beijing-a"
      host_name:            <computed>
      image_id:             "ubuntu_140405_32_40G_cloudinit_20161115.vhd"
      instance_name:        "web"
      instance_type:        "ecs.n1.medium"
      internet_charge_type: "PayByBandwidth"
      io_optimized:         "optimized"
      private_ip:           <computed>
      public_ip:            <computed>
      security_groups.#:    <computed>
      status:               <computed>
      subnet_id:            <computed>
      system_disk_category: "cloud_efficiency"
      system_disk_size:     <computed>

  + alicloud_security_group.default
      id:                   <computed>
      description:          "default"
      name:                 "default"


Plan: 2 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.

通过apply可以预览模板对应创建2个资源,已经对应的属性信息:

  • alicloud_instance.web
  • alicloud_security_group.default (该安全组暂时未设置自定义放行规则)
    其中computed标识为暂时无法得知最终创建出来的信息,所以也可以理解成系统默认分配。

接下通过指令terraform apply执行资源创建:

wangzhipengs-MacBook-Pro:terraform_dir wangzhipeng$ terraform apply
alicloud_security_group.default: Creating...
  description: "" => "default"
  name:        "" => "default"
alicloud_security_group.default: Creation complete after 1s (ID: sg-2zec9v8aq2hgb244qrqf)
alicloud_instance.web: Creating...
  allocate_public_ip:         "" => "false"
  availability_zone:          "" => "cn-beijing-a"
  host_name:                  "" => "<computed>"
  image_id:                   "" => "ubuntu_140405_32_40G_cloudinit_20161115.vhd"
  instance_name:              "" => "web"
  instance_type:              "" => "ecs.n1.medium"
  internet_charge_type:       "" => "PayByBandwidth"
  io_optimized:               "" => "optimized"
  private_ip:                 "" => "<computed>"
  public_ip:                  "" => "<computed>"
  security_groups.#:          "" => "1"
  security_groups.2344301974: "" => "sg-2zec9v8aq2hgb244qrqf"
  status:                     "" => "<computed>"
  subnet_id:                  "" => "<computed>"
  system_disk_category:       "" => "cloud_efficiency"
  system_disk_size:           "" => "<computed>"
alicloud_instance.web: Still creating... (10s elapsed)
alicloud_instance.web: Still creating... (20s elapsed)
alicloud_instance.web: Still creating... (30s elapsed)
alicloud_instance.web: Still creating... (40s elapsed)
alicloud_instance.web: Still creating... (50s elapsed)
alicloud_instance.web: Creation complete after 51s (ID: i-2zedvfowy4m39sg1xdig)

Apply complete! Resources: 2 added, 0 changed, 0 destroyed.

Outputs:

ecs instance name = web
ecs private ip = 10.31.28.93

分钟级功夫该ECS就创建好了
image.png

Input/Output Variables

回顾上面通过Terraform 创建资源的过程,最关键的还是如何描述模板配置信息,这里主要说明一下参数配置2个输入输出的变量实现。

# Configure the Alicloud Provider
provider "alicloud" {
  access_key = "${var.access_key}"
  secret_key = "${var.secret_key}"
  region     = "${var.region}"
}

我们可以在当前文件,但最好另创建一个*.tf后缀结尾文件,存放对应的ak和region信息,这样我们就可以把一些变量信息提取出来避免硬编码,这对整个配置文件具备更好的可维护性:

wangzhipengs-MacBook-Pro:terraform_dir wangzhipeng$ cat variable.tf
variable "access_key" {
  default = "xxx"
}
variable "secret_key" {
  default = "xxx"
}
variable "region" {
  default = "cn-beijing"
}

此外,如上配置指定了output的变量信息,本身如果不指定output的内容也不影响整个资源创建的过程,但通过输出output的信息可以彻底脱离控制台,对于如下的output变量信息也可以存放在另一个*.tf后缀文件,对于resource的输出变量可以参阅每个provider resource的attribute信息。

# Output message
output "ecs instance name" {
  value = "${alicloud_instance.web.instance_name}"
}

output "ecs private ip" {
  value = "${alicloud_instance.web.private_ip}"
}

Destroy Infrastructure

资源使用后如果想要释放,以往通过控制台,往往需要存在资源依赖,导致资源释放的时候需要先释放被依赖的资源,所以整个资源释放的过程还是比较繁琐的,甚至可能存在忘记释放个别资源造成资源浪费,Terraform通过指令terraform destroy 可以一键释放资源:

wangzhipengs-MacBook-Pro:terraform_dir wangzhipeng$ terraform destroy
alicloud_security_group.default: Refreshing state... (ID: sg-2zec9v8aq2hgb244qrqf)
alicloud_instance.web: Refreshing state... (ID: i-2zedvfowy4m39sg1xdig)

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  - destroy

Terraform will perform the following actions:

  - alicloud_instance.web

  - alicloud_security_group.default


Plan: 0 to add, 0 to change, 2 to destroy.

Do you really want to destroy?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: yes

alicloud_instance.web: Destroying... (ID: i-2zedvfowy4m39sg1xdig)
alicloud_instance.web: Still destroying... (ID: i-2zedvfowy4m39sg1xdig, 10s elapsed)
alicloud_instance.web: Destruction complete after 11s
alicloud_security_group.default: Destroying... (ID: sg-2zec9v8aq2hgb244qrqf)
alicloud_security_group.default: Destruction complete after 1s

Destroy complete! Resources: 2 destroyed.
目录
相关文章
|
4月前
|
弹性计算 运维 监控
带你读《云上自动化运维宝典》——多云基础设施的统一纳管与运维实践分享(2)
带你读《云上自动化运维宝典》——多云基础设施的统一纳管与运维实践分享(2)
106 1
|
2月前
|
Kubernetes API 数据库
Crossplane - 比 Terraform 更先进的云基础架构管理平台?
Crossplane - 比 Terraform 更先进的云基础架构管理平台?
|
弹性计算 运维 关系型数据库
云上部署环境(基础设施)的正确姿势——使用资源编排ROS进行基础设施的部署
![image.png](https://ata2-img.cn-hangzhou.oss-pub.aliyun-inc.com/66daa0b176da97de1dc8e66580fc0384.png) # 常见的部署问题 1. 多环境部署。根据实际开发的需要,一般至少需要准备3个环境:日常测试环境,部署预发环境,部署生产环境。 2. 多地域部署。对于云产品还需要支持多地域部署
1711 0
|
2月前
|
弹性计算 运维 监控
多云基础设施的统一纳管与运维实践分享
阿里云弹性计算团队十三位产品专家和技术专家共同分享云上运维深度实践,详细阐述如何利用CloudOps工具实现运维提效、弹性降本。
116 1
|
4月前
|
弹性计算 运维 监控
带你读《云上自动化运维宝典》——多云基础设施的统一纳管与运维实践分享(3)
带你读《云上自动化运维宝典》——多云基础设施的统一纳管与运维实践分享(3)
|
4月前
|
弹性计算 运维 监控
带你读《云上自动化运维宝典》——多云基础设施的统一纳管与运维实践分享(1)
带你读《云上自动化运维宝典》——多云基础设施的统一纳管与运维实践分享(1)
125 1
|
11月前
|
运维 Kubernetes 容灾
基于 Rainbond 的混合云管理解决方案
内容概要:文章探讨了混合云场景中的难点、要点,以及Rainbond平台在跨云平台的混合云管理方面的解决方案。包括通过通过统一控制台对多集群中的容器进行编排和管理,实现了对混合云中应用的一致性管理。文章还介绍了Rainbond平台在混合云环境下的应用模板交付、跨云团队管理等功能,帮助用户简化跨云平台的应用交付和运维操作。
|
2天前
|
弹性计算 运维 负载均衡
【Terraform实现云服务基础设施既代码(IaC)案例实战-2】:通过Terraform实施基础IaC
前言树苗科技公司业务上云2年有余,随着技术的不断发展。运维团队支持业务变更配置、开资源、删资源等过程加班到深夜一两点已是家常便饭。为了进一步提升工作效率,运维总监张三决定对当前业务交付路径进行统一梳理,借助IaC的理念和Terraform自动化工具逐步实施运维生产自动化,改善组内成员频繁加班的现状。...
【Terraform实现云服务基础设施既代码(IaC)案例实战-2】:通过Terraform实施基础IaC
|
弹性计算 架构师 安全
基础设施自动化实践——用Terraform自动化构建云上环境
前言在我们平时的开发工作中,经常会遇到需要搭建各式各样的云上环境,比如项目开发环境、测试环境、部署环境、客户展示环境等。而有时这些搭建过程并不轻松,如可能涉及很多台ECS,可能对镜像或配置有特殊要求,可能涉及较为复杂的网络配置等。而对于这些较为复杂多变的环境部署要求,传统的人工手动配置方法,不仅耗时费力,还有一个很大的问题就是难以追踪管理、难以复用。比如负责搭建环境的架构师如果休假了,团队其他人很
546 0
基础设施自动化实践——用Terraform自动化构建云上环境
|
运维 Kubernetes Cloud Native
混合云环境云原生应用交付和管理平台
云原生生态里已经出现了大量的优秀实践和技术,都可以给我们提供生产力,但各种方案如何有效的组合,更简单地呈现给广大的开发者,变成了一个课题。
混合云环境云原生应用交付和管理平台