1. 程式人生 > >haproxy 7層負載均衡代理轉發實戰講解(一)-老男孩筆記系列

haproxy 7層負載均衡代理轉發實戰講解(一)-老男孩筆記系列

#########################################################
# haproxy L7 實戰講解
#date:2010-06-09
#作者:老男孩---《老男孩linux就業培訓中心 》
#QQ:31333741 MAIL:[email protected]
#QQ交流群:45039636   
#blog: http://oldboy.blog.51cto.com
#pblog:http://blog.etiantian.org
##########################################################
前言:
haproxy 就不多說了。是值得信賴的優秀的7層轉發軟體。
本人實際應用4000以上萬pv的訪問量級站點,做L7的整體表現和F5/netscaler等硬體負載均衡比,毫不遜色。
當然,這需要一個好的架構體系和比較優的配置優化。
主要體現在可維護性,自主掌控能力,價格,擴充套件能力,靈活性等N多方面

本文針對 玩過haproxy的哥們,因此 就不基礎掃盲工作了。望大家諒解,問題可以進群和大家交流。
有想了解初級內容的,請關注我年底將要出版的書籍,暫定名 《老男孩的linux運維筆記》

#######################################################
haproxy 7層負載均衡代理轉發實戰講解(一) 之301跳轉測試
#######################################################

█ 1、rs web server機測試環境準備:

●1.1 在/var下分別建立php、nginx、pic三個站點目錄,並增加index.htms檔案及增加內容
假定 php nginx pic分別代表解析不同的服務。

[[email protected] ~]#for name in php nginx pic ;do mkdir -p /var/$name;echo $name >/var/$name/index.html;done
●1.2 檢查結果
[[email protected] ~]# for name in php nginx pic ;do echo -n "/var/$name/index.html → :";cat /var/$name/index.html; done
/var/php/index.html → :php
/var/nginx/index.html → :nginx
/var/pic/index.html → :pic

●1.3 安裝http服務
[[email protected] ~]# yum install httpd -y

●1.4 配置http服務

先做配置檔案備份
[[email protected] conf]# cd /etc/httpd/conf
[[email protected] conf]# cp httpd.conf httpd.conf.oldboy.110625
[[email protected] conf]# ls -l
total 88
-rw-r--r-- 1 root root 34399 Jun 26 16:40 httpd.conf
-rw-r--r-- 1 root root 34399 Jun 26 16:49 httpd.conf.oldboy.110625
-rw-r--r-- 1 root root 13139 May  4 18:54 magic

編輯httpd.conf 最下面加
<Directory "/var">
    Options FollowSymLinks
    AllowOverride none
    Order allow,deny
    Allow from all
</Directory>

NameVirtualHost *:80
<VirtualHost *:80>
        ServerAdmin [email protected]
        ServerName nginx.etiantian.org
        ServerAlias etiantian.org
        DocumentRoot "/var/nginx"
</VirtualHost>
<VirtualHost *:80>
        ServerAdmin [email protected]
        ServerName php.etiantian.org
        DocumentRoot "/var/php"
</VirtualHost>
<VirtualHost *:80>
        ServerAdmin [email protected]
        ServerName pic.etiantian.org
        DocumentRoot "/var/pic"
</VirtualHost>

提示:配置完成記得重起http服務。

●1.5 http伺服器本地增加host內容如下
echo '10.0.0.162 nginx.etiantian.org' >>/etc/hosts
echo '10.0.0.162 php.etiantian.org' >>/etc/hosts
echo '10.0.0.162 pic.etiantian.org' >>/etc/hosts
echo '10.0.0.162 etiantian.org' >>/etc/hosts

●1.6 在我們的膝上型電腦上
C:\WINDOWS\system32\drivers\etc\hosts增加如下hosts內容

10.0.0.162 nginx.etiantian.org
10.0.0.162 php.etiantian.org
10.0.0.162 pic.etiantian.org
10.0.0.162 etiantian.org
嚴重提示:
1.這裡解析的IP 為http server的IP
2.這裡的host相當於模擬DNS的解析

●1.7測試增加http虛擬主機的配置
訪問:http://nginx.etiantian.org 結果應該為nginx,其它類推。
訪問:http://php.etiantian.org 結果應該為php,其它類推。
訪問:http://pic.etiantian.org 結果應該為pic,其它類推。


█ 2 配置haproxy L7負載均衡
●2.1 haproxy.conf配置
#______________________________________________________________________

 

defaults
        log     global
        mode    http
        retries 3
        option redispatch
        contimeout      5000
        clitimeout      50000
        srvtimeout      50000
        stats enable
        stats hide-version
        stats uri /admin?stats
        stats auth proxy:123456
        option httpclose
 

●2.2 更改hosts
在我們的膝上型電腦上
C:\WINDOWS\system32\drivers\etc\hosts增加如下hosts內容

10.0.0.162 nginx.etiantian.org
10.0.0.162 php.etiantian.org
10.0.0.162 pic.etiantian.org
10.0.0.162 etiantian.org
嚴重提示:這裡解析的IP 為haproxy server的IP

●2.3 測試haproxy的轉發應用
確認host檔案配置正常後,可以瀏覽 etiantian.org
看是否能跳轉到nginx.etiantian.org 檢查點:url和內容顯示
提示:個別瀏覽器,有可能看不到URL跳轉,只要是內容顯示正確就對了。

可以修改配置在測試下:
        acl short_dom hdr(Host) -i etiantian.org
        redirect prefix http://php.etiantian.org code 301 if short_dom
目的:使訪問http://etiantian.org 跳轉到http://php.etiantian.org

重起haproxy服務後,進行訪問檢視。

#########################################################

注意以上 已經應用到正式環境N久 大家可放心使用。
更多7層的應用測試,請關注 http://www.etiantian.org
 

 

●2.4 更多7層的 的技術
更多7層的應用測試,請隨時關注 http://www.etiantian.org

比如 根據字尾進行過濾轉發
acl url_static  path_end         .gif .png .jpg .css .js

在比如根據目錄進行過濾轉發
acl oldboy_java path_beg /java/
acl static_ryan path_beg /images/
acl static_ryan path_beg /css/


偶會盡快整理詳細的文件 近期放出 和大家分享.
謝謝大家瀏覽啊。哈哈!

文章結尾,給大家上傳張圖,誰在用haproxy.