1.环境说明
系统:CentOS-7-x86_64
ip:192.168.18.236
REDIS_HOME:/opt/redis-4.0.1
redis节点:
192.168.18.236:6379 master
192.168.18.236:6380 slave
192.168.18.236:6381 slave
2.修改配置
cd /opt/redis-4.0.1
cp redis.conf redis_6380.conf
cp redis.conf redis_6381.conf
#修改port、pidfile、slaveof
vi redis_6380.conf
port 6380
pidfile /var/run/redis_6380.pid
slaveof 127.0.0.1 6379
vi redis_6381.conf
port 6381
pidfile /var/run/redis_6381.pid
slaveof 127.0.0.1 6379
3.启动测试
cd /opt/redis-4.0.1
#启动master和2个slave
./src/redis-server redis.conf
./src/redis-server redis_6380.conf
./src/redis-server redis_6381.conf
#打开3个shell窗口,登录3个客户端
./src/redis-cli -p 6379
./src/redis-cli -p 6380
./src/redis-cli -p 6381
#查看6379复制系统中的信息
127.0.0.1:6379> INFO replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6380,state=online,offset=1246,lag=1
slave1:ip=127.0.0.1,port=6381,state=online,offset=1246,lag=1
master_replid:8bb4c076d0ce5179282dac083bf526173d8ef33b
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1246
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1246
#查看6380复制系统中的信息
127.0.0.1:6380> INFO replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:3
master_sync_in_progress:0
slave_repl_offset:1442
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:8bb4c076d0ce5179282dac083bf526173d8ef33b
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1442
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1442
4.sentinel配置
sentinel介绍
Redis-Sentinel是Redis官方推荐的高可用性(HA)解决方案
当用Redis做Master-slave的高可用方案时,假如master宕机了,Redis本身(包括它的很多客户端)都没有实现自动进行主备切换
而Redis-sentinel本身也是一个独立运行的进程,它能监控多个master-slave集群,发现master宕机后能进行自懂切换。
它的主要功能有以下几点:- 不时地监控redis是否按照预期良好地运行;
- 如果发现某个redis节点运行出现状况,能够通知另外一个进程(例如它的客户端);
- 能够进行自动切换。当一个master节点不可用时,能够选举出master的多个slave(如果有超过一个slave的话)中的一个来作为新的master,其它的slave节点会将它所追随的master的地址改为被提升为master的slave的新地址。
Sentinel支持集群
很显然,只使用单个sentinel进程来监控redis集群是不可靠的,当sentinel进程宕掉后(sentinel本身也有单点问题,single-point-of-failure)整个集群系统将无法按照预期的方式运行。所以有必要将sentinel集群
搭建2个sentinel节点
cd /opt/redis-4.0.1
cp sentinel.conf sentinel_26380.conf
vi sentinel.conf
#后端启动
daemonize yes
logfile "/var/log/sentinel.log"
sentinel monitor mymaster 127.0.0.1 6379 2
vi sentinel_26380.conf
port 26380
#后端启动
daemonize yes
logfile "/var/log/sentinel_26380.log"
sentinel monitor mymaster 127.0.0.1 6379 2
#启动两个sentinel服务
./src/redis-sentinel sentinel.conf
./src/redis-sentinel sentinel_26380.conf
5.测试sentinel
1).把6379 master节点关闭
ps aux | grep redis-server
#把6379的服务kill掉
kill -9 3584
2).查看sentinel log
cat /var/log/sentinel.log
4323:X 21 Sep 10:15:57.638 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4323:X 21 Sep 10:15:57.638 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=4323, just started
4323:X 21 Sep 10:15:57.638 # Configuration loaded
4324:X 21 Sep 10:15:57.641 * Increased maximum number of open files to 10032 (it was originally set to 1024).
4324:X 21 Sep 10:15:57.643 * Running mode=sentinel, port=26379.
4324:X 21 Sep 10:15:57.643 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
4324:X 21 Sep 10:15:57.643 # Sentinel ID is 8ad4ea46a01c2176d7665b64fad69e08080bf9a8
4324:X 21 Sep 10:15:57.643 # +monitor master mymaster 127.0.0.1 6379 quorum 2
4324:X 21 Sep 10:16:05.756 * +sentinel sentinel 1e3e9ae7f00507f5153ee3cef0ba2dc2a97c2ff1 127.0.0.1 26380 @ mymaster 127.0.0.1 6379
4324:X 21 Sep 10:18:56.619 # +sdown master mymaster 127.0.0.1 6379
4324:X 21 Sep 10:18:56.753 # +new-epoch 1
4324:X 21 Sep 10:18:56.754 # +vote-for-leader 1e3e9ae7f00507f5153ee3cef0ba2dc2a97c2ff1 1
4324:X 21 Sep 10:18:57.189 # +config-update-from sentinel 1e3e9ae7f00507f5153ee3cef0ba2dc2a97c2ff1 127.0.0.1 26380 @ mymaster 127.0.0.1 6379
4324:X 21 Sep 10:18:57.189 # +switch-master mymaster 127.0.0.1 6379 127.0.0.1 6381
4324:X 21 Sep 10:18:57.189 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6381
4324:X 21 Sep 10:18:57.189 * +slave slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6381
4324:X 21 Sep 10:19:27.248 # +sdown slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6381
发现自动将master切换到6381上
3).cat sentinel.conf
#发现自动改为6381
sentinel monitor mymaster 127.0.0.1 6381 2
4).登录6381客户端,查看是否为master
./src/redis-cli -p 6381
#查看复制系统信息,role已经改为master
127.0.0.1:6381> INFO replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6380,state=online,offset=52458,lag=0
master_replid:9d438d265f580c023e1b08e73f0ea5edda5b8a8f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:52591
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:25409
repl_backlog_histlen:27183
5).再重新启动6379节点
./src/redis-server redis.conf
./src/redis-cli -p 6379
#发现role为slave
127.0.0.1:6379> INFO replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6381
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:83939
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:9d438d265f580c023e1b08e73f0ea5edda5b8a8f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:83939
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:77064
repl_backlog_histlen:6876