您的位置:首页 > 数据库 > Redis

redis 分片集群(cluster)搭建

2016-06-29 00:00 706 查看
摘要: redis 分片集群(cluster)搭建

安装依赖

yum -y install tcl ruby rubygems
gem install redis

安装redis

解压压缩包

tar -zxvf redis-3.2.1.tar.gz
cd redis-3.2.1
make
make install

配置文件

目录结构

[root@director redis]# tree
.
├── m1
│   ├── conf
│   │   └── redis.conf
│   └── db
│       ├── appendonly.aof
│       └── nodes.conf
├── m2
│   ├── conf
│   │   └── redis.conf
│   └── db
│       ├── appendonly.aof
│       └── nodes.conf
└── m3
├── conf
│   └── redis.conf
└── db
├── appendonly.aof
└── nodes.conf

9 directories, 9 files

其中m1、m2、m3代表集群服务的三个中心,如果要保证高可用还可以配置sentinel。

db为数据文件夹appendonly.aof文件为持久化文件,nodes.conf为各个中心分配了hash槽位。

conf为配置文件夹 redis.conf为redis-server的启动配置。

redis-server配置

#M1中心配置
daemonize yes //保证后台运行
pidfile "/media/disk1/redis/m1/run/redis.pid" //后台运行配套

port 6379    //redis端口
cluster-enabled yes    //启用集群
cluster-config-file nodes.conf    //集群配置文件
cluster-node-timeout 5000        //集群节点超时时间
dir "/media/disk1/redis/m1/db"    //集群数据文件存放位置
appendonly yes    //aof持久化

启动三个redis集群中心

[root@director redis]# redis-server /media/disk1/redis/m1/conf/redis.conf
[root@director redis]# redis-server /media/disk1/redis/m2/conf/redis.conf
[root@director redis]# redis-server /media/disk1/redis/m3/conf/redis.conf

nodes.conf

nodes.conf文件采用redis-3.2.1/src/redis-trib.rb ruby脚本生成

[root@director src]# ./redis-trib.rb create 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381
>>> Creating cluster
>>> Performing hash slots allocation on 3 nodes...
Using 3 masters:
127.0.0.1:6379
127.0.0.1:6380
127.0.0.1:6381
M: f860cc18bcfd808539e1b19bb4576a11dd85c413 127.0.0.1:6379
slots:0-5460 (5461 slots) master
M: f2c534f6ec62640b9230bd4680ffb2dabc76e52d 127.0.0.1:6380
slots:5461-10922 (5462 slots) master
M: e84654e8b81a3ecc4636a2d65a9bdbe876cf7f00 127.0.0.1:6381
slots:10923-16383 (5461 slots) master
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 127.0.0.1:6379)
M: f860cc18bcfd808539e1b19bb4576a11dd85c413 127.0.0.1:6379
slots:0-5460 (5461 slots) master
M: f2c534f6ec62640b9230bd4680ffb2dabc76e52d 127.0.0.1:6380
slots:5461-10922 (5462 slots) master
M: e84654e8b81a3ecc4636a2d65a9bdbe876cf7f00 127.0.0.1:6381
slots:10923-16383 (5461 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

集群配置启动完毕

使用redis-cli连接测试

127.0.0.1:6379> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:3
cluster_size:3
cluster_current_epoch:3
cluster_my_epoch:1
cluster_stats_messages_sent:356
cluster_stats_messages_received:356

集群已经启动了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: