序章

HashicorpのVaultは、動的なクラウド環境にシークレットと機密データを安全に保存するためのオープンソースツールです。 同じくHashicorpによって開発されたPackerTerraformを一緒に使用して、Vaultのイメージを作成およびデプロイできます。

このチュートリアルでは、Packerを使用して、Vaultがインストールされたシステムの不変のスナップショットを作成し、Terraformを使用してその展開を調整します。

このチュートリアルの詳細については、DigitalOceanでPackerとTerraformを使用してHashicorpVaultサーバーを構築する方法を参照してください。

前提条件

  • ローカルマシンにインストールされたPacker。 手順については、公式ドキュメントをご覧ください。
  • ローカルマシンにインストールされたTerraform。 ガイドについては、公式ドキュメントにアクセスしてください。
  • DigitalOceanアカウントの読み取りおよび書き込み権限を持つ個人用アクセストークン(APIキー)。 パーソナルアクセストークンの作成方法にアクセスして作成してください。
  • デプロイされたVaultDropletsでの認証に使用するSSHキー。ローカルマシンで利用可能であり、DigitalOceanアカウントに追加されます。 また、指紋も必要です。指紋を追加すると、アカウントのセキュリティページからコピーできます。 詳細な手順については、DigitalOceanのドキュメントまたはSSHキーの設定方法チュートリアルを参照してください。

ステップ1—パッカーテンプレートの作成

作成してに移動します ~/vault-orchestration Vaultファイルを保存するディレクトリ:

  1. mkdir ~/vault-orchestration
  2. cd ~/vault-orchestration

次のコマンドを実行して、PackerとTerraformの構成用に別々のディレクトリを作成します。

  1. mkdir packer terraform

Packerディレクトリに移動します。

  1. cd packer

テンプレート変数の使用

作成する variables.json あなたの中で packer プライベート変数データを保存するサブディレクトリ:

  1. nano variables.json

次の行を追加します。

〜/ vault-orchestration / packer / variables.json
{
  "do_token": "your_do_api_key",
  "base_system_image": "ubuntu-18-04-x64",
  "region": "nyc3",
  "size": "s-1vcpu-1gb"
}

作成しようとしているテンプレートでこれらの変数を使用します。 開発者向けドキュメントに従って、ベースイメージ、リージョン、およびドロップレットサイズの値を編集できます。

交換 your_do_api_key APIキーを使用して、ファイルを保存して閉じます。

ビルダーとプロビジョナーの作成

Vault用のPackerテンプレートを次の名前のファイルに作成します template.json:

  1. nano template.json

次の行を追加します。

〜/ vault-orchestration / packer / template.json
{
   "builders": [{
       "type": "digitalocean",
       "api_token": "{{user `do_token`}}",
       "image": "{{user `base_system_image`}}",
       "region": "{{user `region`}}",
       "size": "{{user `size`}}",
       "ssh_username": "root"
   }],
   "provisioners": [{
       "type": "shell",
       "inline": [
           "sleep 30",
           "sudo apt-get update",
           "sudo apt-get install unzip -y",
           "curl -L https://releases.hashicorp.com/vault/1.3.2/vault_1.3.2_linux_amd64.zip -o vault.zip",
           "unzip vault.zip",
           "sudo chown root:root vault",
           "mv vault /usr/local/bin/",
           "rm -f vault.zip"
       ]
}]
}

シングルを定義します digitalocean ビルダー。 Packerは、提供されたAPIキーを使用して、定義されたサイズ、画像、および領域の一時的なドロップレットを作成します。

プロビジョナーは、指定されたユーザー名でSSHを使用して接続し、ドロップレットからDigitalOceanスナップショットを作成して削除する前に、定義されたすべてのプロビジョナーを順番に実行します。

それはタイプです shell、ターゲット上で指定されたコマンドを実行します。 テンプレート内のコマンドは待機します 30 システムが起動するまで数秒かかり、Vault 1.3.2をダウンロードして解凍します。 Linuxの最新バージョンについては、公式Vaultダウンロードページを確認してください。

ファイルを保存して閉じます。

テンプレートの有効性を確認します。

  1. packer validate -var-file=variables.json template.json

次の出力が表示されます。

Output
Template validated successfully.

ステップ2—スナップショットを作成する

Packerを使用してスナップショットを作成します build 指図:

  1. packer build -var-file=variables.json template.json

たくさんの出力が表示されます。これは次のようになります。

Output
digitalocean: output will be in this color. ==> digitalocean: Creating temporary ssh key for droplet... ==> digitalocean: Creating droplet... ==> digitalocean: Waiting for droplet to become active... ==> digitalocean: Using ssh communicator to connect: ... ==> digitalocean: Waiting for SSH to become available... ==> digitalocean: Connected to SSH! ==> digitalocean: Provisioning with shell script: /tmp/packer-shell035430322 ... ==> digitalocean: % Total % Received % Xferd Average Speed Time Time Time Current ==> digitalocean: Dload Upload Total Spent Left Speed digitalocean: Archive: vault.zip ==> digitalocean: 100 45.5M 100 45.5M 0 0 154M 0 --:--:-- --:--:-- --:--:-- 153M digitalocean: inflating: vault ==> digitalocean: Gracefully shutting down droplet... ==> digitalocean: Creating snapshot: packer-1581537927 ==> digitalocean: Waiting for snapshot to complete... ==> digitalocean: Destroying droplet... ==> digitalocean: Deleting temporary ssh key... Build 'digitalocean' finished. ==> Builds finished. The artifacts of successful builds are: --> digitalocean: A snapshot was created: 'packer-1581537927' (ID: 58230938) in regions '...'

最後の行には、スナップショットの名前(たとえば、 packer-1581537927)と括弧内のID。ここで強調表示されています。 次の手順で必要になるため、スナップショットのIDをメモします。

APIエラーが原因でビルドプロセスが失敗した場合は、数分待ってから再試行してください。

ステップ3—Terraform構成の記述

に移動します terraform サブディレクトリ:

  1. cd ~/vault-orchestration/terraform

名前の付いたファイルを作成します do-provider.tf プロバイダーを保存するには:

  1. nano do-provider.tf

次の行を追加します。

〜/ vault-orchestration / terraform / do-provider.tf
variable "do_token" {
}

variable "ssh_fingerprint" {
}

variable "instance_count" {
default = "1"
}

variable "do_snapshot_id" {
}

variable "do_name" {
default = "vault"
}

variable "do_region" {
}

variable "do_size" {
}

variable "do_private_networking" {
default = true
}

provider "digitalocean" {
token = var.do_token
}

このファイルは、デジタルオーシャンプロバイダーにAPIキーを提供します。 これらの変数の値を指定するには、Packerと同様に変数定義ファイルを作成します。 ファイル名はいずれかで終わる必要があります .tfvars また .tfvars.json.

ファイルを保存して閉じます。

変数定義ファイルを作成します。

  1. nano definitions.tfvars

次の行を追加します。

〜/ vault-orchestration / terraform /definitions.tf
do_token         = "your_do_api_key"
ssh_fingerprint  = "your_ssh_key_fingerprint"
do_snapshot_id   = your_do_snapshot_id
do_name          = "vault"
do_region        = "nyc3"
do_size          = "s-1vcpu-1gb"
instance_count   = 1

交換 your_do_api_key, your_ssh_key_fingerprint、 と your_do_snapshot_id (前の手順でメモしたスナップショットID)。 The do_regiondo_size パラメータは、Packer変数ファイルと同じ値である必要があります。

ファイルを保存して閉じます。

次のファイルを作成して、Vaultスナップショットの配置構成を保存します。

  1. nano deployment.tf

次の行を追加します。

〜/ vault-orchestration / terraform / deployment.tf
resource "digitalocean_droplet" "vault" {
count              = var.instance_count
image              = var.do_snapshot_id
name               = var.do_name
region             = var.do_region
size               = var.do_size
private_networking = var.do_private_networking
ssh_keys = [
  var.ssh_fingerprint
]
}

output "instance_ip_addr" {
value = {
  for instance in digitalocean_droplet.vault:
  instance.id => instance.ipv4_address
}
description = "The IP addresses of the deployed instances, paired with their IDs."
}

タイプの単一のリソースを定義します digitalocean_droplet 名前付き vault. 変数値に従ってパラメーターを設定し、(フィンガープリントを使用して)SSHキーをDigitalOceanアカウントからDropletリソースに追加します。 君は output コンソールに新しくデプロイされたすべてのインスタンスのIPアドレス。

ファイルを保存して閉じます。

ディレクトリをTerraformプロジェクトとして初期化します。

  1. terraform init

次の出力が表示されます。

Output
Initializing the backend... 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.digitalocean: version = "~> 1.14" 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.

ステップ4—Terraformを使用したVaultのデプロイ

構成の有効性をテストします。

  1. terraform validate

次の出力が表示されます。

Output
Success! The configuration is valid.

を実行します plan インフラストラクチャのプロビジョニングに関してTerraformが何を試みるかを確認するコマンド:

  1. terraform plan -var-file="definitions.tfvars"

出力は次のようになります。

Output
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. ------------------------------------------------------------------------ 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: # digitalocean_droplet.vault[0] will be created + resource "digitalocean_droplet" "vault" { ... } Plan: 1 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.

計画を実行します。

  1. terraform apply -var-file="definitions.tfvars"

ドロップレットはプロビジョニングを終了し、次のような出力が表示されます。

Output
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: + digitalocean_droplet.vault-droplet ... Plan: 1 to add, 0 to change, 0 to destroy. ... digitalocean_droplet.vault-droplet: Creating... ... Apply complete! Resources: 1 added, 0 changed, 0 destroyed. Outputs: instance_ip_addr = { "181254240" = "your_new_server_ip" }

ステップ5—デプロイされたドロップレットを確認する

次のコマンドを実行して、新しいドロップレットに接続します。

  1. ssh root@your_server_ip

ログインしたら、次のコマンドでVaultを実行します。

  1. vault

その「ヘルプ」出力が表示されます。

Output
Usage: vault <command> [args] Common commands: read Read data and retrieves secrets write Write data, configuration, and secrets delete Delete secrets and configuration list List data or secrets login Authenticate locally agent Start a Vault agent server Start a Vault server status Print seal and HA status unwrap Unwrap a wrapped secret Other commands: audit Interact with audit devices auth Interact with auth methods debug Runs the debug command kv Interact with Vault's Key-Value storage lease Interact with leases namespace Interact with namespaces operator Perform operator-specific tasks path-help Retrieve API help for paths plugin Interact with Vault plugins and catalog policy Interact with policies print Prints runtime configurations secrets Interact with secrets engines ssh Initiate an SSH session token Interact with tokens

結論

これで、TerraformとPackerを使用してHashicorpVaultをDigitalOceanDropletsにデプロイするための自動システムができました。 Vaultの使用を開始するには、初期化して、さらに構成する必要があります。 その方法については、公式ドキュメントにアクセスしてください。

Terraformを使用したその他のチュートリアルについては、Terraformコンテンツページをご覧ください。