詳述Haproxy搭建web羣集

常見的Web集羣調度器

  • 目前常見的Web集羣調度器分爲軟件和硬件,軟件一般使用開源的LVS、Haproxy、Nginx,硬件通常使用比較多的時F5,也有不少人使用國內的一些產品,如梭子魚、綠盟等

Haproxy應用分析

LVS在企業應用中抗負載能力強,但存在不足

  • LVS不支持正則處理,不能實現動靜分離
  • 對於大型網站,LVS的實施配置複雜,維護成本相對較高

Haproxy是一款可提供高可用性、負載均衡、及基於TCP和HTTP應用的代理的軟件

  • 特別適用於負載特別大的Web站點
  • 運行在當前的硬件上可支持數以萬計的併發鏈接請求

Haproxy調度算法原理

Haproxy支持多種調度算法,最經常使用的有三種

  • RR (Round Robin)
    • RR算法是最簡單最經常使用的一種算法,即輪詢調度
  • 理解舉例
    • 有三個節點A、B、C,第一個用戶訪問會被指派到節點A,第二個用戶訪問會被指派到節點B,第三個用戶訪問會被指派到節點
    • 第四個用戶訪問繼續指派到節點A,輪詢分配訪問請求實現負載均衡效果
  • LC (Least Connections)
    • LC算法即最小鏈接數算法,根據後端的節點鏈接數大小動態分配前端請求
  • 理解舉例
    • 有三個節點A、B、C,各節點的鏈接數分別爲A:四、B:五、 C:6, 此時若是有第一個用戶鏈接請求,會被指派到A上,鏈接數變爲A:五、B:五、 C:6
    • 第二個用戶請求會繼續分配到A上,鏈接數變爲A:六、B:五、 C:6; 再有新的請求會分配給B,每次將新的請求指派給鏈接數最小的客戶端
    • 因爲實際狀況下A、B、C的鏈接數會動態釋放,很難會出現同樣鏈接數的狀況,所以此算法相比較rr算法有很大改進,是目前用到比較多的一種算法
  • SH (Source Hashing)
    • SH即基於來源訪問調度算法,此算法用於- -些有Session會話記錄在服務器端的場景,能夠基於來源的IP、Cookie等作集羣調度
  • 理解舉例html

    • 有三個節點A、B、C,第一個用戶第一-次訪問被指派到了A,第二個用戶第一次訪問被指派到了B
    • 當第一個用戶第二次訪問時會被繼續指派到A,第二個用戶第二次訪問時依舊會被指派到B,只要負載均衡調度器不重啓,第一個用戶訪問都會被指派到A,第二個用戶訪問都會被指派到B,實現集羣的調度
    • 此調度算法好處是實現會話保持,但某些IP訪問量很是大時會引|起負載不均衡部分節點訪問量超大,影響業務使用

    案例拓撲圖

    詳述Haproxy搭建web羣集

    Haproxy安裝與啓動

在負載均衡器上安裝Haproxy

  • 安裝步驟
    • 安裝基礎軟件包
    • 編譯安裝haproxy
    • 要注意操做系統版本,是32位系統仍是64位
  • 創建Haproxy的配置文件
    • 建立配置文件目錄/etc/haproxy
    • 將源碼包提供的配置文件樣例haproxy.cfg複製到配置文件目錄中

Haproxy配置文件詳解

  • Haproxy配置文件一般分爲三個部分
    • global:爲全局配置
    • defaults:爲默認配置
    • listen:爲應用組件配置
  • global配置參數
    • log 127.0.0.1 local0: 配置日誌記錄,local0爲日誌設備,默認存放到系統日誌
    • log 127.0.0.1 local1 notice: notice爲日誌級別,一般有24個級別
    • maxconn 4096:最大鏈接數
    • uid 99:用戶uid
    • d 99:用戶gid
  • defaults配置項配置默認參數,通常會被應用組件繼承,若是在應用組件中沒有特別聲明,將安裝默認配置參數設置
    • log global:定義日誌爲global配置中的日誌定義
    • mode http:模式爲http
    • option httplog:採用http日誌格式記錄日誌
    • retries 3:檢查節點服務器失敗連續達到三次則認爲節點不可用
    • maxconn 2000:最大鏈接數
    • contimeout 5000:鏈接超時時間
    • clitimeout 50000:客戶端超時時間
    • srvtimeout 50000:服務器超時時間
  • listen配置項目一-般爲配置應用模塊參數前端

    • listen appli4-backup 0.0.0.0:10004:定義一個appli4-backup的應用
    • option httpchk /index.html:檢查服務器的index.html文件
    • option persist :強制將請求發送到已經down掉的服務器
    • balance roundrobin:負載均衡調度算法使用輪詢算法
    • server inst1 192. 168.114.56:80 check inter 2000 fall 3:定義在線節點
    • server inst2 192. 168.114.56:81 check inter 2000 fall 3 backup:定義備份節點

    配置與測試

  • 根據實際的案例將相應參數進行修改:
    • listen webcluster 0.0.0.0:80
    • option httpchk GET /index.html
    • balance roundrobin
    • server inst1 192. 168.10.61:80 check inter 2000 fall 3
    • server inst2 192.168. 10.62:80 check inter 2000 fall 3
  • 建立自啓動腳本,並啓動Haproxy服務
  • 測試Haproxy集羣
    • 測試高性能
    • 可在兩個不一樣的瀏覽器中分別訪問兩個測試網站,正常狀況下應該出現兩個網站的測試頁面
    • 高可用性
    • 將其中一臺Nginx服務器停用,在客戶端瀏覽器中訪問Haproxy,正常狀況下應出現另外一臺Nginx服務器網站測試頁面

Haproxy日誌管理

  • Haproxy的日誌默認是輸出到系統的syslog中,在生產環境中通常單獨定義出來
  • 定義的方法步驟
    • 修改Haproxy配置文件中關於日誌配置的選項,加入配置:
    • log /dev/log local0 info
    • log /dev/log local0 notice
    • 修改rsyslog配置,將Haproxy相關的配置獨立定義到haproxy.conf,並放到/etc/rsyslog.d/下
    • 保存配置文件並重啓rsyslog服務,完成rsyslog配置
  • 訪問Haproxy集羣測試網頁並測試日誌信息

Haproxy參數優化

  • 隨着企業網站負載增長,haproxy參數優化至關重要
    • maxconn:最大鏈接數,根據應用實際狀況進行調整,推薦使用10240
    • daemon:守護進程模式,Haproxy可使用非守護進程模式啓動,建議使用守護進程模式啓動
    • nbproc:負載均衡的併發進程數,建議與當前服務器CPU核數相等或爲其2倍
    • retries:重試次數,主要用於對集羣節點的檢查,若是節點多,且併發量大,設置爲2次或3次
    • option http-server-close: 主動關閉http請求選項,建議在生產環境中使用此選項
    • timeout http-keep-alive: 長鏈接超時時間,設置長鏈接超時時間能夠設置爲10s
    • timeout http-request: http請求超時時間,建議將此時間設置爲5~ 10s,增長http鏈接釋放速度
    • timeout client: 客戶端超時時間,若是訪問量過大,節點響應慢能夠將此時間設置短一些, 建議設置爲1min左右就能夠了

操做實例

實驗環境

Haporxy服務器IP地址:192.168.144.175
web1服務器IP地址:192.168.144.151
web2服務器IP地址:192.168.144.176
client測試機

在web1,web2服務器上安裝Nginx

[root@web1 ~]# yum install -y \       //安裝環境須要組件包
> pcre-devel \                        //開發包
> zlib-devel \                        //壓縮包
> gcc \
> gcc-c++ \
> make 
[root@web1 ~]# useradd -M -s /sbin/nologin nginx       //建立系統用戶
[root@web1 ~]# mkdir /abc   ##建立掛載點
[root@web1 ~]# mount.cifs //192.168.100.8/LNMP-C7 /abc/       //掛載
Password for root@//192.168.100.3/LNMP-C7:  
[root@web1 ~]# cd /abc/
[root@web1 abc]# tar zxvf nginx-1.12.2.tar.gz -C /opt        //解壓
[root@web1 abc]# cd /opt/nginx-1.12.2/
[root@web1 nginx-1.12.2]# ./configure \                      //進行配置
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx
[root@web1 nginx-1.12.2]# make && make install
[root@web1 nginx-1.12.2]# echo "this is kgv web" > /usr/local/nginx/html/test.html      //建立站點網頁內容,web2上爲this is accp web
[root@web1 nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/             //便於系統識別
[root@web1 nginx-1.12.2]# nginx -t                                                       //檢查語法
[root@web1 nginx-1.12.2]# nginx                                                         //開啓服務
[root@web1 nginx-1.12.2]# systemctl stop firewalld.service                              //關閉防火牆
[root@web1 nginx-1.12.2]# setenforce 0

在haproxy服務器上安裝haproxy調度服務

[root@haproxy ~]# yum install -y \                 //安裝環境組件工具
> pcre-devel \
> bzip2-devel \
> gcc \
> gcc-c++ \
> make
[root@haproxy ~]# systemctl stop firewalld.service          //關閉防火牆
[root@haproxy ~]# setenforce 0
[root@haproxy ~]# mkdir /abc
[root@haproxy ~]# mount.cifs //192.168.100.8/LNMP-C7 /abc/         //掛載
[root@haproxy ~]# cd /abc/
[root@haproxy abc]# tar zxvf haproxy-1.5.19.tar.gz -C /opt/          //解壓
[root@haproxy abc]# cd /opt/haproxy-1.5.19/
[root@haproxy haproxy-1.5.19]# make TARGET=linux26              //編譯
[root@haproxy haproxy-1.5.19]# make install                     //安裝
[root@haproxy haproxy-1.5.19]# mkdir /etc/haproxy               //建立配置文件目錄
[root@haproxy haproxy-1.5.19]# cp examples/haproxy.cfg /etc/haproxy/            //模板複製到配置目錄下
[root@haproxy haproxy-1.5.19]# cd /etc/haproxy/
[root@haproxy haproxy]# vim haproxy.cfg                    //編輯配置文件
#chroot /usr/share/haproxy     //註釋語句
#redispatch                    //註釋語句
//刪除全部listen項目,並添加
listen  webcluster 0.0.0.0:80
                option httpchk GET /test.html       //web網頁
                balance roundrobin                  //開啓輪詢模式
                server inst1 192.168.144.151:80 check inter 2000 fall 3      //健康檢查請求三次
                server inst2 192.168.144.176:80 check inter 2000 fall 3
[root@haproxy haproxy]# cp /opt/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy     //啓動文件
[root@haproxy haproxy]# chmod +x /etc/init.d/haproxy                    //執行權限
[root@haproxy haproxy]# chkconfig --add /etc/init.d/haproxy             //添加到service
[root@haproxy haproxy]# ln -s /usr/local/sbin/haproxy /usr/sbin/        //便於系統識別
[root@haproxy haproxy]# service haproxy start                           //開啓服務
Starting haproxy (via systemctl):                          [  肯定  ]
[root@haproxy haproxy]# netstat -ntap | grep haproxy                         //查看端口
tcp      0     0 0.0.0.0:80       0.0.0.0:*       LISTEN      39884/haproxy

使用client測試網頁

詳述Haproxy搭建web羣集詳述Haproxy搭建web羣集

日誌定義,修改haproxy配置文件

[root@haproxy haproxy]# vim /etc/haproxy/haproxy.cfg  ##修改配置文件
global
                log /dev/log    local0 info   ##添加兩個級別的日誌文件
                log /dev/log    local0 notice
                #log loghost    local0 info
[root@haproxy haproxy]# service haproxy restart  ##重啓服務
[root@haproxy haproxy]# touch /etc/rsyslog.d/haproxy.conf  ##建立系統日誌haproxy配置文件
[root@haproxy haproxy]# vim /etc/rsyslog.d/haproxy.conf
if ($programname == 'haproxy' and $syslogseverity-text == 'info')  ##根據級別建立不一樣的日誌文件
then -/var/log/haproxy/haproxy-info.log
&~
if ($programname == 'haproxy' and $syslogseverity-text == 'notice')
then -/var/log/haproxy/haproxy-notice.log
&~
[root@haproxy haproxy]# systemctl restart rsyslog.service  ##重啓系統日誌服務
[root@haproxy haproxy]# cd /var/log/  ##此時是沒有haproxy日誌
##從新訪問網頁
[root@haproxy haproxy]# cd /var/log/haproxy/
[root@haproxy haproxy]# ls   ##此時就生成了info級別的日誌文件
haproxy-info.log
相關文章
相關標籤/搜索