使用Terraform快速部署VM ESXi

大家好,我叫Ivan,我是酒精系统管理员(OPS)。

我想告诉您如何使用Terraform在不安装vCenter的ESXi上部署虚拟机。

通常,您必须部署/重新创建虚拟机来测试该应用程序。由于懒惰,我考虑过自动化流程。我的搜索导致我一个美妙的hashicorp产品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

在这种情况下,需要VNC连接到虚拟机并指定kickstart文件的路径。

实际上是在重启主机之前。此后,将必须重复此操作。

此外,我将在同一台服务器上的虚拟机中执行所有工作。

特点:

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 / 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.

继续。

我准备了所有关于gita的东西。

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

iso文件夹中,您需要放入操作系统的发行版。在我的情况下是centos7。

还需要编辑centos-7-base.json文件

variables:     
iso_urls:  
iso_checksum:    

完成所有更改后,运行程序集:

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

如果正确配置和指定了所有内容,那么您将看到自动安装操作系统的图片。
packer-centos7-x86_64 output will be in this color.

==> packer-centos7-x86_64: Retrieving ISO
    packer-centos7-x86_64: Using file in-place: file:///root/create-and-deploy-esxi/iso/CentOS-7-x86_64-Minimal-1908.iso
==> packer-centos7-x86_64: Remote cache was verified skipping remote upload...
==> packer-centos7-x86_64: Creating required virtual machine disks
==> packer-centos7-x86_64: Building and writing VMX file
==> packer-centos7-x86_64: Starting HTTP server on port 8494
==> packer-centos7-x86_64: Registering remote VM...
==> packer-centos7-x86_64: Starting virtual machine...
    packer-centos7-x86_64: The VM will be run headless, without a GUI. If you want to
    packer-centos7-x86_64: view the screen of the VM, connect via VNC with the password "" to
    packer-centos7-x86_64: vnc://10.10.10.10:5900
==> packer-centos7-x86_64: Waiting 7s for boot...
==> packer-centos7-x86_64: Connecting to VM via VNC (10.10.10.10:5900)
==> packer-centos7-x86_64: Typing the boot command over VNC...
==> packer-centos7-x86_64: Waiting for SSH to become available...



此过程需要7-8分钟。

成功完成后,ova文件将位于文件夹output-packer-centos7-x86_64中

安装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的提供程序,因此您需要构建它。

放手:

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

首先,编辑variables.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:我很乐意接受建设性的批评。

All Articles