首页 > 代码库 > 基于keepalived对redis做高可用配置---转载
基于keepalived对redis做高可用配置---转载
关于keepalived的详细介绍,请移步本人相关博客:http://wangfeng7399.blog.51cto.com/3518031/1405785
功能 | ip地址 | 安装软件 |
主redis | 10.13.6.13 | redis,keepalived |
从redis | 10.13.6.16 | redis,keepalived |
VIP | 10.13.6.17 |
一、redis主从搭建
1.redis安装
本文通过yum源的安装方式来安装(需要配置epel源),也可以通过源码编译的方式来安装
1 2 | [root@P2Pp_Red01 ~] # yum install -y redis [root@P2Pp_Red02 ~] # yum install -y redis |
2.修改配置文件
①、主redis需要修改的文件及内容
1 2 3 | [root@P2Pp_Red01 ~] # vi /etc/redis.conf bind 0.0.0.0 其余都可以安装默认状态 |
②、从redis需要修改的文件及内容
1 2 3 | [root@P2Pp_Red02 ~] # vi /etc/redis.conf bind 0.0.0.0 slaveof 10.13.6.13 6379 // 指定主redis的地址与端口 |
3.查看主从状态
1 2 3 4 5 6 7 8 9 | [root@P2Pp_Red01 ~] # redis-cli INFO |grep role -A 3 role:master slave0:10.13.6.16,45270,online db0:keys=6,expires=0 [root@P2Pp_Red02 ~] # redis-cli INFO |grep role -A 3 role:slave master_host:10.13.6.13 master_port:6379 master_link_status:up |
二、使用keepalived对redis做高可用
1.安装keepalived,两台机器都需要安装
1 2 | [root@P2Pp_Red01 ~] # yum install -y keepalived [root@P2Pp_Red02 ~] # yum install -y keepalived |
2.准备配置文件
①、主redis的配置文件,关于各行代表的意思,请移步我的另外一篇文档,链接见文章开头
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | [root@P2Pp_Red01 scripts] # vi /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 10.13.4.17 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_redis { script "killall -0 redis-server" interval 2 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 100 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.13.6.17 } track_script { chk_redis } 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 } ~[root@P2Pp_Red01 ~] # vi /etc/keepalived/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 10.13.4.13 6379 >> $LOGFILE 2>&1 sleep 10 #延迟10秒以后待数据同步完成后再取消同步状态 echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE $REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1 ~ [root@P2Pp_Red01 ~] # vi /etc/keepalived/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 #延迟15秒待数据被对方同步完成之后再切换主从角色 echo "Run SLAVEOF cmd ..." >> $LOGFILE $REDISCLI SLAVEOF 10.13.4.13 6379 >> $LOGFILE 2>&1 [root@P2Pp_Red01 scripts] # vi /etc/keepalived/scripts/redis_stop.sh #!/bin/bash LOGFILE= /var/log/keepalived-redis-state .log echo "[stop]" >> $LOGFILE date >> $LOGFILE [root@P2Pp_Red01 scripts] # vi /etc/keepalived/scripts/redis_fault.sh #!/bin/bash GFILE= /var/log/keepalived-redis-state .log echo "[fault]" >> $LOGFILE date >> $LOGFILE ~ |
②、准备从redis的配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | [root@P2Pp_Red02 ~] # vi /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 10.13.4.13 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_redis { script "killall -0 redis-server" interval 1 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 100 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.13.6.17 } #track_script { # chk_redis #} # 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 [root@P2Pp_Red02 ~] # vi /etc/keepalived/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 10.13.6.13 6379 >> $LOGFILE 2>&1 sleep 10 #延迟10秒以后待数据同步完成后再取消同步状态 echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE $REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1 ~ [root@P2Pp_Red02 ~] # vi /etc/keepalived/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.... sleep 15 #延迟15秒待数据被对方同步完成之后再切换主从角色 echo "Run SLAVEOF cmd ..." >> $LOGFILE $REDISCLI SLAVEOF 10.13.6.13 6379 >> $LOGFILE 2>&1 其余的两个文件跟主redis是一样的 |
本处将不再给模拟故障,但是测试时通过的,现在已经在生产环境中使用
本文出自 “IT民工--小枫” 博客,请务必保留此出处http://wangfeng7399.blog.51cto.com/3518031/1591275
基于keepalived对redis做高可用配置---转载
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。