取代 squid 的 HTTP 加速器 varnish

據說用 3台 varnish 可以抵 12台 squid 來用 , 有時間要來玩一下.

Varnish

http://varnish.projects.linpro.no/

 

Varnish is a state-of-the-art, high-performance HTTP accelerator. It uses the advanced features in Linux 2.6, FreeBSD 6/7 and Solaris 10 to achieve its high performance.

Some of the features include

  • A modern design
  • VCL – a very flexible configuration language
  • Load balancing with health checking of backends
  • Partial support for ESI
  • URL rewriting
  • Graceful handling of "dead" backends
  • more features

Varnish is free software and is licenced under a modified BSD licence. Please read the introduction to get started with Varnish.

用 HAProxy 作 load balancer – 窮人的 SLB ( server load balance)

這是簡單版的架構, http flow:
cacae1dfce25d17bd8225bdcd8b7b772

其實, 裝起來並沒有想像中複雜, 只是準備一個測試環境比較麻煩些罷了–> vmware 又幫了我不少忙 😛

參考他的 online document:
http://haproxy.1wt.eu/download/1.3/doc/haproxy-en.txt

及架構圖
http://haproxy.1wt.eu/download/1.3/doc/architecture.txt

略翻完這兩個文件就可以來測試了 , 首先準備三台 web server : webA 到 webC , 然後還要一台 server 當 haproxy server 這台不用跑 apache , 安裝 haproxy 很簡單, 在 gentoo 就是 emerge –av haproxy , 設定檔要自己建 (放到 /etc/ )

我的 /etc/haproxy.cnf

listen webfarm 172.30.0.235:80
monitor-uri /haproxy_status
stats uri /stats
stats auth admin:admin
mode http
balance roundrobin
cookie SERVERID insert indirect
option httpchk HEAD /index.php HTTP/1.0
server webA 172.30.0.206:80 cookie A check
server webB 172.30.0.227:80 cookie B check
server webC 172.30.0.228:80 cookie C check

第一次連上後, haproxy 如他的 menual 寫的 , 會丟一個 cookie 給 client , 作為下次要連的實體 server 的依據 , 我把那台 apache 停掉, 果然就連到別去了 , 然後 phpinfo 中值得紀錄的是:

SERVER_NAME 就是 haproxy.cfg 中寫的 listen 的 IP , 然後 , SERVER_ADDR 就是實體連到的 apache 的 IP , haproxy 會不停的丟 HTTP/1.0 的 HEAD 取得 apache 是否還活著.

在 webA-C 的 aapache access log 中若 沒特別改的話, 就是紀錄 haproxy server 的 IP address

….

好了! 實驗完成了!

結論, HAProxy 在他的官網寫說他:

“ 提供一個免費/快速的 HA / LOAD BALANCE 方案 , 可是我覺得他僅能夠說 HA / BALANCE (並沒有很徹底的偵測 server loading) , 並且在 SPF 方面也並沒有很好的解決辦法.

不過呢, 至少 HAProxy 比 DNS roundrobin 還好.

各種 LOAD BALANCE 方案 評估表

SLB

CPU用量

轉送效率

偵測連線

roundrobin DNS

最佳,直接傳給client

NO

ipvsadm

最低

YES

mod_proxy

YES

haproxy

YES

 

過了一個晚上想了想 , 即使 Citrix / Alteon / Foundry 這些 SLB hardware device 也沒有做到真正的 loading detective , 所以就一個免費又高容量的 HAProxy 來說, 算是很好的 SLB 解決方案了.

ㄎㄎ 有做過 HAProxy 實驗的都會貼這一張:

ec6601bde1fdb02e373efc74ea97e0f1

簡單用一個方法來判斷目前是啥環境

阿毛找到這個方式 , 我覺得很可行 ….

在 php.ini ( apache 跟 cli 兩個目錄下都要設定) 的最後面加上

my_environment = product
( or dev / stage / stage2 / product )

我們在程式中用這方法來判斷, 目前是啥環境…

if ( get_cfg_var(‘my_environment’)==’dev’ ) {
  // setting for dev
} else if ( get_cfg_var(‘my_environment’)==’stage’ ) {
  // setting for stage
} else if ( get_cfg_var(‘my_environment’)==’stage2′ ) {
  // setting for stage2
}
// default setting is production

opensolaris / Optimized Open Source Software Stack (Cool Stack)

看到一個有用的套件

for the Sun Solaris Operating System(TM)

http://cooltools.sunsource.net/coolstack/

Cool Stack includes several packages in the SVR4 package format, so you can install just the ones you need. Some of the applications in Cool Stack already ship with Solaris, but these are either older versions and/or not built with full optimization. Further, Cool Stack has been pre-configured to have the most popular applications (Apache, PHP, MySQL) to work seamlessly out of the box.
Deploying PHP From Cool Stack in Sun Java System Web Server

Configuring Cool Stack PHP With Web Server

Next, do the following:

1. Go to the Cool Stack PHP 5 installation location. Type:

cd /opt/coolstack/php5

In that directory is a script called setup-ws7-php.sh.

2. Run setup-ws7-php.sh. Type:

./setup-ws7-php.sh

This message is displayed:

Usage:
This script will configure Coolstack PHP with Sun Java System Web Server
7. Here, you will need to provide the top level location of your Web Server
7 installation and your Web Server 7 instance_name name to which this
script should configure to run PHP scripts.

Enter your Web Server installation location(/sun/webserver7):

3. Type the full path for your Web Server installation.

The script then prompts you to type a Web Server instance name. That name is the path to a directory in your installation location—one that contains all the configuration files for running your Web site. If you are using Web Server in Sun Java Enterprise System 5, your instances are under /var/opt/SUNWwbsvr7.

For the example in this article, cite the instance https-coolstack that you created previously. To enable a different instance for PHP, type that instance name. Your instance is then ready for PHP, which you can deploy with Cool Stack 1.2 PHP on Web Server.

Finally, do the following:

1. Start Web Server. Type:

/sun/webserver7/https-coolstack/bin/startserv

2. Create sample PHP files under /sun/webserver7/https-coolstack/docs.

用 apache 的 setenv module 在程式中加入判斷 developer 是誰

<VirtualHost 172.30.0.35:80>
        ServerName rimmon.goods.dev.xxx.com.tw
        ServerAdmin [email protected]
        DocumentRoot /home/rimmon/bid/goods/www
        SetEnv DEVELOPER rimmon
        alias /js/ /home/rimmon/bid/www/www/js/
        alias /css/ /home/rimmon/bid/www/www/css/
        <Directory “/home/rimmon/bid/goods/www”>
          Options  FollowSymLinks
          AllowOverride None
          Allow from all
        </Directory>
</VirtualHost>