Redis 主从备份,哨兵设置
环境 Centos 7,redis 4.0.6
三个centos 7实例
节点 | IP | 端口 |
---|---|---|
redis-master | 172.31.193.221 | 6379 |
redis-slave | 172.31.193.222 | 6379 |
redis-LBS | 172.31.193.224 | 6379 |
redis-sentinel-master | 172.31.193.221 | 26379 |
redis-sentinel-slave | 172.31.193.222 | 26379 |
redis-sentinel-LBS | 172.31.193.224 | 26379 |
1.将redis解压到目录(/bak/soft/)
tar -zxvf 压缩包名 解压目录
2.创建日志和数据恢复目录
mkdir -p /bak/soft/logs/redis
mkdir -p /bak/soft/cdata/redis
mkdir -p /bak/soft/logs/redis-sentinel
mkdir -p /bak/soft/cdata/redis-sentinel
3.配置redis-master.conf
bind 127.0.0.1 172.31.193.221
daemonize yes
masterauth "ADMINadmin123"
requirepass "ADMINadmin123"
pidfile "/var/run/redis_6379.pid"
loglevel notice
logfile "/bak/soft/logs/redis/redis.log"
#下面不用复制,作为参考,实际部署中根据需要配置
bind 127.0.0.1 172.31.193.221
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 "/bak/soft/logs/redis/redis.log"
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
dir "/bak/soft/cdata/redis"
masterauth "rdhkdcX5xCoHNg86MV0Y"
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
requirepass "rdhkdcX5xCoHNg86MV0Y"
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
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events "xE"
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
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
# Generated by CONFIG REWRITE
4.配置redis-slave.conf
bind 127.0.0.1 172.31.193.222
daemonize yes
masterauth "ADMINadmin123"
requirepass "ADMINadmin123"
dbfilename "dump.rdb"
dir "/bak/soft/cdata/redis"
pidfile "/var/run/redis_6379.pid"
loglevel notice
logfile "/bak/soft/logs/redis/redis.log"
slaveof 172.31.193.221 6379
#下面不用复制,作为参考,实际部署中根据需要配置
bind 127.0.0.1 172.31.193.221
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 "/bak/soft/logs/redis/redis.log"
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
dir "/bak/soft/cdata/redis"
masterauth "rdhkdcX5xCoHNg86MV0Y"
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
requirepass "rdhkdcX5xCoHNg86MV0Y"
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
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events "xE"
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
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
# Generated by CONFIG REWRITE
slaveof 172.31.193.221 6379
5.配置redis-LBS.conf
bind 127.0.0.1 172.31.193.224
daemonize yes
masterauth "ADMINadmin123"
requirepass "ADMINadmin123"
dbfilename "dump.rdb"
dir "/bak/soft/cdata/redis"
pidfile "/var/run/redis_6379.pid"
loglevel notice
logfile "/bak/soft/logs/redis/redis.log"
slaveof 172.31.193.221 6379
#下面不用复制,作为参考,实际部署中根据需要配置
bind 127.0.0.1 172.31.193.224
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 "/bak/soft/logs/redis/redis.log"
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
dir "/bak/soft/cdata/redis"
masterauth "rdhkdcX5xCoHNg86MV0Y"
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
requirepass "rdhkdcX5xCoHNg86MV0Y"
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
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events "xE"
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
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
# Generated by CONFIG REWRITE
slaveof 172.31.193.221 6379
6.配置sentinel.conf文件master&slave&LBS配置可以一样
protected-mode no
port 26379
daemonize yes
sentinel monitor mymaster 172.31.193.221 6379 2
logfile "/bak/soft/logs/redis/sentinel.log"
dir "/bak/soft/cdata/redis-sentinel"
sentinel auth-pass mymaster ADMINadmin123
#下面不用复制,myid为自动生成,slave节点也是自动添加
protected-mode no
port 26379
daemonize yes
sentinel myid f7564fc40667bb5e336c6566acc28654d0c40d39
sentinel monitor mymaster 172.31.193.221 6379 2
logfile "/bak/soft/logs/redis/sentinel.log"
dir "/bak/soft/cdata/redis-sentinel"
sentinel auth-pass mymaster ADMINadmin123
sentinel config-epoch mymaster 4
sentinel leader-epoch mymaster 4
sentinel known-slave mymaster 172.31.193.222 6379
sentinel known-slave mymaster 172.31.193.224 6379
sentinel known-sentinel mymaster 172.31.193.222 26379 96d7940391c5f9f386dedc1eb7a5b221e458428a
sentinel known-sentinel mymaster 172.31.193.224 26379 d5202361f59b4695ebe8a7cefd26c61466637554
sentinel current-epoch 4
7.配置redis.properties连接信息master&slave&LBS可以一样
redis.address=172.31.193.221:26379;172.31.193.222:26379;172.31.193.224:26379
redis.pool.testOnBorrow=true
redis.pool.testOnReturn=false
redis.pool.testWhileIdle=false
redis.pool.maxIdle=300
redis.pool.minIdle=15
redis.pool.maxActive=1000
redis.pool.maxWait=10000
redis.pool.timeout=10000
redis.pool.numTestsPerEvictionRun=0
redis.pool.timeBetweenEvictionRunsMillis=0
redis.pool.minEvictableIdleTimeMillis=0
redis.password=ADMINadmin123
redis.type=sentinel
redis.master.name=mymaster
8.修改防火墙规则,开放端口
#查看防火墙状态
service iptables status
#测试端口是否开放
telnet ip 端口
#修改防火墙规则
vim /etc/sysconfig/iptables
-A INPUT -p tcp -m tcp --dport 6379 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 26379 -j ACCEPT
service iptables restart
#若已经启动redis,kill进程重启服务
ps aux |grep redis
kill -9 进程号
9.启动redis顺序,master—>slave&LBS—>三个哨兵
/bak/soft/redis-4.0.6/redis-server /etc/redis.conf
/bak/soft/redis-4.0.6/redis-sentinel /etc/sentinel.conf
#查看日志
tail 100 /bak/soft/logs/redis/redis.log
#查看日志
tail 100 /bak/soft/logs/redis/redis.log
10.测试停止master节点,slave或LBS节点将被选举为master节点
再重启master自动变成slave