系统环境:CentOS release 7.2_x86_64
软件环境:redis-3.0.5
机器列表:
redis1 192.168.10.238
redis2 192.168.10.239
redis3 192.168.10.240
redis4 192.168.10.241
redis5 192.168.10.242
redis6 192.168.10.243
1、安装ruby环境依赖
yum install ruby rubygems
2、安装redis集群管理器
gem install redis
Fetching: redis-3.3.1.gem (100%)
Successfully installed redis-3.3.1
Parsing documentation for redis-3.3.1
Installing ri documentation for redis-3.3.1
1 gem installed
3、cp redis源码包/src/redis-trib.rb /usr/local/redis/bin/redis-trib
4、在置文件中添加
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
5、启动服务
redis1 # /etc/init.d/redis start
redis2 # /etc/init.d/redis start
redis3 # /etc/init.d/redis start
redis4 # /etc/init.d/redis start
redis5 # /etc/init.d/redis start
redis6 # /etc/init.d/redis start
6、ps -aux | grep redis #查看服务是否是以集群方式启动
[root@redis1 ~]# ps -aux | grep redis
root 9257 0.1 0.2 131620 2364 ? Ssl 16:29 0:00 /usr/local/redis/bin/redis-server *:6379 [cluster]
root 9266 0.0 0.0 112656 968 pts/0 S+ 16:30 0:00 grep --color=auto redis
7、创建集群(注:redis集群必须要有3个主节点)
[root@redis1 bin]# ./redis-trib.rb create --replicas 1 192.168.10.238:6379 192.168.10.239:6379 192.168.10.240:6379 192.168.10.241:6379 192.168.10.242:6379 192.168.10.243:6379
>>> Creating cluster
Connecting to node 192.168.10.238:6379: OKConnecting to node 192.168.10.239:6379: OK
Connecting to node 192.168.10.240:6379: OKConnecting to node 192.168.10.241:6379: OK
Connecting to node 192.168.10.242:6379: OKConnecting to node 192.168.10.243:6379: OK
>>> Performing hash slots allocation on 6 nodes...Using 3 masters:
192.168.10.238:6379192.168.10.239:6379
192.168.10.240:6379Adding replica 192.168.10.241:6379 to 192.168.10.238:6379
Adding replica 192.168.10.242:6379 to 192.168.10.239:6379Adding replica 192.168.10.243:6379 to 192.168.10.240:6379
M: 995cdef393a899c14cc79a7b103d137264f97e78 192.168.10.238:6379 slots:0-5460 (5461 slots) master
M: 3a84bb12dd9f7fb5f86a204e21b3074579364b12 192.168.10.239:6379 slots:5461-10922 (5462 slots) master
M: 4d5b650e5c23dc55befd72a4005e17196d7f4bf0 192.168.10.240:6379 slots:10923-16383 (5461 slots) master
S: 86baaa60272094b9558e9194f5740a5d1c2608ac 192.168.10.241:6379 replicates 995cdef393a899c14cc79a7b103d137264f97e78
S: 69e268a3a0b4d263c465a481ad1b3cc53f863982 192.168.10.242:6379 replicates 3a84bb12dd9f7fb5f86a204e21b3074579364b12
S: dd4019452121683ebb838f789abeb3755f4bc55e 192.168.10.243:6379 replicates 4d5b650e5c23dc55befd72a4005e17196d7f4bf0
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 192.168.10.238:6379)
M: 995cdef393a899c14cc79a7b103d137264f97e78 192.168.10.238:6379 slots:0-5460 (5461 slots) master
M: 3a84bb12dd9f7fb5f86a204e21b3074579364b12 192.168.10.239:6379 slots:5461-10922 (5462 slots) master
M: 4d5b650e5c23dc55befd72a4005e17196d7f4bf0 192.168.10.240:6379 slots:10923-16383 (5461 slots) master
M: 86baaa60272094b9558e9194f5740a5d1c2608ac 192.168.10.241:6379 slots: (0 slots) master
replicates 995cdef393a899c14cc79a7b103d137264f97e78M: 69e268a3a0b4d263c465a481ad1b3cc53f863982 192.168.10.242:6379
slots: (0 slots) master replicates 3a84bb12dd9f7fb5f86a204e21b3074579364b12
M: dd4019452121683ebb838f789abeb3755f4bc55e 192.168.10.243:6379 slots: (0 slots) master
replicates 4d5b650e5c23dc55befd72a4005e17196d7f4bf0[OK] All nodes agree about slots configuration.
>>> Check for open slots...>>> Check slots coverage...
[OK] All 16384 slots covered.
8、查看集群状态
[root@redis1 bin]# ./redis-cli -c -h 192.168.10.242 -p 6379
192.168.10.242: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:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:2
cluster_stats_messages_sent:470
cluster_stats_messages_received:470
192.168.10.242:6379>
9、查看各节点的状态
192.168.10.242:6379> cluster nodes
86baaa60272094b9558e9194f5740a5d1c2608ac 192.168.10.241:6379 slave 995cdef393a899c14cc79a7b103d137264f97e78 0 1472982007952 4 connected
3a84bb12dd9f7fb5f86a204e21b3074579364b12 192.168.10.239:6379 master - 0 1472982006945 2 connected 5461-10922
dd4019452121683ebb838f789abeb3755f4bc55e 192.168.10.243:6379 slave 4d5b650e5c23dc55befd72a4005e17196d7f4bf0 0 1472982007952 6 connected
4d5b650e5c23dc55befd72a4005e17196d7f4bf0 192.168.10.240:6379 master - 0 1472982006440 3 connected 10923-16383
69e268a3a0b4d263c465a481ad1b3cc53f863982 192.168.10.242:6379 myself,slave 3a84bb12dd9f7fb5f86a204e21b3074579364b12 0 0 5 connected
995cdef393a899c14cc79a7b103d137264f97e78 192.168.10.238:6379 master - 0 1472982007952 1 connected 0-5460
10、redis集群测试
[root@redis1 bin]# ./redis-cli -c -h 192.168.10.238 -p 6379
192.168.10.238:6379> get hello
"word"
192.168.10.238:6379> set foo test
-> Redirected to slot [12182] located at 192.168.10.240:6379
OK
192.168.10.240:6379> get hello
-> Redirected to slot [866] located at 192.168.10.238:6379
"word"
192.168.10.238:6379>
[root@redis5 bin]# ./redis-cli -c -h 192.168.10.242 -p 6379
192.168.10.242:6379> set hello word
-> Redirected to slot [866] located at 192.168.10.238:6379
OK
192.168.10.238:6379> get hello
"word"
linux