Redis主从同步、哨兵模式、集群模式配置
作者:互联网
Redis主从同步、哨兵模式、集群模式配置
一、概述
redis主从:是备份关系, 操作主库,数据也会同步到从库。 如果主库机器坏了,从库可以上。
redis哨兵:哨兵保证的是HA,保证特殊情况故障自动切换,哨兵监控“redis主从集群”,如果主库down,会自动找新的master。实际使用sentinel哨兵模式时,可结合Keepalived使用,保证down的redis可以自动重启,提高高可用性。
redis集群:集群保证的是高并发,同时集群会导致数据的分散,整个redis集群会分成一堆数据槽,即不同的key会放到不不同的槽中。
主从保证了数据备份,哨兵保证了HA 即故障时切换,集群保证了高并发性。
二、Redis主从同步
2.1、配置信息
redis主从同步重要参数(redis.conf)
# slaveof <masterip> <masterport>
# masterauth <master-password>
# requirepass foobared
Master关键配置信息redis.conf
masterauth 123456
requirepass 123456
Slave关键配置信息(redis.conf)
slaveof 192.168.1.100 6379
masterauth 123456
requirepass 123456
redis.conf其他常用配置
bind 0.0.0.0
protected-mode no #关闭保护,允许非本地连接
port 6379 #端口号
daemonize yes #后台运行
pidfile /var/run/redis_6379.pid #进程守护文件
logfile /var/log/redis_6379.log #日志文件
dir /var/lib/redis/6379 #db等相关目录位置
appendonly no #AOF模式持久化,默认未打开
2.2、检查状态
[root@localhost redis]# /usr/local/bin/redis-cli -h 192.168.1.100 -p 6379
192.168.1.100:6379> auth 123456
OK
192.168.1.100:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.1.101,port=6379,state=online,offset=560,lag=0
slave1:ip=192.168.1.102,port=6379,state=online,offset=560,lag=0
master_replid:658f07a291f7e58100e1650bd920f8bcef3e3083
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:560
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:560
2.3、验证主从
#验证Master 写和读
[root@localhost redis]# /usr/local/bin/redis-cli -h 192.168.1.100 -p 6379
192.168.1.100:6379> auth 123456
192.168.1.100:6379> set projectCode 1001
OK
192.168.1.100:6379> get projectCode
"1001"
#验证Slave 写和读
[root@localhost redis]# /usr/local/bin/redis-cli -h 192.168.1.101 -p 6379
192.168.1.101:6379> auth 123456
OK
192.168.1.101:6379> get projectCode
"1001"
192.168.1.101:6379> set projectCode 1002
(error) READONLY You can't write against a read only slave.
三、哨兵模式
3.1、配置信息
redis哨兵模式重要参数(sentinel.conf)
# sentinel monitor <master-name> <ip> <redis-port> <quorum>
# sentinel auth-pass <master-name> <password>
# sentinel down-after-milliseconds <master-name> <milliseconds>
关键配置信息sentinel.conf
sentinel monitor mymaster 192.168.1.100 6379 2
sentinel auth-pass mymaster 123456
sentinel.conf其他配置
bind 0.0.0.0
protected-mode no
port 26379
daemonize yes
# sentinel down-after-milliseconds mymaster 30000
启动sentinel
/usr/local/bin/redis-sentinel ./sentinel.conf
或者
/usr/local/bin/redis-server ./sentinel.conf --sentinel
3.2、检查状态
[root@localhost redis]# /usr/local/bin/redis-cli -h 192.168.1.100 -p 26379
192.168.1.100:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.1.100:6379,slaves=2,sentinels=3
3.3、验证故障转移
#停止master实例
[root@localhost redis]# /usr/local/bin/redis-cli -h 192.168.1.100 -p 6379 -a 123456 shutdown
#再次检查sentinel状态
[root@localhost redis]# /usr/local/bin/redis-cli -h 192.168.1.100 -p 26379
192.168.1.100:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.1.102:6379,slaves=2,sentinels=3
192.168.1.100:26379> 11886:X 19 Apr 18:00:21.912 # +sdown slave 192.168.1.100:6379 192.168.1.100 6379 @ mymaster 192.168.1.102 6379
# 其他检查命令
192.168.1.100:26379> sentinel get-master-addr-by-name mymaster
1) "192.168.1.102"
2) "6379"
192.168.1.100:26379> sentinel slaves mymaster
192.168.1.100:26379> sentinel masters
四、集群模式
梳理Redis集群模式时,发现2018年初工作上有整理过,躺在草稿箱了,干脆直接发布,这里就不在整理,详见:
Redis维护笔记 https://www.cnblogs.com/huligong1234/p/8457102.html
五、相关参考
- redis单点、redis主从、redis哨兵sentinel,redis集群cluster配置搭建与使用 https://www.cnblogs.com/demingblog/p/10295236.html
- Redis主从复制以及主从复制原理 https://segmentfault.com/a/1190000018268350
- Redis集群redis主从自动切换Sentinel(哨兵模式)https://blog.51cto.com/canonind/1899939
- Redis(1.11)Redis4.0.11 cluster 分布式集群搭建 https://www.cnblogs.com/gered/p/11771601.html
- redis5.0.4 集群搭建 https://www.cnblogs.com/fousor/p/10598400.html
标签:Redis,redis,192.168,6379,1.100,集群,模式,sentinel,主从 来源: https://www.cnblogs.com/huligong1234/p/12732901.html