Redis集群搭建

参考文档:

http://redis.readthedocs.org/en/latest/topic/cluster-tutorial.html
http://no-fucking-idea.com/blog/2012/04/16/setting-up-redis-cluster/
http://blog.csdn.net/xu470438000/article/details/42972123
http://blog.csdn.net/xu470438000/article/details/42971091

 

大致过程:

#安装reids-3.0.0.-rc3(最新支持集群的版本)
tar -zxf redis-3.0.0-rc3.tar.gz
cd redis-3.0.0-rc3/
make
make install

mkdir ~/redis-cluster
cp *.conf ~/redis-cluster
cp src/redis-cli src/redis-server src/redis-sentinel src/redis-trib.rb ~/redis-cluster

#优化下系统参数,官方推荐
echo "vm.overcommit_memory = 1" >>/etc/sysctl.conf
sysctl vm.overcommit_memory = 1

#配置redis运行环境(目录结构)
cd ~/redis-cluster
mkdir -p 6380/conf 6380/logs
mkdir -p 6381/conf 6381/logs
mkdir -p 6382/conf 6382/logs
mkdir -p 6383/conf 6383/logs
mkdir -p 6384/conf 6384/logs
mkdir -p 6385/conf 6385/logs

#创建redis运行所需的配置文件,后面会提到具体的修改
cp redis.conf 6380/conf
cp redis.conf 6381/conf
cp redis.conf 6382/conf
cp redis.conf 6383/conf
cp redis.conf 6384/conf
cp redis.conf 6385/conf

#创建空的集群配置文件,redis到时会自动修改
touch 6380/conf/nodes-6380.conf
touch 6381/conf/nodes-6381.conf
touch 6382/conf/nodes-6382.conf
touch 6383/conf/nodes-6383.conf
touch 6384/conf/nodes-6384.conf
touch 6385/conf/nodes-6385.conf

#修改redis.conf,这里给出一个示例,其他参考修改即可
vi 6380/conf/redis.conf

./redis-server 6380/conf/redis.conf
./redis-server 6381/conf/redis.conf
./redis-server 6382/conf/redis.conf
./redis-server 6383/conf/redis.conf
./redis-server 6384/conf/redis.conf
./redis-server 6385/conf/redis.conf

#这部分是为了防止运行redis-trib.rb时出错而做的准备
yum -y install ruby
yum -y install rubygems
gem install redis

#创建集群
./redis-trib.rb create --replicas 1 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 127.0.0.1:6385

#集群中节点的主从关系如下:
master <-> slave
6380 <-> 6383
6381 <-> 6384
6382 <-> 6385

#主从关系设置完成后,就会后台执行异步复制。
#当发生主节点宕机时,从节点会自动接管其角色,后期主节点启动后会自动转换为slave角色(也就是故障时会发生主从角色互换)
#默认情况下,比如像上述创建的集群:16384个hashslot被三等分,即三个master分别管理的slot范围是0-54605461-1092210923-16383。
#而key在集群中的落点公式为:slot = crc16(key) % 16384,应用中应自行解决对于key-value读写节点选择的问题。

 

啊,好像忘了贴redis.conf的配置实例(@http://no-fucking-idea.com/blog/2012/04/16/setting-up-redis-cluster/):

daemonize yes
timeout 0
loglevel notice
logfile "/root/redis-cluster/6385/logs/redis.log"
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /root/redis-cluster/6385
slave-serve-stale-data yes
slave-read-only yes
appendonly no
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
lua-time-limit 5000

# Cluster
#
pidfile /root/redis-cluster/6385/logs/redis.pid
port 6385
cluster-enabled yes
cluster-config-file /root/redis-cluster/6385/conf/nodes-6385.conf
cluster-node-timeout 5000

slowlog-log-slower-than 10000
slowlog-max-len 1024
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60

 

posted @ 2015-02-07 14:11  lichmama  阅读(432)  评论(0编辑  收藏  举报