หากคุณคิดว่าชีวิตน่าเบื่อ และไม่มีซอฟต์แวร์เสมือนภาคีพอที่จะเล่นและทดลอง บทความวันนี้จะทำให้คุณพอใจ บทความทั้งหมดเพื่อ Vagrant โซลูชัน wrapper สำหรับการปรับใช้ (deployment) และการทดสอบเสมือนภาคี
แนวคิดคือ: ซ่อน nitty gritty commands (รายละเอียดยุ่งยาก) และนำเสนอ unified interface (อินเทอร์เฟซแบบรวม) จากนั้นผูก buzzwords เช่น cloud และอื่นๆ เข้าไป มันก็กลายเป็น hot new technology ที่ทุกคนพูดถึง คล้าย OpenStack, OpenShift, OpenCloud และชื่อฟานซีอื่นๆ จริงๆแล้ว หากคุณยัง toss (โยน) name droppers เช่น PostgreSQL, Jenkins และ Node.js เข้าไปด้วย คุณได้คะแนนพิเศษสำหรับการเป็น modern, hip และ cool แต่เว่อะไรก็ตาม ขอพลิกมุมมองไปดูว่า tool นี้ทำอะไรได้บ้าง และมีประโยชน์ คุณค่า และสนุกหรือไม่
คำพูดมีความจำเป็นมาก
แค่ intro สั้นๆ ให้บริบท Vagrant ออกแบบมาเพื่อลดความซับซ้อนของการทดสอบซอฟต์แวร์ การพัฒนา และการจัดการทรัพยากรเสมือนภาคีใน mixed, rapid environments เริ่มต้นทำงานกับ VirtualBox เท่านั้น ซึ่งไม่ได้เป็น hypervisor pilihan (choice) ในอุตสาหกรรม แต่ gầnๆนี้โปรแกรมรองรับเทคโนโลยีอื่นๆ เช่น VMware, KVM และเพื่อนๆ พร้อม integration tricks ที่ดี
ก็อปซ้ำ (in a nutshell) modus operandi คือ คุณใช้คำสั่ง vagrant แทน native commands คุณอาจมี multiple hypervisors และทุกตัวมี syntaxes ต่างกัน ซึ่งเป็น overhead Vagrant จึงซ่อน ugly stuff (สิ่งน่ารังเกียจ) ไว้หลัง layer ของ abstraction ที่หวังว่าจะยืดหยุ่น พอคุย النظرية (theory) แล้ว มาเริ่มปฏิบัติจริงกันดีกว่า
การตั้งค่า (Setting up)
เพื่อให้น่าสนใจ ผมจะ ไม่ สาธิตบน Linux รอสักครู่ (wait, w00t) เราจะใช้ Windows เพื่อเพิ่มความหลากหลาย ในบทความติดตาม (ซึ่งจะมีเยอะ ผมรับรอง) เราจะกลับไป Linux แต่วันนี้ผมอยากแสดงว่าในแนวคิด (conceptually) ไม่มีความแตกต่าง
เปิดหน้าต่าง Powershell เริ่มพิมพ์คำสั่ง เช่น ต้องการรัน Vagrant ทำได้โดย:
vagrant up
อย่างไรก็ตาม หากยังไม่เตรียมและ initialize work environment จะได้ errors น่ารังเกียจ:
PS C:\HashiCorp\Vagrant\bin> .\vagrant.exe up
A Vagrant environment or target machine is required to run this
command. Run `vagrant init` to create a new Vagrant environment. Or, get an ID of a target machine from `vagrant global-status` to run this command on. A final option is to change to a directory with a Vagrantfile and to try again.
หลังจาก initialize environment แล้ว คุณสามารถเริ่มทำงานจริงได้
แต่เราต้องเผชิญกับ errors น่ารังเกียจอีก:
PS C:\HashiCorp\Vagrant\bin> .\vagrant.exe up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'base' could not be found. Attempting to find and install...
default: Box Provider: virtualbox
default: Box Version: >= 0
==> default: Adding box 'base' (v0) for provider: virtualbox
default: Downloading: base
default:
An error occurred while downloading the remote file. The error
message, if any, is reproduced below. Please fix this error and try again.
Couldn't open file /HashiCorp/Vagrant/bin/base
PS C:\HashiCorp\Vagrant\bin>
โดย default Windows version ของ Vagrant จะพยายาม start VM จาก base image image นั้นไม่พบ ดังนั้นคำสั่ง up จึง fail เมื่อรันโดยไม่มีการเตรียมการ
ดังนั้น ขั้นแรกคือ pull images คล้าย Docker Vagrant มี online repository ของตัวเอง ที่เก็บ basic templates คุณสามารถ grab และติดตั้ง OS แบบ manual ได้ แต่มันพลาดจุดมุ่งหมายของ abstraction ที่กล่าวมา มาดึง image กัน เช่น:
vagrant box add hashicorp/base
หากระบุ image ที่ไม่มีอยู่จริง จะได้ errors เพิ่ม:
PS C:\HashiCorp\Vagrant\bin> .\vagrant.exe box add hashicorp/base
The box 'hashicorp/base' could not be found or could not be accessed in the remote catalog. If this is a private box on HashiCorp's Atlas, please verify you're logged in via `vagrant login`. Also, please double-check the name. The expanded
URL and error message are shown below:
URL: ["https://atlas.hashicorp.com/hashicorp/base"]
Error: The requested URL returned error: 404 Not Found
แต่คุณคาดหวัง base image มี online references หลายแห่งเกี่ยวกับทำไมปัญหานี้เกิดขึ้น มาจาก default declaration ใน configuration file หากเปลี่ยนเป็น image ที่มีอยู่จริงและดาวน์โหลด จะแก้ปัญหาได้ ใน Vagrantfile ต้องแก้ default entry แทน string base ด้วย image ที่ต้องการใช้เป็น default เมื่อ start Vagrant อาจเป็น Ubuntu, Fedora, CoreOS หรือแบบนั้น
# Every Vagrant development environment requires a box. You can search for boxes at https://atlas.hashicorp.com/search.
config.vm.box = "base"
สำหรับแบบฝึกหัดนี้ (และมีเหตุผลดีในอนาคต) เราจะทดสอบ CoreOS image (yungsang/coreos) ซึ่งเป็นตัวเลือกที่ less obvious (ไม่น่าจะคิดถึง) ดึง image มา
PS C:\HashiCorp\Vagrant\bin> .\vagrant.exe box add yungsang/coreos
==> box: Loading metadata for box 'yungsang/coreos'
...
==> box: Successfully added box 'yungsang/coreos' (v1.3.8) for 'virtualbox'!
ตรวจสอบว่าเพิ่มเข้า catalog แล้ว:
PS C:\HashiCorp\Vagrant\bin> .\vagrant.exe box list
ubuntu/trusty64 (virtualbox, 20150506.0.0)
yungsang/coreos (virtualbox, 1.3.8)
จากนั้นรัน Vagrant อีกครั้ง CoreOS จะเริ่มต้น รวมถึง user และ network setup และรายละเอียดอื่นๆ หลังจากพักหนึ่ง ระบบจะรัน คุณสามารถพยายามเชื่อมต่อ host ด้วย SSH โดย default แต่ละ running instance จะได้ port บน localhost กรณีนี้ SSH service สำหรับ box ของเรารันบน port 2222 บน 127.0.0.1 คล้ายกับที่เห็นกับ Docker อีกครั้ง repo concept, networking เหมือนกันมาก Hint มีเหตุผลดี
PS C:\HashiCorp\Vagrant\bin> .\vagrant.exe up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'yungsang/coreos'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'yungsang/coreos' is up to date...
==> default: Setting the name of the VM: bin_default_1431611566870_38538
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
==> default: Forwarding ports...
default: 22 => 2222 (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: core
default: SSH auth method: private key
default: Warning: Connection timeout. Retrying...
==> default: Machine booted and ready!
เชื่อมต่อกับ Virtual Machine
VM ของเรารันอยู่แล้ว ดีแล้ว ตรวจสอบด้วย vagrant status:
คุณสามารถใช้ vagrant ssh หรือ vagrant rdp wrapper command เพื่อเข้า running VM มีวิธี advanced อื่นๆ แต่จะพูดใน tutorial ติดตาม ซึ่งเน้น networking และ clusters
สำหรับ Windows clients สามารถใช้ RDP แต่ในกรณีนี้ไม่ได้ เพราะ RDP ไม่มีใน CoreOS ผมไม่แน่ใจว่า VNC เป็นตัวเลือกหรือไม่ แต่ยังไม่ได้ทดสอบ คล้ายกัน SSH ก็เป็นปัญหา เพราะ Windows ไม่มี default SSH agent และหากติดตั้ง PuTTY อาจไม่อยู่ใน path
PS C:\HashiCorp\Vagrant\bin> .\vagrant.exe rdp default
==> default: Detecting RDP info...
RDP connection information for this machine could not be
detected. This is typically caused when we can't find the IP
or port to connect to for RDP. Please verify you're forwarding
an RDP port and that your machine is accessible.
PS C:\HashiCorp\Vagrant\bin> .\vagrant.exe ssh default -- -A
`ssh` executable not found in any directories in the %PATH% variable. Is an SSH client installed? Try installing Cygwin, MinGW or Git, all of which contain an SSH client. Or use your favorite SSH client with the following authentication information shown below:
Host: 127.0.0.1
Port: 2222
Username: core
Private key: C:/Users/<user>/.vagrant.d/insecure_private_key
คุณยังต้อง (หากทำงานบน Windows) แปลง OpenSSH key เป็น PuTTY format หรือคล้ายๆกัน อีกครั้ง ไม่ใช่สิ่งที่คนส่วนใหญ่จะเจอใน Vagrant adventures แต่มีประโยชน์ที่จะเห็นปัญหาคล้ายกันถูกแก้หลายวิธี
ณ จุดนี้ เรา launch Vagrant VM instance แรกสำเร็จแล้ว ใช้ VirtualBox เป็น provider แต่เราไม่ได้เจอ VirtualBox commands เลย หรือกังวล XML files และอื่นๆ นี่แค่จุดเริ่มต้น แต่ก็จบ tutorial วันนี้ เราจบในโน้ตสูง
อ่านเพิ่มเติม
หากอยากอ่านข้อความเพิ่ม:
Introducing Vagrant, บทความ Linux Journal
Crash course on Vagrant
Docker networking thingie
สรุป
Vagrant ดูเป็นแนวคิดน่าสนใจ แม้อาจไม่ใช่ best way ในการแก้ปัญหาในสภาพแวดล้อมของคุณ หากคุณอยาก master เสมือนภาคีจริงๆ และต้องควบคุกทุก facet (ด้าน) ของ setup คุณต้องลงทุนเวลา เลือด และความอดทน เรียนรู้ bottom-up เพราะมิฉะนั้น คุณจะแก้ไข issues ไม่ได้เมื่อมันมา (และมันจะมา) หรือเข้าใจเทคโนโลยี
แต่มันเป็น interesting spin (มุมมองน่าสนใจ) และเราจะได้เห็นว่าจะไปไกลแค่ไหนกับ Vagrant บน virtual journey ของเรา อย่างน้อย เราจัดการ setup issues และ initial configuration เรียนรู้การ pull images และ start พวกมัน ทำ basic checks และ workaround network connectivity ค่อนข้าง useful อีกมากมายจะมาแน่นอน รวมถึง plugins framework ที่สำคัญมาก ซึ่งขยาย connectivity สำหรับ VMware, LXC, libvirt, บวก advanced networking, cluster setups และอื่นๆ ใส่ใจติดตาม updates
ขอแสดงความนับถือ