Redis High-availability configuration (keepalived)

Source: Internet
Author: User
Tags install redis

Master: 172.16.0.104

Preparation: 172.16.0.105

vip:172.16.0.107

The client connects directly to the VIP, and when Redis of Master 104 is hung, 105 as master. When 104 restarts, 104 is the slave of 105. When 105 is dead, 104 as Master ...

1. Install Redis and configure master-slave synchronization

The installation process is omitted

1.1 Redis instance configured with 104:/etc/redis/redis.conf

requirepass 123456 #生产环境千万不要配置这么简单的密码

Masterauth 123456 #备库密码, there is no configuration slaveof but configured Masterauth because the password is required for primary and standby switching

1.2 Redis instances configured with 105

requirepass 123456 #生产环境千万不要配置这么简单的密码

Masterauth 123456 #主库密码

slaveof 172.16.0.104 6379

2, Installation keepalived

The installation process is omitted

3, Configuration keepalived

Master (104) Example:

Keepalived.conf

global_defs {   router_id test}vrrp_script chk_redis {       script  "/etc/keepalived/scripts/redis_check.sh 127.0.0.1 6379 123456"       interval 2      timeout 2      fall 3}vrrp_instance redis {    state backup  #  Master is also configured as Slave    interface eth0      virtual_ router_id 51     priority  150            nopreempt #  not preemption, note plus     advert_int 1             authentication {            auth_type PASS            auth_pass 123456    }    virtual_ipaddress {       172.16.0.107    }    track_script {           chk_redis     }      notify_master  "/etc/keepalived/scripts/redis_master.sh 127.0.0.1 6379 123456"      notify_backup  "/etc/keepalived/scripts/redis_backup.sh 127.0.0.1 6379  123456 172.16.0.105 6379 "    notify_fault /etc/keepalived/scripts/redis_ fault.sh     notify_stop /etc/keepalived/scripts/redis_stop.sh  
}

redis_master.sh

#!/bin/bash rediscli= "Redis-cli-h $1-p $2-a $" logfile= "/var/log/keepalived-redis-state.log" echo "[Master]" >&G T  $LOGFILE Date >> $LOGFILE echo "Being master ..." >> $LOGFILE 2>&1 echo "Run slaveof NO one cmd ..." >> $LOGFILE $REDISCLI slaveof NO one >> $LOGFILE 2>&1

redis_backup.sh

#!/bin/bash rediscli= "Redis-cli-h $1-p $2-a $" logfile= "/var/log/keepalived-redis-state.log" echo "[Backup]" >&G T $LOGFILE Date >> $LOGFILE echo "Being slave ..." >> $LOGFILE 2>&1 echo "Run slaveof cmd ..." >> ; $LOGFILE $REDISCLI slaveof $4 $ >> $LOGFILE 2>&1


Slave (105) example

Keepalived.conf
global_defs {    router_id redis}vrrp_script chk_redis {     script  "/etc/keepalived/scripts/redis_check.sh 127.0.0.1 6379 123456"     ## #监控脚本     interval 2                   ## #监控时间     timeout   2                                   ## #超时时间      fall     3                   ## #重试次数}vrrp_instance redis {  state  BACKUP                   ## #设置为MASTER &Nbsp; interface eth0               ## #监控网卡   virtual_router_id 51  priority 100               ## #权重值   nopreempt #  not preemption, note plus    advert_int 1  authentication {          auth_type pass          ## #加密           auth_pass 123456        ## #密码    }  track_script {    chk_redis               ## #执行上面定义的chk_redis   }  virtual_ipaddress  {       172.16.0.107              # # #VIP   }   notify_master  "/etc/keepalived/scripts/redis_master.sh  127.0.0.1 6379 123456 "   notify_backup "/etc/keepalived/scripts/redis_backup.sh  127.0.0.1 6379 123456 172.16.0.104 6379 "   notify_fault /etc/ KEEPALIVED/SCRIPTS/REDIS_FAULT.SH   NOTIFY_STOP /ETC/KEEPALIVED/SCRIPTS/REDIS_STOP.SH}
redis_master.sh
#!/bin/bash rediscli= "Redis-cli-h $1-p $2-a $" logfile= "/var/log/keepalived-redis-state.log" echo "[Master]" >&G T  $LOGFILE Date >> $LOGFILE echo "Being master ..." >> $LOGFILE 2>&1 echo "Run slaveof NO one cmd ..." >> $LOGFILE $REDISCLI slaveof NO one >> $LOGFILE 2>&1

redis_backup.sh

#!/bin/bash rediscli= "Redis-cli-h $1-p $2-a $" logfile= "/var/log/keepalived-redis-state.log" echo "[Backup]" >&G T $LOGFILE Date >> $LOGFILE echo "Being slave ..." >> $LOGFILE 2>&1 echo "Run slaveof cmd ..." >> ; $LOGFILE $REDISCLI slaveof $4 $ >> $LOGFILE 2>&1

Public configuration

redis_check.sh

#!/bin/bash alive= ' redis-cli-h $1-p $2-a $ PING ' logfile= '/var/log/keepalived-redis-check.log ' echo ' [check] ' >> $LOGFILEdate >> $LOGFILEif [$ALIVE = = "PONG"]; Then:echo "Success:redis-cli-h $1-p $ PING $ALIVE" >> $LOGFILE 2>&1 exit 0 Else Echo "failed:re Dis-cli-h $1-p $ PING $ALIVE ">> $LOGFILE 2>&1 exit 1 fi

redis_fault.sh

#!/bin/bash logfile=/var/log/keepalived-redis-state.log echo "[Fault]" >> $LOGFILEdate >> $LOGFILE

redis_stop.sh

#!/bin/bash logfile=/var/log/keepalived-redis-state.log echo "[Stop]" >> $LOGFILE date >> $LOGFILE

Start keepalived:keepalived-d

View IP drift Condition: IP A

Configuration essentials:

Dual backup + nopreempt, priority first start as Master, when Master hangs, slave is selected as the new master.

Reference article: http://www.cnblogs.com/lulu/archive/2013/04/14/3021261.html

Redis High-availability configuration (keepalived)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.