溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Terraform然后一鍵部署ECS

發(fā)布時間:2021-07-16 01:42:14 來源:億速云 閱讀:222 作者:chen 欄目:云計算

這篇文章主要講解了“Terraform然后一鍵部署ECS”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Terraform然后一鍵部署ECS”吧!

Terraform簡介

HashiCorp Terraform 是一個IT基礎(chǔ)架構(gòu)自動化編排工具,可以用代碼來管理維護(hù) IT 資源。Terraform的命令行接口(CLI)提供一種簡單機(jī)制,用于將配置文件部署到阿里云或其他任意支持的云上,并對其進(jìn)行版本控制。它編寫了描述云資源拓?fù)涞呐渲梦募械幕A(chǔ)結(jié)構(gòu),例如虛擬機(jī)、存儲帳戶和網(wǎng)絡(luò)接口。

Terraform是一個高度可擴(kuò)展的工具,通過 Provider 來支持新的基礎(chǔ)架構(gòu)。Terraform能夠讓您在阿里云上輕松使用 簡單模板語言 來定義、預(yù)覽和部署云基礎(chǔ)結(jié)構(gòu)。您可以使用Terraform來創(chuàng)建、修改、刪除ECS、VPC、RDS、SLB等多種資源。

安裝和配置Terraform

在Cloud Shell中使用Terraform

阿里云Cloud Shell是一款幫助您運(yùn)維的免費(fèi)產(chǎn)品,預(yù)裝了Terraform的組件,并配置好身份憑證(credentials)。因此您可直接在Cloud Shell中運(yùn)行Terraform的命令。

打開瀏覽器,訪問Cloud Shell的地址https://shell.aliyun.com。 Terraform然后一鍵部署ECS

在本地安裝和配置Terraform

登錄 Terraform官網(wǎng) 下載并安裝適用于您的操作系統(tǒng)的程序包。

命令運(yùn)行后將顯示可用的Terraform選項的列表,如下所示,表示安裝完成。

username:~$ terraform
Usage: terraform [-version] [-help] <command> [args]

創(chuàng)建環(huán)境變量,用于存放身份認(rèn)證信息。

export ALICLOUD_ACCESS_KEY="LTAIUrZCw3********"
export ALICLOUD_SECRET_KEY="zfwwWAMWIAiooj14GQ2*************"
export ALICLOUD_REGION="cn-beijing"

編寫terraform腳本

這里選擇在Cloud Shell中使用Terraform,創(chuàng)建相關(guān)目錄:

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"  #實(shí)例規(guī)格
  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  #實(shí)例數(shù)量

  user_data = local.user_data_ecs
}

#設(shè)置本地~/.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
登錄服務(wù)器:
%{ for ip in module.ecs.this_public_ip }
ssh root@ecs${index(module.ecs.this_public_ip, ip) + 1}%{ endfor }

公網(wǎng) IP 地址(用于 ssh 登陸):
%{ for ip in module.ecs.this_public_ip }
ecs${index(module.ecs.this_public_ip, ip) + 1}:    ${ip}%{ endfor }

內(nèi)網(wǎng) IP 地址(用于集群內(nèi)部通信,沒有端口限制):
%{ for ip in module.ecs.this_private_ip }
ecs${index(module.ecs.this_private_ip, ip) + 1}:    ${ip}%{ endfor }

銷毀服務(wù)器:
cd /home/shell/terraform_ecs
terraform destroy --auto-approve
EOF
    filename = "/home/shell/terraform_ecs/readme.txt"
}

output "服務(wù)器信息" {
   value = <<EOF

登錄服務(wù)器:
%{ for ip in module.ecs.this_public_ip }
ssh root@ecs${index(module.ecs.this_public_ip, ip) + 1}%{ endfor }

公網(wǎng) IP 地址(用于 ssh 登錄):
%{ for ip in module.ecs.this_public_ip }
ecs${index(module.ecs.this_public_ip, ip) + 1}:    ${ip}%{ endfor }

內(nèi)網(wǎng) IP 地址(用于集群內(nèi)部通信,沒有端口限制):
%{ for ip in module.ecs.this_private_ip }
ecs${index(module.ecs.this_private_ip, ip) + 1}:    ${ip}%{ endfor }

銷毀服務(wù)器:
cd /home/shell/terraform_ecs
terraform destroy --auto-approve

查看以上信息:
cat /home/shell/terraform_ecs/readme.txt

EOF
}

運(yùn)行以下命令啟動ECS:

terraform init #安裝相關(guān)module
terraform apply --auto-approve #創(chuàng)建ECS

創(chuàng)建成功后會有如下輸出:

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

Outputs:

服務(wù)器信息 = 
登錄服務(wù)器:

ssh root@ecs1
ssh root@ecs2
ssh root@ecs3

公網(wǎng) IP 地址(用于 ssh 登錄):

ecs1:    47.117.170.15
ecs2:    47.117.172.214
ecs3:    47.117.152.20

內(nèi)網(wǎng) IP 地址(用于集群內(nèi)部通信,沒有端口限制):

ecs1:    10.10.1.151
ecs2:    10.10.1.152
ecs3:    10.10.1.153

銷毀服務(wù)器:
cd /home/shell/terraform_ecs
terraform destroy --auto-approve

查看以上信息:
cat /home/shell/terraform_ecs/readme.txt

查看創(chuàng)建好的ECS:

Terraform然后一鍵部署ECS

登錄ECS:

#腳本已經(jīng)將在Cloud shell的公鑰傳到ECS上了,并且在~/.ssh/config配置了登錄信息
ssh root@ecs1

感謝各位的閱讀,以上就是“Terraform然后一鍵部署ECS”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對Terraform然后一鍵部署ECS這一問題有了更深刻的體會,具體使用情況還需要大家實(shí)踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI