用Terraform玩转Sakura Cloud
2020/04/30添加
请注意,v2版本于2020年1月31日作为正式版发布。本文档的内容是基于v1版本的,请注意。有关v2版本的详细信息,请查阅以下文档。
-
- ドキュメント: https://docs.usacloud.jp/terraform/
v2での変更点: https://docs.usacloud.jp/terraform/guides/upgrade_to_v2.0.0/
你好,我是Kame Neko。
最近由於一些原因,我一直在研究櫻雲的用法,不過現在在控制面板上部署服務器變得非常困難。
我需要頻繁地準備和刪除特定環境的服務器,一直在尋找解決方法,結果找到了,那就是Terraform。
这次我们将介绍一下在樱云上使用Terraform进行配置的步骤,尽管有点晚。
安装Terraform
首先,让我们安装Terraform。这次我们将使用CentOS7进行安装。
$ cat /etc/*-release
CentOS Linux release 7.6.1810 (Core)
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"
CentOS Linux release 7.6.1810 (Core)
CentOS Linux release 7.6.1810 (Core)
下载/解压Terraform二进制文件。
在主目录下创建文件夹,并在该文件夹下解压二进制文件。
通过复制粘贴公式网站的链接获取二进制文件。由于本次是CentOS7,因此下载Linux 64位版本。
下载Terraform – 由HashiCorp开发的Terraform
$ mkdir ~/terraform
$ cd ~/terraform
$ wget https://releases.hashicorp.com/terraform/0.11.11/terraform_0.11.11_linux_amd64.zip
$ ls
terraform_0.11.11_linux_amd64.zip
$ unzip terraform_0.11.11_linux_amd64.zip
Archive: terraform_0.11.11_linux_amd64.zip
inflating: terraform
$ ls
terraform terraform_0.11.11_linux_amd64.zip
给已经解压的二进制文件添加路径。
$ export PATH=$PATH:~/terraform/
确认能够执行。
$ terraform
Usage: terraform [-version] [-help] <command> [args]
The available commands for execution are listed below.
The most common, useful commands are shown first, followed by
less common or more advanced commands. If you're just getting
started with Terraform, stick with the common commands. For the
other commands, please read the help and docs before usage.
Common commands:
apply Builds or changes infrastructure
console Interactive console for Terraform interpolations
destroy Destroy Terraform-managed infrastructure
env Workspace management
fmt Rewrites config files to canonical format
get Download and install modules for the configuration
graph Create a visual graph of Terraform resources
import Import existing infrastructure into Terraform
init Initialize a Terraform working directory
output Read an output from a state file
plan Generate and show an execution plan
providers Prints a tree of the providers used in the configuration
push Upload this Terraform module to Atlas to run
refresh Update local state file against real resources
show Inspect Terraform state or plan
taint Manually mark a resource for recreation
untaint Manually unmark a resource as tainted
validate Validates the Terraform files
version Prints the Terraform version
workspace Workspace management
All other commands:
debug Debug output management (experimental)
force-unlock Manually unlock the terraform state
state Advanced state management
如果能提供上述的帮助,那就可以了。
写构建文件
初めに、下記の記事を参考にして、適当な配置ファイルを作成してみました。
《Terraform さくらのクラウド开发指南(第三部分)〜在さくらのクラウド上构建基础设施〜 | さくらのナレッジ》
另外,由于上述的文章还没有存档ID,会被指责,所以我参考了以下内容并进行了部分修改。
ディスク – さくらのクラウドに対するTerraform
$ mkdir ~/terraform/create-server/
$ cd ~/terraform/create-server/
$ vim create-server.tf
resource "sakuracloud_disk" "disk01"{
name = "disk1"
source_archive_id = "${data.sakuracloud_archive.centos.id}"
}
data sakuracloud_archive "centos" {
os_type = "centos"
}
resource "sakuracloud_server" "server01" {
name = "server01"
disks = ["${sakuracloud_disk.disk01.id}"]
tags = ["@virtio-net-pci"]
password = "password"
}
准备API密钥
请根据此参考设置API密钥。
中国版:
安装指南 – Terraform for さくらのクラウド | 获取さくらのクラウド API 密钥
$ export SAKURACLOUD_ACCESS_TOKEN=hogehoge
$ export SAKURACLOUD_ACCESS_TOKEN_SECRET=fugafuga
$ export SAKURACLOUD_ZONE=tk1a
安装插件
我们将安装外部提供商。通过这个外部提供商,似乎可以使用除了官方提供的云服务之外的其他服务。
我们将从以下位置下载提供程序的二进制文件。这次我们将使用最近发布的v2.0.0-alpha版本,该版本仅支持Enhanced Load Balancer。
在主目录下创建一个专用目录,并解压缩二进制文件。
$ mkdir -p ~/.terraform.d/plugins/
$ cd ~/.terraform.d/plugins/
$ wget https://github.com/sacloud/terraform-provider-sakuracloud/releases/download/v2.0.0-alpha.3/terraform-provider-sakuracloud_2.0.0_linux-amd64.zip
$ ls
terraform-provider-sakuracloud_2.0.0_linux-amd64.zip
$ unzip terraform-provider-sakuracloud_2.0.0_linux-amd64.zip
Archive: terraform-provider-sakuracloud_2.0.0_linux-amd64.zip
inflating: terraform-provider-sakuracloud_v2.0.0_x5
$ ls
terraform-provider-sakuracloud_2.0.0_linux-amd64.zip terraform-provider-sakuracloud_v2.0.0_x5
在安装展开的插件之前,请在存放.tf文件的目录中执行terraform init命令来进行安装。
$ cd ~/terraform/create-server/
$ terraform init
Initializing provider plugins...
The following providers do not have any version constraints in configuration,
so the latest version was installed.
To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = "..." constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.
* provider.sakuracloud: version = "~> 2.0"
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.
如果按照上述的方式出现,那就说明安装完成了。
我尝试去执行
现在,让我们立即执行吧。
首先,使用”plan”命令来确认内容。
$ terraform plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.
data.sakuracloud_archive.centos: Refreshing state...
------------------------------------------------------------------------
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
+ sakuracloud_disk.disk01
id: <computed>
connector: "virtio"
graceful_shutdown_timeout: "60"
name: "disk1"
plan: "ssd"
server_id: <computed>
size: "20"
source_archive_id: "113100368797"
tags.#: <computed>
zone: <computed>
+ sakuracloud_server.server01
id: <computed>
additional_display_ipaddresses.#: <computed>
cdrom_id: <computed>
core: "1"
disks.#: <computed>
display_ipaddress: <computed>
dns_servers.#: <computed>
gateway: <computed>
graceful_shutdown_timeout: "60"
interface_driver: "virtio"
ipaddress: <computed>
macaddresses.#: <computed>
memory: "1"
name: "server01"
nic: "shared"
nw_address: <computed>
nw_mask_len: <computed>
packet_filter_ids.#: <computed>
password: <sensitive>
private_host_name: <computed>
tags.#: "1"
tags.0: "@virtio-net-pci"
zone: <computed>
Plan: 2 to add, 0 to change, 0 to destroy.
------------------------------------------------------------------------
Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.
我会尝试申请
$ terraform apply
data.sakuracloud_archive.centos: Refreshing state...
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
+ sakuracloud_disk.disk01
id: <computed>
connector: "virtio"
graceful_shutdown_timeout: "60"
name: "disk1"
plan: "ssd"
server_id: <computed>
size: "20"
source_archive_id: "113100368797"
tags.#: <computed>
zone: <computed>
+ sakuracloud_server.server01
id: <computed>
additional_display_ipaddresses.#: <computed>
cdrom_id: <computed>
core: "1"
disks.#: <computed>
display_ipaddress: <computed>
dns_servers.#: <computed>
gateway: <computed>
graceful_shutdown_timeout: "60"
interface_driver: "virtio"
ipaddress: <computed>
macaddresses.#: <computed>
memory: "1"
name: "server01"
nic: "shared"
nw_address: <computed>
nw_mask_len: <computed>
packet_filter_ids.#: <computed>
password: <sensitive>
private_host_name: <computed>
tags.#: "1"
tags.0: "@virtio-net-pci"
zone: <computed>
Plan: 2 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 # ← 実行しても良いか聞かれるのでyesとタイプ
sakuracloud_disk.disk01: Creating...
connector: "" => "virtio"
graceful_shutdown_timeout: "" => "60"
name: "" => "disk1"
plan: "" => "ssd"
server_id: "" => "<computed>"
size: "" => "20"
source_archive_id: "" => "113100368797"
tags.#: "" => "<computed>"
zone: "" => "<computed>"
sakuracloud_disk.disk01: Still creating... (10s elapsed)
sakuracloud_disk.disk01: Still creating... (20s elapsed)
sakuracloud_disk.disk01: Still creating... (30s elapsed)
sakuracloud_disk.disk01: Still creating... (40s elapsed)
sakuracloud_disk.disk01: Still creating... (50s elapsed)
sakuracloud_disk.disk01: Still creating... (1m0s elapsed)
sakuracloud_disk.disk01: Still creating... (1m10s elapsed)
sakuracloud_disk.disk01: Still creating... (1m20s elapsed)
sakuracloud_disk.disk01: Creation complete after 1m28s (ID: 113100456223)
sakuracloud_server.server01: Creating...
additional_display_ipaddresses.#: "" => "<computed>"
cdrom_id: "" => "<computed>"
core: "" => "1"
disks.#: "" => "1"
disks.0: "" => "113100456223"
display_ipaddress: "" => "<computed>"
dns_servers.#: "" => "<computed>"
gateway: "" => "<computed>"
graceful_shutdown_timeout: "" => "60"
interface_driver: "" => "virtio"
ipaddress: "" => "<computed>"
macaddresses.#: "" => "<computed>"
memory: "" => "1"
name: "" => "server01"
nic: "" => "shared"
nw_address: "" => "<computed>"
nw_mask_len: "" => "<computed>"
packet_filter_ids.#: "" => "<computed>"
password: "<sensitive>" => "<sensitive>"
private_host_name: "" => "<computed>"
tags.#: "" => "1"
tags.0: "" => "@virtio-net-pci"
zone: "" => "<computed>"
sakuracloud_server.server01: Still creating... (10s elapsed)
sakuracloud_server.server01: Still creating... (20s elapsed)
sakuracloud_server.server01: Still creating... (30s elapsed)
sakuracloud_server.server01: Still creating... (40s elapsed)
sakuracloud_server.server01: Still creating... (50s elapsed)
sakuracloud_server.server01: Still creating... (1m0s elapsed)
sakuracloud_server.server01: Creation complete after 1m4s (ID: 113100456235)
Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
结束
如果觉得这个结构很麻烦的话,意外地并没有什么大不了的。
使用这个方法可以轻松地建立大量的服务器!
链接
-
- Terraform by HashiCorp
-
- Terraform for さくらのクラウド
-
- sacloud/terraform-provider-sakuracloud: Terraform for さくらのクラウド | GitHub.com
- Terraform for さくらのクラウド スタートガイド (第三回)〜さくらのクラウド上にインフラ構築〜 | さくらのナレッジ