第一章:Terraform 简介¶
什么是 Terraform?¶
Terraform 是 HashiCorp 公司开发的开源基础设施即代码(Infrastructure as Code,IaC)工具,用于安全高效地构建、更改和版本控制基础设施。
核心特性¶
- 基础设施即代码 - 使用声明式配置文件管理基础设施
- 执行计划 - 生成可视化的变更计划
- 资源图谱 - 构建资源依赖关系图
- 变更自动化 - 自动化复杂的变更操作
- 多云支持 - 支持所有主流云平台
Terraform 架构¶
┌─────────────────────────────────────────────────────────────────────┐
│ Terraform 工作流程 │
│ │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ 编写配置 │────▶│ 执行计划 │────▶│ 应用变更 │ │
│ │ (.tf 文件) │ │ (Plan) │ │ (Apply) │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
│ │ │ │ │
│ │ │ │ │
│ ▼ ▼ ▼ │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ 初始化 │ │ 状态管理 │ │ 输出结果 │ │
│ │ (Init) │ │ (State) │ │ (Output) │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
└─────────────────────────────────────────────────────────────────────┘
│
│ Providers
▼
┌─────────────────────────────────────────────────────────────────────┐
│ 云平台 / 服务 │
│ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ │
│ │ AWS │ │ Azure │ │ GCP │ │阿里云 │ │ Kubernetes│ │
│ └─────────┘ └─────────┘ └─────────┘ └─────────┘ └─────────┘ │
└─────────────────────────────────────────────────────────────────────┘
核心概念¶
Provider(提供者)¶
Provider 是 Terraform 与云平台 API 交互的插件:
provider "aws" {
region = "us-east-1"
}
provider "azurerm" {
features {}
}
provider "google" {
project = "my-project"
region = "us-central1"
}
Resource(资源)¶
Resource 是基础设施的组件:
resource "aws_instance" "web" {
ami = "ami-12345678"
instance_type = "t3.micro"
tags = {
Name = "Web Server"
}
}
Data Source(数据源)¶
Data Source 用于查询已存在的基础设施:
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
}
owners = ["099720109477"]
}
Variable(变量)¶
Variable 用于参数化配置:
Output(输出)¶
Output 用于输出信息:
output "instance_ip" {
description = "Public IP of the instance"
value = aws_instance.web.public_ip
}
Module(模块)¶
Module 是可复用的配置单元:
State(状态)¶
State 记录基础设施的当前状态:
# 本地状态
terraform.tfstate
# 远程状态(推荐)
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "prod/terraform.tfstate"
region = "us-east-1"
}
}
安装 Terraform¶
方式一:二进制安装¶
# 下载
wget https://releases.hashicorp.com/terraform/1.6.0/terraform_1.6.0_linux_amd64.zip
# 解压
unzip terraform_1.6.0_linux_amd64.zip
# 安装
sudo mv terraform /usr/local/bin/
# 验证
terraform version
方式二:包管理器¶
# macOS
brew install terraform
# Ubuntu/Debian
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install terraform
# Windows
choco install terraform
# CentOS/RHEL
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
sudo yum -y install terraform
方式三:Docker¶
# 使用官方镜像
docker run --rm -v $(pwd):/workspace -w /workspace hashicorp/terraform:latest version
# 创建别名
alias terraform='docker run --rm -v $(pwd):/workspace -w /workspace hashicorp/terraform:latest'
基本命令¶
初始化¶
# 初始化工作目录
terraform init
# 升级 Provider
terraform init -upgrade
# 指定插件目录
terraform init -plugin-dir=/path/to/plugins
验证和格式化¶
计划和应用¶
# 生成执行计划
terraform plan
# 保存计划
terraform plan -out=tfplan
# 应用计划
terraform apply
# 应用保存的计划
terraform apply tfplan
# 自动批准
terraform apply -auto-approve
销毁¶
# 销毁所有资源
terraform destroy
# 销毁指定资源
terraform destroy -target=aws_instance.web
# 自动批准
terraform destroy -auto-approve
状态管理¶
# 查看状态
terraform show
# 列出资源
terraform state list
# 查看资源详情
terraform state show aws_instance.web
# 移动资源
terraform state mv aws_instance.web aws_instance.web_new
# 删除资源
terraform state rm aws_instance.web
# 导入资源
terraform import aws_instance.web i-12345678
快速开始¶
# 1. 创建配置文件
cat > main.tf << 'EOF'
provider "docker" {}
resource "docker_image" "nginx" {
name = "nginx:latest"
}
resource "docker_container" "nginx" {
image = docker_image.nginx.image_id
name = "nginx"
ports {
internal = 80
external = 8000
}
}
EOF
# 2. 初始化
terraform init
# 3. 验证配置
terraform validate
# 4. 查看计划
terraform plan
# 5. 应用配置
terraform apply
# 6. 验证
curl http://localhost:8000
# 7. 销毁资源
terraform destroy
Terraform vs 其他工具¶
| 特性 | Terraform | CloudFormation | Pulumi | Ansible |
|---|---|---|---|---|
| 语言 | HCL | YAML/JSON | Python/TS/Go | YAML |
| 云支持 | 多云 | AWS | 多云 | 多云 |
| 状态管理 | 支持 | 支持 | 支持 | 无 |
| 变更计划 | 支持 | 支持 | 支持 | 无 |
| 配置管理 | 有限 | 有限 | 有限 | 强大 |
小结¶
本章学习了:
- ✅ Terraform 概念和特性
- ✅ Terraform 架构和组件
- ✅ 核心概念
- ✅ 安装方法
- ✅ 基本命令
下一章¶
第二章:Terraform 安装与配置 - 学习详细配置和最佳实践。