Terraform を使用した VM ESXi のクイック展開

みなさん、こんにちは。私の名前は Ivan です。アルコール依存症システム管理者 (OPS) です。

Terraform を使用して、vCenter を使用せずに ESXi に仮想マシンをデプロイする方法を説明したいと思います。

多くの場合、特定のアプリケーションをテストするために、仮想マシンをデプロイ/再作成する必要があります。 怠惰なので、プロセスを自動化することを考えました。 検索した結果、その会社の素晴らしい製品にたどり着きました ハシコープ, テラフォーム.

Terraform が何であるかを多くの人が知っていると思いますが、これは IasC コンセプトを使用してクラウド、インフラストラクチャ、サービスを管理するためのアプリケーションです (コードとしてのインフラストラクチャ).

仮想化環境として ESXi を使用しています。 非常にシンプル、便利、そして信頼性があります。
質問を予想しています。

vCenter Server を使用できるのに、なぜ Terraform を使用するのでしょうか?

もちろんできますが、 第一に、これは追加ライセンスであること、第二に、この製品は非常にリソースを大量に消費するため、私のホーム サーバーには適合しないこと、そして第三に、スキルをアップグレードする機能であることです。

Intel NUC プラットフォームはサーバーとして機能します。

CPU: 2 CPUs x Intel(R) Core(TM) i3-4010U CPU @ 1.70GHz
RAM: 8Gb
HDD: 500Gb
ESXi version: ESXi-6.5.0-4564106-standard (VMware, Inc.)

ということで、まず第一に。

とりあえず、esxi を設定しましょう。つまり、ファイアウォール設定で VNC ポートを開きます。

デフォルトでは、ファイルは書き込み保護されています。 次の操作を実行します。

chmod 644 /etc/vmware/firewall/service.xml
chmod +t /etc/vmware/firewall/service.xml
vi /etc/vmware/firewall/service.xml

ファイルの末尾に次のブロックを追加します。

<service id="1000">
  <id>packer-vnc</id>
  <rule id="0000">
    <direction>inbound</direction>
    <protocol>tcp</protocol>
    <porttype>dst</porttype>
    <port>
      <begin>5900</begin>
      <end>6000</end>
    </port>
  </rule>
  <enabled>true</enabled>
  <required>true</required>
</service>

終了して保存します。 権限を元に戻してサービスを再起動します。

chmod 444 /etc/vmware/firewall/service.xml
esxcli network firewall refresh

ホストが再起動されるまで実際。 その後、この操作を繰り返す必要があります。

さらに、すべての作業を同じサーバー上の仮想マシンで実行します。

特徴:

OS: Centos 7 x86_64 minimal
RAM: 1GB
HDD: 20GB
Selinux: disable
firewalld: disable

次に必要なのは、 packer、これもHashiCorpの製品です。

「ゴールデン」イメージを自動的に組み立てるのに必要です。 将来的にはこれを使用します。

yum install unzip git -y
curl -O https://releases.hashicorp.com/packer/1.5.5/packer_1.5.5_linux_amd64.zip
unzip packer_1.5.5_linux_amd64.zip -d /usr/bin && rm -rf packer_1.5.5_linux_amd64.zip
packer version
Packer v1.5.5

移動して パッカーのバージョン RedHat ベースには同じ名前のパッケージが含まれている可能性があるため、エラーが発生する可能性があります。

which -a packer
/usr/sbin/packer

解決策として、シンボリックリンクを作成するか、絶対パスを使用できます。 /usr/bin/packer。

次に ovftool が必要です ダウンロードリンク。 ダウンロードしてサーバーに置き、インストールします。

chmod +x VMware-ovftool-4.4.0-15722219-lin.x86_64.bundle
./VMware-ovftool-4.4.0-15722219-lin.x86_64.bundle
Extracting VMware Installer...done.
You must accept the VMware OVF Tool component for Linux End User
License Agreement to continue.  Press Enter to proceed.
VMWARE END USER LICENSE AGREEMENT
Do you agree? [yes/no]:yes
The product is ready to be installed.  Press Enter to begin
installation or Ctrl-C to cancel. 
Installing VMware OVF Tool component for Linux 4.4.0
    Configuring...
[######################################################################] 100%
Installation was successful.

次に進みます。

必要なものはすべて準備しました。

git clone https://github.com/letnab/create-and-deploy-esxi.git && cd create-and-deploy-esxi

フォルダへ イソ オペレーティングシステムの配布キットを入れる必要があります。 私の場合はcentos 7です。

ファイルを編集する必要もあります。 centos-7-base.json:

variables: указать свои данные для подключения
iso_urls: указать актуальный
iso_checksum: чексумма вашего образа 

すべての変更を行った後、アセンブリを実行します。

/usr/bin/packer build centos-7-base.json

すべてが正しく構成および指定されている場合は、オペレーティング システムの自動インストールの図が表示されます。 このプロセスには 7 ~ 8 分かかります。

フォルダー内で正常に完了したら 出力-packer-centos7-x86_64 ova ファイルが見つかります。

Terraform をインストールします。

curl -O https://releases.hashicorp.com/terraform/0.12.24/terraform_0.12.24_linux_amd64.zip
unzip terraform_0.12.24_linux_amd64.zip -d /usr/bin/ && rm -rf terraform_0.12.24_linux_amd64.zip
terraform version
Terraform v0.12.24

Terraform には ESXi 用のプロバイダーがないため、プロバイダーを構築する必要があります。

go を入れます:

cd /tmp
curl -O https://dl.google.com/go/go1.14.2.linux-amd64.tar.gz
tar -C /usr/local -xzf go1.14.2.linux-amd64.tar.gz && rm -rf go1.14.2.linux-amd64.tar.gz
export PATH=$PATH:/usr/local/go/bin
go version
go version go1.14.2 linux/amd64

次に、プロバイダーを収集します。

go get -u -v golang.org/x/crypto/ssh
go get -u -v github.com/hashicorp/terraform
go get -u -v github.com/josenk/terraform-provider-esxi
export GOPATH="$HOME/go"
cd $GOPATH/src/github.com/josenk/terraform-provider-esxi
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -ldflags '-w -extldflags "-static"' -o terraform-provider-esxi_`cat version`
cp terraform-provider-esxi_`cat version` /usr/bin

私たちはゴールラインにいます。 イメージを展開してみましょう。

フォルダーに移動しましょう:

cd /root/create-and-deploy-esxi/centos7

まず、ファイルを編集します 変数.tf。 ESXi サーバーへの接続を指定する必要があります。

ファイル内 network_config.cfg 将来の仮想マシンのネットワーク設定が含まれています。 ニーズに合わせて変更し、ワンライナーを実行します。

sed -i -e '2d' -e '3i "network": "'$(gzip < network_config.cfg| base64 | tr -d 'n')'",' metadata.json

さて、ファイルの中で main.tf ova ファイルへのパスが異なる場合は、独自のパスに変更します。

真実の瞬間。

terraform init
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.esxi: version = "~> 1.6"
* provider.template: version = "~> 2.1"

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.

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.template_file.Default: Refreshing state...
data.template_file.network_config: 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:

  # esxi_guest.Default will be created
  + resource "esxi_guest" "Default" {
      + boot_disk_size         = (known after apply)
      + disk_store             = "datastore1"
      + guest_name             = "centos7-test"
      + guest_shutdown_timeout = (known after apply)
      + guest_startup_timeout  = (known after apply)
      + guestinfo              = {
          + "metadata"          = "base64text"
          + "metadata.encoding" = "gzip+base64"
          + "userdata"          = "base64text"
          + "userdata.encoding" = "gzip+base64"
        }
      + guestos                = (known after apply)
      + id                     = (known after apply)
      + ip_address             = (known after apply)
      + memsize                = "1024"
      + notes                  = (known after apply)
      + numvcpus               = (known after apply)
      + ovf_properties_timer   = (known after apply)
      + ovf_source             = "/root/create-and-deploy-esxi/output-packer-centos7-x86_64/packer-centos7-x86_64.ova"
      + power                  = "on"
      + resource_pool_name     = (known after apply)
      + virthwver              = (known after apply)

      + network_interfaces {
          + mac_address     = (known after apply)
          + nic_type        = (known after apply)
          + virtual_network = "VM Network"
        }
    }

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.

終了:

terraform apply

すべてが正しく行われた場合、2 ~ 3 分で、以前に作成したイメージから新しい仮想マシンがデプロイされます。

これらすべての用途はあなたの想像力によってのみ制限されます。

私はベスト プラクティスを共有し、これらの製品を使用する際の主要なポイントを示したかっただけです。

ご清聴ありがとうございました!

PS: 建設的な批判は喜んでさせていただきます。

出所: habr.com

コメントを追加します