Mabilis na I-deploy ang vm ESXi gamit ang Terraform

Kumusta sa lahat, ang pangalan ko ay Ivan at ako ay isang alcoholic system administrator (OPS).

Gusto kong sabihin sa iyo kung paano ako nag-deploy ng mga virtual machine sa ESXi nang walang vCenter na gumagamit ng Terraform.

Kadalasan, kailangan mong i-deploy / muling likhain ang mga virtual machine upang masubukan ito o ang application na iyon. Dahil sa katamaran, naisipan kong i-automate ang proseso. Ang aking paghahanap ay humantong sa akin sa isang kahanga-hangang produkto ng kumpanya hashicorp, terraform.

Sa tingin ko maraming tao ang nakakaalam kung ano ang Terraform, at sino ang hindi nakakaalam, ito ay isang application para sa pamamahala ng anumang ulap, imprastraktura o serbisyo gamit ang konsepto ng IasC (Imprastraktura bilang code).

Ginagamit ko ang ESXi bilang aking virtualization environment. Medyo simple, maginhawa at maaasahan.
Inaasahan ko ang isang tanong.

Bakit terraform kung maaari mong gamitin ang vCenter Server?

Maaari mo siyempre, ngunit. Una, ito ay isang karagdagang lisensya, pangalawa, ang produktong ito ay napaka-resource-intensive at simpleng hindi akma sa aking home server, at pangatlo, ang kakayahang mag-upgrade ng mga kasanayan.

Ang Intel NUC platform ay gumaganap bilang isang server:

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

At kaya, una sa lahat.

Sa ngayon, i-set up natin ang esxi, ibig sabihin, buksan ang VNC port sa mga setting ng firewall.

Bilang default, ang file ay protektado ng sulat. Isinasagawa namin ang mga sumusunod na manipulasyon:

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

idagdag ang sumusunod na bloke sa dulo ng file:

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

Lumabas, i-save. Baguhin ang mga karapatan pabalik at i-restart ang serbisyo:

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

Aktwal hanggang sa ma-reboot ang host. Pagkatapos nito, ang pagmamanipula na ito ay kailangang ulitin.

Dagdag pa, isasagawa ko ang lahat ng gawain sa isang virtual machine sa parehong server.

Mga pagtutukoy ng produkto:

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

Susunod, kailangan namin packer, produkto din ng HashiCorp.

Ito ay kinakailangan upang awtomatikong tipunin ang "ginintuang" imahe. na gagamitin natin sa hinaharap.

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

Gumagalaw na bersyon ng packer maaaring magkaroon ng error, dahil ang RedHat-based ay maaaring maglaman ng package na may parehong pangalan.

which -a packer
/usr/sbin/packer

Para sa solusyon, maaari kang lumikha ng isang symlink, o gumamit ng isang ganap na landas /usr/bin/packer.

Ngayon kailangan namin ng ovftool i-download ang link. I-download, ilagay sa server at i-install:

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.

Mag move on na kami.

Sa gith inihanda ko ang lahat ng kailangan mo.

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

Sa folder ISO kailangan mong ilagay ang distribution kit ng operating system. Sa aking kaso, ito ay centos 7.

Kailangan mo ring i-edit ang file. centos-7-base.json:

variables: ΡƒΠΊΠ°Π·Π°Ρ‚ΡŒ свои Π΄Π°Π½Π½Ρ‹Π΅ для ΠΏΠΎΠ΄ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΡ
iso_urls: ΡƒΠΊΠ°Π·Π°Ρ‚ΡŒ Π°ΠΊΡ‚ΡƒΠ°Π»ΡŒΠ½Ρ‹ΠΉ
iso_checksum: чСксумма вашСго ΠΎΠ±Ρ€Π°Π·Π° 

Pagkatapos ng lahat ng mga pagbabago, patakbuhin ang pagpupulong:

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

Kung ang lahat ay na-configure at tinukoy nang tama, pagkatapos ay makikita mo ang isang larawan ng awtomatikong pag-install ng operating system. Ang prosesong ito ay tumatagal sa akin ng 7-8 minuto.

Pagkatapos ng matagumpay na pagkumpleto sa folder output-packer-centos7-x86_64 ova file ay matatagpuan.

I-install ang 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

Dahil ang Terraform ay walang provider para sa ESXi, kailangan mong bumuo ng isa.

Inilagay namin:

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

Susunod, kinokolekta namin ang provider:

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

Nasa finish line na kami. Ilabas natin ang ating imahe.

Pumunta tayo sa folder:

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

Una sa lahat, i-edit ang file variables.tf. Kailangan mong tukuyin ang isang koneksyon sa ESXi server.

Nasa file network_config.cfg naglalaman ng mga setting ng network ng hinaharap na virtual machine. Baguhin sa iyong mga pangangailangan at patakbuhin ang one-liner:

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

Well, sa file pangunahing.tf baguhin ang landas sa ova file sa iyong sarili, kung iba.

Ang oras ng kototohanan.

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.

Tapusin:

terraform apply

Kung ang lahat ay tapos na nang tama, pagkatapos ay sa 2-3 minuto ang isang bagong virtual machine ay i-deploy mula sa naunang ginawa na imahe.

Ang mga gamit para sa lahat ng ito ay limitado lamang ng iyong imahinasyon.

Gusto ko lang ibahagi ang pinakamahuhusay na kagawian at ipakita ang mga pangunahing punto kapag nagtatrabaho sa mga produktong ito.

Salamat sa iyong pansin!

PS: I will be glad to constructive criticism.

Pinagmulan: www.habr.com

Magdagdag ng komento