操作系统环境

[root@62290133db0c ~]# cat /etc/issue
CentOS release 6.7 (Final)
Kernel \r on an \m
[root@62290133db0c ~]# getconf LONG_BIT
64


redis安装请参考redis安装一遍博文,redis集群是在3.0中推出的集群功能...........


由于资源有限,只好在一台机器上搭建redis集群环境,redis版本3.0.6,要让集群正常运作至少需要三个主节点,不过在刚开始试用集群功能时, 强烈建议使用六个节点: 其中三个为主节点, 而其余三个则是各个主节点的从节点。


redis
dirport
6381/opt/63816381
6382/opt/63826382
6383/opt/63836383
6384/opt/63846384
6385/opt/63856385
6386/opt/63866386


创建redis相应的目录

[root@62290133db0c ~]# mkdir /opt/638{1..6}
[root@62290133db0c ~]# tree /opt/
/opt/
├── 6381
├── 6382
├── 6383
├── 6384
├── 6385
└── 6386
6 directories, 0 files


拷贝配置文件以及redis启动redis-server文件

[root@62290133db0c ~]# cp /usr/local/src/redis-3.0.6/redis.conf /opt/6381/
[root@62290133db0c ~]# cp /usr/local/src/redis-3.0.6/src/redis-server /opt/6381/
[root@62290133db0c ~]# cp /usr/local/src/redis-3.0.6/src/redis-server /opt/6382/
[root@62290133db0c ~]# cp /usr/local/src/redis-3.0.6/src/redis-server /opt/6383/
[root@62290133db0c ~]# cp /usr/local/src/redis-3.0.6/src/redis-server /opt/6384/
[root@62290133db0c ~]# cp /usr/local/src/redis-3.0.6/src/redis-server /opt/6385/
[root@62290133db0c ~]# cp /usr/local/src/redis-3.0.6/src/redis-server /opt/6386/
[root@62290133db0c ~]# cd /opt/6381/


6381配置文件

[root@62290133db0c 6381]# vim redis.conf
daemonize yes
pidfile /var/run/redis-6381.pid
port 6381
tcp-keepalive 60
logfile "/var/log/redis/6381.log"
dbfilename dump.rdb
dir /opt/6381/
slave-serve-stale-data yes
appendonly yes
appendfilename "appendonly-6381.aof"
appendfsync everysec
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000


拷贝redis配置文件到其他redis对应的目录下,并修改redis的配置文件

[root@62290133db0c ~]# cp /opt/6381/redis.conf /opt/6382/
[root@62290133db0c ~]# cp /opt/6381/redis.conf /opt/6383/
[root@62290133db0c ~]# cp /opt/6381/redis.conf /opt/6384/
[root@62290133db0c ~]# cp /opt/6381/redis.conf /opt/6385/
[root@62290133db0c ~]# cp /opt/6381/redis.conf /opt/6386/
[root@62290133db0c ~]# sed -i "s#6381#6382#g" /opt/6382/redis.conf 
[root@62290133db0c ~]# sed -i "s#6381#6383#g" /opt/6383/redis.conf 
[root@62290133db0c ~]# sed -i "s#6381#6384#g" /opt/6384/redis.conf 
[root@62290133db0c ~]# sed -i "s#6381#6385#g" /opt/6385/redis.conf 
[root@62290133db0c ~]# sed -i "s#6381#6386#g" /opt/6386/redis.conf



分别启动6381..6386

[root@62290133db0c ~]# /opt/6381/redis-server /opt/6381/redis.conf 
[root@62290133db0c ~]# /opt/6382/redis-server /opt/6382/redis.conf
[root@62290133db0c ~]# /opt/6383/redis-server /opt/6383/redis.conf 
[root@62290133db0c ~]# /opt/6384/redis-server /opt/6384/redis.conf 
[root@62290133db0c ~]# /opt/6385/redis-server /opt/6385/redis.conf 
[root@62290133db0c ~]# /opt/6386/redis-server /opt/6386/redis.conf


查看redis进程

[root@62290133db0c ~]# ps -ef|grep redis|grep -v grep 
root        809      1  0 09:17 ?        00:00:00 /opt/6381/redis-server *:6381 [cluster]    
root        815      1  0 09:18 ?        00:00:00 /opt/6382/redis-server *:6382 [cluster]    
root        821      1  0 09:18 ?        00:00:00 /opt/6383/redis-server *:6383 [cluster]    
root        825      1  0 09:18 ?        00:00:00 /opt/6384/redis-server *:6384 [cluster]    
root        829      1  0 09:18 ?        00:00:00 /opt/6385/redis-server *:6385 [cluster]    
root        833      1  0 09:18 ?        00:00:00 /opt/6386/redis-server *:6386 [cluster]
注:以上只是把各个redis节点启动成功


创建redis集群

[root@62290133db0c ~]# redis-trib.rb create ----replicas 1 172.17.0.1:6381 172.17.0.1:6382 172.17.0.1:6383 172.17.0.1:6384 172.17.0.1:6385 172.17.0.1:6386
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
172.17.0.1:6381
172.17.0.1:6382
172.17.0.1:6383
Adding replica 172.17.0.1:6384 to 172.17.0.1:6381
Adding replica 172.17.0.1:6385 to 172.17.0.1:6382
Adding replica 172.17.0.1:6386 to 172.17.0.1:6383
M: d3b7db6b7b2696be3e2dbb74d99d33e6a3b31658 172.17.0.1:6381
   slots:0-5460 (5461 slots) master
M: be37cf292b638d919e9e39fc3ebe3984697f6026 172.17.0.1:6382
   slots:5461-10922 (5462 slots) master
M: 5e5f54956d424ac491820f10ef0e8be443e1d98f 172.17.0.1:6383
   slots:10923-16383 (5461 slots) master
S: ddf29c1716a7b111f26cfa2376edbf84b7e4f471 172.17.0.1:6384
   replicates d3b7db6b7b2696be3e2dbb74d99d33e6a3b31658
S: 54c405412761e7714062260c7080e0f904d04cfe 172.17.0.1:6385
   replicates be37cf292b638d919e9e39fc3ebe3984697f6026
S: b0d8b47bf039f65e452a661613d97f62ba569dfb 172.17.0.1:6386
   replicates 5e5f54956d424ac491820f10ef0e8be443e1d98f
Can I set the above configuration? (type 'yes' to accept): yes //同意输入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 172.17.0.1:6381)
M: d3b7db6b7b2696be3e2dbb74d99d33e6a3b31658 172.17.0.1:6381
   slots:0-5460 (5461 slots) master
M: be37cf292b638d919e9e39fc3ebe3984697f6026 172.17.0.1:6382
   slots:5461-10922 (5462 slots) master
M: 5e5f54956d424ac491820f10ef0e8be443e1d98f 172.17.0.1:6383
   slots:10923-16383 (5461 slots) master
M: ddf29c1716a7b111f26cfa2376edbf84b7e4f471 172.17.0.1:6384
   slots: (0 slots) master
   replicates d3b7db6b7b2696be3e2dbb74d99d33e6a3b31658
M: 54c405412761e7714062260c7080e0f904d04cfe 172.17.0.1:6385
   slots: (0 slots) master
   replicates be37cf292b638d919e9e39fc3ebe3984697f6026
M: b0d8b47bf039f65e452a661613d97f62ba569dfb 172.17.0.1:6386
   slots: (0 slots) master
   replicates 5e5f54956d424ac491820f10ef0e8be443e1d98f
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.


登陆任意redis节点

[root@62290133db0c ~]# redis-cli -c -p 6381
127.0.0.1:6381> CLUSTER NODES //各个节点以及主从对应的信息
54c405412761e7714062260c7080e0f904d04cfe 172.17.0.1:6385 slave be37cf292b638d919e9e39fc3ebe3984697f6026 0 1451889401026 5 connected
5e5f54956d424ac491820f10ef0e8be443e1d98f 172.17.0.1:6383 master - 0 1451889401529 3 connected 10923-16383
ddf29c1716a7b111f26cfa2376edbf84b7e4f471 172.17.0.1:6384 slave d3b7db6b7b2696be3e2dbb74d99d33e6a3b31658 0 1451889400017 4 connected
d3b7db6b7b2696be3e2dbb74d99d33e6a3b31658 172.17.0.1:6381 myself,master - 0 0 1 connected 0-5460
b0d8b47bf039f65e452a661613d97f62ba569dfb 172.17.0.1:6386 slave 5e5f54956d424ac491820f10ef0e8be443e1d98f 0 1451889402033 6 connected
be37cf292b638d919e9e39fc3ebe3984697f6026 172.17.0.1:6382 master - 0 1451889400522 2 connected 5461-10922


登陆redis 6381

[root@62290133db0c ~]# redis-cli -c -p 6381
127.0.0.1:6381> set name zxl
-> Redirected to slot [5798] located at 172.17.0.1:6382
OK
172.17.0.1:6382> set age 33
-> Redirected to slot [741] located at 172.17.0.1:6381
OK
172.17.0.1:6381> KEYS *
1) "age"
172.17.0.1:6381> GET age
"33"
172.17.0.1:6381> 
[root@62290133db0c ~]# redis-cli -c -p 6382
127.0.0.1:6382> KEYS *
1) "name"
127.0.0.1:6382> get name
"zxl"
127.0.0.1:6382>

注:以上就是redis集群创建



创建redis集群时,错误总结如下:

[root@62290133db0c ~]# cp /usr/local/src/redis-3.0.6/src/redis-trib.rb /usr/local/bin/
[root@62290133db0c ~]# redis-trib.rb create --replicas 1 172.17.0.1:6381 172.17.0.1:6382 172.17.0.1:6383 172.17.0.1:6384 172.17.0.1:6385 172.17.0.1:6386
/usr/bin/env: ruby: No such file or directory

注:缺少ruby环境,需安装

[root@62290133db0c ~]# yum install ruby -y


提示如下:

[root@62290133db0c ~]# redis-trib.rb create --replicas 1 172.17.0.1:6381 172.17.0.1:6382 172.17.0.1:6383 172.17.0.1:6384 172.17.0.1:6385 172.17.0.1:6386
/usr/local/bin/redis-trib.rb:24:in `require': no such file to load -- rubygems (LoadError)
from /usr/local/bin/redis-trib.rb:24

注:需安装rubygems

[root@62290133db0c ~]# yum install rubygems -y


提示如下:

[root@62290133db0c ~]# redis-trib.rb create --replicas 1 172.17.0.1:6381 172.17.0.1:6382 172.17.0.1:6383 172.17.0.1:6384 172.17.0.1:6385 172.17.0.1:6386
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- redis (LoadError)
from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
from /usr/local/bin/redis-trib.rb:25


注:提示不能加载redis,是因为缺少redis和ruby的接口,使用gem安装

[root@62290133db0c ~]# gem install redis
Successfully installed redis-3.2.1
1 gem installed
Installing ri documentation for redis-3.2.1...
Installing RDoc documentation for redis-3.2.1...