haproxy 學習筆記

 

haproxy常用算法:

一、roundrobin,表示簡單的輪詢,這個不多說,這個是負載均衡基本都具備的;

二、static-rr,表示根據權重,建議關注;

三、leastconn,表示最少連接者先處理,建議關注;

四、source,表示根據請求源IP,建議關注;

五、uri,表示根據請求的URI;

六、url_param,表示根據請求的URl參數'balance url_param' requires an URL parameter name

七、hdr(name),表示根據HTTP請求頭來鎖定每一次HTTP請求;

八、rdp-cookie(name),表示根據據cookie(name)來鎖定並哈希每一次TCP請求。

   

      其實這些算法各有各的用法,我們平時應用得比較多的應該是roundrobin、source和lestconn,大家可以重點關注下。


環境說明:

IP 192.168.2.205       haproxy服務器

IP 192.168.2.206       httpd服務器

IP 192.168.2.207       lamp服務器

IP 192.168.2.103       windows客戶端

 

一、完成狀態頁實驗

1、  配置haproxy.cfg文件

listen stats
    mode http
    bind *:1080
    stats enable
    stats hide-version
    stats uri /haproxyadmin?stats
    stats realm Haproxy\ Statistics
    stats auth admin:kevin
    stats auth kevin:skymobi
    stats admin if TRUE 

    acl statsrc src 192.168.2.0/24
    http-request allow if statsrc
    http-request deny

 

二、反向代理實驗+sesion綁定+負載均衡實驗

1、不考慮session會話配置

配置haproxy.cfg:


frontend  main *:80

    default_backend             webserver

backend webserver
    balance     roundrobin
    server  httpd1 192.168.2.206:80 check
    server  httpd2 192.168.2.207:80 check


 


2、通過源ip綁定的方式來解決sesion會話

配置haproxy.cfg


frontend  main *:80

    default_backend             webserver

backend webserver
    balance     source
    server  httpd1 192.168.2.206:80 check
    server  httpd2 192.168.2.207:80 check




 

3、通過roundrobin+cookie的方式來解決sesion會話

配置haproxy.cfg


frontend  main *:80

    default_backend             webserver

backend webserver
    balance     source
       cookie  LAMP insert indirect nocache
       server  httpd1 192.168.2.206:80 cookie httpd1 check
       server  httpd2 192.168.2.207:80 cookie httpd2 check





4、通過rui的方式來將訪問同一個url的請求,代理給同一臺主機

配置haproxy.cfg:


frontend  main *:80

    default_backend             webserver

backend webserver
    balance     uri
    server  httpd1 192.168.2.206:80 check
    server  httpd2 192.168.2.207:80 check



三、三臺主機,完成web請求的動靜分離實驗

1192.168.2.206安裝httpd

2192.168.2.207安裝lamp

3lamp配置php訪問頁面:

<?php
     $conn = mysql_connect('localhost','root','');
     if ($conn)
           echo "192.168.2.207 : success";
     else  
           echo "192.168.2.207 : failed";
     mysql_close();
     phpinfo();
?>



4haproxy配置修改:


frontend main *:80
     acl url_static  path_beg    -i /static /p_w_picpaths /javascript /style  sheets
     acl url_static   path_end    -i .jpg .gif .png .css .js .html
     use_backend static    if url_static
     default_backend     lamp

backend static
     balance   roundrobin
     server    static 192.168.2.206:80 check
backend lamp
     balance   roundrobin
     server    lamp 192.168.2.207:80 check






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章