惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Apple Machine Learning Research
Apple Machine Learning Research
S
Schneier on Security
P
Proofpoint News Feed
The Cloudflare Blog
S
SegmentFault 最新的问题
WordPress大学
WordPress大学
Hugging Face - Blog
Hugging Face - Blog
雷峰网
雷峰网
博客园 - 【当耐特】
博客园 - 叶小钗
大猫的无限游戏
大猫的无限游戏
F
Fortinet All Blogs
宝玉的分享
宝玉的分享
博客园 - 聂微东
Engineering at Meta
Engineering at Meta
G
Google Developers Blog
Know Your Adversary
Know Your Adversary
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
CXSECURITY Database RSS Feed - CXSecurity.com
G
GRAHAM CLULEY
T
Threatpost
T
Threat Research - Cisco Blogs
酷 壳 – CoolShell
酷 壳 – CoolShell
C
Cisco Blogs
Cisco Talos Blog
Cisco Talos Blog
Latest news
Latest news
C
Cybersecurity and Infrastructure Security Agency CISA
L
LINUX DO - 热门话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
SecWiki News
SecWiki News
L
LangChain Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The Last Watchdog
The Last Watchdog
阮一峰的网络日志
阮一峰的网络日志
Security Latest
Security Latest
P
Palo Alto Networks Blog
L
LINUX DO - 最新话题
博客园 - 司徒正美
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
P
Privacy International News Feed
N
News and Events Feed by Topic
Spread Privacy
Spread Privacy
T
Tenable Blog
有赞技术团队
有赞技术团队
MyScale Blog
MyScale Blog
aimingoo的专栏
aimingoo的专栏
AI
AI

陈少文的网站

巨变与机遇的未来十年 Kubernetes 平台管理软件压力测试方案 使用镜像部署 Hexo 静态页面 终于等到你 - GitHub 镜像仓库服务(ghcr.io) 一起来学 Go --(6)Interface 一起来学 Go --(5)Goroutine 和 Channel 什么是函数式编程 如何在 Kubernetes 集群集成 Kata 柯里化与偏函数 使用 PyGithub 自动创建 Label 软件产品是团队能力的输出 Helm 2 、Helm 3 比较 IoT 变现 Kubernetes 中的 DNS 服务 国内的 Helm 镜像源 Harbor 使用自签证书支持 Https 访问 DevOps 工具链之 Prow 如何使用 kfctl 安装 Kubeflow VS Code 无法下载 Go 插件的工具包 工程师更应具有服务精神 你不知道的 Docker 使用技巧 使用 Docker 运行 Tensorflow 论中国 什么是左移 如何清空 Git 仓库全部历史记录 一禅小和尚 有风吹过厨房 时间的玫瑰 如何在 CentOS 安装 GPU 驱动 开发 Tips(19) 使用 Velero 备份 Kubernetes 集群 Kubernetes Cheat Sheet 开发 Tips(18) 如何构建一个 Java 工程 开发 Tips(17) KubeSpray 安装 Kubernetes 报错 ip in ansible_all_ipv4_addresses 基于 Kubernetes 和 Jenkins 搭建自动化测试系统 在 Kubernetes 上动态创建 Jenkins Slave 使用 Jenkins 进行服务拨测 开发 Tips(16) Kubernetes 签发 Ingress 证书及日常故障运维 Kubernetes 中 Deployment 的基本操作 Kubernetes 中的证书 如何使用 KubeBuilder 开发一个 Operator Kubernetes 1.6.0 安装问题汇总 镜像管理工具 -- Harbor 开发 Tips(15) Docker 如何拉取镜像 开发 Tips(14) 使用 Helm 安装 harbor 开发 Tips(13) 使用 S2I 构建云原生应用 在 Kubernetes 中使用 emptyDir、hostPath、localVolume 开发 Tips(12) 开发 Tips(11) 代码质量分析工具 SonarQube 使用 Kubeadm 安装 Kubernetes 集群 一起来学 Go --(4)常用函数 Kubernetes 中的 Ceph Kubernetes 之 Volumes Kubernetes 之 Labels、Selectors 开发 Tips(10) 开源正在重构商业模式 Kubernetes 之网络 Kubernetes 之 API 使用 Helm 和 Operator 快速部署 Prometheus Kubernetes 复杂有状态应用管理框架 -- Operator Kubernetes 的包管理器 -- Helm 一起来学 Go --(3)Go Modules 如何一步一步地优化博客方案 kubectl 实用指南 Kubernetes 中的基本概念 搭建远程 Kubernetes 开发环境 大公司和小公司的 ToB 思路 开发 Tips(9) Go 入门指南 一起来学 Go --(2)数据与逻辑结构 如何预防 Web 富文本中的 XSS 攻击 django-xss-cleaner 云工作时代 一起来学 Go --(1)背景与特点 SaaS 开发团队的不同阶段 你不知道的 Git 使用技巧 输出既服务 微服务设计 继续奔跑 开发 Tips(8) 从账户安全到二次验证 Django 性能之数据库查询优化 Django 性能之分库分表 敏捷开发之研发流程 打造一致性的团队 开发 Tips(7) Pytest 进阶学习之 Mock PaaS 部署之 buildpack Go 开发配置 领域输出才是 PaaS 的核心竞争力 Pytest 入门学习 开发 Tips(6) 如何使用 Jenkins、Docker、GitLab 搭建 Django 自动化部署流程
使用 S3 存储 Terraform 的状态
微信公众号 · 2020-12-27 · via 陈少文的网站

Please enable Javascript to view the contents

1. Terraform 如何管理资源状态

在执行 terraform init 之后,Terraform 会将依赖的插件下载到本地 plugins 目录。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
tree -aL 5

.
|-- myresource.tf
|-- .terraform
|   `-- plugins
|       |-- registry.terraform.io
|       |   |-- hashicorp
|       |   |   `-- null
|       |   `-- shaowenchen
|       |       `-- qingcloud
|       `-- selections.json
`-- var.tf

在执行 terraform apply 之后,Terraform 会使用 terraform.tfstate 文件存储资源的信息。新建 .terraform.tfstate.lock.info 锁定资源,避免多个 Terraform 同时使用一个状态文件。在执行完毕之后, .terraform.tfstate.lock.info 文件会被删除。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
tree -aL 5

.
|-- myresource.tf
|-- .terraform
|   `-- plugins
|       |-- registry.terraform.io
|       |   |-- hashicorp
|       |   |   `-- null
|       |   `-- shaowenchen
|       |       `-- qingcloud
|       `-- selections.json
|-- terraform.tfstate
|-- .terraform.tfstate.lock.info
`-- var.tf

terraform.tfstate 是一个 Json 文件,存储了资源的 ID 、状态等信息,格式如下:

1
2
3
4
5
6
7
8
{
  "version": 4,
  "terraform_version": "0.13.0",
  "serial": 11,
  "lineage": "9359dc1c-9a6d-b1e0-9780-d26500869e82",
  "outputs": {},
  "resources": [...]
}

在执行 terraform destroy 之后,Terraform 会新建一个 terraform.tfstate.backup 文件,备份资源状态,接着删除资源。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
tree -aL 5

.
|-- myresource.tf
|-- .terraform
|   `-- plugins
|       |-- registry.terraform.io
|       |   |-- hashicorp
|       |   |   `-- null
|       |   `-- shaowenchen
|       |       `-- qingcloud
|       `-- selections.json
|-- terraform.tfstate
|-- .terraform.tfstate.backup
`-- var.tf

具体的细节没有深究太多,简单点就是 Terraform 使用 Json 存储了资源的状态,默认存储在本地目录下。

2. 使用 S3 存储 Terraform 的状态

如果在一个团队、GitHub Actions 中,如何持久化存储 Terraform 的状态呢?持久化状态也就是需要将上面的 Json 数据存储在某一个远端持久化服务中。Terraform 支持很多种后端存储,下面是从 Terraform 官网获取的一个 Backend 列表:

  • artifactory
  • azurerm
  • consul
  • cos
  • etcd
  • etcdv3
  • gcs
  • http
  • kubernetes
  • manta
  • oss
  • pg
  • s3
  • swift

Terraform 使用 Backend 模块来处理状态的存储。在 var.tf 文件中,只需要添加一个 backend ,填上参数即可使用远端存储状态。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
terraform {
  backend "s3" {
    bucket   = "terraform-deploy"
    key      = "tf-cd"
    region     = "sh1a"
    endpoint = "s3.sh1a.qingstor.com"
    skip_region_validation      = true
    skip_metadata_api_check     = true
    skip_credentials_validation = true
    access_key = "ACCESS_KEY"
    secret_key = "SECRET_KEY"
  }
}

terraform apply 之后,在支持 S3 的对象存储上,就可以看到状态文件:

如果需要 lock 状态文件,可以使用类似 DynamoDB 的 BaaS 保证数据的一致性。

3. 参考


微信公众号