Quick Deploy vm ESXi nrog Terraform

Nyob zoo sawv daws, kuv lub npe yog Ivan thiab kuv yog tus thawj tswj dej cawv (OPS).

Kuv xav qhia koj li cas kuv xa cov tshuab virtual ntawm ESXi yam tsis muaj vCenter siv Terraform.

Feem ntau, koj yuav tsum xa / rov tsim cov tshuab virtual txhawm rau sim qhov no lossis daim ntawv thov ntawd. Vim tub nkeeg, kuv xav txog automating txheej txheem. Kuv qhov kev tshawb nrhiav tau coj kuv mus rau ib qho khoom zoo ntawm lub tuam txhab hashicorp, terraform.

Kuv xav tias ntau tus neeg paub tias Terraform yog dab tsi, thiab leej twg tsis paub, qhov no yog daim ntawv thov rau kev tswj hwm huab, kev tsim kho lossis kev pabcuam siv lub tswvyim IasC (Infrastructure li code).

Kuv siv ESXi ua kuv qhov chaw virtualization. Yooj yim heev, yooj yim thiab txhim khu kev qha.
Kuv cia siab tias yuav muaj lus nug.

Vim li cas terraform thaum koj tuaj yeem siv vCenter Server?

Koj tuaj yeem tau kawg, tab sis. Ua ntej, qhov no yog daim ntawv tso cai ntxiv, qhov thib ob, cov khoom no yog cov khoom siv siv ntau heev thiab tsuas yog tsis haum rau kuv lub tsev neeg rau zaub mov, thiab thib peb, nws yog lub sijhawm los txhim kho koj cov kev txawj ntse.

Intel NUC platform ua raws li tus neeg rau zaub mov:

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

Thiab yog li ntawd, thawj yam ua ntej.

Txog tam sim no, cia peb teeb tsa esxi, uas yog, qhib VNC chaw nres nkoj hauv qhov chaw teeb tsa hluav taws xob.

Los ntawm lub neej ntawd, cov ntaub ntawv yog sau-tiv thaiv. Peb ua cov nram qab no manipulations:

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

ntxiv cov block hauv qab no mus rau qhov kawg ntawm cov ntaub ntawv:

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

Tawm, txuag. Hloov cov cai rov qab thiab rov pib qhov kev pabcuam:

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

Tiag tiag kom txog thaum tus tswv tsev rov pib dua. Tom qab ntawd, qhov kev hloov pauv no yuav tsum tau rov ua dua.

Tsis tas li ntawd, kuv yuav ua txhua yam haujlwm hauv lub tshuab virtual ntawm tib lub server.

Khoom Specifications:

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

Tom ntej no, peb xav tau packer, kuj yog ib yam khoom ntawm HashiCorp.

Nws yog qhov yuav tsum tau ua kom tiav cov duab "golden". uas peb yuav siv yav tom ntej.

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

Ntawm kev txav mus los packer version Qhov yuam kev tuaj yeem tshwm sim, txij li RedHat-based tuaj yeem muaj pob nrog tib lub npe.

which -a packer
/usr/sbin/packer

Rau txoj kev daws teeb meem, koj tuaj yeem tsim ib qho symlink, lossis siv txoj hauv kev meej /usr/bin/packer.

Tam sim no peb xav tau ovftool download txuas. Download tau, muab tso rau hauv lub server thiab nruab:

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.

Peb tsiv mus.

Ntawm gith kuv npaj txhua yam koj xav tau.

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

Mus rau folder loj koj yuav tsum tau muab cov khoom siv faib khoom ntawm lub operating system. Hauv kuv qhov xwm txheej, qhov no yog centos 7.

Koj kuj yuav tau hloov cov ntaub ntawv. centos-7-base.json:

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

Tom qab tag nrho cov kev hloov pauv, khiav lub rooj sib txoos:

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

Yog hais tias txhua yam yog configured thiab teev kom raug, ces koj yuav pom ib daim duab ntawm lub automatic installation ntawm lub operating system. Cov txheej txheem no yuav siv sij hawm kuv li 7-8 feeb.

Tom qab ua tiav tiav hauv daim nplaub tshev output-packer-centos7-x86_64 ova file yuav nyob.

Nruab 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

Txij li Terraform tsis muaj tus neeg muab kev pabcuam rau ESXi, koj yuav tsum tsim ib qho.

Peb mus:

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

Tom ntej no, peb sau tus neeg muab kev pabcuam:

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

Peb nyob ntawm qhov kawg. Wb mus dov peb daim duab.

Wb mus rau lub folder:

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

Ua ntej tshaj, kho cov ntaub ntawv variables.tf. Koj yuav tsum tau qhia txog kev sib txuas rau ESXi server.

Hauv cov ntaub ntawv network_config.cfg muaj cov kev teeb tsa network ntawm lub tshuab virtual yav tom ntej. Hloov mus rau koj cov kev xav tau thiab khiav ib-liner:

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

Zoo, hauv cov ntaub ntawv loj. tf hloov txoj kev mus rau ova cov ntaub ntawv rau koj tus kheej, yog tias txawv.

Lub caij ntawm qhov tseeb.

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.

Ua tiav:

terraform apply

Yog tias txhua yam ua tiav lawm, tom qab ntawd hauv 2-3 feeb lub tshuab virtual tshiab yuav raug xa tawm los ntawm cov duab ua dhau los.

Kev siv rau tag nrho cov no tsuas yog txwv los ntawm koj qhov kev xav.

Kuv tsuas yog xav qhia cov kev coj ua zoo tshaj plaws thiab qhia cov ntsiab lus tseem ceeb thaum ua haujlwm nrog cov khoom no.

Ua tsaug rau koj mloog!

PS: Kuv yuav zoo siab rau kev thuam.

Tau qhov twg los: www.hab.com

Ntxiv ib saib