首页 > Redis > 正文

RedisCluster集群之搭建

佛若2018-09-170人围观

RedisCluster集群之搭建

一、安装ruby环境

《centos7搭建ruby环境》

安装redis

《Redis安装单实例》

二、配置cluster集群

2.1 准备基础配置环境

端口范围7001~7006(共6个,计划master:slave=1:1)

创建6个端口对应的文件夹

  1. # 进入redis根目录
  2. cd /usr/local/project/redis-4.0.8
  3. # 创建cluster-config文件夹,用于存放各个端口对应的信息
  4. mkdir cluster-config
  5. # 进入cluster-config文件夹
  6. cd cluster-config
  7. # 分别创建6个端口的文件夹,可批量创建
  8. mkdir 7001 7002 7003 7004 7005 7006
  9. # 将配置文件拷贝至7001
  10. cp redis.conf cluster-config/7001/
  11. # 先修改7001下的配置文件,修改后再分别复制到其他端口文件夹中
  12. vim cluster-config/7001/redis.conf

redis.conf修改内容如下

  1. # 修改访问ip
  2. bind 127.0.0.1
  3. # 修改访问端口
  4. port 700*
  5. # 以后台方式运行
  6. daemonize yes
  7. # redis实例的pid文件
  8. pidfile="/usr/local/project/redis-4.0.8/cluster-config/700*/redis.pid"
  9. # 日志文件
  10. logfile=/usr/local/project/redis-4.0.8/cluster-config/700*/redis.log
  11. # 数据文件存储位置
  12. dir=/usr/local/project/redis-4.0.8/cluster-config/700*/
  13. # 启动集群
  14. cluster-enabled=yes
  15. # 这里与port对应即可
  16. cluster-config-file nodes-700*.conf
  17. # 超时
  18. cluster-node-timeout=5000
  19. # 开启aof
  20. appendonly=yes
  21. # 设置aof模式为
  22. appendfsync=always

复制配置文件

  1. # 进入7001目录
  2. cd /usr/local/project/redis-4.0.8/cluster-config/7001/
  3. # 复制配置文件至其他端口目录,并分别修改对应的配置文件
  4. cp redis.conf ../7002/
  5. cp redis.conf ../7003/
  6. cp redis.conf ../7004/
  7. cp redis.conf ../7005/
  8. cp redis.conf ../7006/
  9. # 别忘了修改配置文件

小技巧

vim下查找 / ,按N查询下一个
vim下替换 :%s/7001/7002/g ,%是表示全文检索,将7001替换成7002,g表示替换所有

2.2 启动服务

分别启动6个服务

  1. ./src/redis-server cluster-config/7001/redis.conf
  2. ./src/redis-server cluster-config/7002/redis.conf
  3. ./src/redis-server cluster-config/7003/redis.conf
  4. ./src/redis-server cluster-config/7004/redis.conf
  5. ./src/redis-server cluster-config/7005/redis.conf
  6. ./src/redis-server cluster-config/7006/redis.conf

通过ps查看进程状态

  1. ps -ef | grep redis
  2. // 输出如下
  3. root 5407 1 0 18:36 ? 00:00:00 ./src/redis-server 127.0.0.1:7001 [cluster]
  4. root 5415 1 0 18:36 ? 00:00:00 ./src/redis-server 127.0.0.1:7002 [cluster]
  5. root 5423 1 0 18:36 ? 00:00:00 ./src/redis-server 127.0.0.1:7003 [cluster]
  6. root 5431 1 0 18:36 ? 00:00:00 ./src/redis-server 127.0.0.1:7004 [cluster]
  7. root 5439 1 0 18:36 ? 00:00:00 ./src/redis-server 127.0.0.1:7005 [cluster]
  8. root 5447 1 0 18:36 ? 00:00:00 ./src/redis-server 127.0.0.1:7006 [cluster]

2.3 配置集群

利用redis-trib.rb执行cluster集群配置

  1. ./src/redis-trib.rb create --replicas 1 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 127.0.0.1:7006

提示

  1. >>> Creating cluster
  2. >>> Performing hash slots allocation on 6 nodes...
  3. Using 3 masters:
  4. 127.0.0.1:7001
  5. 127.0.0.1:7002
  6. 127.0.0.1:7003
  7. Adding replica 127.0.0.1:7005 to 127.0.0.1:7001
  8. Adding replica 127.0.0.1:7006 to 127.0.0.1:7002
  9. Adding replica 127.0.0.1:7004 to 127.0.0.1:7003
  10. >>> Trying to optimize slaves allocation for anti-affinity
  11. [WARNING] Some slaves are in the same host as their master
  12. M: 44c678a77a5181b99f6fe0a5e24a98462d687288 127.0.0.1:7001
  13. slots:0-5460 (5461 slots) master
  14. M: 83e9712ba4520f949119b21a4ebb3de2d66ed2a0 127.0.0.1:7002
  15. slots:5461-10922 (5462 slots) master
  16. M: 0d68dfae22060574a19712f2b6ee2608dbf1b471 127.0.0.1:7003
  17. slots:10923-16383 (5461 slots) master
  18. S: ca9146ea9ce9ef7aa60ac2511f90bd91677285ff 127.0.0.1:7004
  19. replicates 0d68dfae22060574a19712f2b6ee2608dbf1b471
  20. S: bb5f01c7e82348e6c90ce7da209696d02e06546b 127.0.0.1:7005
  21. replicates 44c678a77a5181b99f6fe0a5e24a98462d687288
  22. S: 88a52d61d4c5f5ee00551a546460697bf9f1c475 127.0.0.1:7006
  23. replicates 83e9712ba4520f949119b21a4ebb3de2d66ed2a0
  24. Can I set the above configuration? (type 'yes' to accept):

输入

  1. yes

输出

  1. >>> Nodes configuration updated
  2. >>> Assign a different config epoch to each node
  3. >>> Sending CLUSTER MEET messages to join the cluster
  4. Waiting for the cluster to join...
  5. >>> Performing Cluster Check (using node 127.0.0.1:7001)
  6. M: 44c678a77a5181b99f6fe0a5e24a98462d687288 127.0.0.1:7001
  7. slots:0-5460 (5461 slots) master
  8. 1 additional replica(s)
  9. M: 0d68dfae22060574a19712f2b6ee2608dbf1b471 127.0.0.1:7003
  10. slots:10923-16383 (5461 slots) master
  11. 1 additional replica(s)
  12. S: ca9146ea9ce9ef7aa60ac2511f90bd91677285ff 127.0.0.1:7004
  13. slots: (0 slots) slave
  14. replicates 0d68dfae22060574a19712f2b6ee2608dbf1b471
  15. S: bb5f01c7e82348e6c90ce7da209696d02e06546b 127.0.0.1:7005
  16. slots: (0 slots) slave
  17. replicates 44c678a77a5181b99f6fe0a5e24a98462d687288
  18. S: 88a52d61d4c5f5ee00551a546460697bf9f1c475 127.0.0.1:7006
  19. slots: (0 slots) slave
  20. replicates 83e9712ba4520f949119b21a4ebb3de2d66ed2a0
  21. M: 83e9712ba4520f949119b21a4ebb3de2d66ed2a0 127.0.0.1:7002
  22. slots:5461-10922 (5462 slots) master
  23. 1 additional replica(s)
  24. [OK] All nodes agree about slots configuration.
  25. >>> Check for open slots...
  26. >>> Check slots coverage...
  27. [OK] All 16384 slots covered.

最终提示 [OK] All 16384 slots covered. 0~16383 共16384个槽点被包括在内,说明配置集群成功。

温馨提示: 16384个slot是redis内置写死的,固定的,无法修改。

其中我们先分析一下以下内容

  1. M: 44c678a77a5181b99f6fe0a5e24a98462d687288 127.0.0.1:7001
  2. slots:0-5460 (5461 slots) master
  3. 1 additional replica(s)
  4. M: 0d68dfae22060574a19712f2b6ee2608dbf1b471 127.0.0.1:7003
  5. slots:10923-16383 (5461 slots) master
  6. 1 additional replica(s)
  7. S: ca9146ea9ce9ef7aa60ac2511f90bd91677285ff 127.0.0.1:7004
  8. slots: (0 slots) slave
  9. replicates 0d68dfae22060574a19712f2b6ee2608dbf1b471
  10. S: bb5f01c7e82348e6c90ce7da209696d02e06546b 127.0.0.1:7005
  11. slots: (0 slots) slave
  12. replicates 44c678a77a5181b99f6fe0a5e24a98462d687288
  13. S: 88a52d61d4c5f5ee00551a546460697bf9f1c475 127.0.0.1:7006
  14. slots: (0 slots) slave
  15. replicates 83e9712ba4520f949119b21a4ebb3de2d66ed2a0
  16. M: 83e9712ba4520f949119b21a4ebb3de2d66ed2a0 127.0.0.1:7002
  17. slots:5461-10922 (5462 slots) master
  18. 1 additional replica(s)
  • 前面的M/S分别表示Master/Slave
  • 44c678a77a5181b99f6fe0a5e24a98462d687288 表示当前节点的id
  • 127.0.0.1:7001 表示当前节点的ip与端口
  • slots:0-5460 (5461 slots) 表示分配了从0到5460共5461个槽点
  • 1 additional replica(s) 表示有一个slave节点
  • replicates 0d68dfae22060574a19712f2b6ee2608dbf1b471 以此节点为master,自己是slave节点

2.4 验证集群

通过客户端命令查询集群状态(任意节点都可以)

  1. ./src/redis-cli -c -h 127.0.0.1 -p 7001

输入 cluster info

  1. 127.0.0.1:7001> cluster info
  2. cluster_state:ok
  3. cluster_slots_assigned:16384
  4. cluster_slots_ok:16384
  5. cluster_slots_pfail:0
  6. cluster_slots_fail:0
  7. cluster_known_nodes:6 // 说明有6个已知节点
  8. cluster_size:3 // 有三个集群组
  9. cluster_current_epoch:6
  10. cluster_my_epoch:1
  11. cluster_stats_messages_ping_sent:846
  12. cluster_stats_messages_pong_sent:860
  13. cluster_stats_messages_sent:1706
  14. cluster_stats_messages_ping_received:855
  15. cluster_stats_messages_pong_received:846
  16. cluster_stats_messages_meet_received:5
  17. cluster_stats_messages_received:1706

列出所有节点 cluster nodes

  1. 127.0.0.1:7001> cluster nodes
  2. 0d68dfae22060574a19712f2b6ee2608dbf1b471 127.0.0.1:7003@17003 master - 0 1537149964380 3 connected 10923-16383
  3. ca9146ea9ce9ef7aa60ac2511f90bd91677285ff 127.0.0.1:7004@17004 slave 0d68dfae22060574a19712f2b6ee2608dbf1b471 0 1537149965086 4 connected
  4. bb5f01c7e82348e6c90ce7da209696d02e06546b 127.0.0.1:7005@17005 slave 44c678a77a5181b99f6fe0a5e24a98462d687288 0 1537149964582 5 connected
  5. 88a52d61d4c5f5ee00551a546460697bf9f1c475 127.0.0.1:7006@17006 slave 83e9712ba4520f949119b21a4ebb3de2d66ed2a0 0 1537149965086 6 connected
  6. 83e9712ba4520f949119b21a4ebb3de2d66ed2a0 127.0.0.1:7002@17002 master - 0 1537149965388 2 connected 5461-10922
  7. 44c678a77a5181b99f6fe0a5e24a98462d687288 127.0.0.1:7001@17001 myself,master - 0 1537149963000 1 connected 0-5460

提示:只有master节点才有槽点信息。

至此,cluster高可用分布式集群搭建完成,下一篇将会介绍如何动态添加与删除集群节点。

博客

开源中国博客地址

https://my.oschina.net/gmarshal

个人博客地址

http://blog.foruo.top

欢迎关注我的个人微信订阅号:(据说这个头像程序猿专用)

输入图片说明