Redis+keepalived高可用集群实战

Redis主从复制优点及应用场景,WEB应用程序可以基于主从同步实现读写分离以提高服务器的负载能力。

在常见的场景中,读的频率一般比较大,当单机Redis无法应付大量的读请求时,可以通过复制功能建立多个从数据库,主数据库只进行写操作,而从数据库负责读操作,基于keepalived+Redis对Redis实现高可用,保证网站正常访问。如下为Redis+keepalived高可用架构实现步骤:

(1)Redis主库、丛库分别安装Keepalived服务:

tar  -xzvf  keepalived-1.2.1.tar.gz
cd keepalived-1.2.1 
./configure
make
make install 
DIR=/usr/local/
cp $DIR/etc/rc.d/init.d/keepalived  /etc/rc.d/init.d/
cp $DIR/etc/sysconfig/keepalived   /etc/sysconfig/ 
mkdir  -p  /etc/keepalived
cp   $DIR/sbin/keepalived         /usr/sbin/

(2)Redis+Keepalived  Master配置文件代码如下:

! Configuration File for keepalived 
 global_defs { 
  notification_email { 
      wgkgood@163.com 
 } 
    notification_email_from wgkgood@163.com 
    smtp_server 127.0.0.1 
    smtp_connect_timeout 30 
    router_id LVS_DEVEL 
 }
 vrrp_script chk_redis  {
    script "/data/sh/check_redis.sh" 
    interval 2 
    weight 2 
 } 
 # VIP1 
 vrrp_instance VI_1 { 
     state MASTER 
     interface eth0 
     lvs_sync_daemon_inteface eth0 
     virtual_router_id 151 
     priority 100 
     advert_int 5 
     nopreempt 
     authentication { 
         auth_type  PASS 
         auth_pass  1111 
     } 
     virtual_ipaddress { 
         192.168.0.198
     } 
     track_script { 
     chk_redis 
    } 
 }

(3)Redis+Keepalived Backup配置文件代码如下:

! Configuration File for keepalived 
 global_defs { 
  notification_email { 
      wgkgood@163.com 
 } 
    notification_email_from wgkgood@163.com 
    smtp_server 127.0.0.1 
    smtp_connect_timeout 30 
    router_id LVS_DEVEL 
 }
 vrrp_script chk_redis  {
    script "/data/sh/check_redis.sh" 
    interval 2 
    weight 2 
 } 
 # VIP1 
 vrrp_instance VI_1 { 
     state BACKUP 
     interface eth0 
     lvs_sync_daemon_inteface eth0 
     virtual_router_id 151 
     priority 90 
     advert_int 5 
     nopreempt 
     authentication { 
         auth_type  PASS 
         auth_pass  1111 
     } 
     virtual_ipaddress { 
         192.168.0.198
     } 
     track_script { 
     chk_redis 
    } 
 }

(4)两台Redis服务器上配置/data/sh/check_redis.sh脚本,内容如下:

#!/bin/bash 
#auto  check  redis  process
NUM=`ps -ef |grep redis|grep -v grep|grep  -v check|wc -l`
if 
[[ $NUM  -eq  0 ]];then
        /etc/init.d/keepalived  stop
fi

发表评论

后才能评论