本文来自:极狐 GitLab 开发者社区
作者:KaliArch
利用极狐 GitLab CI 实现基础设施编排自动化后,用户就可以使用极狐 GitLab 进行基础设施管理:提交基础设施变更后,会触发 MR 进行极狐 GitLab CI 流水线执行,从而实现基础设施 DevOps 流程。
Terraform + 极狐 GitLab CI 架构解析
流程图
流程详解
开发或运维人员编写基于 Terraform 的目标云资源清单文件,同时项目内管理极狐 GitLab CI 流程,在 K8s 不同 NS 下注册有对应的 Runner,在不同分支下可以触发不同 NS 下的 CI 流程:
开发或运维人员提交代码;
部署在对应名称空间下的 Runner 执行流程,创建运行单个 Stage 的 Pod 来运行 Terraform 对应命令,例如 init/fmt/play/apply 等;
如果要对云上资源进行变更,修改代码,再次提交 MR,触发更新流水线;
如果需要销毁,根据 CI 文件配置提交 Build 为 Destroy,触发云上销毁动作。
Terraform + 极狐 GitLab CI 预置条件
极狐 GitLab 服务器;
注册有项目的极狐 GitLab Runner;
K8s 集群;
腾讯云 AK 账号。
开启极狐 GitLab CI + Terraform 实战
极狐 GitLab CI 配置
.gitlab.yaml
variables:# PHASE: BUILD|DESTROYPHASE: DESTROY# PROXY: http://squiduser:xxzx789@43.134.199.162:3128# PROXY: http://squiduser:xxzx789@43.154.230.17:3128REGION: "ap-guangzhou"PLAN_JSON: plan.jsonBACKEND_CONF: "backend_oss.conf"before_script:# - apk add --no-cache curl git jq- apk add --no-cache jq- export http_proxy=${SQUID_PROXY}- export https_proxy=${SQUID_PROXY}- export TENCENTCLOUD_SECRET_KEY=${TENCENTCLOUD_SECRET_KEY}- export TENCENTCLOUD_SECRET_ID=${TENCENTCLOUD_SECRET_ID}- export TF_REGISTRY_CLIENT_TIMEOUT=120000- export CHECKPOINT_TIMEOUT=500000- export TF_REGISTRY_DISCOVERY_RETRY=5- alias convert_report="jq -r '([.resource_changes[]?.change.actions?]|flatten)|{\"create\":(map(select(.==\"create\"))|length),\"update\":(map(select(.==\"update\"))|length),\"delete\":(map(select(.==\"delete\"))|length)}'"
# 配置缓存
cache:
paths:
- ${CI_PROJECT_DIR}/.terraform/*
stages:
- init
- validate
- plan
- deploy
Init:
image:
name: hashicorp/terraform:0.14.0
entrypoint: [""]
stage: init
retry:
max: 2
when:
- script_failure
tags:
- gitlab-runner-k8s-new
script:
- terraform version
- terraform init -backend-config=${BACKEND_CONF}
only:
- dev
Validate:
image:
name: hashicorp/terraform:0.14.0
entrypoint: [""]
stage: validate
tags:
- gitlab-runner-k8s-new
retry: 2
script:
- terraform init -backend-config=${BACKEND_CONF}
- terraform validate
- terraform fmt -check -recursive || echo 0
cache:
paths:
- ${CI_PROJECT_DIR}/.terraform/*
policy: pull
allow_failure: true
Plan:
image:
name: hashicorp/terraform:0.14.0
entrypoint: [""]
stage: plan
retry: 2
tags:
- gitlab-runner-k8s-new
artifacts:
paths:
- plan.bin
- app_config.zip
expire_in: 2 week
script:
- terraform init -backend-config=${BACKEND_CONF}
- terraform plan -input=false -out=plan.bin -var region=${REGION}
- terraform show --json "plan.bin" | convert_report > ${PLAN_JSON}- cat ${PLAN_JSON}only:variables:- $PHASE == "BUILD"Apply:image:name: hashicorp/terraform:0.14.0entrypoint: [""]
when: manualstage: deployretry: 2tags:- gitlab-runner-k8s-newscript:- terraform init -backend-config=${BACKEND_CONF}- terraform apply -auto-approve -input=false plan.binonly:variables:- $PHASE == "BUILD"environment:name: snunvDestroy:image:name: hashicorp/terraform:0.14.0entrypoint: [""]
stage: deployretry: 2tags:- gitlab-runner-k8s-newscript:- terraform init -backend-config=${BACKEND_CONF}- terraform destroy -auto-approve -var region=${REGION}only:variables:- $PHASE == "DESTROY"
复制代码
环境配置
利用极狐 GitLab CI/CD 的 Environment 进行环境管理。
Terraform 资源
provider "tencentcloud" {
region = var.region
}
terraform {
required_providers {
tencentcloud = {
source = "registry.terraform.io/tencentcloudstack/tencentcloud"
version = ">=1.61.5"
}
}
backend "cos" {}
}
# 输入变量
variable "region" {
type = string
}
# 再次仅为一个查询示例
data "tencentcloud_instances" "cvm" {
}
# 输出
output "result" {
value = {
cvm_result = { for k, v in data.tencentcloud_instances.cvm : k => v },
count = data.tencentcloud_instances.cvm.instance_list[*]
}
}
复制代码
为了 Terraform 后端 Backend 安全,将其存储为单独文件,可在不同分支或环境进行修改。
region = "ap-beijing"
bucket = "tfproject-1253xxxx830"
prefix = "samxxxxitlab/dexxxxxt"
复制代码
测试
查看应用创建出的 VPC。
修改极狐 GitLab CI 文件,销毁。
注意事项
通过以上 3 步,就能实现简单的 Terraform + 极狐 GitLab CI 基础设施编排集成,开启基础设施自动化管理啦。
评论