Haproxy

下載地址:https://www.haproxy.org/#down

tar -xzf haproxy-1.8.14.tar.gz

cd cd haproxy-1.8.14

查看README:相關的編譯參數

make TARGET=linux2628 PREFIX=/main/server/haproxy

mkdir  /main/server/haproxy

make install PREFIX=/main/server/haproxy

創建配置文件:

mkdir /main/server/haproxy/conf

vim /main/srever/haproxy/conf/haproxy.cfg

注:源碼安裝是沒有示例配置文件的,需要自己創建,如果是rpm安裝的,會有默認的示例配置文件。

示例配置文件粘貼:

方式一:

global

        daemon

        maxconn 256

    defaults

        mode http

        timeout connect 5000ms

        timeout client 50000ms

        timeout server 50000ms

    

        frontend  main 

         bind  *:5000

        #基於七層做負載均衡

        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

        #use_backend <服務器組> if <ACL名字>

        #acl <ACL名字> <類型> <大小寫> <規則>

        default_backend             app

    backend servers

        server server1 127.0.0.1:8000 maxconn 32

方式二:

    # The same configuration defined with a single listen block. Shorter but

    # less expressive, especially in HTTP mode.

    用單個監聽塊定義相同的配置,但是較短代表較少的表達,特別是在HTTP模式下。

    global

        daemon

        maxconn 256

    defaults

        mode http

        timeout connect 5000ms

        timeout client 50000ms

        timeout server 50000ms

    listen http-in

        bind *:80

        server server1 127.0.0.1:8000 maxconn 32

注意:haproxy不會修改Client的http請求頭信息。當Client拿域名www.thd99.com 訪問haproxy時,會訪問後端的www.thd99.com虛擬主機;當Client拿Ip 192.168.0.185訪問haproxy時,會訪問後端的默認虛擬主機。

配置文件:

[root@client conf]# cat haproxy.cfg

#全局

global

    log         127.0.0.1 local2

    chroot      /main/server/haproxy  #chroot改變當前工作目錄,增加安全性

    pidfile     /main/server/haproxy.pid

    maxconn     4000

    user        haproxy

    group       haproxy

    nbproc      1    #number of process 進程的數量

    daemon           #以後臺方式運行haproxy

    #stats socket /var/lib/haproxy/stats

#默認

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                 3000

#定義haproxy狀態訪問

listen stats

    bind 0.0.0.0:8080

    option httplog

    stats enable 

    stats uri /stats            #訪問路徑

    stats realm "Haproxy Auth"  #提示信息

    stats auth admin:admin      #認證的用戶名和密碼

    stats admin if TRUE         #通過認證纔可管理

    stats refresh 3s            #頁面刷新間隔

    stats show-desc demo

    stats hide-version          #隱藏版本號

#定義客戶端與haproxy的訪問

frontend  main

    bind *:80

    default_backend             www.thd99.com

#定義後端

backend www.thd99.com

    balance     roundrobin

    server      server1 192.168.0.186:80 check inter 1s rise 2 fall 3 cookie A maxconn 1000

    server      server2 192.168.0.146:80 check inter 1s rise 2 fall 3 cookie A maxconn 1000

配置日誌輸出:

1.

mkdir /var/log/haproxy

chmod a+w /var/log/haproxy

2. 開啓rsyslog記錄haproxy日誌功能

編輯“/etc/rsyslog.conf”打開如下配置項:

# Provides UDP syslog reception

$ModLoad imudp

$UDPServerRun 514

添加如下內容:

# Save haproxy log

local2.*                       /var/log/haproxy/haproxy.log

3. 修改“/etc/sysconfig/rsyslog”文件,內容如下

# Options for rsyslogd

# Syslogd options are deprecated since rsyslog v3.

# If you want to use them, switch to compatibility mode 2 by "-c 2"

# See rsyslogd(8) for more details

SYSLOGD_OPTIONS="-r -m 0 -c 2"

相關參數說明:

-r:    打開接受外來日誌消息的功能,其監控514 UDP端口;

-x:   關閉自動解析對方日誌服務器的FQDN信息,這能避免DNS不完整所帶來的麻煩;

-m:  修改syslog的內部mark消息寫入間隔時間(0爲關閉),例如240爲每隔240分鐘寫入一次"--MARK--"信息;

-h:   默認情況下,syslog不會發送從遠端接受過來的消息到其他主機,而使用該選項,則把該開關打開,所有接受到的信息都可根據syslog.conf中定義的@主機轉發過去.

4. systemctl restart rsyslog

運行服務:/main/server/haproxy/sbin/haproxy -f /main/server/haproxy/conf/haproxy.cfg

訪問haproxy狀態頁面:192.168.0.185:8080/stats

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