全部文档
当前文档

暂无内容

如果没有找到您期望的内容,请尝试其他搜索词

文档中心

使用Terraform管理KS3

最近更新时间:2024-03-01 18:25:35

本文介绍Terraform的安装和配置详情,以及如何使用Terraform来管理KS3。

下载与安装

  • 下载

    前往Terraform官网下载适用于您操作系统的程序包。

  • 验证是否安装成功

执行以下命令验证是否已成功安装Terraform。

terraform -version

返回如下信息表示安装成功(版本号可能存在差异)。

Terraform v 1.6.5
on darwin_amd64

Your version of Terraform is out of date! The latest version
is 1.6.6. You can update by downloading from https://www.terraform.io/downloads.html

配置Terraform

  1. 需要为每个Terraform项目创建一个独立的执行目录,如创建terraform-test目录。

# 创建目录
mkdir terraform-test
# 进入该目录
cd terraform-test

  1. 创建main.tf配置文件,输入以下内容,并保存在当前执行的目录中。

# 指定所需的Terraform版本和使用的Provider模块,此时建议挂载代理
terraform {
  required_providers {
    ks3 = {
      source  = "ks3sdklib/ks3"
      version = "1.0.126"
  }
 }
}

# 配置 ks3 提供程序
provider "ks3" {
  alias  = "bj-prod"        # 使用别名为"bj-prod"
  region = "YOUR_REGION"    # 指定使用的区域为北京
  access_key = "YOUR_ACCESS_KEY"  
  secret_key = "YOUR_SECRET_KEY"  
  endpoint = "YOUR_ENDPOINT"  
}

# 创建一个名为"bucket-new"的KS3存储桶资源
resource "ksyun_ks3_bucket" "bucket-new" {
  provider = ks3.bj-prod
  bucket = "YOUR_BUCKET"  # 指定要创建的虚拟存储桶的名称
  acl = "public-read"     # 桶权限
}

  1. 执行以下命令进行初始化。

terraform init

出现如下所示的日志,则表示初始化成功。

Terraform has been successfully initialized!
You may now begin working with Terraform. Try running "terraform plan" to seeany changes that are required for your infrastructure. All Terraform commandsshould 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, othercommands will detect it and remind you to do so if necessary.

使用Terraform管理KS3

Terraform安装成功后,即可通过Terraform的操作命令管理KS3,以下为常见的操作命令。

1. terraform plan(预览操作)

该命令用于在正式执行配置文件之前,预览将要执行的操作。

  • 执行以下命令创建配置文件test.tf。

    vim test.tf
  • 配置文件信息示例如下。

    resource "ksyun_ks3_bucket" "bucket" {
      provider = ks3.bj-prod
      bucket = "YOUR_BUCKET" 
      acl = "public-read" 
    }

  • 执行以下命令预览将要执行的操作

    terraform plan 

  • 出现如下所示的日志,表示成功。

    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:
    
      # ksyun_ks3_bucket.bucket-new will be created
      + resource "ksyun_ks3_bucket" "bucket-new" {
          + acl           = "public-read"
          + bucket        = "test-terraform-bucket"
          + id            = (known after apply)
          + storage_class = "NORMAL"
        }
    
    Plan: 1 to add, 0 to change, 0 to destroy.
    
    ───────────────────────────────────────────────────────────────
    
    Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now.

2. terraform apply(创建Bucket)

该命令用于创建资源(Bucket)。

  • 执行以下命令创建配置文件test.tf。

    vim test.tf

  • 配置文件信息示例如下。

    resource "ksyun_ks3_bucket" "bucket" {
      provider = ks3.bj-prod
      bucket = "YOUR_BUCKET" 
      acl = "public-read" 
    }

  • 执行以下命令创建资源。

    terraform apply

  • 出现如下所示的日志,表示成功。

    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:
    
      # ksyun_ks3_bucket.bucket-new will be created
      + resource "ksyun_ks3_bucket" "bucket-new" {
          + acl           = "public-read"
          + bucket        = "test-terraform-bucket"
          + id            = (known after apply)
          + storage_class = "NORMAL"
        }
    
    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
    
    ksyun_ks3_bucket.bucket-new: Creating...
    ksyun_ks3_bucket.bucket-new: Creation complete after 0s [id=test-terraform-bucket]
    
    Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

3. terraform destroy(删除Bucket)

该命令用于删除资源(空Bucket)。

  • 执行以下命令创建配置文件test.tf。

    vim test.tf
  • 配置文件信息示例如下。

    resource "ksyun_ks3_bucket" "bucket" {
      provider = ks3.bj-prod
      bucket = "YOUR_BUCKET" 
      acl = "public-read" 
    }

  • 执行以下命令删除资源。

    terraform destroy

  • 出现如下所示的日志,表示成功。

    ksyun_ks3_bucket.bucket-new: Refreshing state... [id=test-terraform-bucket]
    
    Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
      - destroy
    
    Terraform will perform the following actions:
    
      # ksyun_ks3_bucket.bucket-new will be destroyed
      - resource "ksyun_ks3_bucket" "bucket-new" {
          - bucket        = "test-terraform-bucket" -> null
          - id            = "test-terraform-bucket" -> null
          - storage_class = "NORMAL" -> null
        }
    
    Plan: 0 to add, 0 to change, 1 to destroy.
    
    Do you really want to destroy all resources?
      Terraform will destroy all your managed infrastructure, as shown above.
      There is no undo. Only 'yes' will be accepted to confirm.
    
      Enter a value: yes
    
    ksyun_ks3_bucket.bucket-new: Destroying... [id=test-terraform-bucket]
    ksyun_ks3_bucket.bucket-new: Destruction complete after 1s
    
    Destroy complete! Resources: 1 destroyed.

4. terraform import(导入Bucket)

该命令可用于导入现有Bucket。

  • 执行以下命令创建配置文件。

    vim main.tf

  • 配置文件信息示例如下。

    resource "ksyun_ks3_bucket" "bucket" {
      provider = ks3.bj-prod
      bucket = "YOUR_BUCKET" 
      acl = "public-read" 
    }

  • 执行以下命令删除Bucket。

    terraform import ksyun_ks3_bucket.bucket terraform-test-bucket-import

  • 出现如下所示的日志,表示成功。

    ksyun_ks3_bucket.bucket: Importing from ID "terraform-test-bucket-import"...
    ksyun_ks3_bucket.bucket: Import prepared!
      Prepared ksyun_ks3_bucket for import
    ksyun_ks3_bucket.bucket: Refreshing state... [id=terraform-test-bucket-import]
    
    Import successful!
    
    The resources that were imported are shown above. These resources are now in
    your Terraform state and will henceforth be managed by Terraform.

注:更多Bucket配置操作示例请参见文档:Bucket配置操作示例

相关问题

当遇到由于网络延迟等原因造成的terraform init超时,导致无法正常下载Provider等情况时,可以通过配置镜像解决。

方式一

使用TF_CLI_CONFIG_FILE环境变量指定Terraform CLI配置文件的位置,任何此类文件都应遵循命名模式*.tfrc,创建配置文件config.tfrc,文件内容如下所示。

provider_installation {
  network_mirror {
    url = "https://ks3tools-public-read.ks3-cn-beijing.ksyuncs.com/registry.terraform.io"
  }
}
方式二

创建.terraformrcterraform.rc配置文件,文件位置取决于主机的操作系统。

  • Windows 环境:文件必须命名为terraform.rc,并放置在相关用户的%APPDATA%目录中。这个目录的物理位置取决于Windows 版本和系统配置。在PowerShell中使用$env:APPDATA可以找到其在系统上的位置。

  • 其他系统环境:必须将该文件命名为.terraformrc,并直接放在相关用户的主目录中。

文件内容如下:

provider_installation {
  network_mirror {
    url = "https://ks3tools-public-read.ks3-cn-beijing.ksyuncs.com/registry.terraform.io"
  }
}

文档导读
纯净模式常规模式

纯净模式

点击可全屏预览文档内容
文档反馈