Terraform簡介
HashiCorp Terraform 是一個IT基礎架構自動化編排工具,可以用代碼來管理維護 IT 資源。Terraform的指令行接口(CLI)提供一種簡單機制,用于将配置檔案部署到阿裡雲或其他任意支援的雲上,并對其進行版本控制。它編寫了描述雲資源拓撲的配置檔案中的基礎結構,例如虛拟機、存儲帳戶和網絡接口。
Terraform是一個高度可擴充的工具,通過 Provider 來支援新的基礎架構。Terraform能夠讓您在阿裡雲上輕松使用 簡單模闆語言 來定義、預覽和部署雲基礎結構。您可以使用Terraform來建立、修改、删除ECS、VPC、RDS、SLB等多種資源。
安裝和配置Terraform
在Cloud Shell中使用Terraform
阿裡雲Cloud Shell是一款幫助您運維的免費産品,預裝了Terraform的元件,并配置好身份憑證(credentials)。是以您可直接在Cloud Shell中運作Terraform的指令。
打開浏覽器,通路Cloud Shell的位址
https://shell.aliyun.com。
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5SM3UzY3UWYkhTZ2MGOlJDOxQTZ0ITY0IzYiNWY0EjM38CX5d2bs92Yl1iclB3bsVmdlR2LcNWaw9CXt92Yu4GZjlGbh5yYjV3Lc9CX6MHc0RHaiojIsJye.png)
在本地安裝和配置Terraform
登入 Terraform官網 下載下傳并安裝适用于您的作業系統的程式包。
指令運作後将顯示可用的Terraform選項的清單,如下所示,表示安裝完成。
username:~$ terraform
Usage: terraform [-version] [-help] <command> [args]
建立環境變量,用于存放身份認證資訊。
export ALICLOUD_ACCESS_KEY="LTAIUrZCw3********"
export ALICLOUD_SECRET_KEY="zfwwWAMWIAiooj14GQ2*************"
export ALICLOUD_REGION="cn-beijing"
編寫terraform腳本
這裡選擇在Cloud Shell中使用Terraform,建立相關目錄:
mkdir /home/shell/terraform_ecs
cd /home/shell/terraform_ecs
terraform腳本如下:
variable "profile" {
default = "default"
}
#Region
variable "region" {
default = "cn-shanghai"
}
#将公鑰拷貝到ECS上
locals {
user_data_ecs = <<TEOF
#!/bin/bash
cp ~/.ssh/authorized_keys /root/.ssh
TEOF
}
provider "alicloud" {
region = var.region
profile = var.profile
}
#VPC
module "vpc" {
source = "alibaba/vpc/alicloud"
region = var.region
profile = var.profile
vpc_name = "ecs_terraform"
vpc_cidr = "10.10.0.0/16"
availability_zones = ["cn-shanghai-b"]
vswitch_cidrs = ["10.10.1.0/24"]
}
#安全組
module "security_group" {
source = "alibaba/security-group/alicloud"
profile = var.profile
region = var.region
vpc_id = module.vpc.this_vpc_id
ingress_cidr_blocks = ["0.0.0.0/0"]
ingress_ports = [22]
ingress_with_cidr_blocks_and_ports = [
{
protocol = "tcp"
priority = 1
description = "ingress for ssh"
}
]
}
#ECS
module "ecs" {
source = "alibaba/ecs-instance/alicloud"
profile = var.profile
region = var.region
internet_max_bandwidth_out = 1
associate_public_ip_address = true
name = "terraform_ecs"
image_id = "centos_7_9_x64_20G_alibase_20201228.vhd"
instance_type = "ecs.t5-c1m2.xlarge" #執行個體規格
vswitch_id = module.vpc.this_vswitch_ids.0
security_group_ids = [module.security_group.this_security_group_id]
system_disk_size = 30
number_of_instances = 3 #執行個體數量
user_data = local.user_data_ecs
}
#設定本地~/.ssh/config的ssh資訊
resource "local_file" "ssh_config" {
content = <<EOF
%{ for ip in module.ecs.this_public_ip }
Host ecs${index(module.ecs.this_public_ip, ip) + 1}
StrictHostKeyChecking no
HostName ${ip}
User terraform
%{ endfor }
EOF
filename = "/home/shell/.ssh/config"
}
#螢幕輸出提示資訊
resource "local_file" "info" {
content = <<EOF
登入伺服器:
%{ for ip in module.ecs.this_public_ip }
ssh root@ecs${index(module.ecs.this_public_ip, ip) + 1}%{ endfor }
公網 IP 位址(用于 ssh 登陸):
%{ for ip in module.ecs.this_public_ip }
ecs${index(module.ecs.this_public_ip, ip) + 1}: ${ip}%{ endfor }
内網 IP 位址(用于叢集内部通信,沒有端口限制):
%{ for ip in module.ecs.this_private_ip }
ecs${index(module.ecs.this_private_ip, ip) + 1}: ${ip}%{ endfor }
銷毀伺服器:
cd /home/shell/terraform_ecs
terraform destroy --auto-approve
EOF
filename = "/home/shell/terraform_ecs/readme.txt"
}
output "伺服器資訊" {
value = <<EOF
登入伺服器:
%{ for ip in module.ecs.this_public_ip }
ssh root@ecs${index(module.ecs.this_public_ip, ip) + 1}%{ endfor }
公網 IP 位址(用于 ssh 登入):
%{ for ip in module.ecs.this_public_ip }
ecs${index(module.ecs.this_public_ip, ip) + 1}: ${ip}%{ endfor }
内網 IP 位址(用于叢集内部通信,沒有端口限制):
%{ for ip in module.ecs.this_private_ip }
ecs${index(module.ecs.this_private_ip, ip) + 1}: ${ip}%{ endfor }
銷毀伺服器:
cd /home/shell/terraform_ecs
terraform destroy --auto-approve
檢視以上資訊:
cat /home/shell/terraform_ecs/readme.txt
EOF
}
運作以下指令啟動ECS:
terraform init #安裝相關module
terraform apply --auto-approve #建立ECS
建立成功後會有如下輸出:
Apply complete! Resources: 11 added, 0 changed, 0 destroyed.
Outputs:
伺服器資訊 =
登入伺服器:
ssh root@ecs1
ssh root@ecs2
ssh root@ecs3
公網 IP 位址(用于 ssh 登入):
ecs1: 47.117.170.15
ecs2: 47.117.172.214
ecs3: 47.117.152.20
内網 IP 位址(用于叢集内部通信,沒有端口限制):
ecs1: 10.10.1.151
ecs2: 10.10.1.152
ecs3: 10.10.1.153
銷毀伺服器:
cd /home/shell/terraform_ecs
terraform destroy --auto-approve
檢視以上資訊:
cat /home/shell/terraform_ecs/readme.txt
檢視建立好的ECS:
登入ECS:
#腳本已經将在Cloud shell的公鑰傳到ECS上了,并且在~/.ssh/config配置了登入資訊
ssh root@ecs1
官方文檔:
https://registry.terraform.io/providers/aliyun/alicloud/latest/docs https://github.com/terraform-alicloud-modules