“Terraform”的版本间的差异
(→consul) |
|||
第538行: | 第538行: | ||
consul agent -dev -client 0.0.0.0 -ui | consul agent -dev -client 0.0.0.0 -ui | ||
+ | |||
+ | terraform code | ||
+ | |||
+ | backend-ubuntu cat main.tf | ||
+ | terraform { | ||
+ | required_providers { | ||
+ | proxmox = { | ||
+ | source = "Telmate/proxmox" | ||
+ | version = "2.9.11" | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | backend "consul" { | ||
+ | address = "192.168.10.22:8500" | ||
+ | scheme = "http" | ||
+ | path = "my-consul-project" | ||
+ | } | ||
+ | } | ||
+ | 中间省 | ||
+ | provisioner "local-exec" { | ||
+ | command = "sleep 1000" | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
第574行: | 第601行: | ||
</pre> | </pre> | ||
[https://www.cnblogs.com/ruanyifeng/p/5550102.html Consul入门] | [https://www.cnblogs.com/ruanyifeng/p/5550102.html Consul入门] | ||
+ | |||
+ | [https://lonegunmanb.github.io/introduction-terraform/2.2.Terraform%E5%9F%BA%E7%A1%80%E6%A6%82%E5%BF%B5%E2%80%94%E2%80%94%E7%8A%B6%E6%80%81%E7%AE%A1%E7%90%86.html Terraform基础概念——状态管理] | ||
=see also= | =see also= |
2023年4月14日 (五) 12:06的版本
目录
- 1 进阶
- 2 Deploy and Monitor Applications terraform 部署应用
- 3 ins
- 4 usage
- 5 usage on aliyun
- 6 qqcloud
- 7 Google Cloud
- 8 aws
- 9 azure
- 10 ucloud
- 11 terraform-provider-vultr
- 12 terraform local
- 13 terraform-localstack:使用Groovy测试Terraform集成-源码
- 14 proxmox terraform-provider-proxmox
- 15 terraform virtualbox
- 16 book
- 17 基础知识点
- 18 see also
- 19 使用实例标识 image-id etc
进阶
以Terraform为代表的资源编排类 资源编排工具的 Provisioner
tf 文件在 Vim 中的语法加亮是安装的hashivim/vim-terraform;插件
Terraform被设计成一个多云基础设施编排工具,不像CloudFormation那样绑定AWS平台,Terraform可以同时编排各种云平台或是其他基础设施的资源。Terraform实现多云编排的方法就是Provider插件机制。
几个重要的概念 resource 数据源通过一种特殊的资源访问:data资源
terraform电子书 learning June 2022 基本看完了
Deploy and Monitor Applications terraform 部署应用
https://learn.hashicorp.com/collections/terraform/applications
terraform 部署 docker
https://learn.hashicorp.com/tutorials/terraform/docker-build?in=terraform/docker-get-started
ins
#直接 apt kali就直接下载 包啦 vi ~/.bashrc alias terraform='~/data/apps/terraform/terraform' 或者 二进制包 mv到 /usr/local/bin/ or /usr/bin curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add - sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main" sudo apt-get update && sudo apt-get install terraform #也可手工加 哈哈 lsb_release -cs buster wget -c https://releases.hashicorp.com/terraform/0.15.4/terraform_0.15.4_linux_amd64.zip unzip terraform_0.15.4_linux_amd64.zip #sudo mv terraform /usr/bin/ mkdir /home/evan/data/apps/terraform && mv terraform /home/evan/data/apps/terraform/ echo 'export PATH=/home/evan/data/apps/terraform:$PATH' >> /etc/profile echo 'export PATH=/home/evan/data/apps/:$PATH' >> /etc/profile #这个不行 少了一个目录 找不到了 写在你的home ~/.profile 也是可以的 为你本用户使用方便 export PATH=/home/evan/data/apps/terraform:$PATH #if APT Packages for Debian and Ubuntu curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add - 216 sudo apt-add-repository "deb [arch=$(dpkg --print-architecture)] https://apt.releases.hashicorp.com $(lsb_release -cs) main" 218 apt update 219 sudo apt install terraform 像 git 一样,每个 Terraform 项目需要自己单独的目录空间,所以我们创建一个 terraform-learning 目录 mkdir terraform-learning cd terraform-learning 该目录下的所有 *.tf 文件都会被 Terraform 加载,在初始化 Terraform 工作空间之前必须至少要有一个 *.tf 文件。我们这里建立文件 main.tf, 内容如下
terraform的安装和应用
usage
usage on aliyun
以aliyun作为例子
#把相关密码写入 不是写在文本 安全一点吧 export ALICLOUD_ACCESS_KEY="LTAIUrZCw3********" export ALICLOUD_SECRET_KEY="zfwwWAMWIAiooj14GQ2*************" export ALICLOUD_REGION="cn-guangzhou"
evan@myxps:~/.terraform.d$ cat terraform.tf resource "alicloud_vpc" "vpc" { vpc_name = "tf_test_foo" cidr_block = "172.16.0.0/12" } resource "alicloud_vswitch" "vsw" { vpc_id = alicloud_vpc.vpc.id cidr_block = "172.16.0.0/21" zone_id = "cn-guangzhou-b" } evan@myxps:~/.terraform.d$ terraform init #自动安装了alicloud provider plugis Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/alicloud... - Installing hashicorp/alicloud v1.124.0... Terraform has created a lock file .terraform.lock.hcl to record the provider selections it made above. Include this file in your version control repository so that Terraform can guarantee to make the same selections by default when you run "terraform init" in the future. ╷ │ Warning: Additional provider information from registry │ │ The remote registry returned warnings for │ registry.terraform.io/hashicorp/alicloud: │ - For users on Terraform 0.13 or greater, this provider has moved to │ aliyun/alicloud. Please update your source in required_providers. ╵ Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work. If you ever set or change modules or backend configuration for Terraform, rerun this command to reinitialize your working directory. If you forget, other commands will detect it and remind you to do so if necessary. terraform apply # evan@myxps:~/.terraform.d$ terraform apply alicloud_vpc.vpc: Refreshing state... [id=vpc-7xvsw3jjadaf53zjlido6] Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # alicloud_vswitch.vsw will be created + resource "alicloud_vswitch" "vsw" { + availability_zone = (known after apply) + cidr_block = "172.16.0.0/21" + id = (known after apply) + name = (known after apply) + status = (known after apply) + vpc_id = "vpc-7xvsw3jjadaf53zjlido6" + vswitch_name = (known after apply) + zone_id = "cn-guangzhou-b" } Plan: 1 to add, 0 to change, 0 to destroy. Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value: yes alicloud_vswitch.vsw: Creating... alicloud_vswitch.vsw: Creation complete after 6s [id=vsw-7xvwa2oq0fsjm3xuhv9ss] Apply complete! Resources: 1 added, 0 changed, 0 destroyed. https://vpc.console.aliyun.com/vpc/cn-guangzhou/vpcs 打开 web 有了 terraform show ##查看已创建的专有网络和交换机 # alicloud_vpc.vpc: resource "alicloud_vpc" "vpc" { cidr_block = "172.16.0.0/12" id = "vpc-7xvsw3jjadaf53zjlido6" name = "tf_test_foo" route_table_id = "vtb-7xvjpppz3q8kin5vi081k" router_id = "vrt-7xvi5nvfqmvg0mox9rj5j" router_table_id = "vtb-7xvjpppz3q8kin5vi081k" secondary_cidr_blocks = [] status = "Available" user_cidrs = [] vpc_name = "tf_test_foo" } # alicloud_vswitch.vsw: resource "alicloud_vswitch" "vsw" { availability_zone = "cn-guangzhou-b" cidr_block = "172.16.0.0/21" id = "vsw-7xvwa2oq0fsjm3xuhv9ss" status = "Available" vpc_id = "vpc-7xvsw3jjadaf53zjlido6" zone_id = "cn-guangzhou-b" } 2 在上一步创建的专有网络中创建一个安全组,并添加一个允许任何地址访问的安全组规则。 在terraform.tf文件中增加以下内容。 resource "alicloud_security_group" "default" { name = "default" vpc_id = alicloud_vpc.vpc.id } resource "alicloud_security_group_rule" "allow_all_tcp" { type = "ingress" ip_protocol = "tcp" nic_type = "intranet" policy = "accept" port_range = "1/65535" priority = 1 security_group_id = alicloud_security_group.default.id cidr_ip = "0.0.0.0/0" } 运行terraform apply #开始创建。 有时time out 再来一次就好了 运行terraform show查看已创建的安全组和安全组规则。 您也可以登录ECS控制台查看安全组和安全组规则。 https://ecs.console.aliyun.com/?spm=5176.10483814.recommend.decs.78e6124322cN4j#/securityGroup/region/cn-guangzhou 3.创建ECS实例。 在terraform.tf文件中增加以下内容。 resource "alicloud_instance" "instance" { # cn-beijing availability_zone = "cn-beijing-b" security_groups = alicloud_security_group.default.*.id # series III 能找个最小的么 ecs.s6-c1m1.small instance_type = "ecs.s6-c1m1.small" #instance_type = "ecs.n2.small" system_disk_category = "cloud_efficiency" image_id = "ubuntu_18_04_64_20G_alibase_20190624.vhd" instance_name = "test_foo" vswitch_id = alicloud_vswitch.vsw.id internet_max_bandwidth_out =10 password = "$Eevan2240881" } 说明 在上述示例中,指定了internet_max_bandwidth_out= 10,因此会自动为实例分配一个公网IP。 详细的参数解释请参见 image_id 指定ubuntu_16_0402_32,最终会匹配ubuntu_16_0402_32_40G_alibase_20170711.vhd。
最终比较完整的配置文件
evan@myxps:~/.terraform.d$ cat terraform.tf resource "alicloud_vpc" "vpc" { vpc_name = "tf_test_foo" cidr_block = "172.16.0.0/12" } resource "alicloud_vswitch" "vsw" { vpc_id = alicloud_vpc.vpc.id cidr_block = "172.16.0.0/21" zone_id = "cn-guangzhou-b" } resource "alicloud_security_group" "default" { name = "default" vpc_id = alicloud_vpc.vpc.id } resource "alicloud_security_group_rule" "allow_all_tcp" { type = "ingress" ip_protocol = "tcp" nic_type = "intranet" policy = "accept" port_range = "1/65535" priority = 1 security_group_id = alicloud_security_group.default.id cidr_ip = "0.0.0.0/0" } #ec2 create resource "alicloud_instance" "instance" { # cn-beijing availability_zone = "cn-guangzhou-b" security_groups = alicloud_security_group.default.*.id # series III 能找个最小的么 ecs.s6-c1m1.small instance_type = "ecs.s6-c1m1.small" #instance_type = "ecs.n2.small" system_disk_category = "cloud_efficiency" image_id = "centos_7_9_x64_20G_alibase_20201228.vhd" instance_name = "test_foo" vswitch_id = alicloud_vswitch.vsw.id internet_max_bandwidth_out =10 password = "$1234567890" }
R
用Module创建多台ECS实例。在本示例中,创建3台ECS实例。 https://help.aliyun.com/document_detail/95830.html?spm=a2c4g.11186623.6.550.17317c8cNSSlJu
使用terraform创建阿里ecs用ansible完成主机配置
玩转阿里云 Terraform(一):Terraform 是什么
当Terraform遇上ECS(一)——DataSource篇
进阶
terraform-alicloud-modules / terraform-alicloud-ecs-instance官方
https://registry.terraform.io/modules/alibaba/ecs-instance/alicloud/latest
qqcloud
NOTES 将秘钥直接填入到.tf文件中是十分不安全的,在多用户共同管理资源时,不建议把腾讯云API 的秘钥直接写到源代码里,以免一不小心更新到公开的版本中,造成安全风险。 腾讯云提供了另一种更为安全可靠的方式,把秘钥信息放在环境变量中配置 // Configure the secret key in the environment path $ export TENCENTCLOUD_SECRET_ID="your_fancy_accessid" $ export TENCENTCLOUD_SECRET_KEY="your_fancy_accesskey" $ export TENCENTCLOUD_REGION="ap-hongkong" 这样在 provider.tf 文件中就可以省略掉相关信息 $ vim provider.tf // provider.tf provider "tencentcloud" {}
https://cloud.tencent.com/developer/article/1473713
Google Cloud
如何在Google Cloud Platform上使用Terraform
aws
关于 Terraform 工作目录中文件命名 Terraform 运行时会读取工作目录中所有的 *.tf, *.tfvars 文件,所以我们不必把所有的东西都写在单个文件中去,应按职责分列在不同的文件中,例如: provider.tf -- provider 配置 terraform.tfvars -- 配置 provider 要用到的变量 varable.tf -- 通用变量 resource.tf -- 资源定义 data.tf -- 包文件定义 output.tf -- 输出 以此篇最简单的入门出发,以后可以深入了解 Lambda, Lambda 触发器,及 API Gateway, EC2 实例怎么用 Terraform 来管理,也知晓了资源的可用属性应该到哪里去查。 一个小提示:在执行像 terraform plan 或 terraform apply 等命令的时候,可以按下 ctrl + c 让控制台输出详细的日志信息。
R
原版 https://www.howtoforge.com/what-is-terraform-and-how-to-install-it-on-a-aws-ec2-instance/
terraform 自动创建一台aws机器,并安装apache2服务,提供80,443端口进行访问脚本--good
什么是 Terraform 以及如何在 AWS EC2 上安装和使用它
https://docs.aws.amazon.com/zh_cn/vpc/latest/userguide/VPC_Scenario2.html
https://github.com/evan886/2022test/tree/main/terraform-aws
https://registry.terraform.io/providers/hashicorp/aws/latest/docs 官方看这个
Terraform 快速生成aws 实例+ VPC+ 子网学习用不错 一步步来的
使用Terraform在AWS多区部署虚拟货币钱包节点 #31
将现有的AWS资源导出到Terraform样式(tf,tfstate)
terraform s3_使用Terraform自动使用Amazon EC2,EFS,S3,CloudFront启动Wordpress
对所有操作进行自动化:Terraform + Ansible + Elastic Cloud Enterprise
使用 Terraform 在 AWS 中国区域实现自动化部署指南系列(一) TERRAFORM 入门
使用 Terraform 在 AWS 中国区域实现自动化部署指南系列(二) TERRAFORM 进阶
azure
ucloud
服务器太多了不好管?UCloud基于Terraform的资源编排工具详解
https://docs.ucloud.cn/terraform/solutions/1
terraform-provider-vultr
vultr / terraform-provider-vultr
terraform-provider-vultr, 这是Vultr云的terraform提供程序
terraform local
ocal 局部变量可以引用variable,resource,data中的一些资源属性,从而拼接改造需要的样式以供一起资源引用,local比variable更灵活,但local不能从外部来赋值。 局部值可能有助于避免在配置中多次重复相同的值或表达式,但是如果过度使用局部值,也可能通过隐藏使用的实际值而使以后的维护人员难以读取配置。 仅在某些地方使用单个值或结果且将来可能会更改该值的情况下,才应适度使用局部值。易于在中心位置更改值的能力是本地值的主要优势。
https://github.com/hashicorp/terraform-provider-local
terraform-localstack:使用Groovy测试Terraform集成-源码
proxmox terraform-provider-proxmox
https://registry.terraform.io/providers/Telmate/proxmox/latest/docs
1. Proxmox-Provider
目前社区主要有两个 provider:
Telmate/terraform-provider-proxmox: 用户最多,但是只支持管理虚拟机资源。 danitso/terraform-provider-proxmox: stars 少,但是可以管理 PVE 的大部分资源,包括节点、用户、资源池、TLS证书等等。 代码更顺眼,但是作者忙,没时间合并 pr,导致 Bug 更多一些。 不能设置虚拟机开机自启(onboot),而且目前不支持 disk resize
PVE 虚拟机的创建/修改/销毁:pve-vm.tf
https://github.com/ryan4yin/knowledge/blob/master/infrastructure/terraform/README.md
https://github.com/Telmate/terraform-provider-proxmox/blob/master/docs/index.md
evan@myxps:~/.terraform.d/plugins$ mv terraform-provider-proxmox_v0.1.0_x4 terraform-provider-proxmox
terraform virtualbox
book
➜ localfile cat main.tf terraform { required_version = ">= 0.15" required_providers { local = { source = "hashicorp/local" version = "2.2.3" } } } resource "local_file" "literature" { filename = "art_of_war.txt" content = <<-EOT Sun Tzu said: The art of war is of vital importance to the State. It is a matter of life and death, a road either to safety or to ruin. Hence it is a subject of inquiry which can on no account be neglected. EOT }
基础知识点
状态管理(多人协同使用terraform)
consul
安装请看官网非常容易的 consul agent -dev -client 0.0.0.0 -ui terraform code backend-ubuntu cat main.tf terraform { required_providers { proxmox = { source = "Telmate/proxmox" version = "2.9.11" } } backend "consul" { address = "192.168.10.22:8500" scheme = "http" path = "my-consul-project" } } 中间省 provisioner "local-exec" { command = "sleep 1000" } } http://192.168.10.22:8500/ui/dc1/services http://192.168.10.22:8500/ui/dc1/kv Consul中可以看到名为my-consul-project的键 可以看到,原本保存在工作目录下的tfstate文件的内容,被保存在了Consul的名为my-consul-project的键下。 让我们执行terraform destroy后,重新访问 最后的效果 backend-ubuntu terraform apply Acquiring state lock. This may take a few moments... ╷ │ Error: Error acquiring the state lock │ │ Error message: Lock Info: │ ID: bc4b35c4-a8a2-1467-a1e6-3a2fea0dfb39 │ Path: my-consul-project │ Operation: OperationTypeApply │ Who: evan@myxps │ Version: 1.4.0 │ Created: 2023-04-14 09:38:38.938683897 +0000 UTC │ Info: consul session: bc4b35c4-a8a2-1467-a1e6-3a2fea0dfb39 │ │ │ Terraform acquires a state lock to protect the state from being written │ by multiple users at the same time. Please resolve the issue above and try │ again. For most commands, you can disable locking with the "-lock=false" │ flag, but this is not recommended.
see also
https://www.cnblogs.com/syavingcs/tag/Terraform/