安装redis之后,进入usr/local/目录
[root@VM_0_7_centos local]# ll
total 52
drwxr-xr-x. 2 root root 4096 Sep 23 2011 bin
drwxr-xr-x. 2 root root 4096 Sep 23 2011 etc
drwxr-xr-x. 2 root root 4096 Sep 23 2011 games
drwxr-xr-x. 2 root root 4096 Sep 23 2011 include
drwxr-xr-x. 2 root root 4096 Sep 23 2011 lib
drwxr-xr-x. 2 root root 4096 Sep 23 2011 lib64
drwxr-xr-x. 2 root root 4096 Sep 23 2011 libexec
drwxr-xr-x 11 root root 4096 Apr 15 16:46 nginx
drwxr-xr-x 7 root root 4096 Mar 28 22:52 qcloud
drwxr-xr-x 3 root root 4096 Apr 18 23:14 redis
drwxr-xr-x. 2 root root 4096 Sep 23 2011 sbin
drwxr-xr-x. 5 root root 4096 Nov 9 2016 share
drwxr-xr-x. 2 root root 4096 Sep 23 2011 src
创建redis-cluster文件夹
[root@VM_0_7_centos local]# mkdir redis-cluster
复制安装的redis到redis-cluster文件夹下,命名为redis01
[root@VM_0_7_centos local]# cp -r redis/bin/ redis-cluster/redis01
[root@VM_0_7_centos local]# cd redis-cluster/redis01/
[root@VM_0_7_centos redis01]# ll
total 13836
-rw-r--r-- 1 root root 18 Apr 18 23:27 dump.rdb
-rwxr-xr-x 1 root root 4170583 Apr 18 23:27 redis-benchmark
-rwxr-xr-x 1 root root 22225 Apr 18 23:27 redis-check-aof
-rwxr-xr-x 1 root root 45459 Apr 18 23:27 redis-check-dump
-rwxr-xr-x 1 root root 4243019 Apr 18 23:27 redis-cli
-rw-r--r-- 1 root root 29602 Apr 18 23:27 redis.conf
-rwxr-xr-x 1 root root 5640026 Apr 18 23:27 redis-server
[root@VM_0_7_centos redis01]# rm -rf dump.rdb
使用同样命令,共复制6个到redis-cluster文件夹下,然后修改redis.conf配置文件,port从7001至7006,cluster-enabled yes去掉注释#
port 7001
cluster-enabled yes
编辑启动所有redis的脚本,内容如下:
[root@VM_0_7_centos redis-cluster]# vim start-all.sh
cd redis01
./redis-server redis.conf
cd ..
cd redis02
./redis-server redis.conf
cd ..
cd redis03
./redis-server redis.conf
cd ..
cd redis04
./redis-server redis.conf
cd ..
cd redis05
./redis-server redis.conf
cd ..
cd redis06
./redis-server redis.conf
cd ..
给start-all.sh赋予权限
-rw-r--r-- 1 root root 258 Apr 19 00:12 start-all.sh
[root@VM_0_7_centos redis-cluster]# chmod +x start-all.sh
-rwxr-xr-x 1 root root 258 Apr 19 00:12 start-all.sh
执行start-all.sh启动所有redis,ps aux|grep redis查看启动情况
[root@VM_0_7_centos redis-cluster]# ./start-all.sh
[root@VM_0_7_centos redis-cluster]# ps aux|grep redis
root 1772 0.0 0.5 139004 5184 pts/2 S+ Apr18 0:00 vim redis.conf
root 7802 0.0 0.2 137448 2508 ? Ssl 00:14 0:00 ./redis-server *:7001 [cluster]
root 7804 0.0 0.2 137448 2516 ? Ssl 00:14 0:00 ./redis-server *:7002 [cluster]
root 7809 0.0 0.2 137448 2512 ? Ssl 00:14 0:00 ./redis-server *:7003 [cluster]
root 7814 0.0 0.2 137448 2512 ? Ssl 00:14 0:00 ./redis-server *:7004 [cluster]
root 7818 0.0 0.2 137448 2512 ? Ssl 00:14 0:00 ./redis-server *:7005 [cluster]
root 7821 0.0 0.2 137448 2516 ? Ssl 00:14 0:00 ./redis-server *:7006 [cluster]
root 7870 0.0 0.0 103312 876 pts/3 S+ 00:14 0:00 grep redis
安装ruby
[root@VM_0_7_centos redis-cluster]# yum install ruby
安装redis-3.0.0.gem
[root@VM_0_7_centos developer]# ll
total 2432
-rwxr-xr-x 1 root root 1044786 Apr 8 23:11 redis-2.8.0-linux-mac.tar.gz
drwxrwxr-x 6 root root 4096 Apr 1 2015 redis-3.0.0
-rwxr-xr-x 1 root root 57856 Apr 19 00:19 redis-3.0.0.gem
-rw-r--r-- 1 root root 1358081 Apr 1 2015 redis-3.0.0.tar.gz
drwxr-xr-x 4 root root 4096 Apr 15 16:31 setup
[root@VM_0_7_centos developer]# gem install -l redis-3.0.0.gem
进入src目录,复制redis-trib.rb到redis-cluster文件夹下
[root@VM_0_7_centos redis-3.0.0]# cd src/
[root@VM_0_7_centos src]# ll *.rb
-rwxrwxr-x 1 root root 48141 Apr 1 2015 redis-trib.rb
[root@VM_0_7_centos src]# cp *.rb /usr/local/redis-cluster/
查看复制后的结果
[root@VM_0_7_centos src]# cd /usr/local/redis-cluster/
[root@VM_0_7_centos redis-cluster]# ll
total 76
drwxr-xr-x 2 root root 4096 Apr 19 00:14 redis01
drwxr-xr-x 2 root root 4096 Apr 19 00:14 redis02
drwxr-xr-x 2 root root 4096 Apr 19 00:14 redis03
drwxr-xr-x 2 root root 4096 Apr 19 00:14 redis04
drwxr-xr-x 2 root root 4096 Apr 19 00:14 redis05
drwxr-xr-x 2 root root 4096 Apr 19 00:14 redis06
-rwxr-xr-x 1 root root 48141 Apr 19 22:22 redis-trib.rb
-rwxr-xr-x 1 root root 258 Apr 19 00:12 start-all.sh
执行如下命令,创建集群
[root@VM_0_7_centos redis-cluster]# ./redis-trib.rb create --replicas 1 193.122.165.122:7001 193.122.165.122:7002 193.122.165.122:7003 193.122.165.122:7004 193.122.165.122:7005 193.122.165.122:7006
>>> Creating cluster
Connecting to node 193.122.165.122:7001: OK
Connecting to node 193.122.165.122:7002: OK
Connecting to node 193.122.165.122:7003: OK
Connecting to node 193.122.165.122:7004: OK
Connecting to node 193.122.165.122:7005: OK
Connecting to node 193.122.165.122:7006: OK
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
193.122.165.122:7001
193.122.165.122:7002
193.122.165.122:7003
Adding replica 193.122.165.122:7004 to 193.122.165.122:7001
Adding replica 193.122.165.122:7005 to 193.122.165.122:7002
Adding replica 193.122.165.122:7006 to 193.122.165.122:7003
M: dddb9dec17b997493f821e4e4ba796ef8c5a6ca1 193.122.165.122:7001
slots:0-5460 (5461 slots) master
M: a3cdf7d4bff4502c01eb2e8100d37458928f58c2 193.122.165.122:7002
slots:5461-10922 (5462 slots) master
M: 8ce5dd75360144b9828a84030adbb2563984c71c 193.122.165.122:7003
slots:10923-16383 (5461 slots) master
S: e8f2b4b99b6327a86d34aca3b0394528c250a1f9 193.122.165.122:7004
replicates dddb9dec17b997493f821e4e4ba796ef8c5a6ca1
S: db9e52a1c1479a59a1489e4a741c22446f0c356f 193.122.165.122:7005
replicates a3cdf7d4bff4502c01eb2e8100d37458928f58c2
S: 2b6f421178c70026f6de36b84df6a1c401dfc646 193.122.165.122:7006
replicates 8ce5dd75360144b9828a84030adbb2563984c71c
Can I set the above configuration? (type 'yes' to accept):
输入yes
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 193.122.165.122:7001)
M: dddb9dec17b997493f821e4e4ba796ef8c5a6ca1 193.122.165.122:7001
slots:0-5460 (5461 slots) master
M: a3cdf7d4bff4502c01eb2e8100d37458928f58c2 193.122.165.122:7002
slots:5461-10922 (5462 slots) master
M: 8ce5dd75360144b9828a84030adbb2563984c71c 193.122.165.122:7003
slots:10923-16383 (5461 slots) master
M: e8f2b4b99b6327a86d34aca3b0394528c250a1f9 193.122.165.122:7004
slots: (0 slots) master
replicates dddb9dec17b997493f821e4e4ba796ef8c5a6ca1
M: db9e52a1c1479a59a1489e4a741c22446f0c356f 193.122.165.122:7005
slots: (0 slots) master
replicates a3cdf7d4bff4502c01eb2e8100d37458928f58c2
M: 2b6f421178c70026f6de36b84df6a1c401dfc646 193.122.165.122:7006
slots: (0 slots) master
replicates 8ce5dd75360144b9828a84030adbb2563984c71c
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
使用redis-cli连接集群
[root@VM_0_7_centos redis-cluster]# redis01/redis-cli -p 7006 -c
127.0.0.1:7006>
设置值进行测试
127.0.0.1:7006> set key1 123
-> Redirected to slot [9189] located at 193.122.165.122:7002
OK
193.122.165.122:7002> keys *
1) "key1"
193.122.165.122:7002> set key2 abc
-> Redirected to slot [4998] located at 193.122.165.122:7001
OK
193.122.165.122:7001> keys *
1) "key2"
cluster info 查看集群信息
193.122.165.122:7001> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_sent:603
cluster_stats_messages_received:603
cluster nodes查看节点
193.122.165.122:7001> cluster nodes
e8f2b4b99b6327a86d34aca3b0394528c250a1f9 193.122.165.122:7004 slave dddb9dec17b997493f821e4e4ba796ef8c5a6ca1 0 1524148388903 4 connected
db9e52a1c1479a59a1489e4a741c22446f0c356f 193.122.165.122:7005 slave a3cdf7d4bff4502c01eb2e8100d37458928f58c2 0 1524148387901 5 connected
a3cdf7d4bff4502c01eb2e8100d37458928f58c2 193.122.165.122:7002 master - 0 1524148385898 2 connected 5461-10922
8ce5dd75360144b9828a84030adbb2563984c71c 193.122.165.122:7003 master - 0 1524148389906 3 connected 10923-16383
dddb9dec17b997493f821e4e4ba796ef8c5a6ca1 172.16.0.7:7001 myself,master - 0 0 1 connected 0-5460
2b6f421178c70026f6de36b84df6a1c401dfc646 193.122.165.122:7006 slave 8ce5dd75360144b9828a84030adbb2563984c71c 0 1524148386900 6 connected