Terraform

Terraform 架构和核心概念

什么是 Terraform

resource aws_instance "app" {
  ami = data.aws_ami.ubuntu.id
  instance_type = var.instance_type
  tags = {
    Name = "${var.prefix}-meow"
  }
}

什么是 HCL

HCL2 toolkit: https://github.com/hashicorp/hcl

Terraform 使用范式

Terraform + Ansible

Terraform + Kubernetes,Terraform可以用来管理 K8s

Terraform 架构

config 是期望状态

Core 不直接跟云厂商进行交互,而是通过 provider。

provider 相当于插件.

Provider 获取

https://registry.terraform.io/browse/providers

https://registry.terraform.io/providers/hashicorp/aws/latest/docs

Provider 版本控制

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

Terraform 核心命令

Terraform 基本命令

terraform graph | dot -Tsvg > graph.svg

Terraform init

Terraform plan

Terraform apply

Terraform destroy

Terraform State File

Remote Backend 远端存储

Remote Backend(Terraform cloud)

terraform {
  backend "remote" {
    organization = "devops-camp"
    woraspaces {
      name = "course"
    }
  }
}

Remote Backend(AWS)

terraform {
  backend "s3" {
    bucket = ""
    key = ""
    region = ""
    dynamodb_table = ""
    encrypt = true
  }
}

Remote Backend(Tencent COS)

terraform {
  backend "cso" {
    region = ""
    bucket = ""
    prefix = "terraform/state"
  }
}

关于 State 的其他命令

Terraform Layout(推荐布局)

main.tf

outputs.tf

variables.tf

如何设置 variables?

  1. 通过在命令行配置:terraform apply -var="prefix=values"
  2. 指定配置文件:terraform apply -var-file="testing.tfvars"
  3. 环境变量:export TF_VAR_prefix=values
  4. 定义的默认值
  5. 交互式输入

优先级从上(1最高)到下(5最低)对值进行覆盖。

version.tf

进阶实战

表达式和函数

表达式

函数

Meta Arguments(元参数)

作用域

depends_on

count

for_each

lifecycle

Provisioners

在本地或者远端虚拟机中执行动作

file

将本地文件或目录复制到远端虚拟机

local-exec

在本地执行一段命令

注意平台差异

remote-exec

在远端虚拟机中执行命令

可以是 inline shell 命令,也可以指定 bash 脚本文件(通常配合 file)

思考:如何创建多环境基础设施

方案一:使用目录区分

在远端虚拟机中执行命令

可以是 inline shell 命令,也可以指定 bash 脚本文件(通常配合 file)

继续深入:修改环境怎么办?

需要同时修改 dev 和 testing 的代码

可维护性差、代码复用性差

方案二:使用 Terraform Workspace

$ terraform workspace list # 列出当前 workspace
* default # default 为默认的 workspace

$ terraform workspace new dev # 创建 dev workspace
Created and switched to workspace "dev"!

$ terraform apply -auto-approve # 在 dev workspace 中创建资源

$ terraform workspace new testing # 创建 testing workspace
Created and switched to workspace "testing"!

$ terraform apply -auto-approve # 在 testing workspace 中创建资源

$ terraform workspace list # 列出所有的 workspace
$ terraform destroy -auto-approve # 删除 testing workspace 的资源
$ terraform workspace select dev # 切换到 dev workspace
$ terraform destroy -auto-approve # 删除 dev workspace 的资源

继续深入:workspace VS 目录隔离

Workspace

目录隔离

继续深入:Module + 目录隔离

Module

Module 的类型

继续深入:Module 和代码组织结构

├── modules                                                子模块目录
│ ├── cvm                                                CVM 模块
│ │ ├── main.tf
│ │ ├── outputs.tf
│ │ ├── variables.tf
│ │ └── version.tf
│ └── k3s                                                K3s 模块
│ ├── main.tf
│ ├── outputs.tf
│ └── variables.tf
└── testing                                                testing 环境目录
│ ├── main.tf
│ └── variables.tf
├── dev                                                        dev 环境目录
│ ├── main.tf
│ └── variables.tf
dev/main.tf
module "cvm" {
  source = "../modules/cvm"
  secret_id = var.secret_id
  secret_key = var.secret_key
  password = var.password
}
module "k3s" {
  source = "../modules/k3s"
  public_ip = module.cvm.public_ip
  private_ip = module.cvm.private_ip
}

Demo6:Module + 目录隔离

Module 抽象原则

Terragrunt

Demo:Terragrunt 实践

.
├── deploy
│ ├── department1
│ │ ├── team1
│ │ │ ├── account.hcl
│ │ │ └── cvm
│ │ │ └── terragrunt.hcl
│ │ └── team2
│ │ ├── account.hcl
│ │ └── cvm
│ │ └── terragrunt.hcl
│ ├── department2
│ │ ├── team1
│ │ └── team2
│ └── terragrunt.hcl
├── modules

Terragrunt 建议