Terraform然后一鍵部署ECS

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

創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),壽寧企業(yè)網(wǎng)站建設(shè),壽寧品牌網(wǎng)站建設(shè),網(wǎng)站定制,壽寧網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,壽寧網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

Terraform簡介

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

Terraform是一個(gè)高度可擴(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選項(xiàng)的列表,如下所示,表示安裝完成。

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í)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!

名稱欄目:Terraform然后一鍵部署ECS
轉(zhuǎn)載注明:http://muchs.cn/article6/gdieog.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、做網(wǎng)站手機(jī)網(wǎng)站建設(shè)、網(wǎng)站維護(hù)、品牌網(wǎng)站制作、虛擬主機(jī)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化