HaProxy安裝和常用命令

安裝haproxy

對於 haproxy 安裝,網上有大量的資源可以參考,最常見的是使用 yum 和 編譯安裝兩種方式。

yum 安裝 haproxy

CentOS自帶了haproxy,但可能版本比較老。可以在IUS源上找到穩定版的haproxy。

主要步驟如下:

(1)配置yum源

cat <<eof>/etc/yum.repos.d/ius-7.repo
[ius]
name = IUS for Enterprise Linux 7 - $basearch
baseurl = https://repo.ius.io/7/$basearch/
enabled = 1
repo_gpgcheck = 0
gpgcheck = 1
gpgkey = https://repo.ius.io/RPM-GPG-KEY-IUS-7
[ius-debuginfo]
name = IUS for Enterprise Linux 7 - $basearch - Debug
baseurl = https://repo.ius.io/7/$basearch/debug/
enabled = 0
repo_gpgcheck = 0
gpgcheck = 1
gpgkey = https://repo.ius.io/RPM-GPG-KEY-IUS-7
[ius-source]
name = IUS for Enterprise Linux 7 - Source
baseurl = https://repo.ius.io/7/src/
enabled = 0
repo_gpgcheck = 0
gpgcheck = 1
gpgkey = https://repo.ius.io/RPM-GPG-KEY-IUS-7
eof

(2)清理、更新和快速生成 yum 緩存

yum clean all
yum update
yum makecache fast

(3)查看下系統中haproxy版本有哪些

yum list|grep haproxy

(4)yum安裝 haproxy18u

#這裏安裝 haproxy18u 

yum install haproxy18u -y

編譯安裝 haproxy

編譯安裝haproxy時,可以藉助於pcre環境,該環境下編譯時藉助正則表達式分析編譯速度會快很多,但是沒有該環境也可以安裝。

主要步驟如下:

(1)安裝 pcre pcre-devel

yum -y install pcre pcre-devel

(2)創建haproxy運行的用戶和組

groupadd -r haproxy
useradd -g haproxy -M -s /sbin/nologin haproxy

(3)編譯安裝haproxy

#下載地址:
http://www.haproxy.org/ 
#解壓:
tar zxf haproxy-1.8.23.tar.gz -C /usr/local
#編譯安裝:
cd /usr/local/haproxy-1.8.23
make TARGET=linux3100 ARCH=x86_64 PREFIX=/usr/local/haproxy USE_PCRE=1
make install PREFIX=/usr/local/haproxy

說明⚠️:

make時需要使用TARGET指定內核及版本,可以通過uname -r來查看,版本如下:

- linux22     for Linux 2.2
- linux24     for Linux 2.4 and above (default)
- linux24e    for Linux 2.4 with support for a working epoll (> 0.21)
- linux26     for Linux 2.6 and above
- linux2628   for Linux 2.6.28, 3.x, and above (enables splice and tproxy)
- solaris     for Solaris 8 or 10 (others untested)
- freebsd     for FreeBSD 5 to 10 (others untested)
- netbsd      for NetBSD
- osx         for Mac OS/X
- openbsd     for OpenBSD 5.7 and above
- aix51       for AIX 5.1
- aix52       for AIX 5.2
- cygwin      for Cygwin
- haiku       for Haiku
- generic     for any other OS or version.
- custom      to manually adjust every setting

使用ARCH指定架構,不過ARCH選項可省。使用USE_PCRE=1表示使用PCRE環境編譯,加快編譯速度。

本文的操作系統內核版本爲3.10.0,TARGET指定爲 linux2628。我這邊使用 linux3100 也沒有報錯~

編譯安裝完成後,只有3個目錄:doc、share和sbin,sbin裏面只有一個haproxy的主程序haproxy。

(4)管理haproxy服務

ln -s /usr/local/haproxy/sbin/haproxy /usr/sbin/haproxy
cd /usr/local/haproxy-1.8.23/examples
cp haproxy.init /etc/rc.d/init.d/haproxy
cd /etc/rc.d/init.d
chmod +x haproxy

(5)設置haproxy相關的配置文件

#創建配置目錄
mkdir /usr/local/haproxy/conf 
#創建主配置文件
touch /usr/local/haproxy/conf/haproxy.cfg
#創建啓動腳本配置目錄
mkdir /etc/haproxy/  
#添加配置文件的軟連接
ln -s /usr/local/haproxy/conf/haproxy.cfg /etc/haproxy/haproxy.cfg 
#創建日誌目錄
mkdir /usr/local/haproxy/log
#創建日誌文件
touch /usr/local/haproxy/log/haproxy.log
#添加軟連接
ln -s /usr/local/haproxy/log/haproxy.log /var/log/haproxy.log  
#拷貝錯誤頁面
cp -r /usr/local/haproxy-1.8.23/examples/errorfiles/ /usr/local/haproxy/
#給錯誤頁面添加軟連接
ln -s /usr/local/haproxy/errorfiles /etc/haproxy/errorfiles

(6)配置haproxy.cfg參數

#編輯 haproxy.cfg 配置文件
vim /usr/local/haproxy/conf/haproxy.cfg    

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
 log 127.0.0.1 local2   ###[err warning info debug] 
 chroot /usr/local/haproxy
 pidfile /var/run/haproxy.pid ###haproxy的pid存放路徑,啓動進程的用戶必須有權限訪問此文件 
 maxconn 4000     ###最大連接數,默認4000
 user haproxy
 group haproxy
 daemon       ###創建1個進程進入deamon模式運行。此參數要求將運行模式設置爲"daemon"

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will 
# use if not designated in their block
#---------------------------------------------------------------------
defaults
 mode http    ###默認的模式mode { tcp|http|health },tcp是4層,http是7層,health只會返回OK
 log global   ###採用全局定義的日誌
 option dontlognull  ###不記錄健康檢查的日誌信息
 option httpclose  ###每次請求完畢後主動關閉http通道 
 option httplog   ###日誌類別http日誌格式 
 option forwardfor  ###如果後端服務器需要獲得客戶端真實ip需要配置的參數,可以從Http Header中獲得客戶端ip 
 option redispatch  ###serverId對應的服務器掛掉後,強制定向到其他健康的服務器
 timeout connect 10000 #default 10 second timeout if a backend is not found
 timeout client 300000 ###客戶端連接超時
 timeout server 300000 ###服務器連接超時
 maxconn  60000  ###最大連接數
 retries  3   ###3次連接失敗就認爲服務不可用,也可以通過後面設置 
####################################################################
listen stats
  bind 0.0.0.0:1080   #監聽端口 
  stats refresh 30s   #統計頁面自動刷新時間 
  stats uri /stats   #統計頁面url 
  stats realm Haproxy Manager #統計頁面密碼框上提示文本 
  stats auth admin:admin  #統計頁面用戶名和密碼設置 
  #stats hide-version   #隱藏統計頁面上HAProxy的版本信息
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend main
 bind 0.0.0.0:80
 acl url_static path_beg -i /static /p_w_picpaths /javascript /stylesheets
 acl url_static path_end -i .jpg .gif .png .css .js

 use_backend static if url_static  ###滿足策略要求,則響應策略定義的backend頁面
 default_backend dynamic   ###不滿足則響應backend的默認頁面

#---------------------------------------------------------------------
# static backend for serving up p_w_picpaths, stylesheets and such
#---------------------------------------------------------------------

backend static
 balance  roundrobin     ###負載均衡模式輪詢
 server  static 127.0.0.1:80 check ###後端服務器定義

backend dynamic
 balance roundrobin
 server   websrv1 192.168.180.9:80 check maxconn 2000
 server   websrv2 192.168.180.4:80 check maxconn 2000

#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------

errorfile 403 /etc/haproxy/errorfiles/403.http
errorfile 500 /etc/haproxy/errorfiles/500.http
errorfile 502 /etc/haproxy/errorfiles/502.http
errorfile 503 /etc/haproxy/errorfiles/503.http

(7)啓動 haproxy 服務

#haproxy配置文件語法檢查
haproxy -c -f /usr/local/haproxy/conf/haproxy.cfg
Configuration file is valid 
#haproxy服務啓動
systemctl start haproxy
#haproxy服務開機自啓動
chkconfig --add haproxy
chkconfig haproxy on
chkconfig --list|grep haproxy

(8)設置haproxy日誌

#編輯/etc/rsyslog.conf 取消註釋:

$ModLoad imudp 
$UDPServerRun 514 
$ModLoad imtcp 
$InputTCPServerRun 514 
#添加:
local2.*                                                /var/log/haproxy.log          

說明⚠️:

以上修改haproxy日誌會同時寫入到/var/log/haproxy.log/var/log/messages兩個文件裏,由於haproxy日誌量很多所以只讓其寫入到/var/log/haproxy.log文件需要修改/etc/rsyslog.conf文件這個配置:

*.info;mail.none;authpriv.none;cron.none                /var/log/messages
更改爲:
*.info;mail.none;authpriv.none;cron.none;local2.none               /var/log/messages      #這樣只會寫入到/var/log/haproxy.log

修改/etc/sysconfig/rsyslog

#修改”SYSLOGD_OPTIONS”參數
-c 2 使用兼容模式,默認是 -c 5;-r 開啓遠程日誌;-m 0 標記時間戳,單位是分鐘,0表示禁用該功能。
#完整內容如下
# 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=""
SYSLOGD_OPTIONS="-c 2 -r -m 0"
#SYSLOGD_OPTIONS="-c 2 -r -m 0"
&~
#重新啓動rsyslog服務
systemctl restart rsyslog.service

(9)測試

打開瀏覽器打開haproxy監控頁面,192.168.246.168:1080/stats,賬號:admin,密碼:admin

查看 haproxy 的log:tail -f /var/log/haproxy.log

擴展

haproxy常用命令:

# 檢查配置文件語法
haproxy -c -f /etc/haproxy/haproxy.cfg

# 以daemon模式啓動,以systemd管理的daemon模式啓動
haproxy -D -f /etc/haproxy/haproxy.cfg [-p /var/run/haproxy.pid]
haproxy -Ds -f /etc/haproxy/haproxy.cfg [-p /var/run/haproxy.pid]

# 啓動調試功能,將顯示所有連接和處理信息在屏幕
haproxy -d -f /etc/haproxy/haproxy.cfg

# restart。需要使用st選項指定pid列表
haproxy -f /etc/haproxy.cfg [-p /var/run/haproxy.pid] -st `cat /var/run/haproxy.pid`

# graceful restart,即reload。需要使用sf選項指定pid列表
haproxy -f /etc/haproxy.cfg [-p /var/run/haproxy.pid] -sf `cat /var/run/haproxy.pid`

# 顯示haproxy編譯和啓動信息
haproxy -vv

參考文檔

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