在生产环境中,如果想要使用Redis的哨兵模式,也会尽量使用Redis的2.8版本之后的版本。无论是主从模式,还是哨兵模式,这两个模式都有一个问题,不能水平扩容,并且这两个模式的高可用特性都会受到Master主节点内存的限制。还有一点,实现哨兵模式的配置也不简单,甚至可以说有些繁琐,所以在工业场景里这两个模式都不建议使用,如果要使用必须有相关的问题的解决方案,以免后续带来的问题。
1、Sentinel(哨兵)进程的作用:
1】、监控(Monitoring): 哨兵(sentinel) 会不断地检查你的Master和Slave是否运作正常。
2】、提醒(Notification):当被监控的某个Redis节点出现问题时, 哨兵(sentinel) 可以通过 API 向管理员或者其他应用程序发送通知。
3】、自动故障迁移(Automatic failover):当一个Master不能正常工作时,哨兵(sentinel)
会开始一次自动故障迁移操作,它会将失效Master的其中一个Slave升级为新的Master,
并让失效Master的其他Slave改为复制新的Master;当客户端试图连接失效的Master时,集群也会向客户端返回新Master的地址,使得集群可以使用现在的Master替换失效Master。Master和Slave服务器切换后,Master的redis.conf、Slave的redis.conf和sentinel.conf的配置文件的内容都会发生相应的改变,即,Master主服务器的redis.conf配置文件中会多一行slaveof的配置,sentinel.conf的监控目标会随之调换。
下面我们来解释一下两个“下线”的概念,一个是“主观下线”,另一个就是“客观下线”。
主观下线(Subjectively Down, 简称 SDOWN)指的是单个 Sentinel 实例对服务器做出的下线判断。
客观下线(Objectively Down, 简称 ODOWN)指的是多个 Sentinel 实例在对同一个服务器做出 SDOWN
判断,并且通过 SENTINEL is-master-down-by-addr 命令互相交流之后,得出的服务器下线判断。(一个 Sentinel
可以通过向另一个 Sentinel 发送 SENTINEL is-master-down-by-addr
命令来询问对方是否认为给定的服务器已下线。)

1 下载安装包
cd /usr/local/src
wget http://download.redis.io/releases/redis-5.0.2.tar.gz
2 解压
tar zxvf redis-5.0.2.tar.gz
3 安装依赖包
yum -y install gcc gcc-c++
4 编译安装
cd redis-5.0.2
make MALLOC=libc && make install
5 配置redis 一主两从
[root@localhost redis-5.0.2]# cp redis.conf /etc/redis_6379.conf
[root@localhost redis-5.0.2]# cp redis.conf /etc/redis_6380.conf
[root@localhost redis-5.0.2]# cp redis.conf /etc/redis_6381.conf
主配置修改:redis_6379.conf
bind 0.0.0.0
daemonize yes 后台守护
port 6379
从配置修改
=======redis_6380.conf========
bind 0.0.0.0
daemonize yes 后台守护
port 6380
replicaof 192.168.10.131 6379 master_IP master_port
replica-priority 100 优先级
========redis_6381.conf========
bind 0.0.0.0
daemonize yes 后台守护
port 6381
replicaof 192.168.10.131 6379
replica-priority 90 优先级
6 运行服务
[root@localhost ~]# redis-server /etc/redis_6379.conf 28308:C 18 Nov 2019 01:54:16.096 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 28308:C 18 Nov 2019 01:54:16.096 # Redis version=5.0.2, bits=64, commit=00000000, modified=0, pid=28308, just started 28308:C 18 Nov 2019 01:54:16.096 # Configuration loaded [root@localhost ~]# redis-server /etc/redis_6380.conf 28313:C 18 Nov 2019 01:54:23.078 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 28313:C 18 Nov 2019 01:54:23.078 # Redis version=5.0.2, bits=64, commit=00000000, modified=0, pid=28313, just started 28313:C 18 Nov 2019 01:54:23.078 # Configuration loaded [root@localhost ~]# redis-server /etc/redis_6381.conf 28320:C 18 Nov 2019 01:54:26.226 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 28320:C 18 Nov 2019 01:54:26.226 # Redis version=5.0.2, bits=64, commit=00000000, modified=0, pid=28320, just started 28320:C 18 Nov 2019 01:54:26.226 # Configuration loaded 28320:S 18 Nov 2019 01:54:26.230 # Creating Server TCP listening socket 0.0.0.0:6379: bind: Address already in use
7 验证主从
[root@localhost ~]# redis-cli -p 6379 127.0.0.1:6379> set wg01 xxoo OK
从上取key 为wg01的信息
[root@localhost ~]# redis-cli -p 6380 127.0.0.1:6380> get wg01 "xxoo" 127.0.0.1:6380>
8 配置哨兵模式
[root@localhost redis-5.0.2]# pwd /usr/local/src/redis-5.0.2 [root@localhost redis-5.0.2]# cp sentinel.conf /etc/sentinel_26379.conf [root@localhost redis-5.0.2]# cp sentinel.conf /etc/sentinel_26380.conf [root@localhost redis-5.0.2]# cp sentinel.conf /etc/sentinel_26381.conf
删除已#号开头的 :g/^#/d
删除空行 :g/^$/d
vim /etc/sentinel_26379.conf
port 26379 daemonize yes pidfile /var/run/redis-sentinel.pid logfile "/var/log/redis/sentinel_26379.log" dir /tmp sentinel monitor mymaster 192.168.10.131 6379 2 sentinel down-after-milliseconds mymaster 30000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000 sentinel deny-scripts-reconfig yes
sentinel monitor mymaster 192.168.10.131 6379 2 告诉sentinel去监听地址为ip:port的一个master,这里的master-name可以自定义,quorum是一个数字,指明当有多少个sentinel认为一个master失效时,master才算真正失效 sentinel down-after-milliseconds mymaster 30000 这个配置项指定了需要多少失效时间,一个master才会被这个sentinel主观地认为是不可用的。 单位是毫秒,默认为30秒 sentinel parallel-syncs mymaster 1 这个配置项指定了在发生failover主备切换时最多可以有多少个slave同时对新的master进行 同步。 这个数字越小,完成failover所需的时间就越长,但是如果这个数字越大,就意味着越 多的slave因为replication而不可用。 可以通过将这个值设为 1 来保证每次只有一个slave 处于不能处理命令请求的状态。 sentinel failover-timeout mymaster 180000 当进行failover故障转移时,配置所有slaves指向新的master所需的最大时间 sentinel deny-scripts-reconfig yes 避免脚本重置,默认值yes
vim /etc/sentinel_26380.conf
port 26380 daemonize yes pidfile /var/run/redis-sentinel_26380.pid logfile "/var/log/redis/sentinel_26380.log" dir /tmp sentinel monitor mymaster 192.168.10.131 6379 2 sentinel down-after-milliseconds mymaster 30000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000 sentinel deny-scripts-reconfig yes
vim /etc/sentinel_26381.conf
port 26381 daemonize yes pidfile /var/run/redis-sentinel_26381.pid logfile "/var/log/redis/sentinel_26381.log" dir /tmp sentinel monitor mymaster 192.168.10.131 6379 2 sentinel down-after-milliseconds mymaster 30000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000 sentinel deny-scripts-reconfig yes
mkdir /var/log/redis
9 开启哨兵
redis-server /etc/sentinel_26379.conf --sentinel
redis-server /etc/sentinel_26380.conf --sentinel
redis-server /etc/sentinel_26381.conf --sentinel
10 验证
[root@localhost redis-5.0.2]# redis-cli -p 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.10.131:6379,slaves=2,sentinels=3
kill 掉 6379端口
主成功转移
[root@localhost redis-5.0.2]# redis-cli -p 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.10.131:6381,slaves=2,sentinels=3
redis-cluster 模式
cd /usr/local/redis-cluster目录下
mkdir {6379,6380,6381,6382,6383,6384}
依次编辑redis.conf文件,注意端口不同!!
[root@bogon src]# cat /usr/local/redis-cluster/6379/redis.conf |grep -v "^#"|sed '/^$/d' bind 127.0.0.1 daemonize yes port 6379 pidfile /var/run/redis_6379.pid loglevel notice logfile "/usr/local/redis-cluster/6379/node.log" cluster-enabled yes
创建集群
redis-cli --cluster create --cluster-replicas 1 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384
验证:
[root@bogon src]# redis-cli 127.0.0.1:6379> cluster info cluster_state:ok cluster_slots_assigned:16384 cluster_slots_ok:16384 cluster_slots_pfail:0 cluster_slots_fail:0 cluster_known_nodes:6 cluster_size:3 cluster_current_epoch:6 cluster_my_epoch:1 cluster_stats_messages_ping_sent:635 cluster_stats_messages_pong_sent:644 cluster_stats_messages_sent:1279 cluster_stats_messages_ping_received:639 cluster_stats_messages_pong_received:635 cluster_stats_messages_meet_received:5 cluster_stats_messages_received:1279 127.0.0.1:6379> cluster nodes 3a8bd0283ad148a602e6251689417932b902ad24 127.0.0.1:6381@16381 master - 0 1593598432000 3 connected 10923-16383 86f3e3ad534144aa2a0101dd5282f7b7289b27f0 127.0.0.1:6382@16382 slave 1d60c85782cf99ae01a1707b5e8b98632c040386 0 1593598433070 4 connected 1abdde7a538439d343cdbb06454d8acf04b81463 127.0.0.1:6383@16383 slave 3a8bd0283ad148a602e6251689417932b902ad24 0 1593598434077 5 connected 39ab2793f5270a97ee28286b4c98da453cac0b2a 127.0.0.1:6384@16384 slave 241d2952a4a55e2092b237f838f40faf552e7037 0 1593598433000 6 connected 241d2952a4a55e2092b237f838f40faf552e7037 127.0.0.1:6379@16379 myself,master - 0 1593598431000 1 connected 0-5460 1d60c85782cf99ae01a1707b5e8b98632c040386 127.0.0.1:6380@16380 master - 0 1593598432063 2 connected 5461-10922