redis搭建 一主二从 三哨兵 模式(单台linux服务器模拟)

1、进入redis的安装目录下,找到bin目录
(如果还没安装redis的,可以参考https://www.cnblogs.com/xsge/p/13841875.html这篇文章,注意这里install的时候,自己可以选择一下安装目录,免得自己后面找不到了
make install PREFIX=/usr/local/redis-6.x     ##将Redis安装在指定位置:)
2、在bin目录下mkdir一个conf 文件夹,用来存放 redis.conf 和 sentinel.conf 的配置文件

root@iZwz99v2o2uqo0x8j8qZ:/usr/local/redis-6.0.8# make install PREFIX=/usr/local/redis-sentinel
root@iZwz98xev2o2uqo0x8j8qZ:/usr/local/redis-6.0.8/src# cd ..
root@iZwz99xev2o2uqo0x8j8qZ:/usr/local/redis-6.0.8# cd ..
root@iZwz998v2o2uqo0x8j8qZ:/usr/local# cd redis-sentinel/
root@iZwz998xo2uqo0x8j8qZ:/usr/local/redis-sentinel# ls
bin
root@iZwz9982o2uqo0x8j8qZ:/usr/local/redis-sentinel# cd bin/
root@iZwz99ev2o2uqo0x8j8qZ:/usr/local/redis-sentinel/bin# ls
redis-benchmark  redis-check-aof  redis-check-rdb  redis-cli  redis-sentinel  redis-server
root@iZwz998xev2o2qo0x8j8qZ:/usr/local/redis-sentinel/bin# mkdir conf

# 拷贝配置文件到conf下
root@iZwz998xe2uqo0x8j8qZ:/usr/local/redis-6.0.8# cp sentinel.conf /usr/local/redis-sentinel/bin/conf
root@iZwz9982o2uqo0x8j8qZ:/usr/local/redis-6.0.8# cp redis.conf /usr/local/redis-sentinel/bin/conf

root@iZwz998xoqo0x8j8qZ:/usr/local/redis-sentinel/bin# cd conf/
root@iZwz998xuqo0x8j8qZ:/usr/local/redis-sentinel/bin/conf# pwd
/usr/local/redis-sentinel/bin/conf
root@iZwz998x2ouqo0x8j8qZ:/usr/local/redis-sentinel/bin/conf# ls
redis.conf  sentinel.conf

#复制2份 redis.conf 和两份 sentinel.conf
root@iZwz99v2o2uqox8j8qZ:/usr/local/redis-sentinel/bin/conf# cp redis.conf redis_s1.conf
root@iZwz9xev2o2uqo08j8qZ:/usr/local/redis-sentinel/bin/conf# cp redis.conf redis_s2.conf
root@iZwz9ev2o2uqox8j8qZ:/usr/local/redis-sentinel/bin/conf# cp sentinel.conf sentinel02.conf
root@iZwz99xev2o2uo0x8j8qZ:/usr/local/redis-sentinel/bin/conf# cp sentinel.conf sentinel03.conf
root@iZwz9982o2uqox8j8qZ:/usr/local/redis-sentinel/bin/conf# ls
redis.conf  redis_s1.conf  redis_s2.conf  sentinel02.conf  sentinel03.conf  sentinel.conf

(3)修改配置文件
vim 相应的配置文件,修改以下的配置即可

# redis.conf
daemonize yes //后台运行
pidfile /var/run/redis_6379.pid
dbfilename dump-6379.rdb
logfile "6379.log"  //记录日志文件

#redis_s1.conf
port 7000
daemonize yes
pidfile /var/run/redis_7000.pid
dbfilename dump-7000.rdb
logfile "7000log"
#slaveof表示作为从库的配置
slaveof 127.0.0.1 6379
#从库只能读操作
slave-read-only yes

#redis_s2.conf
port 7001
daemonize yes
pidfile /var/run/redis_7001.pid
dbfilename dump-7001.rdb
logfile "7001log"
#slaveof表示作为从库的配置
slaveof 127.0.0.1 6379
#从库只能读操作
slave-read-only yes

这里完成配置的修改之后,可以启动一下服务,检查有没有问题
这里写了一个脚本,一键开启三个redis服务

# 路径为/usr/local/redis-sentinel/bin/start-redis.sh
root@iZwz998xevx8j8qZ:/usr/local/redis-sentinel/bin# vim start-redis.sh
./redis-server conf/redis.conf
./redis-server conf/redis_s1.conf
./redis-server conf/redis_s2.conf
# 注意这里redis-server的路径,我这里的start-redis.sh文件 与 redis-server在同一目录下的

# 结果
root@iZwz9982o2uqo0x8j8qZ:/usr/local/redis-sentinel/bin# sh start-redis.sh
root@iZwz998xev2oqo0x8j8qZ:/usr/local/redis-sentinel/bin# ps -aux | grep redis
root     21083  0.0  0.0  62608  3764 ?        Ssl  14:08   0:00 ./redis-server 127.0.0.1:6379
root     21089  0.0  0.0  62608  3760 ?        Ssl  14:08   0:00 ./redis-server 127.0.0.1:7000
root     21095  0.0  0.0  62608  3768 ?        Ssl  14:08   0:00 ./redis-server 127.0.0.1:7001
root     21101  0.0  0.0  14436  1016 pts/0    S+   14:08   0:00 grep --color=auto redis

连接上去看看
root@iZwz998x2o2uqo0x8j8qZ:/usr/local# redis-cli -p 6379
127.0.0.1:6379> info
...
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=7000,state=online,offset=70,lag=0
slave1:ip=127.0.0.1,port=7001,state=online,offset=70,lag=0
master_replid:edb87a980d90c52f8b400d0a296a40f52fdb4175
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:70
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:70
...

连接master后,可以看到两个从节点的配置。

接着看一下sentinel配置的修改

#sentinel.conf
port 26379
daemonize yes
pidfile /var/run/redis-sentinel26379.pid
logfile "26379.log"
sentinel monitor mymaster 127.0.0.1 6379 2

#sentinel02.conf
port 27000
daemonize yes
pidfile /var/run/redis-sentinel27000.pid
logfile "27000.log"
sentinel monitor mymaster 127.0.0.1 7000 2

#sentinel03.conf
port 27001
daemonize yes
pidfile /var/run/redis-sentinel27001.pid
logfile "27001.log"
sentinel monitor mymaster 127.0.0.1 7001 2

写一个一键开启哨兵的脚本

root@iZwz998xev2o2uq8j8qZ:/usr/local/redis-sentinel/bin# vim start-sentinel.sh
./redis-server conf/sentinel.conf --sentinel
./redis-server conf/sentinel02.conf --sentinel
./redis-server conf/sentinel03.conf --sentinel

# 结果
root@iZwz998xev2o2uqo0x8j8qZ:/usr/local/redis-sentinel/bin# sh start-sentinel.sh
root@iZwz998xev2o2uqo0x8j8qZ:/usr/local/redis-sentinel/bin# ps -aux | grep sentinel
root     21833  0.2  0.1  62608  4560 ?        Ssl  14:47   0:00 ./redis-server *:26379 [sentinel]
root     21839  0.2  0.1  62608  4596 ?        Ssl  14:47   0:00 ./redis-server *:27000 [sentinel]
root     21845  0.2  0.1  62608  4544 ?        Ssl  14:47   0:00 ./redis-server *:27001 [sentinel]
root     21853  0.0  0.0  14436  1044 pts/0    S+   14:47   0:00 grep --color=auto sentinel

自此 一主二从 三哨兵 模式就已经搭建完毕了。

root@iZwz998xev2oqo0x8j8qZ:/usr/local/redis-sentinel/bin# ps -aux | grep redis
root     21139  0.1  0.1  71312  4796 ?        Ssl  14:11   0:03 ./redis-server 127.0.0.1:6379
root     21145  0.1  0.1 140948  4680 ?        Ssl  14:11   0:03 ./redis-server 127.0.0.1:7000
root     21152  0.1  0.1  65168  4768 ?        Ssl  14:11   0:03 ./redis-server 127.0.0.1:7001
root     21750  0.0  0.0  25996  3188 pts/2    S+   14:38   0:00 redis-cli -p 7000
root     21833  0.2  0.1  62608  4548 ?        Ssl  14:47   0:00 ./redis-server *:26379 [sentinel]
root     21839  0.2  0.1  62608  4588 ?        Ssl  14:47   0:00 ./redis-server *:27000 [sentinel]
root     21845  0.2  0.1  62608  4536 ?        Ssl  14:47   0:00 ./redis-server *:27001 [sentinel]
root     21855  0.0  0.0  14436  1112 pts/0    S+   14:47   0:00 grep --color=auto redis

可以连接对应的redis服务查看具体的信息

127.0.0.1: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=127.0.0.1:6379,slaves=2,sentinels=1 #sentinels=1表示启动了1个Sentinel


root@iZwz998xev2o2uqo0x8j8qZ:~# redis-cli -p 7000
127.0.0.1:7000> set ijh kjlj
(error) READONLY You can't write against a read only replica.
127.0.0.1:7000> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:64927
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:edb87a980d90c52f8b400d0a296a40f52fdb4175
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:64927
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:64927

现在kill掉master看具体的情况

root@iZwz998xev2o2uqo0x8j8qZ:/usr/local/redis-sentinel/bin# ps -aux | grep redis
root     21139  0.1  0.1  71312  4796 ?        Ssl  14:11   0:03 ./redis-server 127.0.0.1:6379
root     21145  0.1  0.1 140948  4680 ?        Ssl  14:11   0:03 ./redis-server 127.0.0.1:7000
root     21152  0.1  0.1  65168  4768 ?        Ssl  14:11   0:03 ./redis-server 127.0.0.1:7001
root     21750  0.0  0.0  25996  3188 pts/2    S+   14:38   0:00 redis-cli -p 7000
root     21833  0.2  0.1  62608  4548 ?        Ssl  14:47   0:00 ./redis-server *:26379 [sentinel]
root     21839  0.2  0.1  62608  4588 ?        Ssl  14:47   0:00 ./redis-server *:27000 [sentinel]
root     21845  0.2  0.1  62608  4536 ?        Ssl  14:47   0:00 ./redis-server *:27001 [sentinel]
root     21855  0.0  0.0  14436  1112 pts/0    S+   14:47   0:00 grep --color=auto redis
root@iZwz998xev2o2uqo0x8j8qZ:/usr/local/redis-sentinel/bin# vim start-sentinel.sh
root@iZwz998xev2o2uqo0x8j8qZ:/usr/local/redis-sentinel/bin# ps -aux | grep redis
root     21139  0.1  0.1  71312  4812 ?        Ssl  14:11   0:04 ./redis-server 127.0.0.1:6379
root     21145  0.1  0.1 140948  4940 ?        Ssl  14:11   0:04 ./redis-server 127.0.0.1:7000
root     21152  0.1  0.1  65168  4868 ?        Ssl  14:11   0:04 ./redis-server 127.0.0.1:7001
root     21750  0.0  0.0  25996  3188 pts/2    S+   14:38   0:00 redis-cli -p 7000
root     21833  0.2  0.1  62608  4672 ?        Ssl  14:47   0:01 ./redis-server *:26379 [sentinel]
root     21839  0.3  0.1  62608  4808 ?        Ssl  14:47   0:01 ./redis-server *:27000 [sentinel]
root     21845  0.3  0.1  62608  4772 ?        Ssl  14:47   0:01 ./redis-server *:27001 [sentinel]
root     21860  0.0  0.0  25996  3064 pts/3    S+   14:52   0:00 redis-cli -p 7001
root     22145  0.0  0.0  25732  2188 pts/4    S+   14:53   0:00 redis-cli -p 26379
root     22147  0.0  0.0  14436  1112 pts/0    S+   14:55   0:00 grep --color=auto redis
root@iZwz998xev2o2uqo0x8j8qZ:/usr/local/redis-sentinel/bin# kill -9 21139
root@iZwz998xev2o2uqo0x8j8qZ:/usr/local/redis-sentinel/bin# ps -aux | grep redis
root     21145  0.1  0.1 140948  4872 ?        Ssl  14:11   0:04 ./redis-server 127.0.0.1:7000
root     21152  0.1  0.1  65168  4848 ?        Ssl  14:11   0:04 ./redis-server 127.0.0.1:7001
root     21750  0.0  0.0  25996  3188 pts/2    S+   14:38   0:00 redis-cli -p 7000
root     21833  0.2  0.1  62608  4564 ?        Ssl  14:47   0:01 ./redis-server *:26379 [sentinel]
root     21839  0.3  0.1  62608  4672 ?        Ssl  14:47   0:01 ./redis-server *:27000 [sentinel]
root     21845  0.3  0.1  62608  4596 ?        Ssl  14:47   0:01 ./redis-server *:27001 [sentinel]
root     21860  0.0  0.0  25996  3064 pts/3    S+   14:52   0:00 redis-cli -p 7001
root     22145  0.0  0.0  25732  2188 pts/4    S+   14:53   0:00 redis-cli -p 26379
root     22149  0.0  0.0  14436  1112 pts/0    S+   14:55   0:00 grep --color=auto redis

可以看到现在已经kill掉 6379

看一下 7000 和 7001 的info

127.0.0.1:7000> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=7001,state=online,offset=103254,lag=0
master_replid:4f97a409c1679dd32206e543b70ec25bac9f6446
master_replid2:edb87a980d90c52f8b400d0a296a40f52fdb4175
master_repl_offset:103254
second_repl_offset:101489
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:103254

127.0.0.1:7001> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:7000
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:104199
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:4f97a409c1679dd32206e543b70ec25bac9f6446
master_replid2:edb87a980d90c52f8b400d0a296a40f52fdb4175
master_repl_offset:104199
second_repl_offset:101489
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:104199

可以看到7000成为了新的 master,实际上是根据slave_priority来选择的,slave_priority更高的更容易被选为新的master,但是之前查看的时候两个slave的slave_priority都是100,所以就是任意一个都行。

现在把就的master重新启动,再看具体的信息

127.0.0.1:6379> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:7000
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:140517
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:4f97a409c1679dd32206e543b70ec25bac9f6446
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:140517
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:140362
repl_backlog_histlen:156

可以看到,重新启动后6379自动成为了 7000 的slave

以上就是sentinel机制实现高可用的 实践了。
具体的概念可以参考:
面试必问,redis高可用原理,哨兵机制详解 - 知乎 (zhihu.com)
https://blog.csdn.net/men_wen/article/details/72724406
https://blog.csdn.net/qq_36850813/article/details/102723655
等博文。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值