Haproxy搭建web羣集——(實踐!)

常見的web集羣調度器

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

Haproxy應用分析

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

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

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

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

Haproxy調度算法原理

RR:最簡單常用的,輪詢調度
LC:最小連接數算法,根據後端的節點連接數大小動態分配前端請求
SH:來源訪問調度算法,用於有session會話記錄在服務器端,可以基於來源ip,cookie做羣集調度

實驗拓撲圖

Haproxy搭建web羣集——(實踐!)

實驗環境

Haporxy服務器 192.168.13.175
web1服務器 192.168.13.151
web2服務器 192.168.13.176
client測試機

1,在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.3/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

2,在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.3/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   ##編輯配置文件
----------刪除所有listen項目,並添加------------------------------------
註釋以下語句
chroot /usr/share/haproxy
redispatch
添加
listen  webcluster 0.0.0.0:80
                option httpchk GET /test.html  ##web網頁
                balance roundrobin  ##輪詢
                server inst1 192.168.13.151:80 check inter 2000 fall 3  ##健康檢查請求三次
                server inst2 192.168.13.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 

3,使用client測試網頁

Haproxy搭建web羣集——(實踐!)
Haproxy搭建web羣集——(實踐!)

4,日誌定義,修改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

謝謝閱讀!!

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