Cleachd gu sgiobalta vm ESXi le Terraform

Halo a h-uile duine, is e Ivan an t-ainm a th’ orm agus tha mi nam rianadair siostam alcol (OPS).

Bu mhath leam innse dhut mar a chleachdas mi innealan brìgheil air ESXi gun vCenter a’ cleachdadh Terraform.

Gu math tric, feumaidh tu innealan brìgheil a chuir an sàs / ath-chruthachadh gus seo no an tagradh sin a dhearbhadh. Air sgàth leisg, smaoinich mi air a’ phròiseas fèin-ghluasadach. Thug an rannsachadh agam mi gu toradh iongantach den chompanaidh hashicorp, terraform.

Tha mi a’ smaoineachadh gu bheil fios aig mòran dhaoine dè a th’ ann an Terraform, agus cò aig nach eil fios, is e tagradh a tha seo airson sgòth, bun-structar no seirbheis sam bith a riaghladh a’ cleachdadh bun-bheachd IasC (Bun-structair mar chòd).

Bidh mi a’ cleachdadh ESXi mar an àrainneachd virtualization agam. Gu math sìmplidh, goireasach agus earbsach.
Tha dùil agam ri ceist.

Carson terraform nuair as urrainn dhut vCenter Server a chleachdadh?

Faodaidh tu gu dearbh, ach. An toiseach, is e cead a bharrachd a tha seo, san dàrna àite, tha an toradh seo gu math dian air goireasan agus dìreach chan eil e a’ freagairt air an t-seirbheisiche dachaigh agam, agus san treas àite, an comas sgilean ùrachadh.

Tha an àrd-ùrlar Intel NUC ag obair mar fhrithealaiche:

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

Agus mar sin, a 'chiad rudan an toiseach.

Airson a-nis, leig dhuinn esxi a stèidheachadh, is e sin, am port VNC fhosgladh anns na roghainnean balla-teine.

Gu gnàthach, tha am faidhle fo dhìon sgrìobhaidh. Bidh sinn a’ dèanamh na modhan-làimhseachaidh a leanas:

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

cuir am bloc a leanas ri deireadh an fhaidhle:

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

Fàg, sàbhail. Atharraich na còraichean air ais agus ath-thòisich an t-seirbheis:

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

Dìreach gus an tèid an òstair ath-thòiseachadh. Às deidh sin, feumar an làimhseachadh seo ath-aithris.

Nas fhaide, nì mi an obair gu lèir ann an inneal brìgheil air an aon fhrithealaiche.

Mion-fhiosrachadh air bathar:

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

An ath rud, feumaidh sinn pacair, cuideachd toradh de HashiCorp.

Tha e riatanach an ìomhaigh "òir" a chruinneachadh gu fèin-ghluasadach. a chleachdas sinn san àm ri teachd.

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

Air an gluasad tionndadh pacaidh faodaidh mearachd tachairt, leis gum faodadh pasgan a bhith ann stèidhichte air RedHat leis an aon ainm.

which -a packer
/usr/sbin/packer

Airson an fhuasglaidh, faodaidh tu symlink a chruthachadh, no slighe iomlan a chleachdadh /usr/bin/pacer.

A-nis feumaidh sinn ovftool download cheangal. Luchdaich sìos, cuir air an fhrithealaiche agus stàlaich:

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.

Bidh sinn a 'gluasad air adhart.

Air an gith dh'ullaich mi a h-uile dad a dh 'fheumas tu.

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

Gu pasgan mòr feumaidh tu pasgan cuairteachaidh an t-siostaim obrachaidh a chuir. Anns a 'chùis agam, is e seo centos 7.

Feumaidh tu cuideachd am faidhle a dheasachadh. centos-7-bonn.json:

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

Às deidh na h-atharrachaidhean gu lèir, ruith an co-chruinneachadh:

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

Ma tha a h-uile càil air a rèiteachadh agus air a shònrachadh gu ceart, chì thu dealbh de stàladh fèin-ghluasadach an t-siostaim obrachaidh. Bheir am pròiseas seo 7-8 mionaidean dhomh.

An dèidh crìochnachadh soirbheachail anns a 'phasgan toradh-pacaiche-centos7-x86_64 lorgar faidhle ova.

Stàlaich 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

Leis nach eil solaraiche aig Terraform airson ESXi, feumaidh tu fear a thogail.

Chuir sinn air falbh:

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

An ath rud, cruinnichidh sinn an solaraiche:

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

Tha sinn aig an loidhne crìochnachaidh. Rachamaid a-mach an ìomhaigh againn.

Rachamaid dhan phasgan:

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

An toiseach, deasaich am faidhle caochladairean.tf. Feumaidh tu ceangal a shònrachadh don fhrithealaiche ESXi.

Ann am faidhle lìonra_config.cfg cuir a-steach roghainnean lìonra an inneal brìgheil san àm ri teachd. Atharraich gu na feumalachdan agad agus ruith an aon loidhne:

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

Uill, anns an fhaidhle prìomh.tf atharraich an t-slighe chun fhaidhle ova agad fhèin, ma tha e eadar-dhealaichte.

Uair na fìrinn.

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.

Crìochnaich:

terraform apply

Ma thèid a h-uile càil a dhèanamh ceart, an uairsin ann an 2-3 mionaidean thèid inneal brìgheil ùr a chuir a-steach bhon ìomhaigh a chaidh a dhèanamh roimhe.

Tha na cleachdaidhean airson seo uile cuingealaichte le do mhac-meanmna a-mhàin.

Bha mi dìreach airson na cleachdaidhean as fheàrr a cho-roinn agus na prìomh phuingean a nochdadh nuair a bha mi ag obair leis na toraidhean sin.

Tapadh leibh airson d ’aire!

PS: Bidh mi toilichte càineadh cuideachail a dhèanamh.

Source: www.habr.com

Cuir beachd ann