我的实验环境
三台CentOS7的操作系统,Redis5.0.4,网络环境如下。
系统版本
[root@lz5 ~]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
第一台
[root@lz3 ~]# ip a | grep ens34
3: ens34: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
inet 10.1.1.5/24 brd 10.1.1.255 scope global ens34
第二台
[root@lz4 ~]# ip a | grep ens34
3: ens34: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
inet 10.1.1.6/24 brd 10.1.1.255 scope global ens34
第三台
[root@lz5 ~]# ip a | grep ens34
3: ens34: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
inet 10.1.1.7/24 brd 10.1.1.255 scope global ens34
计划
三台服务器都安装redis,安装完成后在进行不同的配置。
lz3 redis主 哨兵进程开启
lz4 redis从 哨兵进程开启
lz5 redis从 哨兵进程开启
Redis的安装
下面方法来自Redis官网
$ wget http://download.redis.io/releases/redis-5.0.4.tar.gz
$ tar xzf redis-5.0.4.tar.gz
$ cd redis-5.0.4
$ make
系统参数调整
调整一下文件描述符
[root@lz5 ~]# cat /etc/profile | tail -1
ulimit -n 10240
记得执行 source命令。
配置主即master的redis配置文件(redis.conf)
除了注释的,其余的配置都可使用默认的
#绑定本机IP
63:bind 10.1.1.5
81:protected-mode yes
#端口默认就行
84:port 6379
92:tcp-backlog 511
#客户端超时时间
102:timeout 30
118:tcp-keepalive 300
#开启守护进程
122:daemonize yes
132:supervised no
142:pidfile /var/run/redis_6379.pid
149:loglevel notice
#日志文件存放位置,提前创建目录
153:logfile "/var/log/redis/redis.log"
164:databases 16
171:always-show-logo yes
193:save 900 1
194:save 300 10
195:save 60 10000
209:stop-writes-on-bgsave-error yes
214:rdbcompression yes
222:rdbchecksum yes
224:dbfilename dump.rdb
#SNAPSHOTTING目录提前创建
233:dir /data/redis
274:replica-serve-stale-data yes
289:replica-read-only yes
319:repl-diskless-sync no
330:repl-diskless-sync-delay 5
360:repl-disable-tcp-nodelay no
398:replica-priority 100
616:lazyfree-lazy-eviction no
617:lazyfree-lazy-expire no
618:lazyfree-lazy-server-del no
619:replica-lazy-flush no
#使用AOF持久化
638:appendonly yes
640:appendfilename "appendonly.aof"
#实时同步
663:appendfsync always
684:no-appendfsync-on-rewrite no
701:auto-aof-rewrite-percentage 100
702:auto-aof-rewrite-min-size 64mb
725:aof-load-truncated yes
735:aof-use-rdb-preamble yes
751:lua-time-limit 5000
909:slowlog-log-slower-than 10000
912:slowlog-max-len 128
931:latency-monitor-threshold 0
975:notify-keyspace-events ""
980:hash-max-ziplist-entries 512
981:hash-max-ziplist-value 64
995:list-max-ziplist-size -2
1010:list-compress-depth 0
1016:set-max-intset-entries 512
1020:zset-max-ziplist-entries 128
1021:zset-max-ziplist-value 64
1034:hll-sparse-max-bytes 3000
1043:stream-node-max-bytes 4096
1044:stream-node-max-entries 100
1063:activerehashing yes
1097:client-output-buffer-limit normal 0 0 0
1098:client-output-buffer-limit replica 256mb 64mb 60
1099:client-output-buffer-limit pubsub 32mb 8mb 60
1127:hz 10
1142:dynamic-hz yes
1147:aof-rewrite-incremental-fsync yes
1152:rdb-save-incremental-fsync yes
配置从服务器的配置文件(reids.conf)
两台从服务器的配置除了IP外都一致,所以配一个,另外一个改一下IP就可以了。除了下面注释出来的几项,其余的和master配置一样。
#绑定本机IP
63:bind 10.1.1.6
81:protected-mode yes
84:port 6379
92:tcp-backlog 511
102:timeout 30
118:tcp-keepalive 300
122:daemonize yes
132:supervised no
142:pidfile /var/run/redis_6379.pid
149:loglevel notice
153:logfile "/var/log/redis/redis.log"
164:databases 16
171:always-show-logo yes
193:save 900 1
194:save 300 10
195:save 60 10000
209:stop-writes-on-bgsave-error yes
214:rdbcompression yes
222:rdbchecksum yes
224:dbfilename dump.rdb
233:dir /data/redis
#指定主服务IP 端口
255:replicaof 10.1.1.5 6379
#如果slave 无法与master 同步,设置成slave不可读,方便监控脚本发现问题。
275:replica-serve-stale-data no
290:replica-read-only yes
320:repl-diskless-sync no
331:repl-diskless-sync-delay 5
361:repl-disable-tcp-nodelay no
399:replica-priority 100
617:lazyfree-lazy-eviction no
618:lazyfree-lazy-expire no
619:lazyfree-lazy-server-del no
620:replica-lazy-flush no
639:appendonly yes
641:appendfilename "appendonly.aof"
664:appendfsync always
685:no-appendfsync-on-rewrite no
702:auto-aof-rewrite-percentage 100
703:auto-aof-rewrite-min-size 64mb
726:aof-load-truncated yes
736:aof-use-rdb-preamble yes
752:lua-time-limit 5000
910:slowlog-log-slower-than 10000
913:slowlog-max-len 128
932:latency-monitor-threshold 0
976:notify-keyspace-events ""
981:hash-max-ziplist-entries 512
982:hash-max-ziplist-value 64
996:list-max-ziplist-size -2
1011:list-compress-depth 0
1017:set-max-intset-entries 512
1021:zset-max-ziplist-entries 128
1022:zset-max-ziplist-value 64
1035:hll-sparse-max-bytes 3000
1044:stream-node-max-bytes 4096
1045:stream-node-max-entries 100
1064:activerehashing yes
1098:client-output-buffer-limit normal 0 0 0
1099:client-output-buffer-limit replica 256mb 64mb 60
1100:client-output-buffer-limit pubsub 32mb 8mb 60
1128:hz 10
1143:dynamic-hz yes
1148:aof-rewrite-incremental-fsync yes
1153:rdb-save-incremental-fsync yes
哨兵配置 sentinel.conf 文件
三台服务器使用同一份配置文件,所以改好后分别上传到三台服务器
port 26379
#开启守护进程
daemonize yes
pidfile /var/run/redis-sentinel.pid
#指定日志文件存放位置
logfile "/var/log/redis/sentinel/"
dir /tmp
#指定主的IP 端口 “2”代表有两个哨兵进程发现主服务器宕机便选取一个从服务器为主
sentinel monitor mymaster 10.1.1.5 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
启动redis-server进程和redis-sentinel进程
依次在主,从,从三台上执行下面命令启动redis-server
[root@lz3 redis]# ls
00-RELEASENOTES CONTRIBUTING deps Makefile README.md redis.conf.bak runtest-cluster sentinel.conf src utils
BUGS COPYING INSTALL MANIFESTO redis.conf runtest runtest-sentinel sentinel.conf.bak tests
[root@lz3 redis]# pwd
/soft/redis
[root@lz3 redis]# ./src/redis-server ./redis.conf
redis-server执行完毕后再启动redis-sentinel
[root@lz5 redis]# ./src/redis-sentinel ./sentinel.conf
查看状态
在master上查看
[root@lz3 redis]# ./src/redis-cli -h 10.1.1.5 -p 6379
10.1.1.5:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=10.1.1.7,port=6379,state=online,offset=477138,lag=1
slave1:ip=10.1.1.6,port=6379,state=online,offset=477138,lag=1
master_replid:5feb667d4058707c9fa1346fd5e4c9c88d32d1bf
master_replid2:3bd078f0ffa11b315b2bb3c1588785b304b3c2c2
master_repl_offset:477269
second_repl_offset:425485
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:477269
在从服务器上查看
[root@lz4 redis]# ./src/redis-cli -h 10.1.1.6 -p 6379
10.1.1.6:6379> info replication
# Replication
role:slave
master_host:10.1.1.5
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:493232
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:5feb667d4058707c9fa1346fd5e4c9c88d32d1bf
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:493232
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:429344
repl_backlog_histlen:63889
故障模拟
将主服务器上的redis-server进程关闭后验证是否会出现新的master
[root@lz3 redis]# ps aux | grep redis
root 61427 0.2 1.1 163092 11468 ? Ssl 11:04 1:04 ./src/redis-server 10.1.1.5:6379
root 64745 0.0 0.8 153876 8144 pts/0 Sl 17:04 0:00 ./src/redis-sentinel *:26379 [sentinel]
root 64750 0.0 0.0 112720 984 pts/0 R+ 17:04 0:00 grep --color=auto redis
[root@lz3 redis]# kill -9 61427
[root@lz3 redis]# ps aux | grep redis
root 64745 0.3 0.8 153876 8144 pts/0 Sl 17:04 0:00 ./src/redis-sentinel *:26379 [sentinel]
root 64760 0.0 0.0 112720 984 pts/0 S+ 17:04 0:00 grep --color=auto redis
在另外两个从服务器上查看
10.1.1.6:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=10.1.1.7,port=6379,state=online,offset=560611,lag=1
master_replid:2b0ff7dae9ccf09e4fa1918138f6377cd99df9b2
master_replid2:5feb667d4058707c9fa1346fd5e4c9c88d32d1bf
master_repl_offset:560611
second_repl_offset:553866
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:429344
repl_backlog_histlen:131268
可以看出master已经从原来的10.1.1.5这台机子变成了10.1.1.6;而且查看配置文件的话,也发生了改变
总结
配置过程随然简单,但是要注意的是IP地址不要搞错了,否则很容易报错
网友评论