Redis 3.0 Cluster集群配置

Redis 3.0 Cluster集群配置

安装环境依赖

安装gcc:yum install gcc
安装zlib:yum install zib
安装ruby:yum install ruby
安装rubygems:yum install rubygems
安装ruby的redis驱动:gem install redis

安装redis

参考:http://www.cnblogs.com/rwxwsblog/p/5285732.html

修改配置文件

vi 6379.conf

port=6379
pidfile /var/run/redis_6379.pid
logfile /var/log/redis_6379.log
daemonize=yes
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 15000
appendonly yes
dir /var/lib/redis/6379

复制相应的配置文件并按上述配置文件修改相应的配置(注意端口和路径)

cp 6379.conf 6380.conf
cp 6379.conf 6381.conf
cp 6379.conf 6382.conf
cp 6379.conf 6383.conf
cp 6379.conf 6384.conf
cp 6379.conf 6385.conf

依次修改相应的配置文件6380.conf6381.conf6382.conf6383.conf6384.conf6385.conf

如:vi 6380.conf

批量替换端口:%s/6379/6380/g并保存

创建数据文件目录

mkdir -p /var/lib/redis/6380 /var/lib/redis/6381 /var/lib/redis/6382 /var/lib/redis/6383 /var/lib/redis/6384 /var/lib/redis/6385

创建集群

  • 安装并配置完后6379端口的redis实例需要重启,否则会报错
  • 创建前需要分别启动redis实例

cp redis-3.0.7/src/redis-trib.rb /usr/local/bin/

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

创建集群实例及异常情况

[root@cas ~]# redis-trib.rb create --replicas 1  127.0.0.1:6379 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
>>> Creating cluster
[ERR] Node 127.0.0.1:6379 is not configured as a cluster node.
[root@cas ~]# ps -ef|grep redis
root     22342     1  0 10:29 ?        00:00:03 /usr/local/bin/redis-server *:6379
root     22456     1  0 10:51 ?        00:00:00 redis-server *:6380 [cluster]
root     22460     1  0 10:51 ?        00:00:00 redis-server *:6381 [cluster]
root     22464     1  0 10:52 ?        00:00:00 redis-server *:6382 [cluster]
root     22470     1  0 10:52 ?        00:00:00 redis-server *:6383 [cluster]
root     22474     1  0 10:52 ?        00:00:00 redis-server *:6384 [cluster]
root     22478     1  0 10:52 ?        00:00:00 redis-server *:6385 [cluster]
root     23530 22364  0 10:58 pts/1    00:00:00 grep --color=auto redis
[root@cas ~]# kill 22342
[root@cas ~]# redis-server /etc/redis/6379.conf 
[root@cas ~]# ps -ef|grep redis
root     22456     1  0 10:51 ?        00:00:00 redis-server *:6380 [cluster]
root     22460     1  0 10:51 ?        00:00:00 redis-server *:6381 [cluster]
root     22464     1  0 10:52 ?        00:00:00 redis-server *:6382 [cluster]
root     22470     1  0 10:52 ?        00:00:00 redis-server *:6383 [cluster]
root     22474     1  0 10:52 ?        00:00:00 redis-server *:6384 [cluster]
root     22478     1  0 10:52 ?        00:00:00 redis-server *:6385 [cluster]
root     23534     1  0 10:59 ?        00:00:05 redis-server *:6379 [cluster]
root     23532 22364  0 10:59 pts/1    00:00:00 grep --color=auto redis
[root@cas ~]# redis-trib.rb create --replicas 1  127.0.0.1:6379 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
>>> Creating cluster
>>> Performing hash slots allocation on 7 nodes...
Using 3 masters:
127.0.0.1:6379
127.0.0.1:6380
127.0.0.1:6381
Adding replica 127.0.0.1:6382 to 127.0.0.1:6379
Adding replica 127.0.0.1:6383 to 127.0.0.1:6380
Adding replica 127.0.0.1:6384 to 127.0.0.1:6381
Adding replica 127.0.0.1:6385 to 127.0.0.1:6379
M: 477a3dad67b2a3ff8bd70e3b3c8a6112eab081e9 127.0.0.1:6379
   slots:0-5460 (5461 slots) master
M: c8b630395c21c10edaf7644bd691ec06f73b9403 127.0.0.1:6380
   slots:5461-10922 (5462 slots) master
M: 0d9eaa1f846081556ceedf41d92939b9f142b698 127.0.0.1:6381
   slots:10923-16383 (5461 slots) master
S: 124283594652936b01287e0c7fda5b9b8b6e15ad 127.0.0.1:6382
   replicates 477a3dad67b2a3ff8bd70e3b3c8a6112eab081e9
S: febaa9b89d0589e36bc7fd66d14173042e2158ed 127.0.0.1:6383
   replicates c8b630395c21c10edaf7644bd691ec06f73b9403
S: 402c2e5d1f9284b09098f43cd9f14821fc7be8cf 127.0.0.1:6384
   replicates 0d9eaa1f846081556ceedf41d92939b9f142b698
S: 51af4cfa2ab1b144cc72058175442ff15808a912 127.0.0.1:6385
   replicates 477a3dad67b2a3ff8bd70e3b3c8a6112eab081e9
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: 477a3dad67b2a3ff8bd70e3b3c8a6112eab081e9 127.0.0.1:6379
   slots:0-5460 (5461 slots) master
M: c8b630395c21c10edaf7644bd691ec06f73b9403 127.0.0.1:6380
   slots:5461-10922 (5462 slots) master
M: 0d9eaa1f846081556ceedf41d92939b9f142b698 127.0.0.1:6381
   slots:10923-16383 (5461 slots) master
M: 124283594652936b01287e0c7fda5b9b8b6e15ad 127.0.0.1:6382
   slots: (0 slots) master
   replicates 477a3dad67b2a3ff8bd70e3b3c8a6112eab081e9
M: febaa9b89d0589e36bc7fd66d14173042e2158ed 127.0.0.1:6383
   slots: (0 slots) master
   replicates c8b630395c21c10edaf7644bd691ec06f73b9403
M: 402c2e5d1f9284b09098f43cd9f14821fc7be8cf 127.0.0.1:6384
   slots: (0 slots) master
   replicates 0d9eaa1f846081556ceedf41d92939b9f142b698
M: 51af4cfa2ab1b144cc72058175442ff15808a912 127.0.0.1:6385
   slots: (0 slots) master
   replicates 477a3dad67b2a3ff8bd70e3b3c8a6112eab081e9
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

检查redis集群的健康状况

[root@cas ~]# redis-trib.rb check 127.0.0.1:6379
>>> Performing Cluster Check (using node 127.0.0.1:6379)
M: 477a3dad67b2a3ff8bd70e3b3c8a6112eab081e9 127.0.0.1:6379
   slots:0-5460 (5461 slots) master
   2 additional replica(s)
S: 402c2e5d1f9284b09098f43cd9f14821fc7be8cf 127.0.0.1:6384
   slots: (0 slots) slave
   replicates 0d9eaa1f846081556ceedf41d92939b9f142b698
M: c8b630395c21c10edaf7644bd691ec06f73b9403 127.0.0.1:6380
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: 0d9eaa1f846081556ceedf41d92939b9f142b698 127.0.0.1:6381
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 124283594652936b01287e0c7fda5b9b8b6e15ad 127.0.0.1:6382
   slots: (0 slots) slave
   replicates 477a3dad67b2a3ff8bd70e3b3c8a6112eab081e9
S: febaa9b89d0589e36bc7fd66d14173042e2158ed 127.0.0.1:6383
   slots: (0 slots) slave
   replicates c8b630395c21c10edaf7644bd691ec06f73b9403
S: 51af4cfa2ab1b144cc72058175442ff15808a912 127.0.0.1:6385
   slots: (0 slots) slave
   replicates 477a3dad67b2a3ff8bd70e3b3c8a6112eab081e9
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

管理cluster

添加节点

add-node的使用方法为new_host:new_port existing_host:existing_port,前面是新添加的节点信息,后面是已存在的节点信息
redis-trib.rb add-node 192.168.3.61:6379 192.168.3.88:6379

数据分片及删除节点

参考:http://blog.sina.com.cn/s/blog_75ad98f30102w6po.html

参考文档:

  • http://blog.sina.com.cn/s/blog_75ad98f30102w6po.html
  • http://jingyan.baidu.com/article/922554465f7e19851648f4d4.html
  • http://www.cnblogs.com/wuxl360/p/5920330.html
    

你可能感兴趣的:(Redis 3.0 Cluster集群配置)