使用 Terraform 快速部署 vm ESXi

大家好,我叫 Ivan,我是一名酗酒的系統管理員 (OPS)。

我想告訴您我如何在沒有 vCenter 的情況下使用 Terraform 在 ESXi 上部署虛擬機。

通常,您必須部署/重新創建虛擬機才能測試這個或那個應用程序。 由於懶惰,我想到了自動化這個過程。 我的搜索讓我找到了該公司的一款很棒的產品 哈希公司, 地貌.

我想很多人都知道 Terraform 是什麼,誰不知道,這是一個使用 IasC 概念管理任何云、基礎設施或服務的應用程序(基礎設施即代碼).

我使用 ESXi 作為我的虛擬化環境。 非常簡單、方便和可靠。
我期待一個問題。

既然可以使用 vCenter Server,為什麼還要進行 terraform?

你當然可以,但是。 首先,這是一個額外的許可證,其次,這個產品非常耗費資源,根本不適合我的家庭服務器,第三,升級技能的能力。

英特爾 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

接下來,我們需要 打包機,也是 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/打包機。

現在我們需要 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.

我們繼續前進。

在 gith 上,我準備了你需要的一切。

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

到文件夾 ISO 你需要把操作系統的分發包。 就我而言,這是 centos 7。

您還需要編輯該文件。 centos-7-base.json:

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

完成所有更改後,運行程序集:

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

如果一切配置和指定都正確,那麼您將看到操作系統自動安裝的圖片。 這個過程需要我 7-8 分鐘。

在文件夾中成功完成後 輸出打包機-centos7-x86_64 ova 文件將位於。

安裝地形:

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 的提供程序,因此您需要構建一個。

我們開始:

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 服務器的連接。

在文件中 網絡配置文件 包含未來虛擬機的網絡設置。 更改您的需求並運行單行程序:

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

那麼在文件中 主.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:我很樂意提出建設性的批評。

來源: www.habr.com

添加評論