Haproxy的負載均衡、動靜分離、狀態監控、近期網絡架構

1、Haproxy的簡介javascript

    Haproxy的官網站點:http://haproxy.com/php

    HAProxy提供高可用性、負載均衡以及基於TCP和HTTP應用的代 理,支持虛擬主機,它是免費、快速而且可靠的一種解決方案。HAProxy特別適用於那些負載特大的web站點,這些站點一般又須要會話保持或七層處理。HAProxy運行在當前的硬件上,徹底能夠支持數以萬計的併發鏈接。而且它的運行模式使得它能夠很簡單安全的整合進您當前的架構中, 同時能夠保護你的web服務器不被暴露到網絡上。css

     HAProxy實現了一種事件驅動, 單一進程模型,此模型支持很是大的併發鏈接數。多進程或多線程模型受內存限制 、系統調度器限制以及無處不在的鎖限制,不多能處理數千併發鏈接。事件驅動模型由於在有更好的資源和時間管理的用戶端(User-Space) 實現全部這些任務,因此沒有這些問題。此模型的弊端是,在多核系統上,這些程序一般擴展性較差。這就是爲何他們必須進行優化以 使每一個CPU時間片(Cycle)作更多的工做。html

2、Haproxy的工做模式前端

wKioL1Np5h_i9MFbAADhowCMXv8632.jpg

大體的意思就是:能夠分爲兩部分,java

第一部分是互聯網,包括互聯網上衆多的路由設備、Firewall等;web

第二部分是本地的網絡架構,包括Firewall,proxy、Web server,Database等redis

Haproxy的註釋Local balanceer in proxy mode -- 工做在代理模下是一個負載均衡的功能算法

經過Haproxy代理後端的Web server羣集,若是須要與數據庫交互,有Web server與Database創建鏈接。數據庫

3、Haproxy的安裝和配置說明

CentOS 6.5系統中yum源提供的Haproxy的版本爲1.5.18,在這裏咱們直接用yum安裝

一、安裝haproxy

yum install haproxy -y
rpm -ql haproxy                    #能夠查看yum安裝Haproxy生成了哪些文件
/etc/haproxy
/etc/haproxy/haproxy.cfg            #haproxy的配置文件
/etc/logrotate.d/haproxy
/etc/rc.d/init.d/haproxy            #haproxy的服務腳本文件
/etc/sysconfig/haproxy
/usr/bin/halog
/usr/bin/iprange
/usr/sbin/haproxy
/usr/share/doc/haproxy-1.5.18
/usr/share/doc/haproxy-1.5.18/CHANGELOG
/usr/share/doc/haproxy-1.5.18/LICENSE
/usr/share/doc/haproxy-1.5.18/README
/usr/share/doc/haproxy-1.5.18/acl-content-sw.cfg
/usr/share/doc/haproxy-1.5.18/acl.fig
/usr/share/doc/haproxy-1.5.18/architecture.txt
/usr/share/doc/haproxy-1.5.18/close-options.txt
/usr/share/doc/haproxy-1.5.18/coding-style.txt
/usr/share/doc/haproxy-1.5.18/configuration.txt
/usr/share/doc/haproxy-1.5.18/content-sw-sample.cfg
/usr/share/doc/haproxy-1.5.18/cookie-options.txt
/usr/share/doc/haproxy-1.5.18/cttproxy-src.cfg
/usr/share/doc/haproxy-1.5.18/design-thoughts
/usr/share/doc/haproxy-1.5.18/design-thoughts/backends-v0.txt
/usr/share/doc/haproxy-1.5.18/design-thoughts/backends.txt
/usr/share/doc/haproxy-1.5.18/design-thoughts/be-fe-changes.txt
/usr/share/doc/haproxy-1.5.18/design-thoughts/binding-possibilities.txt
/usr/share/doc/haproxy-1.5.18/design-thoughts/buffer-redesign.txt
/usr/share/doc/haproxy-1.5.18/design-thoughts/buffers.fig
/usr/share/doc/haproxy-1.5.18/design-thoughts/config-language.txt
/usr/share/doc/haproxy-1.5.18/design-thoughts/connection-reuse.txt
/usr/share/doc/haproxy-1.5.18/design-thoughts/cttproxy-changes.txt
/usr/share/doc/haproxy-1.5.18/design-thoughts/entities-v2.txt
/usr/share/doc/haproxy-1.5.18/design-thoughts/how-it-works.txt
/usr/share/doc/haproxy-1.5.18/design-thoughts/http_load_time.url
/usr/share/doc/haproxy-1.5.18/design-thoughts/rate-shaping.txt
/usr/share/doc/haproxy-1.5.18/design-thoughts/sess_par_sec.txt
/usr/share/doc/haproxy-1.5.18/gpl.txt
/usr/share/doc/haproxy-1.5.18/haproxy-en.txt
/usr/share/doc/haproxy-1.5.18/haproxy-fr.txt
/usr/share/doc/haproxy-1.5.18/haproxy.1
/usr/share/doc/haproxy-1.5.18/haproxy.cfg
/usr/share/doc/haproxy-1.5.18/internals
/usr/share/doc/haproxy-1.5.18/internals/acl.txt
/usr/share/doc/haproxy-1.5.18/internals/body-parsing.txt
/usr/share/doc/haproxy-1.5.18/internals/buffer-operations.txt
/usr/share/doc/haproxy-1.5.18/internals/buffer-ops.fig
/usr/share/doc/haproxy-1.5.18/internals/connect-status.txt
/usr/share/doc/haproxy-1.5.18/internals/connection-header.txt
/usr/share/doc/haproxy-1.5.18/internals/connection-scale.txt
/usr/share/doc/haproxy-1.5.18/internals/entities.fig
/usr/share/doc/haproxy-1.5.18/internals/entities.pdf
/usr/share/doc/haproxy-1.5.18/internals/entities.svg
/usr/share/doc/haproxy-1.5.18/internals/entities.txt
/usr/share/doc/haproxy-1.5.18/internals/hashing.txt
/usr/share/doc/haproxy-1.5.18/internals/header-parser-speed.txt
/usr/share/doc/haproxy-1.5.18/internals/header-tree.txt
/usr/share/doc/haproxy-1.5.18/internals/http-cookies.txt
/usr/share/doc/haproxy-1.5.18/internals/http-docs.txt
/usr/share/doc/haproxy-1.5.18/internals/http-parsing.txt
/usr/share/doc/haproxy-1.5.18/internals/naming.txt
/usr/share/doc/haproxy-1.5.18/internals/pattern.dia
/usr/share/doc/haproxy-1.5.18/internals/pattern.pdf
/usr/share/doc/haproxy-1.5.18/internals/polling-states.fig
/usr/share/doc/haproxy-1.5.18/internals/repartition-be-fe-fi.txt
/usr/share/doc/haproxy-1.5.18/internals/sequence.fig
/usr/share/doc/haproxy-1.5.18/internals/stats-v2.txt
/usr/share/doc/haproxy-1.5.18/internals/stream-sock-states.fig
/usr/share/doc/haproxy-1.5.18/internals/todo.cttproxy
/usr/share/doc/haproxy-1.5.18/lgpl.txt
/usr/share/doc/haproxy-1.5.18/proxy-protocol.txt
/usr/share/doc/haproxy-1.5.18/queuing.fig
/usr/share/doc/haproxy-1.5.18/tarpit.cfg
/usr/share/doc/haproxy-1.5.18/url-switching.cfg
/usr/share/haproxy
/usr/share/haproxy/400.http         #400錯誤的狀態碼頁面
/usr/share/haproxy/403.http         #403錯誤的狀態碼頁面
/usr/share/haproxy/408.http         #408錯誤的狀態碼頁面
/usr/share/haproxy/500.http         #500錯誤的狀態碼頁面
/usr/share/haproxy/502.http         #502錯誤的狀態碼頁面
/usr/share/haproxy/503.http         #503錯誤的狀態碼頁面
/usr/share/haproxy/504.http         #504錯誤的狀態碼頁面
/usr/share/haproxy/README
/usr/share/man/man1/halog.1.gz      #能夠直接使用man halog查看命令的使用語法
/usr/share/man/man1/haproxy.1.gz    #能夠直接使用man haproxy查看命令的使用語法
/var/lib/haproxy

二、haproxy的配置文件說明

# cd /etc/haproxy/
# cp haproxy.cfg haproxy.cfg.bak
# vim haproxy.cfg
**********************************************************************
#---------------------------------------------------------------------
# Example configuration for a possible web application.  See the
# full configuration options online.
#
#   http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------
 
#---------------------------------------------------------------------
# Global settings    
#---------------------------------------------------------------------
global    #全局配置文件
    # to have these messages end up in /var/log/haproxy.log you will
    # need to:     #配置日誌
    #
    # 1) configure syslog to accept network log events.  This is done
    #    by adding the '-r' option to the SYSLOGD_OPTIONS in
    #    /etc/sysconfig/syslog    #修改syslog配置文件
    #
    # 2) configure local2 events to go to the /var/log/haproxy.log
    #   file. A line like the following can be added to
    #   /etc/sysconfig/syslog    #定義日誌設備
    #
    #    local2.*                       /var/log/haproxy.log
    #
    log         127.0.0.1 local2        #日誌配置,全部的日誌都記錄本地,經過local2輸出
 
    chroot      /var/lib/haproxy        #改變haproxy的工做目錄
    pidfile     /var/run/haproxy.pid    #指定pid文件的路徑
    maxconn     4000                    #最大鏈接數的設定
    user        haproxy                 #指定運行服務的用戶
    group       haproxy                 #指定運行服務的用戶組
    daemon
 
    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats
 
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
     
    mode                    http                  #默認使用協議,能夠爲{http|tcp|health} http:是七層協議 tcp:是四層 health:只返回OK
    log                     global                #全局日誌記錄
    option                  httplog               #詳細記錄http日誌
    option                  dontlognull           #不記錄空日誌
    option http-server-close                      #啓用http-server-close
    option forwardfor       except 127.0.0.0/8    #來自這些信息的都不forwardfor
    option                  redispatch            #從新分發,ServerID對應的服務器宕機後,強制定向到其餘運行正常的服務器
    retries                 3                      #3次鏈接失敗則認爲服務不可用
    timeout http-request    10s                    #默認http請求超時時間
    timeout queue           1m                     #默認隊列超時時間
    timeout connect         10s                    #默認鏈接超時時間
    timeout client          1m                     #默認客戶端超時時間
    timeout server          1m                     #默認服務器超時時間
    timeout http-keep-alive 10s                    #默認持久鏈接超時時間
    timeout check           10s                    #默認檢查時間間隔
    maxconn                 3000                   #最大鏈接數
 
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend  main *:5000
    #定義ACL規則以如".html"結尾的文件;-i:忽略大小寫
    acl url_static       path_beg       -i /static /images /javascript /stylesheets
    acl url_static       path_end       -i .jpg .gif .png .css .js
 
    use_backend static          if url_static    #調用後端服務器並檢查ACL規則是否被匹配
    default_backend             app              #客戶端訪問時默認調用後端服務器地址池
 
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static                    #定義後端服務器
    balance     roundrobin        #定義算法;基於權重進行輪詢
    server      static 127.0.0.1:4331 check    check:啓動對後端server的健康狀態檢測
 
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
    balance     roundrobin
    server  app1 127.0.0.1:5001 check
    server  app2 127.0.0.1:5002 check
    server  app3 127.0.0.1:5003 check
    server  app4 127.0.0.1:5004 check

三、haproxy的命令詳解

# haproxy -h
HA-Proxy version 1.5.18 2016/05/10
Copyright 2000-2016 Willy Tarreau <willy@haproxy.org>

Usage : haproxy [-f <cfgfile>]* [ -vdVD ] [ -n <maxconn> ] [ -N <maxpconn> ]
        [ -p <pidfile> ] [ -m <max megs> ] [ -C <dir> ]
        -v displays version ; -vv shows known build options.
        -d enters debug mode ; -db only disables background mode.
        -dM[<byte>] poisons memory with <byte> (defaults to 0x50)
        -V enters verbose mode (disables quiet mode)
        -D goes daemon ; -C changes to <dir> before loading files.
        -q quiet mode : don't display messages
        -c check mode : only check config files and exit
        -n sets the maximum total # of connections (2000)
        -m limits the usable amount of memory (in MB)
        -N sets the default, per-proxy maximum # of connections (2000)
        -L set local peer name (default to hostname)
        -p writes pids of all children to this file
        -de disables epoll() usage even when available
        -dp disables poll() usage even when available
        -dS disables splice usage (broken on old kernels)
        -dV disables SSL verify on servers side
        -sf/-st [pid ]* finishes/terminates old pids. Must be last arguments.

四、配置Haproxy的日誌

默認狀況下,Haproxy沒有啓用日誌文件,可是咱們能夠根據haproxy的配置文件作修改。
(1)修改系統日誌的配置文件
# vim /etc/sysconfig/rsyslog
SYSLOGD_OPTIONS="-c 2 -r"
(2)增長日誌設備
# vim /etc/rsyslog.conf 
local2.*                                                /var/log/haproxy.log
(3)重啓一下日誌服務
# /etc/init.d/rsyslog restart
關閉系統日誌記錄器:                                       [肯定]
啓動系統日誌記錄器:                                       [肯定]
(4)查看日誌記錄信息
# tail -f /var/log/haproxy.log

4、Haproxy負載均衡web

一、一個配置示例:

Haproxy-Server:10.10.172.195

Apache1-Client:10.10.172.196

Apache2-Client:10.10.172.197

**********************************************************************
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    # to have these messages end up in /var/log/haproxy.log you will
    # need to:
    #
    # 1) configure syslog to accept network log events.  This is done
    #    by adding the '-r' option to the SYSLOGD_OPTIONS in
    #    /etc/sysconfig/syslog
    #
    # 2) configure local2 events to go to the /var/log/haproxy.log
    #   file. A line like the following can be added to
    #   /etc/sysconfig/syslog
    #
    #    local2.*                       /var/log/haproxy.log
    #
    log         127.0.0.1 local2
 
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon
 
defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 30000
 
listen stats
    mode http
    bind 0.0.0.0:8080
    stats enable
    stats hide-version
    stats uri     /haproxyadmin?stats
    stats realm   Haproxy\ Statistics
    stats auth    admin:123456
    stats admin if TRUE
 
 
frontend http-in
    bind *:80
    mode http
    log global
    option httpclose
    option logasap
    option dontlognull
    capture request  header Host len 20
    capture request  header Referer len 60
    default_backend servers
 
frontend healthcheck
    bind :1099
    mode http
    option httpclose
    option forwardfor
    default_backend servers
 
backend servers
    balance roundrobin
    server websrv1 10.10.172.196:80 check maxconn 2000
    server websrv2 10.10.172.197:80 check maxconn 2000
**********************************************************************

二、啓動Haproxy服務

# service haproxy start
Starting haproxy: [  OK  ]
# chkconfig haproxy on

三、瀏覽器訪問

單獨訪問Apache1:10.10.172.196

2017-12-13_153947.png

單獨訪問Apache2:10.10.172.197

2017-12-13_154141.png


屢次刷新訪問Haproxy:10.10.172.195

屢次刷新Haproxy的website,會切換到後端不一樣的webserver主機上。

2017-12-13_154141.png

2017-12-13_154240.png

5、Haproxy的狀態監控

一、Haproxy的監控頁面

listen stats                            #關聯前端和後端定義一個完整的代理
    mode http                           #設置代理協議
    bind 0.0.0.0:8080                   #綁定相應的端口
    stats enable                        #開啓Haproxy統計狀態
    stats refresh 3s             #統計頁面自動刷新時間間隔
    stats hide-version                  #隱藏代理服務器版本
    stats uri     /haproxyadmin?stats   #訪問的url
    stats realm   Haproxy\ Statistics   #統計頁面認證時提示內容信息
    stats auth    admin:123456          #設置登陸用戶和密碼     
    stats admin if TRUE                 #若是認證經過,則就能夠打開stats

二、訪問監控頁面

(1)登錄監控頁面

admin/123456

2017-12-13_154619.png

(2)查看監控頁面

2017-12-13_154921.png

(3)模擬一下故障

# service httpd stop
Stopping httpd: [  OK  ]

(4)在次查看監控頁面

2017-12-13_155301.png

6、Haproxy的動靜分離

haproxy動靜分離的配置

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    # to have these messages end up in /var/log/haproxy.log you will
    # need to:
    #
    # 1) configure syslog to accept network log events.  This is done
    #    by adding the '-r' option to the SYSLOGD_OPTIONS in
    #    /etc/sysconfig/syslog
    #
    # 2) configure local2 events to go to the /var/log/haproxy.log
    #   file. A line like the following can be added to
    #   /etc/sysconfig/syslog
    #
    #    local2.*                       /var/log/haproxy.log
    #
    log         127.0.0.1 local2
 
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon
 
defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 30000
 
listen stats
    mode http
    bind 0.0.0.0:1080
    stats enable
    stats hide-version
    stats uri     /haproxyadmin?stats
    stats realm   Haproxy\ Statistics
    stats auth    admin:123456
    stats admin if TRUE
 
 
frontend http-in
    bind *:80
    mode http
    log global
    option httpclose
    option logasap
    option dontlognull
    capture request  header Host len 20
    capture request  header Referer len 60
    acl url_static  path_end -i .html .jpg .gif
    acl url_dynamic path_end -i .php
    default_backend servers
    use_backend lnmmp if url_dynamic
 
backend servers
    balance roundrobin
    server websrv1 10.10.172.196:80 check rise 2 fall 1 weight 2 maxconn 2000
    server websrv2 10.10.172.197:80 check rise 2 fall 1 weight 2 maxconn 2000
 
backend lnmmp
    balance source
    server websrv3 10.10.172.198:80 check rise 2 fall 1 maxconn 2000

動靜分離實現的需求:

  訪問index.html頁面,由Apache1,、Apache2輪詢顯示;訪問index.php頁面,有LNMP或者LAMP服務器顯示,

相關文章
相關標籤/搜索