2013年8月18日 星期日

Easy use of HAProxy

專門的事情交給專業去做 - 如果要做LB,交給 - HAProxy準沒錯!
感謝許多網友的介紹(這位仁兄的部落格有非常詳細的說明)
透過HAProxy可以快速做到網站服務基本的Load Balancing
在MiCloud上要架設一台HAProxy就更快了...

準備:
申請一台SmartOS,建議申請Standard64主機,套件支援到2013Q1

STEP1: 查找支援的haproxy版本

$ pkgin se haproxy
haproxy-1.4.21       Reliable, high performance TCP/HTTP load balancer

=: package is installed and up-to-date
<: data-blogger-escaped-available="" data-blogger-escaped-but="" data-blogger-escaped-installed="" data-blogger-escaped-is="" data-blogger-escaped-newer="" data-blogger-escaped-package="" data-blogger-escaped-version="">: installed package has a greater version than available package


STEP2: 安裝合適版本套件

$ pkgin install haproxy-1.4.21
calculating dependencies... done.

nothing to upgrade.
1 packages to be installed: haproxy-1.4.21 (2592K to download, 0B to install)

proceed ? [Y/n] Y
downloading packages...
haproxy-1.4.21.tgz                               100%  928KB 132.6KB/s 149.9KB/s   00:07
installing packages...
installing haproxy-1.4.21...
haproxy-1.4.21: copying /opt/local/share/examples/haproxy/haproxy.cfg to /opt/local/etc/haproxy.cfg
//////////////////////////////////////////////////////////////////////////////

This package is SMF enabled, which means you can use SMF to 'enable',
'disable' or 'restart' the persistent daemon process, e.g.:

  svcadm enable haproxy:default

The SMF manifest was automatically imported now.

See our wiki on what's SMF and how to use it to your advantage:

  http://wiki.joyent.com/display/smart/About+the+Service+Management+Facility

//////////////////////////////////////////////////////////////////////////////
pkg_install warnings: 0, errors: 0
reading local summary...
processing local summary...
updating database: 100%
marking haproxy-1.4.21 as non auto-removable
- See more at: http://blogger.micloud.tw/#sthash.9ZPGTyp4.dpuf

STEP3: 設定HAProxy

完整的設定在網路上很多,這邊不細談設定部分,只精簡的說明要proxy某台主機的作法:

STEP4: 最後,起動一下服務(因為服務是由SMF方式掛載,可以透過svc*系列指令操作,詳情可以參考:)

$ svcadm enable haproxy

檢查:如果state(第一個欄位)為online的話,表示服務啟動OK拉∼

$ svcs haproxy
STATE          STIME    FMRI
online          3:03:20 svc:/pkgsrc/haproxy:default

Debug:

$ svcs -xv haproxy <-- 檢查啟動是否有相依性錯誤
$ tail -f `svcs -L haproxy` <-- 檢查服務運作screen output log

關閉服務:

$ svcadm disable haproxy

重起服務:

$ svcadm restart haproxy