前言

Redis集群模式主要有2种:

  • 主从集群
  • 分布式集群。

前者主要是为了高可用或是读写分离,后者为了更好的存储数据,负载均衡。 
本文主要讲解主从集群。本章主要讲解后一半部分,Redis集群。

与本文相关的代码与配置文件都已经上传至github上: 
地址: https://github.com/SeanYanxml/bigdata


原理

Redis为了实现负载均衡,提供集群模式。以三个节点为例,集群模式相当于将1-15000片分片,分为1-5000、5000-10000、10000-15000。每个节点分一段数据片。这样的话,当一个节点宕机后,这个节点没有备份的话,此段分片将不再可以使用。所以,官方推荐,集群内的每个节点都应该配备一个从节点,作为冷备。部署原理图如下所示(暂略)。


部署

由于没有那么多的机器,所以我们一般单机部署6个节点(3主3从),也就是官网推荐的模式。

主要步骤如下:

  • 安装ruby,因为分配集群的代码时ruby脚本;
  • 分配集群
  • 查看集群结果

部署代码

  1. # mkdir cluster-test
  2. # cd cluster-test
  3. # mkdir 7000 7001 7002 7003 7004 7005
  4. # 在文件夹内 分别放置redis.conf文件 文件内容见下
  5. # 启动6个节点
  6. # nohup ../../src/redis-server redis.conf > start.log 2>&1 &
  7. 使用ruby脚本分配集群资源
  8. ./redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  1. # redis.conf
  2. port 7000
  3. cluster-enabled yes
  4. cluster-config-file nodes.conf
  5. cluster-node-timeout 5000
  6. appendonly yes
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  1. ### 启动redis进程7000 7001 7002 7003 7004 7005
  2. touch start-all.sh
  3. ### 内容如下
  4. cd 7000
  5. nohup ../../src/redis-server redis.conf > start.log 2>&1 &
  6. cd ../7001
  7. nohup ../../src/redis-server redis.conf > start.log 2>&1 &
  8. cd ../7002
  9. nohup ../../src/redis-server redis.conf > start.log 2>&1 &
  10. cd ../7003
  11. nohup ../../src/redis-server redis.conf > start.log 2>&1 &
  12. cd ../7004
  13. nohup ../../src/redis-server redis.conf > start.log 2>&1 &
  14. cd ../7005
  15. nohup ../../src/redis-server redis.conf > start.log 2>&1 &
  16. chmod +x start-all.sh
  17. ./start-all.sh
  18. ps -ef|grep redis 查看进程
  19. [cpic@cpic-redis2-77 src]$ ps -ef|grep redis
  20. cpic 19876 1 0 321 ? 05:14:19 ./redis-server *:6379
  21. cpic 19886 1 0 321 ? 06:17:36 ./redis-sentinel *:26379 [sentinel]
  22. cpic 25971 1 0 15:07 pts/0 00:00:00 ../../src/redis-server *:7000 [cluster]
  23. cpic 25972 1 0 15:07 pts/0 00:00:00 ../../src/redis-server *:7001 [cluster]
  24. cpic 25973 1 0 15:07 pts/0 00:00:00 ../../src/redis-server *:7002 [cluster]
  25. cpic 25974 1 0 15:07 pts/0 00:00:00 ../../src/redis-server *:7003 [cluster]
  26. cpic 25975 1 0 15:07 pts/0 00:00:00 ../../src/redis-server *:7004 [cluster]
  27. cpic 25976 1 0 15:07 pts/0 00:00:00 ../../src/redis-server *:7005 [cluster]
  28. cpic 26273 24815 0 15:15 pts/0 00:00:00 grep --color=auto redis

成功结果

  1. create 脚本初始化后脚本
  2. [root@cpic-redis-76 src]# ./redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005
  3. >>> Creating cluster
  4. >>> Performing hash slots allocation on 6 nodes...
  5. Using 3 masters:
  6. 127.0.0.1:7000
  7. 127.0.0.1:7001
  8. 127.0.0.1:7002
  9. Adding replica 127.0.0.1:7003 to 127.0.0.1:7000
  10. Adding replica 127.0.0.1:7004 to 127.0.0.1:7001
  11. Adding replica 127.0.0.1:7005 to 127.0.0.1:7002
  12. M: 562665aca5f7db25bbd81e7b971c2f4c9aa65f96 127.0.0.1:7000
  13. slots:0-5460 (5461 slots) master
  14. M: 6f93b84f7da7d6ea1b77bce1709432de472bbbe3 127.0.0.1:7001
  15. slots:5461-10922 (5462 slots) master
  16. M: 6522a9ddec5b40cade141793351df44fe40a529f 127.0.0.1:7002
  17. slots:10923-16383 (5461 slots) master
  18. S: f122aea6aa02be27f77454c0fcde9c65b891cd47 127.0.0.1:7003
  19. replicates 562665aca5f7db25bbd81e7b971c2f4c9aa65f96
  20. S: e5b5bbd378d8b80250dca6fb5db80e755f88c53c 127.0.0.1:7004
  21. replicates 6f93b84f7da7d6ea1b77bce1709432de472bbbe3
  22. S: 93ac017741ad4e65a4de3cb17ae295e6e5505b9a 127.0.0.1:7005
  23. replicates 6522a9ddec5b40cade141793351df44fe40a529f
  24. Can I set the above configuration? (type 'yes' to accept): yes
  25. >>> Nodes configuration updated
  26. >>> Assign a different config epoch to each node
  27. >>> Sending CLUSTER MEET messages to join the cluster
  28. Waiting for the cluster to join...
  29. >>> Performing Cluster Check (using node 127.0.0.1:7000)
  30. M: 562665aca5f7db25bbd81e7b971c2f4c9aa65f96 127.0.0.1:7000
  31. slots:0-5460 (5461 slots) master
  32. 1 additional replica(s)
  33. M: 6f93b84f7da7d6ea1b77bce1709432de472bbbe3 127.0.0.1:7001
  34. slots:5461-10922 (5462 slots) master
  35. 1 additional replica(s)
  36. S: e5b5bbd378d8b80250dca6fb5db80e755f88c53c 127.0.0.1:7004
  37. slots: (0 slots) slave
  38. replicates 6f93b84f7da7d6ea1b77bce1709432de472bbbe3
  39. M: 6522a9ddec5b40cade141793351df44fe40a529f 127.0.0.1:7002
  40. slots:10923-16383 (5461 slots) master
  41. 1 additional replica(s)
  42. S: 93ac017741ad4e65a4de3cb17ae295e6e5505b9a 127.0.0.1:7005
  43. slots: (0 slots) slave
  44. replicates 6522a9ddec5b40cade141793351df44fe40a529f
  45. S: f122aea6aa02be27f77454c0fcde9c65b891cd47 127.0.0.1:7003
  46. slots: (0 slots) slave
  47. replicates 562665aca5f7db25bbd81e7b971c2f4c9aa65f96
  48. [OK] All nodes agree about slots configuration.
  49. >>> Check for open slots...
  50. >>> Check slots coverage...
  51. [OK] All 16384 slots covered.
  • 1
  1. # 自动生成的node.conf文件
  2. [root@cpic-redis-76 7005]# cat nodes.conf
  3. 1c7cbff1069612078d63329e5841c7430757a2cd 127.0.0.1:7003@17003 slave f5cc2de688640de572d16d72c2f174de027609a6 0 1516672833000 4 connected
  4. dd09a06827e9bfeab3876f4464cd467f6d1279f0 127.0.0.1:7002@17002 master - 0 1516672833086 3 connected 10923-16383
  5. 5863f9ee11d3ee3beef466d76f48f221b48aa6b5 127.0.0.1:7004@17004 slave 3ed6531ec985e50e87ab8436d49f4c32ce82bdef 0 1516672832000 5 connected
  6. 3ed6531ec985e50e87ab8436d49f4c32ce82bdef 127.0.0.1:7001@17001 master - 0 1516672832000 2 connected 5461-10922
  7. 9013209020c2fe2322df8c420b1c251fe9c84955 127.0.0.1:7005@17005 myself,slave dd09a06827e9bfeab3876f4464cd467f6d1279f0 0 1516672831000 6 connected
  8. f5cc2de688640de572d16d72c2f174de027609a6 127.0.0.1:7000@17000 master - 0 1516672832885 1 connected 0-5460
  9. vars currentEpoch 6 lastVoteEpoch 0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  1. # 启动的log日志
  2. [root@cpic-redis-76 7005]# cat start.log
  3. nohup: 忽略输入
  4. 5627:C 22 Jan 21:02:58.212 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
  5. 5627:C 22 Jan 21:02:58.212 # Redis version=4.0.6, bits=64, commit=00000000, modified=0, pid=5627, just started
  6. 5627:C 22 Jan 21:02:58.212 # Configuration loaded
  7. 5627:M 22 Jan 21:02:58.214 * Increased maximum number of open files to 10032 (it was originally set to 1024).
  8. 5627:M 22 Jan 21:02:58.215 * Node configuration loaded, I'm 9013209020c2fe2322df8c420b1c251fe9c84955
  9. 5627:M 22 Jan 21:02:58.216 * Running mode=cluster, port=7005.
  10. 5627:M 22 Jan 21:02:58.216 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
  11. 5627:M 22 Jan 21:02:58.216 # Server initialized
  12. 5627:M 22 Jan 21:02:58.216 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
  13. 5627:M 22 Jan 21:02:58.216 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
  14. 5627:M 22 Jan 21:02:58.216 * Ready to accept connections
  15. 5627:M 23 Jan 10:00:27.212 # configEpoch set to 6 via CLUSTER SET-CONFIG-EPOCH
  16. 5627:M 23 Jan 10:00:27.286 # IP address for this node updated to 127.0.0.1
  17. 5627:S 23 Jan 10:00:31.238 * Before turning into a slave, using my master parameters to synthesize a cached master: I may be able to synchronize with the new master with just a partial transfer.
  18. 5627:S 23 Jan 10:00:31.239 # Cluster state changed: ok
  19. 5627:S 23 Jan 10:00:31.981 * Connecting to MASTER 127.0.0.1:7002
  20. 5627:S 23 Jan 10:00:31.981 * MASTER <-> SLAVE sync started
  21. 5627:S 23 Jan 10:00:31.981 * Non blocking connect for SYNC fired the event.
  22. 5627:S 23 Jan 10:00:31.981 * Master replied to PING, replication can continue...
  23. 5627:S 23 Jan 10:00:31.982 * Trying a partial resynchronization (request 033127a1144abc3683a83a6b7257c2b56d2bd0b8:1).
  24. 5627:S 23 Jan 10:00:31.983 * Full resync from master: e5e51569d1f49762952e6cc05e1f13ddff68719e:0
  25. 5627:S 23 Jan 10:00:31.983 * Discarding previously cached master state.
  26. 5627:S 23 Jan 10:00:32.084 * MASTER <-> SLAVE sync: receiving 175 bytes from master
  27. 5627:S 23 Jan 10:00:32.084 * MASTER <-> SLAVE sync: Flushing old data
  28. 5627:S 23 Jan 10:00:32.084 * MASTER <-> SLAVE sync: Loading DB in memory
  29. 5627:S 23 Jan 10:00:32.084 * MASTER <-> SLAVE sync: Finished with success
  30. 5627:S 23 Jan 10:00:32.085 * Background append only file rewriting started by pid 10164
  31. 5627:S 23 Jan 10:00:32.108 * AOF rewrite child asks to stop sending diffs.
  32. 10164:C 23 Jan 10:00:32.108 * Parent agreed to stop sending diffs. Finalizing AOF...
  33. 10164:C 23 Jan 10:00:32.108 * Concatenating 0.00 MB of AOF diff received from parent.
  34. 10164:C 23 Jan 10:00:32.108 * SYNC append only file rewrite performed
  35. 10164:C 23 Jan 10:00:32.109 * AOF rewrite: 0 MB of memory used by copy-on-write
  36. 5627:S 23 Jan 10:00:32.181 * Background AOF rewrite terminated with success
  37. 5627:S 23 Jan 10:00:32.181 * Residual parent diff successfully flushed to the rewritten AOF (0.00 MB)
  38. 5627:S 23 Jan 10:00:32.181 * Background AOF rewrite finished successfully


BUG

  • BUG1
  1. [root@cpic-redis-76 src]# ./redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005
  2. >>> Creating cluster
  3. [ERR] Node 127.0.0.1:7005 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
  4. 解决办法: 删掉原有数据dump.rdb/appendonly.aof/nodes.conf文件。
  • 1
  • 2
  • 3
  • 4
  • BUG2
  1. ruby安装一些列困难。
  • 1

详见Redis Cluster 运维环境安装记录


Reference

Redis Cluster 运维环境安装记录

 

Redis集群模式之分布式集群模式的更多相关文章

  1. solr 集群(SolrCloud 分布式集群部署步骤)

    SolrCloud 分布式集群部署步骤 安装软件包准备 apache-tomcat-7.0.54 jdk1.7 solr-4.8.1 zookeeper-3.4.5 注:以上软件都是基于 Linux ...

  2. Solr系列二:solr-部署详解(solr两种部署模式介绍、独立服务器模式详解、SolrCloud分布式集群模式详解)

    一.solr两种部署模式介绍 Standalone Server 独立服务器模式:适用于数据规模不大的场景 SolrCloud  分布式集群模式:适用于数据规模大,高可靠.高可用.高并发的场景 二.独 ...

  3. Hadoop(三)手把手教你搭建Hadoop全分布式集群

    前言 上一篇介绍了伪分布式集群的搭建,其实在我们的生产环境中我们肯定不是使用只有一台服务器的伪分布式集群当中的.接下来我将给大家分享一下全分布式集群的搭建! 其实搭建最基本的全分布式集群和伪分布式集群 ...

  4. Hadoop(三)搭建Hadoop全分布式集群

    原文地址:http://www.cnblogs.com/zhangyinhua/p/7652686.html 阅读目录(Content) 一.搭建Hadoop全分布式集群前提 1.1.网络 1.2.安 ...

  5. MinIO分布式集群的扩展方案及实现

    目录 一.命令行方式扩展 1. MinIO扩展集群支持的命令语法 2. 扩容示例 二.etcd扩展方案 1. 环境变量 2. 运行多个集群 3. 示例 相关链接 MinIO 支持两种扩展方式: 通过修 ...

  6. 分布式缓存技术redis学习系列(四)——redis高级应用(集群搭建、集群分区原理、集群操作)

    本文是redis学习系列的第四篇,前面我们学习了redis的数据结构和一些高级特性,点击下面链接可回看 <详细讲解redis数据结构(内存模型)以及常用命令> <redis高级应用( ...

  7. 分布式缓存技术redis学习(四)——redis高级应用(集群搭建、集群分区原理、集群操作)

    本文是redis学习系列的第四篇,前面我们学习了redis的数据结构和一些高级特性,点击下面链接可回看 <详细讲解redis数据结构(内存模型)以及常用命令> <redis高级应用( ...

  8. 分布式缓存技术redis系列(四)——redis高级应用(集群搭建、集群分区原理、集群操作)

    本文是redis学习系列的第四篇,前面我们学习了redis的数据结构和一些高级特性,点击下面链接可回看 <详细讲解redis数据结构(内存模型)以及常用命令> <redis高级应用( ...

  9. Redis面试题及分布式集群

    Reference: http://blog.csdn.net/yajlv/article/details/73467865 1. 使用Redis有哪些好处? (1) 速度快,因为数据存在内存中,类似 ...

随机推荐

  1. [Vuex系列] - Module的用法(终篇)

    于使用单一状态树,应用的所有状态会集中到一个比较大的对象.当应用变得非常复杂时,store 对象就有可能变得相当臃肿.为了解决以上问题,Vuex 允许我们将 store 分割成模块(module).每 ...

  2. MySQL: Can’t connect to MySQL server on (111 “Connection refused”)

    1. Mysql连接问题 远程访问mysql或者通过docker访问宿主机mysql经常会碰到下面的问题: Can't connect to MySQL server on (111 "Co ...

  3. 实现LAMP架构

    LAMP介绍 LAM(M)P: L: linux A: apache (httpd) M: mysql, mariadb M:memcached P: php, perl, python WEB资源类 ...

  4. 什么是领域模型(domain model)?贫血模型(anaemic domain model)和充血模型(rich domain model)有什么区别

    领域模型是领域内的概念类或现实世界中对象的可视化表示,又称为概念模型或分析对象模型,它专注于分析问题领域本身,发掘重要的业务领域概念,并建立业务领域概念之间的关系. 贫血模型是指使用的领域对象中只有s ...

  5. *p 和p[i] 区别

    注意:*(arr+ n -1)指向的 是原来&a[n-1]是个地址 与arr[n-1]不同 !!重点!

  6. 创建守护进程步骤与setsid()

    原创:http://www.cnblogs.com/mickole/p/3188321.html 一,守护进程概述 Linux Daemon(守护进程)是运行在后台的一种特殊进程.它独立于控制终端并且 ...

  7. javascript中如何判断数组是数组

    if (!Array.isArray) { Array.isArray = function(arg) { return Object.prototype.toString.call(arg) === ...

  8. Fillder的前后端的应用

    测试需求:有一个功能,允许用钻石兑换金币,假设1钻石=1金币,前端控制一次至少兑换10个,最多100个,后台不做验证. 测试方案:输入10,也就是告诉前端我要兑换10个金币,等前端验证通过之后,截取要 ...

  9. WebRequest与WebResponse抽象类,DNS静态类、Ping类

    一.概述 1.WebRequest: 对统一资源标识符 (URI) 发出请求. 这是一个 abstract 类. WebRequest的派生类:PackWebRequest.FileWebReques ...

  10. duilib学习领悟(4)

    使用duilib创建的主窗口绘制工作全都发生在一个 真实存在的主窗口句柄当中,这个绘制过程稍稍有些复杂,但再复杂也逃不过WM_PAINT消息,所有的绘制工作也都由这个WM_PAINT消息完成.在上几篇 ...