haproxy对redis进行负载均衡

实现思路:

将两个redis-server作为后端,然后通过haproxy做为负载均衡器,每个redis-server的机器上配置配置一个用于健康检查的shell,并通过xinetd将这个shell设置为服务监听9981端口并进行管理。

haproxy通过redis-server机器上的9981端口进行健康检查,如果检查失败,就直接移除该redis-server,恢复后又自动添加

 

haproxy.conf

global
        maxconn 2
#       debug
        quiet
        user zhxia
        group zhxia
        nbproc 1
        log 127.0.0.1 local3
        spread-checks 2
defaults
        timeout server  3s
        timeout connect 3s
        timeout client 60s
        timeout http-request 3s
        timeout queue 3s
frontend redis_read
        bind 192.168.187.140:52020
        default_backend cluster_redis
backend cluster_redis
        mode tcp
        option tcpka
        balance static-rr
        option httpchk
        server  redis_01        192.168.180.101:6380    weight 1 check port 9981 inter 2s rise 2 fall 1
        server  redis_02        192.168.180.101:6381    weight 1 check port 9981 inter 2s rise 2 fall 1

PS:

check:启用健康检测

inter:健康检测间隔

rise:检测服务可用的连续次数

fall:检测服务不可用的连续次数

 

安装xinetd,统一对服务进行管理与端口监听

chk_redis.sh

#!/bin/bash
#===================================================================================
#this script just for check the redis server if it alive
#author:zhxia
#date:2012-08-09
#===================================================================================
redis_host=192.168.180.101
redis_port=6380
redis_client=/usr/local/bin/redis-cli
result=`$redis_client -h $redis_host -p $redis_port -r 1 -i 1 'info' 2>/dev/null`
if [ "$result" != "" ];then
    echo -e "HTTP/1.1 200 OK\r\n"
    echo -e "Content-Type: Content-Type: text/plain\r\n"
    echo -e "\r\n"
    echo -e "redis is running,listening port is:${redis_port}.\r\n"
    echo -e "\r\n"
else
    echo -e "HTTP/1.1 503 Service Unavailable\r\n"
    echo -e "Content-Type: Content-Type: text/plain\r\n"
    echo -e "\r\n"
    echo -e "redis is down! listen port is:${redis_port}"
    echo -e "\r\n"
fi

 /etc/xinetd.d/redischk

service redischk
{
    flags        = REUSE
    protocol    = tcp
    socket_type    = stream
    port        = 9981
    wait        = no
    user        = haozu
    server        = /home/haozu/bin/chk_redis.sh
    log_on_failure +=USERID
    disable        =no
}

/etc/services

# Local services
redischk    9981/tcp    

 

 

 

 

 

 

posted @ 2012-11-13 09:31  涛光  阅读(7991)  评论(0编辑  收藏  举报