haproxy對redis進行負載平衡

來源:互聯網
上載者:User

實現思路:

將兩個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    

 

 

 

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.