Terraform Aliyun 創建ecs, kubernetes 實例
terraform demo for aliyun
創建vpc, 網關, EIP, ecs, kubernetes, Serverless Kubernetes
env :
Terraform v0.12.28
provider.alicloud v1.92.0By Elvin ,2020-8-23, http://blog.elvin.vip
git source https://gitee.com/alivv/terraform-aliyun.git
#環境准備
#aliyun cli
#下載和安裝 download and install
wget https://aliyuncli.alicdn.com/aliyun-cli-linux-latest-amd64.tgz
tar -zxf aliyun-cli-linux-latest-amd64.tgz -C /usr/local/bin/
#配置認證 config AccessKey,SecretKey,Region
aliyun configure --profile default
#list config 列出配置
aliyun configure list --profile default
# --profile 配置名,用於管理多配置,默認名稱default
阿里雲幫助文檔 aliyun help doc
https://help.aliyun.com/document_detail/121541.html
https://help.aliyun.com/document_detail/121198.html
#terraform
#download and install
wget http://files.elvin.vip/cli/terraform_0.12.28_linux_amd64.zip
unzip -oq terraform_0.12.28_linux_amd64.zip -d /usr/local/bin/
#provider-alicloud
wget http://files.elvin.vip/cli/terraform-provider-alicloud_1.92.0_linux_amd64.zip
mkdir -p ~/.terraform.d/plugins
unzip -oq terraform-provider-alicloud_1.92.0_linux_amd64.zip -d ~/.terraform.d/plugins/
#provider資源下載保存到terraform相同目錄,或~/.terraform.d/plugins/
#避免terraform init初始化時每個目錄自動下載provider而耗時 !!!
官網 download url
https://releases.hashicorp.com/terraform/
https://releases.hashicorp.com/terraform-provider-alicloud/
#clone demo source
git clone https://gitee.com/alivv/terraform-aliyun.git /opt/
#創建OSS
cd /opt/terraform-aliyun/modules/oss/
#初始化
terraform init
#執行計划
terraform plan
#創建資源(等待輸入yes)
terraform apply
#創建oss資源 實例
resource "alicloud_oss_bucket" "terraform-elvin" {
bucket = "terraform-elvin"
#bucket名稱已存在會創建失敗
acl = "private"
tags = {
"Terraform" = "true"
}
versioning {
status = "Enabled"
}
}
#使用oss存儲Terraform Backend狀態文件(不配置默認本地)
#后面實例需要修改backend.tf文件bucket對應名稱
#創建VPC, vswitch, EIP, nat gateway, security_group
#網絡參考於xxx生產環境簡化版, 三個網段demo, prod, k8s, 六個可用區a,b,c,d,e,f
#內部服務實例不綁定公網IP,網絡出口使用NAT網關
# 實例 -> vswitch -> nat gateway -> EIP(snat) -> Internet
cd /opt/terraform-aliyun/modules/network
terraform init
terraform plan
terraform apply
#銷毀
#terraform destroy
#注意, 不要反復創建網絡資源
#aliyun nat網關最低按12元/天,其它按小時
#創建一個ECS實例作為跳板機
#使用已有網絡創建bastion(VM),並綁定EIP,作為跳板機
cd /opt/terraform-aliyun/bastion
terraform init
#根據workspace使用demo網段
terraform workspace new demo
terraform workspace select demo
terraform workspace list
terraform apply
#創建Aliyun Serverless Kubernetes
#Serverless集群不創建k8s master和node節點,按POD資源收費,和適合測試或運行小應用
cd /opt/terraform-aliyun/k8s-serverless
terraform init
terraform plan
terraform apply
#創建完成后,查看web控制台.kube/config配置,在bastion使用(沒配置公網IP, 使用內網IP訪問)
#創建Aliyun Kubernetes 托管版
#aliyun Kubernetes 托管版,只有work節點, master節點使用托管(共享資源,資源隔離)
cd /opt/terraform-aliyun/k8s-node
#根據需要更改實例規格和節點數量
terraform init
terraform apply
#創建Aliyun Kubernetes 標准版
#配置實例為3個master節點, 2個work節點
cd /opt/terraform-aliyun/kubernetes-cluster
terraform init
terraform plan
#master節點創建成功, work節點失敗,多次創建失敗 ...
更多詳情,查看git倉庫 https://gitee.com/alivv/terraform-aliyun.git