วันเสาร์ที่ 26 กรกฎาคม พ.ศ. 2557

ใช้ apache2 เป็น proxy กันเถอะ

ด้วยสาเหตุอะไรก็ช่าง
เอาเป็นว่า เราสามารถใช้ apache2 เป็น proxy สำหรับ load URL อื่น ๆ มาได้ทันที

เช่น
ServerA มี apache2 ใช้งานอยู่
ปกติแล้ว เราสามารถเปิด web http://www.ServerA.com
เพื่อเรียกดูเวปที่ /var/www ของ ServerA

ถ้า proxy ล่ะ คืออะไร
เราสามารถเซตให้ apache2 ไปเปิดเวปอื่น เช่น http://www.AnyWWW.com/
เมื่อเราเปิด web http://www.ServerA.com

แทนที apache2 จะเรียกเวปจาก /var/www ของ ServerA
จะกลายเป็นว่า apache2 ไปเรียกเวปจาก http://www.AnyWWW.com/

หาก Config apache2 ที่ ServerA ด้วย
ProxyPass / http://www.AnyWWW.com/
apache2 ก็จะทำให้ ServerA กลายเป็น mirrors ของ http://www.AnyWWW.com/

หรือ หาก Config apache2 ที่ ServerA ด้วย
ProxyPass /anywww/ http://www.AnyWWW.com/
เมื่อเราเปิด http://www.ServerA.com/anywww/ เราก็จะได้ข้องมูล http://www.AnyWWW.com/

โดยเราจะเรียก ServerA ว่าเป็น reverse proxy หรือ gateway สำหรับ AnyWWW.com
*ซึ่งหากเราต้องการใช้ คำสั่ง ProxyPass ก็ควรเซต ProxyRequests ให้เป็น Off 
** ProxyRequests On จะหมายถึงเราต้องการใช้เป็น forward proxy (คล้าย ๆ พวก squid)

อย่าลืม enable module ด้วยคำสั่ง
a2enmod proxy
a2enmod proxy_http
a2enmod proxy_ftp <- ถ้าใช้

เอาหละ ทีนี้ เราสามารถใช้ proxy ร่วมกับ VirtualHost ของ apache2 ได้ด้วย
เพื่อเพิ่มความวุ่นวายให้กับชีวิต

<VirtualHost *:80>
     ServerName AnyWWW.ServerA.com

     ProxyRequests off
     ProxyPass / http://www.AnyWWW.com/
</VirtualHost>

หมายความว่า หากมี Request เข้ามาที่ Port 80 ด้วย DNS AnyWWW.ServerA.com
ให้ไปเรียกเวปจาก http://www.AnyWWW.com/

ทีนี้ถ้าจะให้ดี ก็เพิ่ม
ProxyPassReverse / http://www.AnyWWW.com/
เพื่อให้ apache2 แก้ไข header ที่ได้มาจาก http://www.AnyWWW.com/
ให้เหมือนกับว่าข้อมูลมาจาก AnyWWW.ServerA.com เอง

ที่เหลือก็สุดแล้วแต่จะประยุกต์ใช้จ้า


วันพฤหัสบดีที่ 24 กรกฎาคม พ.ศ. 2557

xen-hypervisor กับ debian (Debian Xen)

มา Setup Debian Xen กัน


สิ่งที่ต้องมี
PC/Server ที่ลง OS Debian Linux
Internet (ไว้ Load Package)

อันดับแรก
Install Linux ให้พร้อมใช้งาน
โดยแนะนำให้ลงบน LVM เพื่อความง่าย ในการจัดการต่าง ๆ
และใช้ Grub ลงไว้ที่ MBR (ใช้อย่างอื่นก็ได้ แต่จะเซตต่างกันนิดหน่อย)

โดยเราจะใช้ เป็น Dom0 คือเป็นแม่ ที่เอาไว้เลี้ยงดูลูก ๆ เฉย ๆ ดังนั้นไม่ควรลง Package อะไรมากมาย แค่มี openssh-server เพื่อให้เรามาสามารถ Remote เข้ามา Maintanance ได้

# aptitude install openssh-server

อันดับที่ 2 ลง Package xen-hypervisor

# aptitude install xen-hypervisor

Debian Package ก็จะทำการ Install Package ต่าง ๆ ที่จำเป็นสำหรับ xen-hypervisor ให้ทั้งหมด
ซึ่งรวมไปถึง Kernel Image ที่ Support hypervisor ด้วย

แต่ยังใช้ไม่ได้นะ เพราะเราก็แค่ลง xen-hypervisor เพิ่มเข้าไป
ดังนั้น Grub จะยังคงไป Boot ที่ Kernel เดิมอยู่
เรายังไม่ได้ เซต เพื่อให้ Default เป็น kernel ที่ Support hypervisor

# ls -l /etc/grub.d/
total 72
-rwxr-xr-x 1 root root  9009 Dec 12  2013 00_header
-rwxr-xr-x 1 root root  5949 Nov 27  2013 05_debian_theme
-rwxr-xr-x 1 root root 11713 Dec 12  2013 10_linux
-rwxr-xr-x 1 root root 10285 Dec 12  2013 20_linux_xen
-rwxr-xr-x 1 root root 10643 Dec 12  2013 30_os-prober
-rwxr-xr-x 1 root root  1426 Dec 12  2013 30_uefi-firmware
-rwxr-xr-x 1 root root   214 Dec 12  2013 40_custom
-rwxr-xr-x 1 root root   216 Dec 12  2013 41_custom
-rw-r--r-- 1 root root   483 Dec 12  2013 README

จะเห็นว่า ลำดับในการ Boot ของ Grub จะให้ linux มาก่อน linux_xen
ดังนั้น เราก็แค่ เปลี่ยนลำดับใหม่ ให้ linux_xen มาก่อน linux

เพื่อไม่ให้ลูกทุ่งมากเกินไป ให้ใช้คำสั่ง

# dpkg-divert --divert /etc/grub.d/08_linux_xen --rename /etc/grub.d/20_linux_xen
Adding 'local diversion of /etc/grub.d/20_linux_xen to /etc/grub.d/08_linux_xen'

เป็นการสั่งให้ Debian Package เปลี่ยนชื่อ จาก 20_linux_xen เป็น 08_linux_xen
เผื่อคราวหน้า เรา Update Package มันจะได้ไม่เปลี่ยนกลับไปเป็นแบบเดิม แล้วเดี๋ยวจะใช้ไม่ได้

เช็คซะหน่อยว่า ถูกต้องปล่าว
# ls -l /etc/grub.d/
total 72
-rwxr-xr-x 1 root root  9009 Dec 12  2013 00_header
-rwxr-xr-x 1 root root  5949 Nov 27  2013 05_debian_theme
-rwxr-xr-x 1 root root 10285 Dec 12  2013 08_linux_xen
-rwxr-xr-x 1 root root 11713 Dec 12  2013 10_linux
-rwxr-xr-x 1 root root 10643 Dec 12  2013 30_os-prober
-rwxr-xr-x 1 root root  1426 Dec 12  2013 30_uefi-firmware
-rwxr-xr-x 1 root root   214 Dec 12  2013 40_custom
-rwxr-xr-x 1 root root   216 Dec 12  2013 41_custom
-rw-r--r-- 1 root root   483 Dec 12  2013 README

ลำดับของ linux_xen ไปอยู่ก่อน linux เรียบร้อย

# update-grub
Generating grub.cfg ...
Found linux image: /boot/vmlinuz-3.14-1-amd64
Found initrd image: /boot/initrd.img-3.14-1-amd64
Found linux image: /boot/vmlinuz-3.14-1-amd64
Found initrd image: /boot/initrd.img-3.14-1-amd64
done


ตอนนี้ Debian เราก็พร้อมจะทำงานเป็น Dom0 แล้ว

ยัง.. ไม่เสร็จนะ
เนื่องจาก Xen ใช้งาน Network ได้หลายแบบมากมาก เช่น
- Bridge (มาอยู่ Network เดียวกับ ethX)
- NAT (เป็น Private Network )
หรือท่าอื่น ๆ ก็แล้วแต่ละดวก

แต่เพื่อความง่าย เราจะใช้แบบ Bridge กัน
ให้ลบ Comment ในบรรทัด network-bridge ในไฟล์ /etc/xen/xend-config.sxp

# (network-script network-bridge)
ลบ # ออก ให้กลายเป็น
(network-script network-bridge)

แล้ว Reboot ซะ เพื่อใช้ Kernel ใหม่ ที่ Support hypervisor
และใช้ Setting Network ตามที่เราเซตไว้

# shutdown -r now
The system is going down for reboot NOW!

เช็คระบบกันหน่อยว่า xen ทำงานรึยัง
# xm list
Name                                        ID   Mem VCPUs      State   Time(s)
Domain-0                                     0  7352     4     r-----      6.6

เช็ค Network อีกนิด ว่ามี Bridge ให้ใช้งานรึยัง
# brctl show
bridge name     bridge id               STP enabled     interfaces

eth0            8000.00247e0247fa       no              peth0

เรียบร้อย Domain-0 ทำงานปกติ

เริ่มสร้าง VM (หรือ DomU) ใน Debian Xen


DomU คืออะไร ไปหาอ่านจากที่อื่นนะครับ เพื่อให้ง่าย ใช้ Package xen-tools ดีกว่า

# aptitude install xen-tools

มาลองสร้าง DomU เล่น ๆ ด้วยคำสั่ง

# xen-create-image --hostname littleDebian --memory=512M --lvm=vg1 --size=10Gb --swap=512Mb --ip 192.168.1.201

อธิบาย Option กันก่อน (จริง ๆ Option มีมากมาย แต่เอาหลัก ๆ ที่ทำให้ใช้งานได้ก่อน)
--hostname littleDebian เป็นการตั้งชื่อ DomU ตัวนี้ ว่าจะให้ชื่ออะไร โดยชื่อนี้ จะมาเป็นชื่อ file config สำหรับ DomU นั้น ๆ ผมตั้งชื่อน่ารัก ๆ ว่า เดอเบี้ยนน้อย littleDebian

--memory=512M กำหนดให้มี Ram สำหรับ DomU นี้ เป็น 512M ไปก่อน ไปแก้ใน Config file ทีหลังได้
--lvm=vg1 เป็นการบอก script ว่า ให้ไปสร้าง logical volume ไว้ใน Logical Group ชื่อว่า vg1 โดยจะสร้าง 2 lv คือ littleDebian-disk และ littleDebian-swap
--size=10Gb เป็นการกำหนดขนาด image หรือ LV ของ littleDebian-disk
--swap=512Mb เป็นการกำหนดขนาด image หรือ LV ของ littleDebian-swap
--ip 192.168.1.201 กำหนด IP ที่ DomU ต้องการใช้ (ไว้แก้ไขทีหลัง)

# xen-create-image --hostname littleDebian --memory=512M --lvm=vg1 --size=10Gb --swap=512Mb --ip 192.168.1.201

WARNING:  No gateway address specified!
WARNING:  No netmask address specified!

General Information
--------------------
Hostname       :  littleDebian
Distribution   :  jessie
Mirror         :  http://ftp.us.debian.org/debian/
Partitions     :  swap            512Mb (swap)
                  /               10Gb  (ext3)
Image type     :  full
Memory size    :  512M
Kernel path    :  /boot/vmlinuz-3.14-1-amd64
Initrd path    :  /boot/initrd.img-3.14-1-amd64

Networking Information
----------------------
IP Address 1   : 192.168.1.201 [MAC: 00:16:3E:61:7B:5A]


Creating swap on /dev/vg1/littleDebian-swap
Done

Creating ext3 filesystem on /dev/vg1/littleDebian-disk
Done
Installation method: debootstrap
Done

Running hooks
Done

No role scripts were specified.  Skipping

Creating Xen configuration file
Done

No role scripts were specified.  Skipping
Setting up root password
Generating a password for the new guest.
All done


Logfile produced at:
         /var/log/xen-tools/littleDebian.log

Installation Summary
---------------------
Hostname        :  littleDebian
Distribution    :  jessie
MAC Address     :  00:16:3E:61:7B:5A
IP Address(es)  :  192.168.1.201
RSA Fingerprint :  36:46:f6:04:6d:8c:c1:dc:bd:5f:06:23:af:a6:de:1a
Root Password   :  hDtZByav

เรียบร้อย DomU พร้อมใช้งาน (อาจจะยังไม่สมบูรณ์เท่าไหร่)
ไว้ค่อยปรับอีกครั้ง

เริ่มใช้งาน DomU ด้วยคำสั่ง
# xm create /etc/xen/littleDebian.cfg
Using config file "/etc/xen/littleDebian.cfg".
Started domain littleDebian (id=1)

ลองเช็คว่าทำงานได้หรือปล่าว

# xm list
Name                                        ID   Mem VCPUs      State   Time(s)
Domain-0                                     0  6964     4     r-----     29.7
littleDebian                                 1   512     1     -b----      2.8

หากต้องการใช้ Console ของ DomU สามารถทำได้โดย
# xm console littleDebian

เท่านี้ Debian Xen ก็พร้อมใช้งานแล้ว