實現思路:
將兩個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 2defaults timeout server 3s timeout connect 3s timeout client 60s timeout http-request 3s timeout queue 3sfrontend redis_read bind 192.168.187.140:52020 default_backend cluster_redisbackend 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.101redis_port=6380redis_client=/usr/local/bin/redis-cliresult=`$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 servicesredischk 9981/tcp