redis主从架构与redis+sentinel 哨兵机制架构搭建(生产可用)

╰半橙微兮° 2022-01-17 10:13 306阅读 0赞

redis的搭建过程,请参考 https://blog.51cto.com/12445535/2385106

接下来,我们再找一台服务器,进行安装redis 实现redis的主从架构

和上面的方法搭建一个redis
只不过
在从redis中的配置文件中写
[root@prd3-redis02-10-184 conf]# grep slaveof 6379.conf
######## slaveof
slaveof 192.168.10.183 6379 //直接添加这个一行,然后

启动redis
[root@prd3-redis02-10-184 conf]# redis-server /ivargo/app/redis/conf/6379.conf

接下来就是看差距

在主redis可以看到
[root@prd3-redis01-10-183 conf]# redis-cli -a XXX
Warning: Using a password with ‘-a’ option on the command line interface may not be safe.
127.0.0.1:6379> info

Replication

role:master
connected_slaves:1

在从redis上可以看到
[root@prd3-redis02-10-184 conf]# redis-cli -aXXX
Warning: Using a password with ‘-a’ option on the command line interface may not be safe.
127.0.0.1:6379> info
####### Replication
role:slave
master_host:192.168.10.183
master_port:6379

这样redis的主从就搭建好了

搭建完redis后,目录结构

  1. [root@prd3-redis01-10-183 ivargo]# pwd
  2. /ivargo
  3. [root@prd3-redis01-10-183 ivargo]# tree
  4. .
  5. ├── app
  6. ├── redis
  7. ├── 6379.pid
  8. ├── conf
  9. └── 6379.conf
  10. ├── data
  11. ├── 6379.rdb
  12. └── appendonly_6379.aof
  13. └── log
  14. └── sentinel
  15. └── conf
  16. └── log
  17. └── 6379.log
  18. 8 directories, 5 files
  19. 实现redissentinel
  20. [root@prd3-redis01-10-183 conf]# pwd
  21. /ivargo/app/sentinel/conf
  22. [root@prd3-redis01-10-183 conf]# cat sentinel.conf //这个配置文件是prd3上的sentinel的配置文件,我们需要修改的
  23. # Example sentinel.conf
  24. # bind 127.0.0.1 192.168.1.1
  25. bind 0.0.0.0
  26. protected-mode no
  27. port 26379
  28. daemonize yes
  29. loglevel notice
  30. logfile "/ivargo/log/sentinel.log"
  31. dir "/tmp"
  32. # sentinel auth-pass <master-name> <password>
  33. # sentinel down-after-milliseconds <master-name> <milliseconds>
  34. # Default is 30 seconds.
  35. sentinel myid ba794535d3a65e14b266e28462fa7c68de609f39
  36. # sentinel parallel-syncs <master-name> <numslaves>
  37. sentinel deny-scripts-reconfig yes
  38. # sentinel failover-timeout <master-name> <milliseconds>
  39. # Default is 3 minutes = 180000.
  40. sentinel monitor mymaster 10.80.85.20 6379 2
  41. # Generated by CONFIG REWRITE
  42. sentinel down-after-milliseconds mymaster 6000
  43. sentinel auth-pass mymaster xxx
  44. sentinel config-epoch mymaster 0
  45. sentinel leader-epoch mymaster 1
  46. sentinel known-slave mymaster 10.80.85.21 6379
  47. sentinel known-sentinel mymaster 10.80.85.21 26379 c3002e02c6a2d0041afd2569f3fd1985bc38993e
  48. sentinel current-epoch 1
  49. [root@prd3-redis01-10-183 conf]# cat sentinel.conf //这里面都是我们自己指定的,他其他的文件内容,都是他自己生成的
  50. # Example sentinel.conf
  51. # bind 127.0.0.1 192.168.1.1
  52. bind 0.0.0.0
  53. protected-mode no
  54. port 26379
  55. daemonize yes
  56. loglevel notice
  57. logfile "/ivargo/log/sentinel.log"
  58. pidfile "/ivargo/app/sentinel/26379.pid"
  59. dir "/tmp"
  60. sentinel monitor mymaster 192.168.10.183 6379 2
  61. sentinel auth-pass mymaster XXX
  62. 184上的sentinel配置文件和183一样的
  63. [root@prd3-redis01-10-183 conf]# redis-sentinel /ivargo/app/sentinel/conf/sentinel.conf
  64. [root@prd3-redis02-10-184 conf]# redis-sentinel /ivargo/app/sentinel/conf/sentinel.conf
  65. [root@prd3-redis01-10-183 conf]# cat sentinel.conf
  66. # Example sentinel.conf
  67. # bind 127.0.0.1 192.168.1.1
  68. bind 0.0.0.0
  69. protected-mode no
  70. port 26379
  71. daemonize yes
  72. loglevel notice
  73. logfile "/ivargo/log/sentinel.log"
  74. pidfile "/ivargo/app/sentinel/26379.pid"
  75. dir "/tmp"
  76. sentinel myid 6075d58bdc7c2602f98d90c0aa48470c4dbb50d9
  77. sentinel deny-scripts-reconfig yes
  78. # Generated by CONFIG REWRITE
  79. sentinel monitor mymaster 192.168.10.183 6379 2
  80. sentinel auth-pass mymaster XXX
  81. sentinel config-epoch mymaster 0
  82. sentinel leader-epoch mymaster 0
  83. sentinel known-slave mymaster 192.168.10.184 6379
  84. sentinel known-sentinel mymaster 192.168.10.184 26379 039bd11a572245b6c16c6e204523d781ffb6ba4c
  85. sentinel current-epoch 0
  86. 查看不同点
  87. 我们检查
  88. [root@prd3-redis01-10-183 conf]# redis-cli -h 192.168.10.183 -p 26379 info
  89. # Sentinel
  90. sentinel_masters:1
  91. sentinel_tilt:0
  92. sentinel_running_scripts:0
  93. sentinel_scripts_queue_length:0
  94. sentinel_simulate_failure_flags:0
  95. master0:name=mymaster,status=ok,address=192.168.10.183:6379,slaves=1,sentinels=2
  96. 验证sentinel高可用
  97. [root@prd3-redis01-10-183 redis]# kill `cat 6379.pid`
  98. 杀死183的进程
  99. 然后在184上看
  100. [root@prd3-redis02-10-184 conf]# redis-cli -h 192.168.10.183 -p 26379 info
  101. # Sentinel
  102. sentinel_masters:1
  103. sentinel_tilt:0
  104. sentinel_running_scripts:0
  105. sentinel_scripts_queue_length:0
  106. sentinel_simulate_failure_flags:0
  107. master0:name=mymaster,status=ok,address=192.168.10.184:6379,slaves=1,sentinels=2 //主节点变成了184了
  108. 在启动183
  109. [root@prd3-redis01-10-183 redis]# redis-server /ivargo/app/redis/conf/6379.conf
  110. [root@prd3-redis01-10-183 redis]# redis-cli -h 192.168.10.183 -p 26379 info
  111. # Sentinel
  112. sentinel_masters:1
  113. sentinel_tilt:0
  114. sentinel_running_scripts:0
  115. sentinel_scripts_queue_length:0
  116. sentinel_simulate_failure_flags:0
  117. master0:name=mymaster,status=ok,address=192.168.10.184:6379,slaves=1,sentinels=2
  118. 然后查看redis 原来的183是主节点,现在是从节点了
  119. [root@prd3-redis01-10-183 redis]# redis-cli -h 192.168.10.183 -p 6379 -a XXX info
  120. # Replication
  121. role:slave
  122. master_host:192.168.10.184
  123. master_port:6379
  124. [root@prd3-redis02-10-184 conf]# redis-cli -h 192.168.10.184 -p 6379 -a XXX info
  125. # Replication
  126. role:master
  127. connected_slaves:1
  128. slave0:ip=192.168.10.183,port=6379,state=online,offset=169470,lag=1
  129. 以上实现了redissentinel机制 也就是 HA机制

其实对于现上环境的话,当redis发生了主从切换的时候,这个比如程序调用的ip就会改变,写两个ip和两个sentinel地址显然是不合理的,我们可以实现vip机制自动切换,来实现程序调用只需要一个ip,相关内容,请期待。

转载于:https://blog.51cto.com/12445535/2385117

发表评论

表情:
评论列表 (有 0 条评论,306人围观)

还没有评论,来说两句吧...

相关阅读

    相关 redis主从架构

    一.绪论    Redis的复制功能是基于内存快照的持久化策略基础上的,也就是说无论你的持久化策略选择的是什么,只要用到了Redis的复制功能,就一定会有内存快照发生。

    相关 redis主从哨兵

    1. 为什么要有哨兵机制     哨兵机制是对Redis系统的运行情况的监控,解决主从复制的缺点的。  原理:当主节点出现故障时,由Redis Sentinel自动完成故障