redis-keepalived High Availability

Source: Internet
Author: User
Tags redis server

1, version. are yum installed.

[Email protected] ~]# Redis-server--version
Redis Server version 2.4.10 (00000000:0)
[Email protected] ~]# REDIS-CLI--version
REDIS-CLI 2.4.10
[Email protected] ~]# keepalived--version
Keepalived v1.2.13 (03/19,2015)
2, configuration file content considerations.

A, to achieve Redis master-slave replication, add a line from the node configuration file slaveof 172.16.226.129 (Master node IP) 6379 (primary node port).

B,keepalived Configuration file Contents:

Vrrp_script Chk_redis {
Script "/etc/keepalived/scripts/redis_check.sh" # # #监控脚本
Interval 2 # # #监控时间
}
Vrrp_instance Vi_1 {
State MASTER # # #设置为MASTER
Interface Eth0 # # #监控网卡
VIRTUAL_ROUTER_ID 51
Priority 101 # # #权重值
Authentication {
Auth_type PASS # # #加密
Auth_pass Redis # # #密码
}
Track_script {
Chk_redis # # #执行上面定义的chk_redis
}
virtual_ipaddress {
172.16.226.108 # # #VIP
}
notify_master/etc/keepalived/scripts/redis_master.sh
notify_backup/etc/keepalived/scripts/redis_backup.sh
notify_fault/etc/keepalived/scripts/redis_fault.sh
notify_stop/etc/keepalived/scripts/redis_stop.sh
}
From a node you only need to set the weight value to a smaller point.

3, the implementation of Redis master-Slave conversion script:

[email protected] keepalived]# cat scripts/redis_master.sh
#!/bin/bash
Rediscli= "/usr/bin/redis-cli"
Logfile= "/var/log/keepalived-redis-state.log"
echo "[Master]" >> $LOGFILE
Date >> $LOGFILE
echo "Being master ..." >> $LOGFILE 2>&1
echo "Run slaveof cmd ..." >> $LOGFILE
$REDISCLI slaveof 192.168.241.35 6379 >> $LOGFILE 2>&1
Sleep #延迟10秒以后待数据同步完成后再取消同步状态
echo "Run slaveof NO one cmd ..." >> $LOGFILE
$REDISCLI slaveof NO one >> $LOGFILE 2>&1


[email protected] keepalived]# cat scripts/redis_check.sh
#!/bin/bash
Alive= '/usr/bin/redis-cli PING '
If ["$ALIVE" = = "PONG"]; Then
Echo $ALIVE
Exit 0
Else
Echo $ALIVE
Exit 1
Fi


[email protected] keepalived]# cat scripts/redis_backup.sh
#!/bin/bash
Rediscli= "/usr/bin/redis-cli"
Logfile= "/var/log/keepalived-redis-state.log"
echo "[Backup]" >> $LOGFILE
Date >> $LOGFILE
echo "Being slave ..." >> $LOGFILE 2>&1
Sleep #延迟15秒待数据被对方同步完成之后再切换主从角色
echo "Run slaveof cmd ..." >> $LOGFILE
$REDISCLI slaveof 192.168.241.35 (IP of another node) 6379 >> $LOGFILE 2>&1


[email protected] keepalived]# cat scripts/redis_fault.sh
#!/bin/bash
Logfile=/var/log/keepalived-redis-state.log
echo "[Fault]" >> $LOGFILE
Date >> $LOGFILE


[email protected] keepalived]# cat scripts/redis_stop.sh
#!/bin/bash
Logfile=/var/log/keepalived-redis-state.log
echo "[Stop]" >> $LOGFILE
Date >> $LOGFILE

Give the script permission to execute.

Test: Slightly

Remote link redis:redis-cli-h 172.16.226.108 (server IP)-p 6379 (service port)



This article is from the Linux notes blog, so be sure to keep this source http://lsfeng.blog.51cto.com/7959286/1718331

redis-keepalived High Availability

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.