1.结构
master1 redis-server(6079) redis-sentinel(26379)
node1 redis-server(6079) redis-sentinel(26379)
node2 redis-server(6079) redis-sentinel(26379)
2. redis安装
1 下载 wget http://download.redis.io/releases/redis-5.0.8.tar.gz 2 安装 mv redis-5.0.8.tar.gz /data/tools/ tar -zxvf redis-5.0.8.tar.gz cd redis-5.0.8 make make PREFIX=/data/redis install make install #安装自动shell cd utils/ ./install_server.sh Welcome to the redis service installer This script will help you easily set up a running redis server Please select the redis port for this instance: [6379] Selecting default: 6379 Please select the redis config file name [/etc/redis/6379.conf] Selected default - /etc/redis/6379.conf Please select the redis log file name [/var/log/redis_6379.log] /data/redis/logs/redis_6379.conf Please select the data directory for this instance [/var/lib/redis/6379] /data/redis/data/6379 Please select the redis executable path [/usr/local/bin/redis-server] Selected config: Port : 6379 Config file : /etc/redis/6379.conf Log file : /data/redis/logs/redis_6379.conf Data dir : /data/redis/data/6379 Executable : /usr/local/bin/redis-server Cli Executable : /usr/local/bin/redis-cli Is this ok? Then press ENTER to go on or Ctrl-C to abort. Copied /tmp/6379.conf => /etc/init.d/redis_6379 Installing service... Successfully added to chkconfig! Successfully added to runlevels 345! Starting Redis server... Installation successful!
3. 修改配置文件
master1 配置文件
bind 172.16.230.22 protected-mode yes port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize yes supervised no pidfile "/var/run/redis_6379.pid" loglevel notice logfile "/data/redis/logs/redis_6379.conf" databases 16 always-show-logo yes save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename "dump.rdb" dir "/data/redis/data/6379" replica-serve-stale-data yes replica-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no replica-priority 100 lazyfree-lazy-eviction no lazyfree-lazy-expire no lazyfree-lazy-server-del no replica-lazy-flush no appendonly no appendfilename "appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes aof-use-rdb-preamble 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-size -2 list-compress-depth 0 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 stream-node-max-bytes 4096 stream-node-max-entries 100 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit replica 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 dynamic-hz yes aof-rewrite-incremental-fsync yes rdb-save-incremental-fsync yes
node1 配置文件
bind 172.16.230.23 protected-mode yes port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize yes supervised no pidfile "/var/run/redis_6379.pid" loglevel notice logfile "/data/redis/logs/redis_6379.log" databases 16 always-show-logo yes save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename "dump.rdb" dir "/data/redis/data/6379" replicaof 172.16.230.23 6379 replica-serve-stale-data yes replica-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no replica-priority 100 lazyfree-lazy-eviction no lazyfree-lazy-expire no lazyfree-lazy-server-del no replica-lazy-flush no appendonly no appendfilename "appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes aof-use-rdb-preamble 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-size -2 list-compress-depth 0 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 stream-node-max-bytes 4096 stream-node-max-entries 100 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit replica 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 dynamic-hz yes aof-rewrite-incremental-fsync yes rdb-save-incremental-fsync yes
node2 配置文件
bind 172.16.230.24 protected-mode yes port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize yes supervised no pidfile "/var/run/redis_6379.pid" loglevel notice logfile "/data/redis/logs/redis_6379.log" databases 16 always-show-logo yes save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename "dump.rdb" dir "/data/redis/data/6379" replicaof 172.16.230.24 6379 replica-serve-stale-data yes replica-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no replica-priority 100 lazyfree-lazy-eviction no lazyfree-lazy-expire no lazyfree-lazy-server-del no replica-lazy-flush no appendonly no appendfilename "appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes aof-use-rdb-preamble 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-size -2 list-compress-depth 0 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 stream-node-max-bytes 4096 stream-node-max-entries 100 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit replica 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 dynamic-hz yes aof-rewrite-incremental-fsync yes rdb-save-incremental-fsync yes
重新启动redis
[root@node1 ~]# /etc/init.d/redis_6379 restart
二、Redis哨兵模式
Redis Sentinel是Redis高可用的实现方案。Sentinel是一个管理多个Redis实例的工具,它可以实现对Redis的监控、通知、自动故障转移。
Sentinel的主要功能包括主节点存活检测、主从运行情况检测、自动故障转移(failover)、主从切换。Redis的Sentinel最小配置是一主一从。 Redis的Sentinel系统可以用来管理多个Redis服务器,该系统可以执行以下四个任务:
-
监控
Sentinel会不断的检查主服务器和从服务器是否正常运行。
-
通知
当被监控的某个Redis服务器出现问题,Sentinel通过API脚本向管理员或者其他的应用程序发送通知。
-
自动故障转移
当主节点不能正常工作时,Sentinel会开始一次自动的故障转移操作,它会将与失效主节点是主从关系的其中一个从节点升级为新的主节点, 并且将其他的从节点指向新的主节点。
-
配置提供者
在Redis Sentinel模式下,客户端应用在初始化时连接的是Sentinel节点集合,从中获取主节点的信息。
sentinel master1 配置文件
cat /etc/redis/sentinel.conf # 端口 port 26379 # 是否后台启动 daemonize yes # pid文件路径 pidfile /var/run/redis-sentinel.pid # 日志文件路径 logfile "/data/redis/logs/sentinel.log" # 定义工作目录 dir /data/redis/sentinel # 定义Redis主的别名, IP, 端口,这里的2指的是需要至少2个Sentinel认为主Redis挂了才最终会采取下一步行为 sentinel monitor mymaster 172.16.230.22 6379 2 # 如果mymaster 30秒内没有响应,则认为其主观失效 sentinel down-after-milliseconds mymaster 30000 # 如果master重新选出来后,其它slave节点能同时并行从新master同步数据的台数有多少个,显然该值越大,所有slave节点完成同步切换的整体速度越快,但如果此时正好有人在访问这些slave,可能造成读取失败,影响面会更广。最保守的设置为1,同一时间,只能有一台干这件事,这样其它slave还能继续服务,但是所有slave全部完成缓存更新同步的进程将变慢。 sentinel parallel-syncs mymaster 1 # 该参数指定一个时间段,在该时间段内没有实现故障转移成功,则会再一次发起故障转移的操作,单位毫秒 sentinel failover-timeout mymaster 180000 # 不允许使用SENTINEL SET设置notification-script和client-reconfig-script。 sentinel deny-scripts-reconfig yes
node1 node2 节点配置文件
[root@node1 ~]# cat /etc/redis/sentinel.conf port 26379 daemonize yes pidfile "/var/run/redis-sentinel.pid" logfile "/data/redis/logs/sentinel.log" dir "/data/redis/sentinel" sentinel monitor mymaster 172.16.230.22 6379 2 sentinel down-after-milliseconds mymaster 30000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000 sentinel deny-scripts-reconfig yes
[root@node2 ~]# cat /etc/redis/sentinel.conf port 26379 daemonize yes pidfile "/var/run/redis-sentinel.pid" logfile "/data/redis/logs/sentinel.log" dir "/data/redis/sentinel" sentinel monitor mymaster 172.16.230.22 6379 2 sentinel down-after-milliseconds mymaster 30000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000 sentinel deny-scripts-reconfig yes
启动sentinel
启动顺序 主redis ---- 从redis ---- sentinel 1 2 3 [root@master1 ~]# redis-sentinel /etc/redis/sentinel.conf [root@node1 ~]# redis-sentinel /etc/redis/sentinel.conf [root@node2 ~]# redis-sentinel /etc/redis/sentinel.conf
sentinel 查看状态信息
redis-cli -p 26379 127.0.0.1:26379> sentinel master mymaster 1) "name" 2) "mymaster" 3) "ip" 4) "172.16.230.22" 5) "port" 6) "6379" 7) "runid" 8) "b5c70274f2c58e7d7263c67dde4e00d351f5d826" 9) "flags" 10) "master" 11) "link-pending-commands" 12) "0" 13) "link-refcount" 14) "1" 15) "last-ping-sent" 16) "0" 17) "last-ok-ping-reply" 18) "490" 19) "last-ping-reply" 20) "490" 21) "down-after-milliseconds" 22) "30000" 23) "info-refresh" 24) "4362" 25) "role-reported" 26) "master" 27) "role-reported-time" 28) "3909391" 29) "config-epoch" 30) "1" 31) "num-slaves" 32) "2" 33) "num-other-sentinels" 34) "2" 35) "quorum" 36) "2" 37) "failover-timeout" 38) "180000" 39) "parallel-syncs" 40) "1" # 查看从状态 127.0.0.1:26379> sentinel slaves mymaster 1) 1) "name" 2) "172.16.230.23:6379" 3) "ip" 4) "172.16.230.23" 5) "port" 6) "6379" 7) "runid" 8) "442367635e8d0a844fdb5c6d1ead098f6fb77790" 9) "flags" 10) "slave" 11) "link-pending-commands" 12) "0" 13) "link-refcount" 14) "1" 15) "last-ping-sent" 16) "0" 17) "last-ok-ping-reply" 18) "815" 19) "last-ping-reply" 20) "815" 21) "down-after-milliseconds" 22) "30000" 23) "info-refresh" 24) "4351" 25) "role-reported" 26) "slave" 27) "role-reported-time" 28) "3659268" 29) "master-link-down-time" 30) "0" 31) "master-link-status" 32) "ok" 33) "master-host" 34) "172.16.230.24" 35) "master-port" 36) "6379" 37) "slave-priority" 38) "100" 39) "slave-repl-offset" 40) "905325" 2) 1) "name" 2) "172.16.230.24:6379" 3) "ip" 4) "172.16.230.24" 5) "port" 6) "6379" 7) "runid" 8) "dc740b5af985475d7268c88658bd5eddec0eb812" 9) "flags" 10) "slave" 11) "link-pending-commands" 12) "0" 13) "link-refcount" 14) "1" 15) "last-ping-sent" 16) "0" 17) "last-ok-ping-reply" 18) "815" 19) "last-ping-reply" 20) "815" 21) "down-after-milliseconds" 22) "30000" 23) "info-refresh" 24) "9754" 25) "role-reported" 26) "slave" 27) "role-reported-time" 28) "3935708" 29) "master-link-down-time" 30) "0" 31) "master-link-status" 32) "ok" 33) "master-host" 34) "172.16.230.24" 35) "master-port" 36) "6379" 37) "slave-priority" 38) "100" 39) "slave-repl-offset" 40) "904183"
参考: