使用Haproxy来实现sphinx负载均衡与健康监测
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的查询日志打开进行观察)
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的查询日志打开进行观察)
相关推荐
wenwentana 2020-07-05
zhenpy 2019-11-29
qianjq 2019-11-29
zk0 2019-11-20
看上去很美 2019-11-18
willcoder 2018-04-17
冷月醉雪 2018-04-17
赫赫小虾 2018-04-16
vitasfly 2016-12-28
半壁灯 2013-06-15
magiclake 2010-12-11
看上去很美 2019-07-01
leshem 2016-02-17
年轻就要对味 2015-01-25
momomoniqwer 2012-06-15
aikaibo 2012-02-14
快雪时晴天 2011-11-10
瑞邦 2019-06-27