Haproxy 搭建Web羣集

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、實驗環境

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-gvmGQ9XK-1582196906771)(C:\Users\xumin\AppData\Roaming\Typora\typora-user-images\1582167462555.png)]

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
  • 掛載並解壓nginx安裝包
[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")'
  • 優化路徑,啓動nginx ,關閉防火牆
[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
  • ### Haproxy服務器的配置

  • 安裝相關軟件包
[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
  • 新建配置文件目錄/etc/haproxy ,並將源碼包提供的配置文件樣例 haproxy.cfg複製到新建文件目錄中
[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 

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-1tLwmWHM-1582196906772)(C:\Users\xumin\AppData\Roaming\Typora\typora-user-images\1582192948486.png)]

  • 創建自啓動腳本
[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
  • 在win10客戶端訪問兩個測試網站,正常情況下會出現兩個網站的測試頁面

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-4BMl2qKW-1582196906772)(C:\Users\xumin\AppData\Roaming\Typora\typora-user-images\1582194511687.png)]

  • ### Haproxy日誌管理

對調度器的配置文件進行優化和修改,可以將正常的訪問信息和錯誤的信息分別存放在不同的日誌文件中,方便管理;Haproxy的日誌默認是輸出到系統的 syslog 中,在生產環境中一般單獨定義出來。

  • 調整日誌級別,修改主配置文件
[root@haproxy init.d]# cd /etc/haproxy/
[root@haproxy haproxy]# ls
haproxy.cfg
[root@haproxy haproxy]# vim haproxy.cfg

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-POv1WZHH-1582196906774)(C:\Users\xumin\AppData\Roaming\Typora\typora-user-images\1582194923096.png)]

  • 修改 rsyslog配置,創建配置文件,將 Haproxy相關的配置獨立定義到haproxy.conf,並放到/etc/rsyslog.d/下
[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 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章