กำลังเข้ามาใกล้ชิดแล้ว หลายวันที่ผ่านมา ผมได้สาธิต Vagrant โซลูชันเสมือนภาคีที่มุ่งใหญ่โดยการเป็น wrapper ที่สะอาดและเรียบร้อยให้ซอฟต์แวร์ที่ซับซ้อนกว่าดูเหมือน เช่น VirtualBox, KVM และเพื่อนๆ อีกทั้งยังเล่นในลีกอาชีพโดยรองรับ Docker containers และ cloud server environments
ขยายจากโมเดลนี้ เราจะพูดถึง use case ที่แตกต่างออกไปเล็กน้อย แต่ cloudy มาก CoreOS ซึ่งเราใช้ในแบบฝึกหัดก่อนหน้า เป็นผู้เล่นอีกคนที่พยายามทำกำไรจากคลาวด์มาเนีย (cloud mania) และเสนอคุณสมบัติ cluster และ automation ที่เรียบง่าย วันนี้เราจะเรียนรู้วิธีสร้าง cluster และพูดถึง networking ซึ่งถูกมองข้าม และไม่ได้อธิบายชัดเจนแม้ในเอกสารต้นฉบับ โปรดติดตามมา
การตั้งค่า Discovery Token
ไม่เหมือนครั้งก่อน ที่ผมพยายามใช้ Vagrant บน Windows ผ่าน Powershell เพื่อแสดงตัวอย่างว่าทำได้หลายวิธี ครั้งนี้จะทำบน Linux โดยเฉพาะ Xubuntu Vivid
ตามคู่มืออย่างเป็นทางการ การตั้งค่า CoreOS สำหรับ Vagrant ไม่อยากจะยาก แต่มันก็ไม่ได้ trivial (ง่ายๆ) ตามที่อ้างอิงออนไลน์จะทำให้คุณเชื่อ มีความ अधीर (impatience) ที่ชัดเจนในการเริ่มต้น
หลังจาก clone repository แล้ว คุณจะมีไฟล์หลายไฟล์ ซึ่งต้องแก้ไขเล็กน้อยเพื่อ initialize และ start Vagrant ไฟล์แรกคือ user-data.sample ต้อง rename เป็น user-data สิ่งสำคัญที่สุดคือ token:
#cloud-config
coreos:
etcd:
# generate a new token for each unique cluster from
https://discovery.etcd.io/new?size=3
# specify the initial size of your cluster with ?size=X
# WARNING: replace each time you 'vagrant destroy'
discovery: https://discovery.etcd.io/<token>
พื้นฐานคือ CoreOS ใช้ unique identifiers เรียกว่า tokens เพื่อให้ instances ที่รันอยู่ระบุตัวตนได้ ทุก hosts ที่มี token เดียวกัน จะอยู่ใน swarm เดียวกัน จึงสามารถควบคุมแบบรวมศูนย์ (centralized) ผ่าน etcd shared configuration และ discovery service ได้
https://discovery.etcd.io/new?size=<size>
คุณสามารถใช้ token ของตัวเอง หรือสร้างใหม่ โดยไปที่ URL ด้านบน และระบุ cluster size ที่ต้องการ Default คือ 3 hosts ให้ copy token ที่ได้มาใส่ใน config file เช่น:
https://discovery.etcd.io/21e4099c23b52a8403640c2d48cdca6f
เราจะเห็นว่าทำไมสิ่งนี้ถึงสำคัญในภายหลัง
การตั้งค่า Cluster
ส่วนที่สอง คือกำหนดว่าเราต้องการให้ Vagrant รัน instances ของ CoreOS กี่ตัวเมื่อ start up นี่คือการตั้งค่าที่ง่าย ให้เพิ่มการเปลี่ยนแปลงใน config.rb.sample ไฟล์ ภายใน Git directory ที่ clone มา และ rename เป็น config.rb เราจะลอง 4 instances เพื่อให้แตกต่างและพิเศษ
# Size of the CoreOS cluster created by Vagrant
$num_instances=X
เริ่มต้น Cluster
ตอนนี้รัน Vagrant ได้แล้ว หาก clone online repository มา คุณจะมี Vagrantfile default อยู่ใน current directory แล้ว ดังนั้นถ้าลอง vagrant init จะ fail:
vagrant init
`Vagrantfile` already exists in this directory. Remove it before
running `vagrant init`.
หากยังไม่ติดตั้ง VirtualBox หรือ virtualization software อื่นที่เกี่ยวข้อง จะได้ error อีกครั้งเมื่อพยายาม start โปรแกรม โปรดแก้ไขปัญหาเล็กๆน้อยๆเหล่านี้ก่อน
vagrant up
The provider 'virtualbox' that was requested to back the machine
'core-01' is reporting that it isn't usable on this system. The
reason is shown below:
Vagrant could not detect VirtualBox! Make sure VirtualBox is properly installed. Vagrant uses the `VBoxManage` binary that ships with VirtualBox, and requires this to be available on the PATH. If VirtualBox is installed, please find the `VBoxManage` binary and add it to the PATH environmental variable.
หากทำสำเร็จ Vagrant จะเริ่มสร้าง CoreOS instances ทีละตัว อาจใช้เวลานาน และต้องการ network bandwidth ที่ดี และ memory มากพอสำหรับ clusters ขนาดใหญ่
vagrant up
Bringing machine 'core-01' up with 'virtualbox' provider...
Bringing machine 'core-02' up with 'virtualbox' provider...
Bringing machine 'core-03' up with 'virtualbox' provider...
Bringing machine 'core-04' up with 'virtualbox' provider...
==> core-01: Box 'coreos-alpha' could not be found. Attempting to find and install...
core-01: Box Provider: virtualbox
core-01: Box Version: >= 308.0.1
==> core-04: Importing base box 'coreos-alpha'...
==> core-04: Matching MAC address for NAT networking...
==> core-04: Checking if box 'coreos-alpha' is up to date...
==> core-04: Setting the name of the VM: coreos-vagrant_core-04_1431799934824_55775
==> core-04: Fixed port collision for 22 => 2222. Now on port 2202.
==> core-04: Clearing any previously set network interfaces...
==> core-04: Preparing network interfaces based on configuration...
core-04: Adapter 1: nat
core-04: Adapter 2: hostonly
==> core-04: Forwarding ports...
core-04: 22 => 2202 (adapter 1)
==> core-04: Running 'pre-boot' VM customizations...
==> core-04: Booting VM...
==> core-04: Waiting for machine to boot. This may take a few minutes...
core-04: SSH address: 127.0.0.1:2202
core-04: SSH username: core
core-04: SSH auth method: private key
core-04: Warning: Connection timeout. Retrying...
==> core-04: Machine booted and ready!
==> core-04: Setting hostname...
==> core-04: Configuring and enabling network interfaces...
==> core-04: Running provisioner: file...
==> core-04: Running provisioner: shell...
core-04: Running: inline script
เมื่อสร้างระบบทั้งหมดเสร็จแล้ว ให้รัน vagrant status เพื่อดูข้อมูลเครื่องทั้งหมดและสถานะปัจจุบัน
Networking
ตอนนี้ถึงส่วนน่าสนใจจริงๆ เรามี 4 VMs แล้ว แต่เราจะเชื่อมต่อยังไง? เราเคยเจอ dilemma คล้ายกันตอนทดสอบ Docker แต่เราหาทางออกได้ และตรรกะเดียวกันก็ใช้ได้ที่นี่
ขั้นแอน สามารถเชื่อมต่อแต่ละเครื่องที่รันอยู่ด้วยคำสั่ง vagrant ssh ซึ่งจะจัดการ keys และรายละเอียดต่างๆ ให้ คุณต้องรันคำสั่งนี้จาก cloned directory มิฉะนั้นจะ error:
vagrant ssh core-02 -- -A
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.
แต่ใน directory ที่ถูกต้อง ควรทำงานได้ดี:
vagrant ssh core-02 -- -A
Welcome to CoreOS...
หากรัน ifconfig จะสังเกตว่า machines ทุกตัวมี IP address เหมือนกันหมด และไม่สามารถ route ระหว่างกันได้ อีกทั้ง VirtualBox interface รันบน segment 172.0.0.1 ทำให้ยุ่งยากขึ้น อีกครั้ง คล้ายกับ Docker
คุณสามารถใช้ VirtualBox internal networking range และปรับ firewall rules และ routing ได้ แต่วิธีนี้ไม่บอกว่า VM ใดใช้ IP ใด และยากที่จะกำหนด on the fly กล่าวคือ ไม่ได้แค่ parse ตัวเลขเหล่านั้นจากอะไรก็ได้ มีวิธีที่ข้ามข้อง (elegant) มากกว่านี้
เชื่อมต่อผ่าน localhost
ถ้าจำได้ ตอน startup Vagrant ตั้งค่า SSH ให้แต่ละ VM รันบน localhost ใช้ ports ต่างกัน เริ่มจาก 2200 (หรือประมาณนั้น) ดังนั้นหากต้องการ SSH เข้า clients ใช้วิธีนี้:
ssh 127.0.0.1 -p 2202
The authenticity of host '[127.0.0.1]:2202 ([127.0.0.1]:2202)' can't be established.
ED25519 key fingerprint is 32:a0:57:58:b3:55:fc:03:c8:89:7d:7c:cc:6f:85:9d.
Are you sure you want to continue connecting (yes/no)?
เชื่อมต่อผ่าน IP address จากที่ไหนก็ได้
อย่างไรก็ตาม วิธีข้างต้นไม่ได้สำหรับ VM-to-VM communication เราต้องการสิ่งอื่น นี่คือที่ที่ discovery piece มีประโยชน์จริงๆ เมื่อ cluster รันแล้ว คุณสามารถไปที่ URL อีกครั้ง และตอนนี้มันจะมี JSON data ที่น่าสนใจ:
สังเกต keys และ values แต่ละ entry มี IP address เช่น 172.17.8.103 และคุณสามารถ parse มันจาก output นี้ ได้ IP addresses ภายในแล้ว สามารถใช้เชื่อมต่อ clients โดยตรง และสำคัญกว่า อนุญาตให้พวกเขาติดต่อกับ peers ได้
ชิ้นส่วนเล็กๆที่ขาดคือ เราไม่รู้ password ของ user core จึงจะใช้ key ที่ให้มา ซึ่งอยู่ใน vagrant.d sub-directory เพิ่ม key แล้วคุณจะเชื่อมต่อได้ราบรื่น เราเคยเห็นในคู่มือแรก
Port Forwarding
เรายังไม่ได้ลอง configuration มากนักสำหรับ boxes ของเรา แต่ตอนนี้เรารู้ IP addresses ของ running instances แล้ว เราสามารถเริ่มสร้างสรรค์ได้ เช่น port forwarding ซึ่งมีค่ามากสำหรับ VMs ที่รัน services
การเปลี่ยนแปลงที่จำเป็นต้องระบุเป็น directives ภายใน Vagrantfile คุณสามารถมี multiple configurations สำหรับ multiple setups ได้—คล้าย Dockerfiles ตัวอย่าง forwarding HTTPS port:
config.vm.forward_port 443, <host port>
หากคุณติดตามบทความ Docker ของผม ทุกอย่างจะง่าย Host port, client port และเสร็จสิ้น คุณสามารถมี multiple declarations สำหรับแต่ละ box แล้วคุณยังสามารถตั้งค่า firewall rules แบบ manual ได้ หากต้องการควบคุมเต็มที่ นี่แค่การลองชิม (teaser) เราจะใช้เวลาฟิดเดิล (fiddle) มากขึ้นในอนาคต
ขั้นตอนต่อไปคือเสียบ Docker เข้าไป ด้วย default range คล้ายกัน ซึ่งอาจไม่ได้มาจากการสุ่ม คุณสามารถเริ่มเล่นกับ clusters, containers, parallel execution และแนวคิดฟานซีอื่นๆ นี่คือเหตุผลที่ Vagrant มาพร้อม plugins ซึ่งเราจะพูดถึงในบทความแยกต่างหาก อย่างไรก็ตาม เมื่อเสร็จสิ้นงาน ให้ destroy instances
อ่านเพิ่มเติม
เรื่องดีๆเพิ่มเติมจาก Dedoimedo's forges:
CoreOS quickstart guide; อาจหนักหน่อย
Supervisord tutorial ซึ่งมีความสามารถคล้าย etcd
KVM & VirtualBox side-by-side configuration tweak
สรุป
Vagrant และ CoreOS ดูเหมือนไอเดียที่น่าสนใจ โหดร้าย และขี้ขวางขัดผมยังไม่มั่นใจว่ามีค่าเท่าไหร่ในสภาพแวดล้อมทางธุรกิจ แม้ว่า wrapper technologies ที่ซ่อนรายละเอียดน่ากลัวของงานจริง กำลังเจริญเติบโตและเป็นที่นิยมขึ้นเรื่อยๆ ทุกคนต้องการ frontend และ orchestration tools ปัญหาคือตลาด변덕 (fickle) และผันผวน (volatile) ไม่มีมาตรฐาน และคนใช้สิ่งน่ารังเกียจอย่าง Python, Ruby, และ JSON แต่ไม่เป็นไร
เรากลับมาชนะอีกชิ้นส่วนเล็กๆของ terror incognita (เขตที่ไม่รู้จัก) และคุณคุ้นเคยกับ Vagrant และ CoreOS มากขึ้น ดีแล้ว เพราะชิ้นต่อไปจะเป็นการเล่นกับ cluster services และอื่นๆ และนี่แค่จุดเริ่มต้น
จำไว้ ผมต้องทำทั้งหมด ดังนั้นในอีกหลายเดือน เราจะสำรวจเกือบทุกอย่าง รวมถึง Fleet, Kubernetes, Mesos, Etcd และโครงการ cool อื่นๆ มากมาย เราจะเชื่อม CoreOS และอื่นๆ เข้ากับ cloud providers, ทดลอง distributed และ parallel filesystems, automation tools เช่น Jenkins, Ansible, name dropping และ buzz อีกมากจนคุณอยากอาเจียน และอื่นๆ อีก ใส่ใจติดตาม
ขอแสดงความนับถือ