本文的主線 DevOps => IaC => Terraform
DevOps = Dev + Ops
- 現有開發&運維的問題
扔過牆頭(toss it over the wall)
配置漂移(configuration drift)
雪花服務器(snowflake server)
- 開發&運維更親密合作
DevOps不應該只是團隊名稱、職稱或特定技術 更應該代表過程、思想、技術
DevOps的目標是極大地提高軟件交付效率
- DevOps四大核心價值
文化(culture)
自動化(automation)
度量(measurement)
共享(sharing)
- 達成DevOps九大途徑
1 CI(Continuous Integration)
2 CD(Continuous Delivery)
3 Version Control
4 Agile planning and lean project management
5 Monitoring and Logging
6 Public and Hybrid Clouds
7 Infrastructure as Code
8 Microservices
9 Containers
IaC = Infrastructure as Code
- 通過編寫和執行代碼來: 定義、部署、更新和銷燬基礎設施 包括但不限於以下方面
1 服務器
2 數據庫
3 網絡
4 日誌文件
5 應用程序配置
6 文檔
7 自動測試
8 部署過程 等
- IaC七大好處
1 自動服務
2 速度和安全性
3 文檔
4 版本控制
5 驗證
6 重用
7 幸福感
- IaC五大類工具
1 專項腳本 Shell、Python、Ruby
2 配置管理工具 Ansible、SaltStack、Chef、Puppet
3 服務器模板工具 Vagrant、Docker Packer
4 編排工具 Kubernetes、AWS ECS
5 服務開通工具 Terraform、AWS CloudFormation、OpenStack Heat
- IaC工具比較和組合
開源 | 類型 | 基礎設施 | 語言 | 主控 | |
---|---|---|---|---|---|
Ansible | 開源 | 配置管理 | 可變 | 過程性 | 無 |
Terraform | 開源 | 服務開通 | 不可變 | 聲明性 | 無 |
What Is Immutable Infrastructure? = 生產環境中僅通過替換組件而不是修改組件來更改基礎設施
Terraform
brew install terraform
terraform -v
# Terraform v0.14.2
export ALICLOUD_ACCESS_KEY="*"
export ALICLOUD_SECRET_KEY="*"
vim main.tf
provider "alicloud" {
profile = "terraform"
region = "cn-hangzhou"
}
terraform init
vim main.tf
# 省略了未修改代碼
resource "alicloud_vpc" "vpc" {
name = "tf_vpc"
cidr_block = "172.16.0.0/12"
}
resource "alicloud_vswitch" "vsw" {
name = "tf_vsw"
vpc_id = alicloud_vpc.vpc.id
cidr_block = "172.16.0.0/21"
availability_zone = "cn-hangzhou-f"
}
resource "alicloud_security_group" "default" {
name = "default"
vpc_id = alicloud_vpc.vpc.id
}
resource "alicloud_security_group_rule" "allow_all_tcp" {
type = "ingress"
ip_protocol = "tcp"
nic_type = "intranet"
policy = "accept"
port_range = "1/65535"
priority = 1
security_group_id = alicloud_security_group.default.id
cidr_ip = "0.0.0.0/0"
}
resource "alicloud_instance" "instance" {
availability_zone = "cn-hangzhou-f"
security_groups = alicloud_security_group.default.*.id
instance_type = "ecs.n2.small"
system_disk_category = "cloud_efficiency"
image_id = "ubuntu_18_04_64_20G_alibase_20190624.vhd"
instance_name = "tf_vm"
vswitch_id = alicloud_vswitch.vsw.id
internet_max_bandwidth_out = 10
password = "Hellotf!@#"
}
terraform plan
terraform apply