使用 Terraform 快速部署 vm ESXi

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

我想告诉您我如何在没有 vCenter 的情况下使用 Terraform 在 ESXi 上部署虚拟机。

通常,您必须部署/重新创建虚拟机才能测试这个或那个应用程序。 由于懒惰,我想到了自动化这个过程。 我的搜索让我找到了该公司的一款很棒的产品 哈希公司, terraform.

我想很多人都知道 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

到文件夹 你需要把操作系统的分发包。 就我而言,这是 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

那么在文件中 主文件 将 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

添加评论