安装配置 Terraform

安装配置 Terraform

安装

  1. macOS 苹果系统安装
1#安装
2brew tap hashicorp/tap
3brew install hashicorp/tap/terraform
4# 更新
5brew update
6brew upgrade hashicorp/tap/terraform
7#验证安装
8terraform -help
  1. windows 系统安装
1#安装
2choco install terraform
3#直接到这个url里下载64位系统
4https://www.terraform.io/downloads
5#验证安装
6terraform -help
  1. Linux 安装
1curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
2sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
3sudo apt-get update && sudo apt-get install terraform
4#验证安装
5terraform -help
1wget -O- https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo | sudo tee /etc/yum.repos.d/hashicorp.repo
2sudo yum install terraform -y

terrafrom 控制 proxmox 虚拟机

来源:https://github.com/Telmate/terraform-provider-proxmox

首先你要有一台 pve 主机

安装过程本篇文章就不想了,主要是要写一下关于他的配置
https://pve.proxmox.com/pve-docs/

  1. 下载
1wget https://github.com/Telmate/terraform-provider-proxmox/releases/download/v2.9.3/terraform-provider-proxmox_2.9.3_linux_amd64.zip
2unzip terraform-provider-proxmox_2.9.3_linux_amd64.zip

编写 terrafrom 程序

  1. 虚拟机 main.tf
 1terraform {
 2  required_version = ">= 0.14"
 3  required_providers {
 4    proxmox = {
 5      source  = "telmate/proxmox"
 6    }
 7  }
 8}
 9
10provider "proxmox" {
11  # 配置选项
12    pm_tls_insecure = true
13    pm_api_url = "https://localhost:8006/api2/json"
14    pm_user = "root@pam"
15    pm_password = "passwd"
16    pm_otp = ""
17}
18
19# 创建VM
20resource "proxmox_vm_qemu" "cloudinit-test" {
21    name = "terraform-test-vm"
22    desc = "A test for using terraform and cloudinit"
23
24    #节点名称必须与集群内的名称相同
25    #这可能不包括 FQDN
26    target_node = "pve"
27
28    #新虚拟机的目标资源池
29    pool = "pool0"
30
31    #从中克隆这个虚拟机的模板名称
32    clone = "node0"
33
34    #为这个虚拟机激活 QEMU 代理
35    agent = 1
36
37    os_type = "cloud-init"
38    cores = 2
39    sockets = 1
40    vcpus = 0
41    cpu = "host"
42    memory = 2048
43    scsihw = "lsi"
44
45    #设置磁盘
46    disk {
47        size = 32
48        type = "virtio"
49        storage = "local-lvm"
50        storage_type = "lvmthin"
51        iothread = 1
52        ssd = 1
53        discard = "on"
54    }
55
56    #设置网络接口并分配一个 vlan 标签:256
57    network {
58        model = "virtio"
59        bridge = "vmbr0"
60        tag = 256
61    }
62}
  1. 运行 terrafrom
1# 初始化
2terraform init
3# 查看产生的变更
4terraform plan
5# 运行
6terraform apply

配置

配置这个 terraform 我们这个需要持续更新,首先我们先配置 Azure 吧

Azure 配置

  1. 安装 azurecli
1# linux
2curl -L https://aka.ms/InstallAzureCli | bash
3apt install azure-cli
4# macOS
5brew update && brew install azure-cli
  1. 登录 azure
1# 中国区azure
2az cloud set --name AzureCloud
3az login -u <账户> -p <密码>
4
5#海外azure
6az cloud set --name AzureChinaCloud
7az login -u <账户> -p <密码>
  1. 创建 terrafrom 代码 创建 main.tf
 1# 正在使用的 Azure 提供程序源和版本
 2terraform {
 3    required_version = ">=0.12"
 4
 5    required_providers {
 6        azurerm = {
 7            source = "hashicorp/azurerm"
 8            version = "~>2.0"
 9        }
10    }
11}
12# 配置 Microsoft Azure 提供程序
13provider "azurerm" {
14  features {}
15}
16
17# 资源组前缀
18resource "random_pet" "rg-name" {
19  prefix    = var.resource_group_name_prefix
20}
21
22# 创建资源组
23resource "azurerm_resource_group" "rg" {
24  name      = random_pet.rg-name.id
25  location  = var.resource_group_location
26}

创建 variable.tf

1variable "resource_group_name_prefix" {
2  default       = "rg"
3  description   = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription."
4}
5
6variable "resource_group_location" {
7  default = "eastus"
8  description   = "Location of the resource group."
9}