将Terraform脚本模块化,并应用于GCP的多个环境
我正在使用Terraform的v0.12.16版本(此文章发布时的最新版本)。
本文的目的是介绍如何将Terraform脚本模块化,以适用于GCP的开发环境、测试环境和生产环境。
如果您是第一次使用Terraform,请查看这篇文章(使用Terraform工具进行GCP资源管理)。
1. 创建Terraform脚本
在这篇文章中,我们将以GKE集群、存储桶Bucket和Pubsub主题与订阅作为例子来创建部署脚本。
Terraform脚本文件夹的结构
terraform_script_folder
├── _modules
│ ├── cluster
│ │ ├── main.tf
│ │ ├── outputs.tf
│ │ └── variables.tf
│ ├── pubsub
│ │ ├── main.tf
│ │ ├── outputs.tf
│ │ └── variables.tf
│ └── storage
│ ├── main.tf
│ ├── outputs.tf
│ └── variables.tf
├── dev
│ ├── account.json
│ └── terraform.tfstate
├── dev.tfvars
├── main.tf
├── prod
│ ├── account.json
│ └── terraform.tfstate
├── prod.tfvars
├── staging
│ ├── account.json
│ └── terraform.tfstate
├── staging.tfvars
└── variables.tf
文件夹结构的说明
-
- _modulesフォルダ:リソース種類のごとに共有定義スクリプトを格納する
-
- dev、staging、prodのフォルダ:開発、ステージング、本番の環境用にアクセス用のアカウントファイルとStateファイルを格納
- dev.tfvars、staging.tfvars、prod.tfvarsのファイル:各種環境によるパラメータ設定ファイル
2. 创建Terraform脚本
2.1 共有模块脚本(_modules)
_模块/存储/主.tf
resource "google_storage_bucket" "sample-bucket" {
name = var.name
location = var.region
storage_class = var.storage_class
labels = {
app = var.app
env = var.env
}
}
_modules/storage/variables.tf 的中文释义是什么?
resource "google_storage_bucket" "sample-bucket" {
name = var.name
location = var.region
storage_class = var.storage_class
labels = {
app = var.app
env = var.env
}
}
不使用「outputs.tf」脚本。
2.2 主脚本
主要.tf
terraform {
required_version = "~>0.12.14"
}
## project ##
provider "google" {
project = var.project
region = var.region
}
## storage buckets ##
module "storage" {
source = "./_modules/storage"
name = var.storage_name
region = var.region
app = var.app
env = var.env
}
## cluster ##
module "cluster" {
source = "./_modules/cluster"
name = var.cluster_name
zone = var.zone
node_count = var.node_count
initial_node_count = var.initial_node_count
node_pool_name = var.node_pool_name
node_machine_type = var.node_machine_type
}
## topic & subscription ##
## the first one
module "the-first-topic" {
source = "./_modules/pubsub"
topic_name = var.first_topic_name
subscription_name = var.first_subscription_name
app = var.app
env = var.env
}
## the second one
module "the-second-topic" {
source = "./_modules/pubsub"
topic_name = var.second_topic_name
subscription_name = var.second_subscription_name
app = var.app
env = var.env
}
variables.tf 变量.tf
## global variables
variable "project" {}
variable "app" {}
variable "env" {}
variable "region" {
default = "asia-northeast1"
}
variable "zone" {
default = "asia-northeast1-b"
}
## storage variables
variable "storage_name" {}
## cluster variables
variable "cluster_name" {}
variable "node_count" {}
variable "initial_node_count" {}
variable "node_pool_name" {}
variable "node_machine_type" {}
## pubsub variables
variable "first_topic_name" {}
variable "first_subscription_name" {}
variable "second_topic_name" {}
variable "second_subscription_name" {}
2.3 环境每个参数文件
开发环境(dev.tfvars)
## global variables
project = "project-dev"
env = "dev"
app = "sample-app"
region = "asia-northeast1"
zone = "asia-northeast1-b"
## storage variables
storage_name = "dev_private_bucket_abc123"
## cluster variables
cluster_name = "sample-cluster"
initial_node_count = 1
node_count = 2
node_pool_name = "sample-node-pool"
node_machine_type = "n1-standard-1"
## pubsub variables
first_topic_name = "sample-first-topic"
first_subscription_name = "sample-first-topic-sub"
second_topic_name = "sample-second-topic"
second_subscription_name = "sample-second-topic-sub"
暂存环境(staging.tfvars)
## global variables
project = "project-staging"
env = "staging"
app = "sample-app"
region = "asia-northeast1"
zone = "asia-northeast1-b"
## storage variables
storage_name = "staging_private_bucket_abc123"
## cluster variables
cluster_name = "sample-cluster"
initial_node_count = 1
node_count = 2
node_pool_name = "sample-node-pool"
node_machine_type = "n1-standard-1"
## pubsub variables
first_topic_name = "sample-first-topic"
first_subscription_name = "sample-first-topic-sub"
second_topic_name = "sample-second-topic"
second_subscription_name = "sample-second-topic-sub"
产品环境(prod.tfvars)
## global variables
project = "project-prod"
env = "prod"
app = "sample-app"
region = "asia-northeast1"
zone = "asia-northeast1-b"
## storage variables
storage_name = "prod_private_bucket_abc123"
## cluster variables
cluster_name = "sample-cluster"
initial_node_count = 1
node_count = 2
node_pool_name = "sample-node-pool"
node_machine_type = "n1-standard-1"
## pubsub variables
first_topic_name = "sample-first-topic"
first_subscription_name = "sample-first-topic-sub"
second_topic_name = "sample-second-topic"
second_subscription_name = "sample-second-topic-sub"
3. 根据环境进行部署。
3.1 开发环境 translates to “Development environment” in English.
# 専用の環境変数にCredentialファイルを設定する
$ export GOOGLE_CLOUD_KEYFILE_JSON=path_to/dev/account.json
# tfファイルを適用する前に必ず差分を確認する
cd [TERRAFORM_FOLDER]
terraform plan -var-file="dev.tfvars" -state=./dev/terraform.tfstate
# planの結果が想定通りなら、tfファイルを適用する
terraform apply -var-file="dev.tfvars" -state=./dev/terraform.tfstate
3.2 临时环境
# 専用の環境変数にCredentialファイルを設定する
$ export GOOGLE_CLOUD_KEYFILE_JSON=path_to/staging/account.json
# tfファイルを適用する前に必ず差分を確認する
cd [TERRAFORM_FOLDER]
terraform plan -var-file="staging.tfvars" -state=./staging/terraform.tfstate
# planの結果が想定通りなら、tfファイルを適用する
terraform apply -var-file="staging.tfvars" -state=./staging/terraform.tfstate
3.3 本地环境
# 専用の環境変数にCredentialファイルを設定する
$ export GOOGLE_CLOUD_KEYFILE_JSON=path_to/prod/account.json
# tfファイルを適用する前に必ず差分を確認する
cd [TERRAFORM_FOLDER]
terraform plan -var-file="staging.tfvars" -state=./prod/terraform.tfstate
# planの結果が想定通りなら、tfファイルを適用する
terraform apply -var-file="staging.tfvars" -state=./prod/terraform.tfstate
本文使用的源代码可在此处找到:
https://github.com/dssolutioninc/dss_gke/tree/master/terraform_gcp_module
非常感谢您读到最后!DSS桥本
相关文章:使用Terraform工具进行GCP资源管理