redis cluster 搭建
redis cluster 是 redis 官方一个高可用解决方案,cluster 中 redis 共有 2^14 (16384)个slot 槽。创建 cluster 后会平均分配到每个 redis 上。cluster 可以对标 分片机制。
这里介绍为本机启动6个redis集群服务,并使用 redis-trib.rb 创建 3主3从的 cluster 。
主要步骤:
1. 创建 redis 各实例目录
2. 配置 redis 各实例配置文件
3. 启动 redis 各实例
4. 执行 redis cluster 脚本搭建集群
与普通 redis 实例不同之处在于 2和4步骤 配置文件和执行 redis cluster 脚本搭建集群,详细如下:
1、redis 服务数据目录
mkdir cluster cd cluster mkdir 6400 6401 6402 6403 6404 6405
2、配置文件
vim 6400/redis.conf
port 6400 dir "/usr/local/redis-3.2.9/cluster/6400" pidfile "/usr/local/redis-3.2.9/cluster/6400/redis.pid" logfile "/usr/local/redis-3.2.9/cluster/6400/redis.log" cluster-enabled yes cluster-config-file nodes-6400.conf appendonly yes dbfilename "dump.rdb" bind 192.168.33.11 daemonize yes
对应其他 5个配置文件,
cp 6400/redis.conf 6401/redis.conf && sed -i "s/6400/6401/g" 6401/redis.conf ... cp 6400/redis.conf 6405/redis.conf && sed -i "s/6400/6405/g" 6405/redis.conf
3、启动 redis 服务
./src/redis-server cluster/6400/redis.conf ... ./src/redis-server cluster/6405/redis.conf
4、创建 cluster 集群
redis 很贴心的自带提供 ruby 脚本< ./src/redis-trib.rb > 创建集群,但是需要安装 ruby 相关信息
安装ruby:yum install ruby 安装rubygems:yum install rubygems 安装ruby的redis驱动:gem install redis 可能还需要安装: 安装gcc:yum install gcc 安装zlib:yum install zib
搭建集群了,很简单,执行命令:
./src/redis-trib.rb create --replicas 1 192.168.33.11:6400 192.168.33.11:6401 192.168.33.11:6402 192.168.33.11:6403 192.168.33.11:6404 192.168.33.11:6405
以上命令得到如下结果
>>> Creating cluster >>> Performing hash slots allocation on 7 nodes... Using 3 masters: 192.168.33.11:6400 192.168.33.11:6401 192.168.33.11:6402 Adding replica 192.168.33.11:6403 to 192.168.33.11:6400 Adding replica 192.168.33.11:6404 to 192.168.33.11:6401 Adding replica 192.168.33.11:6405 to 192.168.33.11:6402 M: d53cbf49fea9403bd4bb4835c45679d8014c8a11 192.168.33.11:6400 slots:0-5460 (5461 slots) master M: da9ffc92d337467975063ac7b82b9b6895b1c93b 192.168.33.11:6401 slots:5461-10922 (5462 slots) master M: df494c2c1152d2125ab92148fe772cf92e0e8abd 192.168.33.11:6402 slots:10923-16383 (5461 slots) master S: d0296ce856cfe94b7aad0600a7d16c9dc3995608 192.168.33.11:6403 replicates d53cbf49fea9403bd4bb4835c45679d8014c8a11 S: db28b4113c7faeba02d8f9c5e821d7de00df2f5f 192.168.33.11:6404 replicates da9ffc92d337467975063ac7b82b9b6895b1c93b S: 03abf7aaa2b4d3399b26c64d476f1a561215366f 192.168.33.11:6405 replicates df494c2c1152d2125ab92148fe772cf92e0e8abd Can I set the above configuration? (type 'yes' to accept): yes >>> Nodes configuration updated >>> Assign a different config epoch to each node >>> Sending CLUSTER MEET messages to join the cluster Waiting for the cluster to join... >>> Performing Cluster Check (using node 192.168.33.11:6400) M: d53cbf49fea9403bd4bb4835c45679d8014c8a11 192.168.33.11:6400 slots:0-5460 (5461 slots) master 2 additional replica(s) S: 03abf7aaa2b4d3399b26c64d476f1a561215366f 192.168.33.11:6405 slots: (0 slots) slave replicates df494c2c1152d2125ab92148fe772cf92e0e8abd M: da9ffc92d337467975063ac7b82b9b6895b1c93b 192.168.33.11:6401 slots:5461-10922 (5462 slots) master 1 additional replica(s) S: db28b4113c7faeba02d8f9c5e821d7de00df2f5f 192.168.33.11:6404 slots: (0 slots) slave replicates da9ffc92d337467975063ac7b82b9b6895b1c93b M: df494c2c1152d2125ab92148fe772cf92e0e8abd 192.168.33.11:6402 slots:10923-16383 (5461 slots) master 1 additional replica(s) S: d0296ce856cfe94b7aad0600a7d16c9dc3995608 192.168.33.11:6403 slots: (0 slots) slave replicates d53cbf49fea9403bd4bb4835c45679d8014c8a11 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
最后查看集群信息
./src/redis-cli -h 192.168.33.11 -p 6400
参考:
http://www.cnblogs.com/rwxwsblog/p/6030606.html
http://www.cnblogs.com/junl0513/p/833f4d3eefc69485958fe83b5c9057d7.html
http://blog.51yip.com/nosql/1726.html 【cluster 命令行管理】