暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Linux 搭建Redis集群

秃头架构 2018-09-20
279

一、准备Linux服务器多台,

正常按照生产环境部署redis集群是需要3台机器,1台机器双节点(主节点、从节点),3台机器6个节点集群(具体可看系统数据量、服务器配置来定)。redis集群官方给出了一个标准,必须要6个节点以上集群。(当然自己学习一台服务器也可以,但是要启6个节点)不然创建集群的时候会报错。

二、准备需要的安装包等工具:

  1. 百度网盘:https://pan.baidu.com/s/1RQhrdnMXHU-yGc6Izz7xoA      密码:sfzn

  2. 里面是Linuxredis安装包、搭建集群需要的插件redis-3.3.3.gem、一个可视化客户端

三、准备环境安装:

  1. 1.解压redis安装包:tar -zxvf  redis-3.2.8.tar.gz

  2. 2.cd redis-3.2.8文件夹 (如果不熟悉Linux命令的话,可以看我之前的博客学习:      Linux命令大全)

  3. 3.make  (编译环境)

  4. 4.cd 进入/opt/redis-3.2.8/src/      执行: make install PREFIX=/opt/redis

注:如果中间编译发生错误,那就是你的机器没有安装C++什么之类的编译环境因为Redis是C语言开发的需要这些环境支 持,机器能连接外网可以直接源命令下载按 : yum install gcc-c++

不能连接外网就非常、非常、非常麻烦,需要你自己去网上一个个下载环境包,一个个安装而且中间不能出现纰漏。以上步骤是3台机器的通用步骤

四、cp /opt/redis/redis-3.2.8/src/redis-trib.rb /opt/redis/bin

  1. #只需要第一台机器操作

在每台机器上建立两个实例 使用一下步骤
  1. 1.mkdir -p /opt/redis/instance/ins端口号/conf

  2. 2.mkdir -p /opt/redis/instance/ins端口号/data

  3. 3.mkdir -p /opt/redis/instance/ins端口号/data

  4. 4.mkdir -p /opt/redis/instance/ins端口号/log

  5. 5.vim  /opt/redis/instance/ins端口号/conf/redis.conf

内容如下(通用配置): 需注意标红的地方
  1. //GENERAL

  2. protected-mode no


  3. bind 192.168.0.182 127.0.0.1

  4. daemonize yes  

  5. tcp-backlog 511  

  6. timeout 0  

  7. tcp-keepalive 0  

  8. loglevel notice  

  9. databases 16  

  10. dir /opt/redis/instance/ins7000/data  

  11. slave-serve-stale-data yes  


  12. //slave只读  

  13. slave-read-only yes  


  14. //ot use default  

  15. repl-disable-tcp-nodelay yes  


  16. slave-priority 100  


  17. //打开aof持久化  

  18. appendonly yes  


  19. //每秒一次aof写  

  20. appendfsync everysec  


  21. //关闭在aof rewrite的时候对新的写操作进行fsync  

  22. no-appendfsync-on-rewrite yes  

  23. auto-aof-rewrite-min-size 64mb  

  24. lua-time-limit 5000  


  25. //打开redis集群  

  26. cluster-enabled yes  


  27. //节点互连超时的阀值  

  28. cluster-node-timeout 15000  

  29. cluster-migration-barrier 1  

  30. slowlog-log-slower-than 10000  

  31. slowlog-max-len 128  

  32. notify-keyspace-events ""  

  33. hash-max-ziplist-entries 512  

  34. hash-max-ziplist-value 64  

  35. list-max-ziplist-entries 512  

  36. list-max-ziplist-value 64  

  37. set-max-intset-entries 512  

  38. zset-max-ziplist-entries 128  

  39. zset-max-ziplist-value 64  

  40. activerehashing yes  

  41. client-output-buffer-limit normal 0 0 0  

  42. client-output-buffer-limit slave 256mb 64mb 60  

  43. client-output-buffer-limit pubsub 32mb 8mb 60  

  44. hz 10  

  45. aof-rewrite-incremental-fsync yes


  46. //包含通用配置  

  47. include /root/redis/conf/redis-common.conf  


  48. //监听tcp端口  

  49. port 7000  


  50. //最大可用内存  

  51. maxmemory 100m  


  52. //内存耗尽时采用的淘汰策略:  

  53. //volatile-lru -> remove the key with an expire set using an LRU algorithm  

  54. //allkeys-lru -> remove any key accordingly to the LRU algorithm  

  55. //volatile-random -> remove a random key with an expire set  

  56. //allkeys-random -> remove a random key, any key  

  57. //volatile-ttl -> remove the key with the nearest expire time (minor TTL)  //noeviction -> don't expire at all, just return an error on write operations  

  58. maxmemory-policy allkeys-lru  

  59. //aof存储文件  

  60. appendfilename "appendonly.aof"  

  61. //不开启rdb存储,只用于添加slave过程  

  62. dbfilename dump.rdb  

  63. //cluster配置文件(启动自动生成)  

  64. cluster-config-file nodes.conf  

  65. //部署在同一机器的redis实例,把auto-aof-rewrite搓开,因为cluster环境下内存占用基本一致.  

  66. //防止同意机器下瞬间fork所有redis进程做aof rewrite,占用大量内存  

  67. auto-aof-rewrite-percentage 80-100

五、启动所有的6个实例 ./redis-server conf配置文件的地址

  1. /opt/redis/bin/redis-server /opt/redis/instance/ins端口号/conf/redis.conf

在一台机器上安装 yum install ruby rubygems 搭建集群的环境
安装插件 gem install -l redis-3.3.3.gem (这个非常重要,是管理redis集群的一个工具)

六、创建集群

  1. /opt/redis/bin/redis-trib.rb create --replicas 1 ip1:port1 ip2:port2

创建之后如果没有报错就会显示集群的主从节点信息

七、使用redis-trib.rb命令redis集群管理工具

1.查看集群信息 ./redis-trib.rb info 192.168.0.181:7001 (集群中的任意节点都可以)

一些集群常用命令:
1、create:创建集群
2、check:检查集群
3、info:查看集群信息
4、fix:修复集群
5、reshard:在线迁移slot
6、rebalance:平衡集群节点slot数量
7、add-node:将新节点加入集群
8、del-node:从集群中删除节点
9、set-timeout:设置集群节点间心跳连接的超时时间
10、call:在集群全部节点上执行命令
11、import:将外部redis数据导入集群
其他的具体自行百度吧


文章转载自秃头架构,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论