在生产场景下都必须做到高可用,否则出现问题就会造成服务停止。这里介绍下Redis的Sentinel功能实现的高可用,当然也有Redis提供的cluster功能。
1.Redis的安装
1
2
3
4
5
6
7
8
9
10
|
wget http: //download .redis.io /releases/redis-3 .0.7. tar .gz
tar zxf redis-3.0.7. tar .gz
cd redis-3.0.7
make cd src
mkdir -p /usr/local/redis3 .0.7/{sbin,data,conf}
scp -r mkreleasehdr.sh redis-benchmark redis-check-aof redis-check-dump redis-cli redis-sentinel redis-server localhost: /usr/local/redis3 .0.7 /sbin
cp .. /redis .conf /usr/local/redis3 .0.7 /conf/
echo 'PATH=$PATH:/usr/local/redis3.0.7/sbin' >> /etc/profile
source /etc/profile
|
2.Redis的配置
1
|
vim /usr/local/redis3 .0.7 /conf/redis .conf
|
(以下为db1的配置,db2的配置只需要增加一行slaveof 192.168.2.241 6379)
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
|
#是否以服务方式开启 daemonize no pidfile /var/run/redis .pid
#端口 port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 0 #日志提醒级别 loglevel notice #日志文件路径 logfile ""
#数据库数量 databases 16 #日志刷新策略(Master禁用) save 900 1 #说明900秒内1次改动,自动保存一次数据集 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes
#以rdb方式存储时是否压缩 rdbcompression yes
#rdb是否校验 rdbchecksum yes
#数据文件名 dbfilename dump.rdb #数据文件路径 dir /usr/local/redis3 .0.7 /data
slave-serve-stale-data yes
#从服务器只读 slave- read -only yes
#指定与主数据库连接时需要的密码验证,主库无需设置 #masterauth #masterauth repl-diskless- sync no
repl-diskless- sync -delay 5
repl-disable-tcp-nodelay no slave-priority 100 ##启用增量(Master禁用) appendonly no #增量日志文件名,默认值为appendonly.aof appendfilename "appendonly.aof"
#设置对 appendonly.aof 文件进行同步的频率 #always 表示每次有写操作都进行同步,everysec 表示对写操作进行累积,每秒同步一次。 #no表示等操作系统进行数据缓存同步到磁盘,都进行同步,everysec 表示对写操作进行累#积,每秒同步一次 appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes
lua- time -limit 5000
slowlog-log-slower-than 10000 slowlog-max-len 128 latency-monitor-threshold 0 notify-keyspace-events ""
hash -max-ziplist-entries 512
hash -max-ziplist-value 64
list-max-ziplist-entries 512 list-max-ziplist-value 64 set -max-intset-entries 512
zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 activerehashing yes
client-output-buffer-limit normal 0 0 0 client-output-buffer-limit slave 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 aof-rewrite-incremental-fsync yes
#最大可用内存 #maxmemory 2mb #配置成从服务器 #slaveof 192.168.2.250 6379 #客户端连接访问口令 # requirepass foobared #限制同时连接的客户数量,防止过多的client导致内存耗尽。如果有足够内存可以不进行#设置 #maxclients 10000 |
3.Sentinel的配置
1
|
vim /usr/local/redis3 .0.7 /conf/sentinel .conf
|
1
2
3
4
5
6
7
8
9
|
port 26379 sentinel monitor master 192.168.2.241 6379 2 sentinel down-after-milliseconds master 5000 sentinel failover-timeout master 900000 #sentinel can-failover master yes sentinel parallel-syncs master 2 # Generated by CONFIG REWRITE dir "/usr/local/redis3.0.7/conf"
#sentinel client-reconfig-script master /usr/local/redis3.0.7/sbin/vip.sh |
4.启动redis-server及redis-sentinel
在db1,db2,db3上分别启动redis-server及redis-sentinel
1
2
|
/usr/local/redis3 .0.7 /sbin/redis-server /usr/local/redis3 .0.7 /conf/redis .conf
/usr/local/redis3 .0.7 /sbin/redis-sentinel /usr/local/redis3 .0.7 /conf/sentinel .conf
|
这个时候就已经做好了Redis的主从复制及故障转移,我们客户端的连接就需要修改,因此就涉及到VIP的问题,客户端只要连接VIP的问题,目前VIP的解决方法有
-
利用redis-sentinel的sentinel client-reconfig-script参数进行设置
-
利用keepalived进行VIP的转移
-
利用consul进行服务注册
a.利用redis-sentinel自带的参数进行VIP的配置
在db1,db2的sentinel.conf中加入
sentinel client-reconfig-script master1 /usr/local/redis3.0.7/sbin/vip.sh
然后创建脚本/usr/local/redis3.0.7/sbin/vip.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#!/bin/bash MASTER_IP=$6 LOCAL_IP= '192.168.2.241' #从库修改为192.168.2.242
VIP= '192.168.2.250'
NETMASK= '24'
INTERFACE= 'eth0'
if [ ${MASTER_IP} = ${LOCAL_IP} ]; then
/sbin/ip addr add ${VIP}/${NETMASK} dev ${INTERFACE}
/sbin/arping -q -c 3 -A ${VIP} -I ${INTERFACE}
exit 0
else /sbin/ip addr del ${VIP}/${NETMASK} dev ${INTERFACE}
exit 0
fi exit 1
|
在集群启动的时候需要手动添加VIP
1
|
/sbin/ip addr add 192.168.2.250 /24 dev eth0
|
使用上面脚本的时候需要注意你的网卡是eth0,否则可能出现VIP无法使用的情况
b.利用keepalived进行VIP的切换
1
2
3
4
5
6
7
8
9
|
wget http: //www .keepalived.org /software/keepalived-1 .2.19. tar .gz
tar zxf keepalived-1.2.19. tar .gz
cd keepalived-1.2.19
. /configure --prefix= /usr/local/keepalived1 .2.19 --disable-fwmark
make && make install
ln -s /usr/local/keepalived1 .2.19 /sbin/keepalived /usr/sbin/
ln -s /usr/local/keepalived1 .2.19 /etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/keepalived1 .2.19 /etc/keepalived/ /etc/keepalived
ln -s /usr/local/keepalived1 .2.19 /etc/rc .d /init .d /keepalived /etc/init .d/
|
db1的keepalived配置文件
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
|
! Configuration File for keepalived
global_defs { notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 10
router_id keepalivedha_1
} vrrp_script chk_redis_role { script "/usr/local/redis3.0.7/sbin/redis-cli info | grep role:master >/dev/null 2>&1" interval 1
timeout 2
fall 2
rise 1
} vrrp_sync_group VG_1 { group {
VI_1
}
} vrrp_instance VI_1 { state BACKUP
interface eth0
#use_vmac keepalived
#vmac_xmit_base
smtp_alert
virtual_router_id 20
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass password
}
virtual_ipaddress {
192.168.2.250
}
track_script {
chk_redis_role
}
} |
db2的配置文件,只需要将以上文件复制过来并修改下面参数
1
|
priority 98 |
#注意一下interface eth0看下你机器的网卡接口
c.利用consul服务注册
参考:http://dgd2010.blog.51cto.com/1539422/1745314
从上面可以看出以上三种方法:
脚本跟keepalived的形式一样,但是脚本相对轻量,首次启动麻烦,配置简单,依赖于redis-sentinel
keepalived启动简单,排错简便
consul适合大型场景,及云环境没有多余的IP这种情况
以上也适用于2台服务器的场景,也可以不用3台服务器。只需要更改
1
|
sentinel monitor master 192.168.2.241 6379 1 |
后面值更改为1即可
本文转自 rong341233 51CTO博客,原文链接:http://blog.51cto.com/fengwan/1746731