在 CentOS 上安裝 HAProxy

2018/07/25 18:19 1 評論 A+ a-


HAProxy 是一個使用 C 語言編寫的自由及開放原始碼軟體,
其提供高可用性、負載均衡,以及基於 TCP 和 HTTP 的應用程式代理。

安裝環境:
CentOS 6.5
HAProxy 1.8.12
安裝必要套件:
yum install mysql #如果要將 MySQL 做 HA 的話才需要
yum install openssl-devel

下載 HAProxy 1.8.12:
cd /tmp
wget http://www.haproxy.org/download/1.8/src/haproxy-1.8.12.tar.gz
tar -zxvf haproxy-1.8.12.tar.gz
cd haproxy-1.8.12

安裝 HAProxy:
cd haproxy-1.8.12
make clean
make TARGET=linux2628 PREFIX=/usr/local/haproxy USE_OPENSSL=1 ADDLIB=-lz
上述的 TARGET使用 uname -r 查看 CentOS 內核版本
Linux 2.6 及以上版本使用 linux26
Linux 2.6.28, 3.x及以上版本使用 linux2628(啟用splice和tproxy)
make install PREFIX=/usr/local/haproxy
mkdir /var/lib/haproxy #建立文件目錄
mkdir /usr/local/haproxy/conf #建立文件目錄

新增 HAProxy 設定檔:
vi /usr/local/haproxy/conf/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
    #
    # 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              #進程在後台作為守護進程運行

    # 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
    log                     global
    option                  dontlognull
    option                  redispatch          #當客戶端連接到出現故障的服務器時,將請求轉發給其他機器
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s                 #連接server超時時間
    timeout client          1m                  #客戶端相應超時時間
    timeout server          1m                  #server端嚮應超時時間
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000

#---------------------------------------------------------------------
# main frontend which proxys to the backends
# 接收請求的前端虛擬節點
#---------------------------------------------------------------------

listen webservice #HTTP 做 HA,webservice 為此組設定的名稱,可以自行調整
    bind *:80
    mode                    http
    option                  httplog
    option http-server-close
    option forwardfor       except 127.0.0.0/8  #在http請求頭中添加X-Forwarded-For,把客戶端IP發送給服務器

    balance     roundrobin
    cookie SERVERID insert indirect nocache #可以讓某個瀏覽器發出的 Request 統一導向同一台機器,避免一些資料交換的問題
    server  webserver1 192.168.0.167:80 check #第一台機器
    server  webserver2 192.168.0.196:80 check #第二台機器

    # HAProxy 監控儀表板
    stats enable
    stats hide-version
    stats realm "Haproxy Statistics"
    stats uri /ha?stats #之後在 HTTP 的網域後方加上 /ha?stats 即可以看到監控畫面
    # 帳號密碼
    stats auth admin:admin721
    stats refresh 10s

listen dbservice #MySQL 做 HA,dbservice 為此組設定的名稱,可以自行調整
    bind *:3306
    mode                    tcp

    option mysql-check user haproxy_check

    balance     roundrobin
    server  dbserver1 192.168.0.167:3306 check #第一台機器
    server  dbserver2 192.168.0.196:3306 check #第二台機器

將 rc.local 文件添加可執行權限:
chmod +x /etc/rc.d/rc.local
chmod +x /etc/rc.local

把 HAProxy 的啓動腳本添加到開機自啓動:
echo "/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg" >> /etc/rc.local

執行 HAProxy 的啓動腳本:
killall haproxy
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg

1 留言 :

留言
2022年4月1日 下午5:34:00 刪除

Thank you for sharing useful information with us. please keep sharing like this. And if you are searching a unique and Top University in India, Colleges discovery platform, which connects students or working professionals with Universities/colleges, at the same time offering information about colleges, courses, entrance exam details, admission notifications, scholarships, and all related topics. Please visit below links:

Advantages of Getting Master of Computer Application (MCA) Degree After Graduation

What are the career and jobs opportunities after Master of Computer application (MCA)

回覆留言
avatar

歡迎留下寶貴的建議,或者給我一個鼓勵喔!