HAProxy

haproxy是一款專業的7層的反向代理負載均衡器,可以實現4層和7層的負載均衡,四層:lvs(DR),nginx(stream),haproxy(mode tcp)
    七層:nginx(http-upstream),httpd,haproxy(mode http),Pound,...

HAProxy 官方網站:
www.haproxy.com/www.haproxy.org

 HAProxy 與nginx作爲反代服務器的區別:同樣工作在用戶空間,nginx是一款輕量級,能實現緩存、webserver、郵件、負載均衡等功能,但nginx的許多功能都需要第三方的模塊,而haproxy的轉發能力比nginx有更強更靈活的定製性,可以運用splice實現0複製的轉發,並且有更直觀的圖形化管理界面,不過通用性不如nginx,並無緩存功能。

     安裝和配置:
     .可直接通過base  yum源安裝,也可編譯安裝。
     yum -y install haproxy

    程序環境:
主程序:/usr/sbin/haproxy
配置文件:/etc/haproxy/haproxy.cfg
Unit File:/usr/lib/systemd/system/haproxy.service

    主配置文件的結構:
兩個配置段:
    global:全局配置段;
        進程及安全配置相關的參數;
        性能調整相關的參數;
        Debug參數;

    注意:全局配置段中,除了maxconn之外,其他的配置一般無需修改;

    proxies:代理配置段;
        defaults:爲下面的frontend、backend、listen配置段提供默認配置;
        frontend:前端,面向客戶端提供請求信息的接收和處理;相當於nginx的server{};
        backend:後端,面向後端服務器發送請求和接收響應結果;相當於nginx的upstream{};
        listen:用於四層反代和LB,相當於nginx的stream{};

            調度算法
                    roundrobin  動態,加權輪詢,所謂動態就是可以實時生效,不用重啓服務,但是連接數受限,最多支持4128
                    static-rr   靜態輪詢,需重啓服務
                    leastconn   動態,根據後端主機的負載數量進行調度
                    source   類似源地址hash,可以指定hash-type ,有map-based(取膜法,靜態),    consistent(一致性哈希,動態)
                uri  類似於DH算法,目標地址哈希,可以指定hash-type ,有map-based(取膜法,靜態), consistent(一致性哈希,動態)
                hdr(  ):根據請求報文中指定的header(User-agent,referer,hostname,cookie)進行調度,把指定的header的值做hash計算;可根據header首部來進行調度,非常強大,比如根據User-Agent瀏覽器類型來進行調度,可以指定hash-type ,有map-based(取膜法,靜態), consistent(一致性哈希,動態)

                基於會話綁定的cookie
                cookie <name> [ rewrite | insert | prefix ] [ indirect ] [ nocache ] [ postonly ] [ preserve ] [ httponly ] [ secure ] [ domain <domain> ]* [ maxidle <idle> ] [ maxlife <life> ]

應用配置段:
    defaults, listen, backend

    rewrite:將指定名稱的cookie的值重寫,即修改cookie值;
    insert:在原cookie值後面插入新的內容;
    prefix:在原cookie值前面添加新的內容;
    indirect:間接插入,只在insert模式中使用;
    nocache:不做緩存,只在insert模式中使用;

注意:cookie參數的作用爲:基於cookie的session sticky的實現方案;
小實驗
準備三臺host:

host1:172.16.66.71
host2: 172.16.66.72
host3:172.16.66.73
host1: yum install haproxy ,已經被收錄進centos的base源了
分別現在host2和host3上面準備兩臺httpd服務,並存放數個html頁面
yum install -y httpd mariadb-server php
host2: for i in {1..10};do echo "<h1>Page $i on node3</h1>" > /var/www/html/test$i.html; done
host3: for i in {1..10};do echo "<h1>Page $i on node4</h1>" > /var/www/html/test$i.html; done

eg1:利roundrobin實現最簡單的調度,host1,可以同時綁定多個端口
frontend webs
bind :80,:8080
default_backend webserver
backend webserver
balance roundrobin
server web1 172.16.66.72:80 check
server web2 172.16.66.73:80 check

eg2:基於uri的一致性hash

frontend webs
bind :80
default_backend webserver
backend webserver
balance uri
hash-type consistent
server web1 172.16.66.72:80 check
server web2 172.16.66.73:80 check

測試:http://172.16.66.71/test1.html
測試:http://172.16.66.71/test2.html

eg3:基於hdr進行一致性hash調度,User-Agent
frontend webs
bind :80
default_backend webserver
backend webserver
balance hdr(User-Agent)
hash-type consistent
server web1 172.16.66.72:80 check
server web2 172.16.66.73:80 check

eg4: 設置cookie,基於會話綁定
frontend webs
bind :80
default_backend webserver
backend webserver
balance roundrobin
server web1 172.16.66.72:80 check weight 1 cookie web1
server web2 172.16.66.73:80 check weight 3 cookie web2

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