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    

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM