🦏

OpenTofuで ASR環境を構築

2024/05/03に公開

ASRとはなにものなのか。必要となるリソースとその説明。ASRの操作方法等は一切合切省略します。
それらについての説明は先輩諸氏のサイトを参考にしてください。
ここではただ OpenTofu で構築したときの資材と操作方法の概要のみ提示します。

参考

https://qiita.com/k-yasuhiro/items/6a59e95e7a979c8170f0

構築資材

商用環境だと閉域についても考慮していく必要があることも多かろうと思いますが、
こちらは閉域構成とか考慮しない構成となっています。

asr_main.tf ファイル

resource group, vnet, nsg, storege account, recovery service vault, vm 等を構築する tf ファイル。
Linux VMのカーネルダウングレードを考慮して、レプリケーションの設定は 別のtf ファイルに分けている。

  • バックエンドについては適宜設定してください。
  • https://example.net/getaddress/ については、アクセスすると { "sourceip": "x.x.x.x" } といった形でアクセス元グローバルIPアドレスを返すURLを指定します。
asr_replication.tf ファイル

レプリケーションの設定を記載したファイル。Linux VMのカーネルダウングレードを実施してから適用する。
一つのtfファイルにまとめていても、要領よくやれば vmが立ち上がってからレプリケーション開始までの間にカーネルダウングレードを実行することは時間的に可能だけど、念のため分けている。

asr_parameters.tfvars ファイル

資材の利用方法

構築

  1. 上記資材をカレントフォルダに保存
  2. asr_main.tf、asr_parameters.tfvars の内容を修正。
  3. init を実行
tofu validate
tofu init
  1. asr_replication.tfは退避して、asr_main.tf のみ適用
mv asr_replication.tf asr_replication.tf.tmp
tofu plan -var-file=asr_parameters.tfvars -out=tofu.out
tofu apply "tofu.out"
  1. 必要なら VM のカーネルをダウングレード
  2. asr_replication.tf を適用
mv asr_replication.tf.tmp asr_replication.tf
tofu plan -var-file=asr_parameters.tfvars -var disk_id=$(az disk list --query "[?name=='<environment>_vm_OsDisk' && location=='japaneast'].id" --output tsv) -out=tofu.out
tofu apply "tofu.out"

レプリケーションの有効化とディスク同期の間ずっと待機になるので、相当時間かかる。(30分ぐらい)
フェールオーバー、再保護(ストレージアカウントの指定を忘れずに)、フェールバック等の操作は、Recovery Services コンテナーの「復旧計画(Recovery Plan)」の画面で行う。

replication のやり直し

  1. Azureの管理画面でレプリケーションを無効化
  2. 復旧計画を削除。tofu の管理下からレプリケーションを除外。
tofu plan -var-file=asr_parameters.tfvars -var disk_id=$(az disk list --query "[?name=='<environment>_vm_OsDisk' && location=='japaneast'].id" --output tsv) -out=tofu.out -destroy -target=azurerm_site_recovery_replication_recovery_plan.recovery_plan
tofu state rm azurerm_site_recovery_replicated_vm.replicated_vm
  1. tofu で再度 apply する
tofu plan -var-file=asr_parameters.tfvars -var disk_id=$(az disk list --query "[?name=='<environment>_vm_OsDisk' && location=='japaneast'].id" --output tsv) -out=tofu.out
tofu apply "tofu.out"

vault の作り直し

  1. Azureの管理画面でレプリケーションを無効化。tofu の管理下からレプリケーションを除外。
  2. vaultとvaultに依存しているリソースをdestroy
tofu plan -destroy -target=azurerm_recovery_services_vault.vault -var-file=asr_parameters.tfvars -var disk_id=$(az disk list --query "[?name=='<environment>_vm_OsDisk' && location=='japaneast'].id" --output tsv) -out=tofu.out
tofu apply "tofu.out"

削除に失敗したら、管理画面から削除後、下記を実行。

tofu state list | grep rec
tofu state list | grep rec | xargs tofu state rm

全削除

  1. destroy する
tofu plan -destroy -var-file=asr_parameters.tfvars -var disk_id=$(az disk list --query "[?name=='<environment>_vm_OsDisk' && location=='japaneast'].id" --output tsv) -out=tofu.out
tofu apply "tofu.out"
  1. 消えないリソースがあったら管理画面から手動で削除
  2. state ファイルから手動で削除
tofu state list
tofu state list | xargs tofu state rm

一部リソースの破棄

-target オプションで、破棄するリソースを指定する。複数リソースを破棄するときは、-target オプションを複数回指定する。

VM、NIC、パブリックIPを削除するplanの例。

$ tofu plan -destroy -target=azurerm_linux_virtual_machine.vm -target=azurerm_network_interface.nic -target=azurerm_public_ip.pip -var-file=asr_parameters.tfvars -out tfplan.out
:
:
OpenTofu used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  - destroy

OpenTofu will perform the following actions:
:
:
Plan: 0 to add, 0 to change, 3 to destroy.
╷
│ Warning: Resource targeting is in effect
│
│ You are creating a plan with the -target option, which means that the result of this plan may not represent all of the changes requested by the current configuration.
│
│ The -target option is not for routine use, and is provided only for exceptional situations such as recovering from errors or mistakes, or when OpenTofu specifically suggests to use it as part of an error message.
╵

Saved the plan to: tfplan.out

To perform exactly these actions, run the following command to apply:
    tofu apply "tfplan.out"

out ファイルを適用して実際に削除する。

$ tofu apply "tfplan.out"
:
:
╷
│ Warning: Applied changes may be incomplete
│
│ The plan was created with the -target option in effect, so some changes requested in the configuration may have been ignored and the output values may not be fully updated. Run the following command to verify that no other changes are
│ pending:
│     tofu plan
│
│ Note that the -target option is not suitable for routine use, and is provided only for exceptional situations such as recovering from errors or mistakes, or when OpenTofu specifically suggests to use it as part of an error message.
╵

Apply complete! Resources: 0 added, 0 changed, 3 destroyed.

Outputs:

public_ip_address = "x.x.x.x"

削除したリソースの再作成

通常どおり plan を実行。

$ tofu plan -var-file=parameters.tfvars -out tfplan.out
:
:
OpenTofu used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

OpenTofu will perform the following actions:
:
:
Plan: 3 to add, 0 to change, 0 to destroy.

Changes to Outputs:
  ~ public_ip_address = "x.x.x.x" -> (known after apply)

Saved the plan to: tfplan.out

To perform exactly these actions, run the following command to apply:
    tofu apply "tfplan.out"

通常どおり apply を実行。

$ tofu apply "tfplan.out"
:
:
Apply complete! Resources: 3 added, 0 changed, 0 destroyed.

Outputs:

public_ip_address = ""

作成したパブリックIPの値が入っていないので plan を実行して確認してみる。

$ tofu plan -var-file=parameters.tfvars -out tfplan.out


Note: Objects have changed outside of OpenTofu

OpenTofu detected the following changes made outside of OpenTofu since the
last "tofu apply" which may have affected this plan:

  # azurerm_public_ip.pip has changed
  ~ resource "azurerm_public_ip" "pip" {
        id                      = "/subscriptions/<subscription id>/resourceGroups/<environment>/providers/Microsoft.Network/publicIPAddresses/<environment>_pip"
      + ip_address              = "x.x.x.x"
        name                    = "<environment>_pip"
        # (8 unchanged attributes hidden)
    }


Unless you have made equivalent changes to your configuration, or ignored the
relevant attributes using ignore_changes, the following plan may include
actions to undo or respond to these changes.

─────────────────────────────────────────────────────────────────────────────

Changes to Outputs:
  ~ public_ip_address = "" -> "x.x.x.x"

You can apply this plan to save these new output values to the OpenTofu
state, without changing any real infrastructure.

─────────────────────────────────────────────────────────────────────────────

Saved the plan to: tfplan.out

To perform exactly these actions, run the following command to apply:
    tofu apply "tfplan.out"

tofu 外の操作で変更されたというのは事実と異なるので気に食わないところだが、
文句を言っても仕方ないのでおとなしく apply を実行する。

$ tofu apply "tfplan.out" | cat

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

public_ip_address = "x.x.x.x"

リソースの完全破棄

なお、tofu destroy 〰 は tofu plan -destroy のエイリアスとのこと。

$ tofu plan -destroy -var-file=parameters.tfvars -out tfplan.out | cat
:
:
OpenTofu used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
  - destroy

OpenTofu will perform the following actions:
:
:
Plan: 0 to add, 0 to change, 27 to destroy.

Changes to Outputs:
  - public_ip_address = "x.x.x.x" -> null

─────────────────────────────────────────────────────────────────────────────

Saved the plan to: tfplan.out

To perform exactly these actions, run the following command to apply:
    tofu apply "tfplan.out"

out ファイルを適用して実際に削除する。

$ tofu apply "tfplan.out" | cat
:
:
Apply complete! Resources: 0 added, 0 changed, 27 destroyed.
GitHubで編集を提案

Discussion