النشر السريع vm ESXi مع Terraform

مرحباً بالجميع ، اسمي إيفان وأنا مسؤول نظام كحولي (OPS).

أود أن أخبرك بكيفية نشر الأجهزة الافتراضية على ESXi بدون vCenter باستخدام Terraform.

في كثير من الأحيان ، يجب عليك نشر / إعادة إنشاء الأجهزة الافتراضية لاختبار هذا التطبيق أو ذاك. بسبب الكسل ، فكرت في أتمتة العملية. قادني بحثي إلى منتج hashicorp الرائع ، terraform .

أعتقد أن الكثير من الناس يعرفون ما هو Terraform ، ومن لا يعرف ، هذا تطبيق لإدارة أي سحابة أو بنية تحتية أو خدمة باستخدام مفهوم IasC ( البنية التحتية كرمز ).

كبيئة افتراضية ، أستخدم ESXi. بسيطة جدا ومريحة وموثوقة.
أتوقع سؤالا.
لماذا terraform إذا كان بإمكانك استخدام vCenter Server؟
يمكنك بالطبع ، ولكن. أولاً ، هذا ترخيص إضافي ، وثانيًا ، هذا المنتج كثيف الاستخدام للموارد ولا يتناسب مع خادم المنزل الخاص بي ، وثالثًا ، القدرة على ترقية المهارات.

الخادم هو منصة Intel 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.

الانتقال.

في غيتا ، أعددت كل ما أحتاجه.

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

في مجلد iso تحتاج إلى وضع توزيع نظام التشغيل. في حالتي هو centos 7. من

الضروري أيضًا تحرير ملف 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 دقائق.

بعد الانتهاء بنجاح ، سيتم وضع ملف البويضات في مجلد 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 دقائق سيتم نشر جهاز افتراضي جديد من الصورة التي تم إنشاؤها سابقًا.

خيارات استخدام كل هذا محدودة بالخيال فقط.

أردت فقط مشاركة أفضل الممارسات وإظهار النقاط الرئيسية عند العمل مع هذه المنتجات.

شكرآ لك على أهتمامك!

ملاحظة: سأكون سعيدًا بالنقد البناء.

All Articles