使用haproxy來實現sphinx負載均衡與健康監測 引自http://www.ttlsa.com/archives/295#more-295
環境如下:
【node1】
Sphinx:192.168.1.189
【node2】
Sphinx:192.168.1.101
【node3】
Haproxy:192.168.1.103
【node3】上配置:
# apt-get install make gcc g++ automake libtool
1.安裝
# wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.18.tar.gz
# tar zxvf haproxy-1.4.18.tar.gz -C ../software/
# cd haproxy-1.4.18
# make TARGET=linux26 PREFIX=/usr/local/haproxy-1.4.18
# make install PREFIX=/usr/local/haproxy-1.4.18
配置
# cd /usr/local/haproxy-1.4.18
# vim haproxy.cfg
global
log 127.0.0.1 local0 info //日誌[err warning info debug]
maxconn 4096 //單個進程的最大連接數
pidfile /usr/local/haproxy-1.4.18/log/haproxy.pid
user haproxy
group haproxy
daemon //守護進程運行
nbproc 4 //作爲守護進程運行時,創建多少個進程
defaults
log global
mode http
option dontlognull //不記錄空連接
option log-health-checks //記錄健康檢測日誌
retries 3 //連接失敗後的重連次數
option redispatch //在連接失敗或斷開情況下,允許當前會話被重新分發
maxconn 2000 //最大連接數
timeout connect 5000ms
timeout client 30000ms
timeout server 30000ms
listen haproxy-status 0.0.0.0:8888
mode http
option httplog
stats uri /hasphinx //統計頁面url
stats realm Global\ statistics
stats auth hasphinx:hasphinx //登錄用戶和密碼
listen proxy-sphinx-1 0.0.0.0:3312
mode tcp
balance roundrobin //服務器分配算法
option tcpka
option httpchk
server node1 192.168.1.189:3312 weight 1 check port 9312 inter 1s rise 2 fall 2
server node2 192.168.1.101:3312 weight 1 check port 9312 inter 1s rise 2 fall 2
//後臺服務器
#weight 服務器權重
#check port 檢測端口
#inter 設置連續的兩次健康檢測間的時間
#rise 指定多少次連接成功的健康檢測後,可認定該服務器可用
#fall 指定多少次失敗的健康檢測後,可認定該服務器當掉
配置日誌
# vim /etc/rsyslog.d/haproxy.conf
$ModLoad imudp
$UDPServerRun 514
local0.* /var/log/haproxy.log
重啓日誌服務
# restart rsyslog
【node1/node2】上配置:
1.健康檢測
# apt-get install xinetd
# vim /etc/inetd.conf
sphinxchk stream tcp wait root /usr/local/scripts/sphinxchk.sh
註釋:
a.服務名稱:inetd通過查詢/etc/services獲得該服務相關信息
b.套接字類型:該服務使用的通信協議tcp用stream,udp用dgram
c.Inetd是否等待守護進程結束才繼續接管端口。Wait表示等待,nowait表示不等待,inetd每次接到一個請求就啓動守護進程的新副本
d.運行該守護進程的用戶
e.守護進程二進制文件的路徑以及參數
# vim /etc/xinetd.d/sphinxchk
service sphinxchk
{
flags = REUSE
socket_type = stream
port = 9312
wait = no
user = root
server = /usr/local/scripts/sphinxchk.sh
log_on_failure += USERID
disable = no
only_from = 192.168.1.0/24
}
sphinx配置省略
測試:
將sphinx源碼api目錄下的sphinxapi.py test.py拷貝到測試服務器上。
# vim sphinx-test.sh
#!/bin/bash
i=0
while [ "$i" -lt "99999" ]
do
/usr/bin/python /root/test.py -a $*
let i=$i+1
done
# ./sphinx-test.sh php aaa bbb ccc dddd
Kill掉node1上的sphinx服務
Oct 13 01:59:59 localhost haproxy[12117] Health check for server proxy-sphinx-1/node1 failed, reason: Layer7 wrong status, code: 503, info: "Service Unavailable", check duration: 32ms, status: 1/2 UP.
Oct 13 01:59:59 localhost haproxy[12119] Health check for server proxy-sphinx-1/node1 failed, reason: Layer7 wrong status, code: 503, info: "Service Unavailable", check duration: 31ms, status: 1/2 UP.
Oct 13 01:59:59 localhost haproxy[12118] Health check for server proxy-sphinx-1/node1 failed, reason: Layer7 wrong status, code: 503, info: "Service Unavailable", check duration: 31ms, status: 1/2 UP.
Oct 13 01:59:59 localhost haproxy[12120] Health check for server proxy-sphinx-1/node1 failed, reason: Layer7 wrong status, code: 503, info: "Service Unavailable", check duration: 31ms, status: 1/2 UP.
Oct 13 02:00:00 localhost haproxy[12117] Health check for server proxy-sphinx-1/node1 failed, reason: Layer7 wrong status, code: 503, info: "Service Unavailable", check duration: 35ms, status: 0/2 DOWN.
Oct 13 02:00:00 localhost haproxy[12117] Server proxy-sphinx-1/node1 is DOWN. 1 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
Oct 13 02:00:00 localhost haproxy[12119] Health check for server proxy-sphinx-1/node1 failed, reason: Layer7 wrong status, code: 503, info: "Service Unavailable", check duration: 32ms, status: 0/2 DOWN.
Oct 13 02:00:00 localhost haproxy[12119] Server proxy-sphinx-1/node1 is DOWN. 1 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
Oct 13 02:00:00 localhost haproxy[12118] Health check for server proxy-sphinx-1/node1 failed, reason: Layer7 wrong status, code: 503, info: "Service Unavailable", check duration: 30ms, status: 0/2 DOWN.
Oct 13 02:00:00 localhost haproxy[12118] Server proxy-sphinx-1/node1 is DOWN. 1 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
Oct 13 02:00:00 localhost haproxy[12120] Health check for server proxy-sphinx-1/node1 failed, reason: Layer7 wrong status, code: 503, info: "Service Unavailable", check duration: 32ms, status: 0/2 DOWN.
Oct 13 02:00:00 localhost haproxy[12120] Server proxy-sphinx-1/node1 is DOWN. 1 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
請求將會發送到node2上面。(將sphinx的查詢日誌打開進行觀察)