共计 4827 个字符,预计需要花费 13 分钟才能阅读完成。
基于 Keepalived 对 Redis 做高可用配置
关于 Keepalived 的详细介绍,请移步本人相关文章:http://www.linuxidc.com/Linux/2014-12/110815.htm
功能 | 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 源),也可以通过源码编译的方式来安装
[root@P2Pp_Red01 ~]# yum install -y redis
[root@P2Pp_Red02 ~]# yum install -y redis
2. 修改配置文件
①、主 redis 需要修改的文件及内容
[root@P2Pp_Red01 ~]# vi /etc/redis.conf
bind 0.0.0.0
其余都可以安装默认状态
②、从 redis 需要修改的文件及内容
[root@P2Pp_Red02 ~]# vi /etc/redis.conf
bind 0.0.0.0
slaveof 10.13.6.13 6379 // 指定主 redis 的地址与端口
3. 查看主从状态
[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,两台机器都需要安装
[root@P2Pp_Red01 ~]# yum install -y keepalived
[root@P2Pp_Red02 ~]# yum install -y keepalived
2. 准备配置文件
①、主 redis 的配置文件, 关于各行代表的意思,请移步我的另外一篇文档,链接见文章开头
[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 的配置文件
[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 是一样的
本处将不再给模拟故障,但是测试时通过的,现在已经在生产环境中使用
Haproxy+Keepalived 搭建 Weblogic 高可用负载均衡集群 http://www.linuxidc.com/Linux/2013-09/89732.htm
Keepalived+HAProxy 配置高可用负载均衡 http://www.linuxidc.com/Linux/2012-03/56748.htm
CentOS 6.3 下 Haproxy+Keepalived+Apache 配置笔记 http://www.linuxidc.com/Linux/2013-06/85598.htm
Haproxy + KeepAlived 实现 WEB 群集 on CentOS 6 http://www.linuxidc.com/Linux/2012-03/55672.htm
Haproxy+Keepalived 构建高可用负载均衡 http://www.linuxidc.com/Linux/2012-03/55880.htm
Haproxy 实现 Exchange 全透明代理服务 http://www.linuxidc.com/Linux/2014-10/108530.htm