Haproxy 搭建Web羣集
一:常見的Web集羣調度器
Web集羣調度器分爲軟件和硬件,軟件通常使用開源的LVS、Haproxy、Nginx ;硬件一般使用比較多的F5。
二:Haproxy應用分析
Haproxy是一 款可提供高可用性、負載均衡、及基於TCP和HTTP應用的代理的軟件
● 特別適用於負載特別大的Web站點
● 運行在當前的硬件上可支持數以萬計的併發連接連接請求
三:Haproxy調度算法
Haproxy支持多種算法,最常見的三種:
- RR(Round Robin)
RR算法是最簡單的一種算法,即輪詢調度
- LC(Least Connections)
LC算法即最小連接數算法,根據後端的節點連接數大小動態分配前端請求
- SH(Source Hashing)
SH即基於來源訪問調度算法,此算法用於一些有Session會話記錄在服務器端
的場景,可以基於來源的IP、Cookie等做集羣調度。
四:實驗步驟
1、實驗環境
Nginx安裝包,鏈接: https://pan.baidu.com/s/1W2dvxYlyEP4QgHqXNtQWXQ 提取碼: 9vh7
Hproxy安裝包,鏈接: https://pan.baidu.com/s/1ST5CsFCVvM1kFLK537kybQ 提取碼: 2csa 複製這段內容後打開百度網盤手機App,操作更方便哦
2、實驗過程
- ### Nginx的安裝與啓動
(兩臺nginx配置相同)
[root@localhost ~]# hostnamectl set-hostname nginx01
[root@localhost ~]# su
[root@nginx01 ~]#yum install pcre-devel zlib-devel gcc gcc-c++ make -y
[root@nginx01 ~]# useradd -M -s /sbin/nologin nginx ‘創建名爲nginx用戶,且不允許登錄系統’
[root@nginx01 ~]# mkdir /abc ‘創建掛載點’
[root@nginx01 abc]# mount.cifs //192.168.0.107/share /abc ‘192.168.0.107是宿主機地址,share是存放安裝包的文件(需共享)’
[root@nginx01 abc]# tar zxvf nginx-1.12.2.tar.gz -C /opt/
[root@nginx01 abc]#cd /opt/nginx-1.12.2/
[root@nginx01 nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
[root@nginx01 nginx-1.12.2]# make && make install
[root@nginx01 nginx-1.12.2]# cd /usr/local/nginx/html
[root@nginx01 html]# echo "this is kg web" > test.html '網頁內容' '(另一臺nginx內容爲 "this is ac web")'
[root@nginx01 html]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ ‘優化路徑’
[root@nginx02 html]# nginx -t ‘檢查語法’
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx01 html]# nginx ‘開啓nginx’
[root@nginx02 html]# systemctl stop firewalld.service
[root@nginx02 html]# setenforce 0
[root@localhost ~]# hostnamectl set-hostname haproxy
[root@haproxy ~]# yum install pcre-devel bzip2-devel gcc gcc-c++ make -y
[root@haproxy ~]# mkdir /abc
[root@haproxy ~]# mount.cifs //192.168.0.107/share /abc
[root@haproxy abc]# tar zvxf 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]# ls
haproxy.cfg
[root@haproxy haproxy]# vim haproxy.cfg
[root@haproxy haproxy]# cp /opt/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy ‘複製到init.d 啓動進程中’
[root@haproxy haproxy]# chmod +x /etc/init.d/haproxy ‘添加執行權限’
[root@haproxy haproxy]# cd /etc/init.d
[root@haproxy init.d]# chkconfig --add /etc/init.d/haproxy
[root@haproxy init.d]# ln -s /usr/local/sbin/haproxy /usr/sbin ‘創建軟連接’
[root@haproxy init.d]# service haproxy start ‘開啓服務’
Starting haproxy (via systemctl): [ 確定 ]
[root@haproxy init.d]# netstat -ntap | grep haproxy
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3410/haproxy
[root@haproxy init.d]# systemctl stop firewalld.service
[root@haproxy init.d]# setenforce 0
- ### Haproxy日誌管理
對調度器的配置文件進行優化和修改,可以將正常的訪問信息和錯誤的信息分別存放在不同的日誌文件中,方便管理;Haproxy的日誌默認是輸出到系統的 syslog 中,在生產環境中一般單獨定義出來。
[root@haproxy init.d]# cd /etc/haproxy/
[root@haproxy haproxy]# ls
haproxy.cfg
[root@haproxy haproxy]# vim haproxy.cfg
[root@haproxy haproxy]# service haproxy restart
Restarting haproxy (via systemctl): [ 確定 ]
[root@haproxy haproxy]# touch /etc/rsyslog.d/haproxy.conf
[root@haproxy haproxy]# cd /etc/rsyslog.d/
[root@haproxy rsyslog.d]# ls
haproxy.conf listen.conf
[root@haproxy rsyslog.d]# vim 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 rsyslog.d]# systemctl restart rsyslog.service
(不訪問網頁,則不會生成haproxy文件)
[root@haproxy log]# cd haproxy/
[root@haproxy haproxy]# ls
haproxy-info.log
[root@haproxy haproxy]# cat haproxy-info.log