Linux系統架構-----Haproxy與Nginx羣集

一.Haproxy簡介

  • Haproxy是一個使用C語言編寫的自由及開放源代碼軟件,其提供高可用性、負載均衡,以及基於TCP和HTTP的應用的程序代理。
  • Haproxy特別適用於負載特大的web站點,這些站點通常又需要會話保持或七層處理。Haproxy運行在當前的硬件上,完全可以支持數以萬計的併發連接。並且它的運行模式使得它可以很簡單安全的整合進您當前的架構中,同時可以保護你的web服務器不被暴露在網絡上
  • Haproxy實現了一種事件驅動,單一進程模型,此模型支持非常大的併發連接數。多進行或線程模型受內存的限制,系統調度器限制以及無處不在的鎖限制,很少能處理數千併發連接。事件驅動模型因爲在有更好的資源和時間管理的用戶空間實現所有這些任務,所以沒有這些問題。此模型的弊端是,在多核系統上,這些程序通常擴展性較差。這就是爲什麼他們必須進行優化以使每個cpu時間片做更多的工作
  • 此外,包括GItHub、Bitbucket、Stack Overflow、Reddit、Tumber、Twitter、Tuenti在內的知名網站,以及亞馬遜網絡服務系統使用了Haproxy

二.Haproxy搭建web羣集分析

  • Haproxy是目前比較流行的一種羣集調度工具,同類羣集調度工具也有很多,比如LVS和Nginx.相對而言,LVS的性能最好,但是搭建相對複雜,Nginx的upstream模塊支持羣集功能,但是對羣集節點的健康檢查功能不強,性能沒有Haproxy好
  • Haproxy官網是http://haproxy.1wt.eu/
  • 目前常見的web羣集調度器分爲軟件和硬件兩類,軟件通常使用開源的LVS、Haproxy、Nginx,硬件一般使用比較多的是F5,當然國內也有一些產品,比如:梭子魚、綠盟等。
  • Haproxy配置文件的分析
[root@localhost ~]# cd haproxy-1.5.19/examples
[root@localhost examples]# vim haproxy.cfg 
##Haproxy配置文件通常分爲三個部分,即global、defaults、listen
##global爲全局配置,defaults爲默認配置,listen爲應用組件配置
global
        log 127.0.0.1   local0  //配置日誌記錄,local0爲日誌設備,默認存放到系統日誌
        log 127.0.0.1   local1 notice //notice爲日誌級別
        #log loghost    local0 info
        maxconn 4096       //最大連接數
        chroot /usr/share/haproxy
        uid 99  //用戶uid
        gid 99  //用戶gid
        daemon
        #debug
        #quiet

defaults
        log     global   //定義日誌爲global配置中的日誌定義
        mode    http     //模式爲http
        option  httplog  //採用http日誌格式記錄日誌
        option  dontlognull 
        retries 3    //檢查節點服務器失敗次數,連續達到三次失敗,則節點不可用
        redispatch
        maxconn 2000    //最大連接數
        contimeout      5000    //連接超時時間
        clitimeout      50000    //客戶端超時時間
        srvtimeout      50000    //服務器超時時間

listen  appli1-rewrite 0.0.0.0:10001  //定義一個appli1-rewrite的應用
        cookie  SERVERID rewrite
        balance roundrobin //負載均衡算法使用輪詢算法
        server  app1_1 192.168.34.23:8080 cookie app1inst1 check inter 2000 rise 2 fall 5
        server  app1_2 192.168.34.32:8080 cookie app1inst2 check inter 2000 rise 2 fall 5
        server  app1_3 192.168.34.27:8080 cookie app1inst3 check inter 2000 rise 2 fall 5
        server  app1_4 192.168.34.42:8080 cookie app1inst4 check inter 2000 rise 2 fall 5

listen  appli2-insert 0.0.0.0:10002
        option  httpchk  //檢查服務器的文件
        balance roundrobin
        cookie  SERVERID insert indirect nocache
        server  inst1 192.168.114.56:80 cookie server01 check inter 2000 fall 3
        server  inst2 192.168.114.56:81 cookie server02 check inter 2000 fall 3
        capture cookie vgnvisitor= len 32

        option  httpclose               # disable keep-alive
        rspidel ^Set-cookie:\ IP=       # do not let this cookie tell our internal IP address

listen  appli3-relais 0.0.0.0:10003
        dispatch 192.168.135.17:80

注:日誌的級別,0(EMERG,緊急),1(ALERT,警告),2(CRIT,嚴重),3(ERR,錯誤),4(WARNING,提醒),5(NOTICE,注意),6(INFO,信息),7(DEBUG,調試)

三.搭建Nginx和Haproxy羣集架構

  • 網絡拓補圖

  • 實驗環境
類型 IP地址 系統 軟件包
Haproxy調度器 192.168.43.101 centos7 haproxy-1.5.19.tar.gz
Nginx服務器1 192.168.43.102 centos7 nginx-1.12.2.tar.gz
Nginx服務器2 192.168.43.103 centos7 nginx-1.12.2.tar.gz
cilent 192.168.43.105 cents7  

注:以上軟件包可至官網下載

  • 配置Nginx服務器1
Nginx web server 1
1.下載相關軟件包
yum install -y pcre-devel zlib-devel gcc gcc-c++ make
2.創建nginx的賬戶
useradd -M -s /sbin/nologin nginx
3.解壓軟件包
tar zxvf nginx-1.12.2.tar.gz -C /opt
4.配置環境,編譯且安裝
cd /opt/nginx-1.12.0/

./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx

make && make install

5.設置主頁面
[root@localhost nginx-1.12.2]# cd /usr/local/nginx/html
[root@localhost html]# ls
50x.html  index.html
[root@localhost html]# rz

[root@localhost html]# ls
1.jpg  50x.html  index.html
[root@localhost html]# vim index.html 

<img height=200px src="1.jpg"/>

6.將執行腳本建立軟鏈接
[root@localhost html]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin
7.驗證語法錯誤
[root@localhost 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
8.開啓nginx服務,關閉防火牆
[root@localhost html]# nginx 
[root@localhost html]# systemctl stop firewalld
[root@localhost html]# setenforce 0
  • 驗證Nginx服務器1的配置

  • 配置Nginx服務器2
Nginx web server 2
1.下載相關軟件包
yum install -y pcre-devel zlib-devel gcc gcc-c++ make
2.創建nginx的賬戶
useradd -M -s /sbin/nologin nginx
3.解壓軟件包
tar zxvf nginx-1.12.2.tar.gz -C /opt
4.配置環境,編譯且安裝
cd /opt/nginx-1.12.0/

./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx

make && make install

5.設置主頁面
[root@localhost nginx-1.12.2]# cd /usr/local/nginx/html
[root@localhost html]# ls
50x.html  index.html
[root@localhost html]# rz

[root@localhost html]# ls
1.jpg  50x.html  index.html
[root@localhost html]# vim index.html 

<img height=200px src="1.jpg"/>

6.將執行腳本建立軟鏈接
[root@localhost html]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin
7.驗證語法錯誤
[root@localhost 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
8.開啓nginx服務,關閉防火牆
[root@localhost html]# nginx 
[root@localhost html]# systemctl stop firewalld
[root@localhost html]# setenforce 0
  • 驗證Nginx服務器2的配置

  • 配置haproxy調度器
1.下載軟件包
yum install -y pcre-devel bzip2-devel gcc gcc-c++ make
2.解壓haproxy軟件包
tar zxvf haproxy-1.5.19.tar.gz
3.編譯安裝haproxy
cd haproxy-1.5.19/

make TARGET=linux26        //64位系統
make install
4.創建配置文件的目錄,且複製模板
[root@localhost ~]# mkdir /etc/haproxy
[root@localhost ~]# cd haproxy-1.5.19
[root@localhost haproxy-1.5.19]# cp examples/haproxy.cfg /etc/haproxy/

5.編輯配置文件
vim /etc/haproxy/haproxy.cfg
# this config needs haproxy-1.1.28 or haproxy-1.2.1

global
        log /dev/log    local0 info   //開啓日誌記錄功能
        log /dev/log    local0 notice
        #log loghost    local0 info
        maxconn 4096
#       chroot /usr/share/haproxy  //關閉固定家目錄
        uid 99
        gid 99
        daemon
        #debug
        #quiet

defaults
        log     global
        mode    http
        option  httplog
        option  dontlognull
        retries 3
#       redispatch  //關閉對於失敗服務器發送請求
        maxconn 2000
        contimeout      5000
        clitimeout      50000
        srvtimeout      50000

listen webcluster 0.0.0.0:80  //監聽任意網段的80端口
        option httpchk GET /index.html   
        balance roundrobin
##兩個節點服務器,2000毫秒進行健康檢查,如果三次沒有迴應則認爲節點故障   
        server inst1 192.168.43.102:80 check inter 2000 fall 3
        server inst1 192.168.43.103:80 check inter 2000 fall 3

6.配置服務控制方式
[root@localhost ~]# cp /root/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy
[root@localhost ~]# chmod +x /etc/init.d/haproxy
[root@localhost ~]# chkconfig --add /etc/init.d/haproxy 
[root@localhost ~]# ln -s /usr/local/sbin/haproxy /usr/sbin

7.配置日誌記錄,添加腳本
[root@localhost haproxy]# touch /etc/rsyslog.d/haproxy.conf
[root@localhost haproxy]# cd /etc/rsyslog.d/
[root@localhost rsyslog.d]# ls
haproxy.conf  listen.conf
[root@localhost 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
&~
8.關閉安全性功能,開啓服務
systemctl stop firewalld
setenforce 0
service haproxy start
systemctl restart rsyslog.service    //重啓系統日誌

  • 驗證haproxy配置,調度與日誌記錄是否成功

注:在使用客戶機訪問時,可能不會調轉到另外一臺服務器,可以選擇不同IP地址的客戶機訪問

發佈了118 篇原創文章 · 獲贊 132 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章