Redis 哨兵安装-Centos
由于机器有限,所以接下来的教程都是在一台虚拟机中进行部署安装.
Redis哨兵高可用架构
sentinel哨兵是特殊的redis服务,不提供读写服务,主要用来监控redis实例节点。哨兵架构下client端第一次从哨兵找出redis的主节点,后续就直接访问redis的主节点,不会每次都通过sentinel代理访问redis的主节点,当redis的主节点发生变化,哨兵会第一时间感知到,并且将新的redis主节点通知给client端(这里面redis的client端一般都实现了订阅功能,订阅sentinel发布的节点变动消息)
搭建主从
参考上篇 Redis 主从安装-Centos文章,先搭建Redis主从
搭建哨兵
因为电脑配置有限,我这里使用一台服务器,分三个端口进行演示,由于是演示就不配置额外的参数。
其中6379 端口作为默认主节点,6380和6381端口作为slave节点。
创建Sentinel 端口号26379
- 创建目录和配置文件
$ mkdir -p /usr/local/sentinel/26379
$ vim /usr/local/sentinel/26379/sentinel.conf
- 编辑sentinel.conf内容如下
# 端口
port 26379
# 是否后台运行
daemonize yes
# 禁止保护模式
protected-mode no
pidfile /usr/local/sentinel/26379/sentinel.pid
# 配置目录
dir "/usr/local/sentinel/26379"
# 配置监听的主服务器,这里sentinel monitor代表监控,mymaster代表服务器的名称,可以自定义,host代表监控的主服务器(需要使用真实ip,不然客户端在连接时会连接本地),6379代表端口,2代表只有两个或两个以上的哨兵认为主服务器不可用的时候,才会进行failover操作。
sentinel monitor mymaster 192.168.0.138 6379 2
# sentinel author-pass 定义服务的密码,mymaster是服务名称,123456是Redis服务器密码
sentinel auth-pass mymaster soft01
创建Sentinel 端口号26380
- 创建目录和配置文件
$ mkdir -p /usr/local/sentinel/26380
$ vim /usr/local/sentinel/26380/sentinel.conf
- 编辑sentinel.conf内容如下
# 端口
port 26380
# 是否后台运行
daemonize yes
# 禁止保护模式
protected-mode no
pidfile /usr/local/sentinel/26380/sentinel.pid
# 配置目录
dir "/usr/local/sentinel/26380"
# 配置监听的主服务器,这里sentinel monitor代表监控,mymaster代表服务器的名称,可以自定义,host代表监控的主服务器(需要使用真实ip,不然客户端在连接时会连接本地),6379代表端口,2代表只有两个或两个以上的哨兵认为主服务器不可用的时候,才会进行failover操作。
sentinel monitor mymaster 192.168.0.138 6379 2
# sentinel author-pass 定义服务的密码,mymaster是服务名称,123456是Redis服务器密码
sentinel auth-pass mymaster soft01
创建Sentinel 端口号26381
- 创建目录和配置文件
$ mkdir -p /usr/local/sentinel/26381
$ vim /usr/local/sentinel/26381/sentinel.conf
- 编辑sentinel.conf内容如下
# 端口
port 26381
# 是否后台运行
daemonize yes
# 禁止保护模式
protected-mode no
pidfile /usr/local/sentinel/26381/sentinel.pid
# 配置目录
dir "/usr/local/sentinel/26381"
# 配置监听的主服务器,这里sentinel monitor代表监控,mymaster代表服务器的名称,可以自定义,host代表监控的主服务器(需要使用真实ip,不然客户端在连接时会连接本地),6379代表端口,2代表只有两个或两个以上的哨兵认为主服务器不可用的时候,才会进行failover操作。
sentinel monitor mymaster 192.168.0.138 6379 2
# sentinel author-pass 定义服务的密码,mymaster是服务名称,123456是Redis服务器密码
sentinel auth-pass mymaster soft01
启动Sentinel
- 先启动redis主从(下面是我搭建的主从)
# 启动主服务
$ /usr/local/redis/bin/redis-server /usr/local/master-slave/6379/redis.conf
# 启动从服务
$ /usr/local/redis/bin/redis-server /usr/local/master-slave/6380/redis.conf
$ /usr/local/redis/bin/redis-server /usr/local/master-slave/6381/redis.conf
$ ps -ef | grep redis
root 9673 1 0 00:23 ? 00:00:06 /usr/local/redis/bin/redis-server *:6379
root 30195 1 0 10月18 ? 00:00:08 /usr/local/redis/bin/redis-server *:6380
root 130775 1 0 00:21 ? 00:00:06 /usr/local/redis/bin/redis-server *:6381
- 分别启动哨兵
$ /usr/local/redis/bin/redis-sentinel /usr/local/sentinel/26379/sentinel.conf
$ /usr/local/redis/bin/redis-sentinel /usr/local/sentinel/26380/sentinel.conf
$ /usr/local/redis/bin/redis-sentinel /usr/local/sentinel/26381/sentinel.conf
$ ps -ef | grep sentinel
root 116864 1 0 01:30 ? 00:00:00 /usr/local/redis/bin/redis-sentinel *:26379 [sentinel]
root 119538 1 0 01:30 ? 00:00:00 /usr/local/redis/bin/redis-sentinel *:26380 [sentinel]
root 119765 1 0 01:31 ? 00:00:00 /usr/local/redis/bin/redis-sentinel *:26381 [sentinel]
启动sentinel的方式:
# 方式一 $ ./redis-sentinel sentinel.conf # 方式二 $ ./redis-server sentinel.conf --sentinel
查看Redis信息
- 查看redis服务的信息:
$ /usr/local/redis/bin/redis-cli -p 6379 -a soft01
127.0.0.1:6379> info
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.0.138,port=6380,state=online,offset=219732,lag=1
slave1:ip=192.168.0.138,port=6381,state=online,offset=219873,lag=0
master_replid:25a1a32168e72f2f5f6f5fc449c2f9b89f9eae66
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:219873
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:219873
$ /usr/local/redis/bin/redis-cli -p 6380 -a soft01
127.0.0.1:6380> info
# Replication
role:slave
master_host:192.168.0.138
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:227684
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:25a1a32168e72f2f5f6f5fc449c2f9b89f9eae66
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:227684
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:227684
$ /usr/local/redis/bin/redis-cli -p 6381 -a soft01
127.0.0.1:6381> info
# Replication
role:slave
master_host:192.168.0.138
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:236482
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:25a1a32168e72f2f5f6f5fc449c2f9b89f9eae66
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:236482
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:236482
- 查看哨兵配置文件变更信息:
$ less /usr/local/sentinel/26379/sentinel.conf
sentinel myid 511958e1eac946e44dfb00a4a97764137839d8b2 #唯一标识
sentinel deny-scripts-reconfig yes
# Generated by CONFIG REWRITE
sentinel monitor mymaster 192.168.0.138 6379 2
sentinel auth-pass mymaster soft01
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 0
sentinel known-replica mymaster 192.168.0.138 6381 # 监控到的从节点
sentinel known-replica mymaster 192.168.0.138 6380 # 监控到的从节点
sentinel known-sentinel mymaster 192.168.0.138 26380 e2dbf2594ddbef6088001be07f4a9651c18260a6 # 感知到的sentinel节点
sentinel known-sentinel mymaster 192.168.0.138 26381 8dd9dfd6d1a4234ee3d25a250f61cfd1fece419e # 感知到的sentinel节点
sentinel current-epoch 0
$ less /usr/local/sentinel/26380/sentinel.conf
sentinel myid e2dbf2594ddbef6088001be07f4a9651c18260a6
sentinel deny-scripts-reconfig yes
# Generated by CONFIG REWRITE
sentinel monitor mymaster 192.168.0.138 6379 2
sentinel auth-pass mymaster soft01
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 0
sentinel known-replica mymaster 192.168.0.138 6381
sentinel known-replica mymaster 192.168.0.138 6380
sentinel known-sentinel mymaster 192.168.0.138 26381 8dd9dfd6d1a4234ee3d25a250f61cfd1fece419e
sentinel known-sentinel mymaster 192.168.0.138 26379 511958e1eac946e44dfb00a4a97764137839d8b2
sentinel current-epoch 0
$ less /usr/local/sentinel/26381/sentinel.conf
sentinel myid 8dd9dfd6d1a4234ee3d25a250f61cfd1fece419e
sentinel deny-scripts-reconfig yes
# Generated by CONFIG REWRITE
sentinel monitor mymaster 192.168.0.138 6379 2
sentinel auth-pass mymaster soft01
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 0
sentinel known-replica mymaster 192.168.0.138 6380
sentinel known-replica mymaster 192.168.0.138 6381
sentinel known-sentinel mymaster 192.168.0.138 26380 e2dbf2594ddbef6088001be07f4a9651c18260a6
sentinel known-sentinel mymaster 192.168.0.138 26379 511958e1eac946e44dfb00a4a97764137839d8b2
sentinel current-epoch 0
由此可知,6379是Redis的主节点,6380和6381是从节点。
主节点故障测试
kill主节点
# 9673 是主节点的pid
$ kill -9 9673
正常启动主从和哨兵后,根据配置文件我们知道,6379端口号对应的redis是主节点,我们现在将主节点杀掉。
查看redis服务信息
首先查看sentinel(26379、26380、26381)配置文件,发现它们都修改了监控主节点的配置信息:
# 改之前
sentinel monitor mymaster 192.168.0.138 6379 2
# 改之后
sentinel monitor mymaster 192.168.0.138 6380 2
等sentinel选举完成后,查看redis服务信息,发现6380变为了新的主节点,6381变为6380的从节点。
$ /usr/local/redis/bin/redis-cli -p 6380 -a soft01
127.0.0.1:6380> info
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.0.138,port=6381,state=online,offset=319536,lag=1
master_replid:723a15ea136602f4205288882d403e3734405fa0
master_replid2:25a1a32168e72f2f5f6f5fc449c2f9b89f9eae66
master_repl_offset:319677
second_repl_offset:277075
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:319677
$ /usr/local/redis/bin/redis-cli -p 6381 -a soft01
127.0.0.1:6381> info
# Replication
role:slave
master_host:192.168.0.138
master_port:6380
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:323935
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:723a15ea136602f4205288882d403e3734405fa0
master_replid2:25a1a32168e72f2f5f6f5fc449c2f9b89f9eae66
master_repl_offset:323935
second_repl_offset:277075
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:323935
最后查看各个redis.conf的变化
/usr/local/master-slave/6379/redis.conf
未变化,因为服务停止了/usr/local/master-slave/6380/redis.conf
去掉了replicaof
配置,因为它已经成为新的主节点了。/usr/local/master-slave/6381/redis.conf
内将replicaof 192.168.0.138 6379
修改成replicaof 192.168.0.138 6380
,指向了新的主节点。
恢复6379服务
- 启动6379
$ /usr/local/redis/bin/redis-server /usr/local/master-slave/6379/redis.conf
- 查看
/usr/local/master-slave/6379/redis.conf
文件新增了replicaof 192.168.0.138 6380
,自动转为6380的从节点。 - 查看redis服务信息,6380主节点,6379和6381都是它的从节点
$ /usr/local/redis/bin/redis-cli -p 6379 -a soft01
127.0.0.1:6379> info
# Replication
role:slave
master_host:192.168.0.138
master_port:6380
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:568652
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:723a15ea136602f4205288882d403e3734405fa0
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:568652
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:544774
repl_backlog_histlen:23879
$ /usr/local/redis/bin/redis-cli -p 6380 -a soft01
127.0.0.1:6380> info
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.0.138,port=6381,state=online,offset=580566,lag=1
slave1:ip=192.168.0.138,port=6379,state=online,offset=580566,lag=1
master_replid:723a15ea136602f4205288882d403e3734405fa0
master_replid2:25a1a32168e72f2f5f6f5fc449c2f9b89f9eae66
master_repl_offset:580989
second_repl_offset:277075
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:580989
$ /usr/local/redis/bin/redis-cli -p 6381 -a soft01
127.0.0.1:6381> info
# Replication
role:slave
master_host:192.168.0.138
master_port:6380
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:587094
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:723a15ea136602f4205288882d403e3734405fa0
master_replid2:25a1a32168e72f2f5f6f5fc449c2f9b89f9eae66
master_repl_offset:587094
second_repl_offset:277075
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:587094
这样就完成了故障转移。
总结
当主节点宕机后,sentinel会在slave中选举一个变成新的master,将其他节点指向新的主节点(解决了主从模式不能自动恢复的问题)。
但是在主节点异常,哨兵进行选举时,会出现Redis短暂的不可以用。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/78240.html