首页 > 代码库 > Redis-Cluster实战--5.使用redis-cli安装

Redis-Cluster实战--5.使用redis-cli安装

Redis-Cluster实战--5.使用redis-cli安装

博客分类:
 
redis
缓存
redis-cluster
redisCluster指派槽cluster-infomeetslots 
转载请注明出处哈:http://carlosfu.iteye.com/blog/2240426

安装视频:

    8.2.2 redis-cluster安装1

    8.2.3 redis-cluster安装2

    8.2.4 redis-cluster安装纠正   

 

一、目的
     为什么官方提供了ruby构建集群工具,还要实现一个redis-cli版的集群构建?
    答案很简单:
    1. 熟悉redis-cluster的集群命令和协议(作者给的ruby工具都是在这些命令组合起来)
    2. 更好的理解redis-cluster
    注意:
本文档只为了演示redis-cli搭建Redis-Cluster,实际大集群还是以工具(例如ruby, 其他语言封装的自动化安装工具为第一选择)。
 
二、准备redis(下载、编译、安装、配置目录、数据目录)

 

1. 下载、编译、安装

Java代码  
cd /opt/soft  
wget http://download.redis.io/releases/redis-3.0.3.tar.gz  
tar xzf redis-3.0.3.tar.gz  
cd redis-3.0.3  
make  
make install  
 

2. 配置目录、数据目录

Java代码  
cd /opt/soft/redis-3.0.3  
mkdir -p data  
mkdir -p conf  
 

3.建立软链接:

Java代码  
ln -s /opt/soft/redis-3.0.3 /opt/soft/redis  
 

 

二、配置、启动Redis节点(本例子以3主、3从组成Redis-Cluster)

 
实际中应该在多台机器进行安装,这里为了方便演示只用了一台机器,ip=10.10.53.1591. 配置redis节点,在conf目录下添加6个(8000-8005)redis-${port}.conf作为6个节点的配置文件

      其中8000-8005是六个端口号

Java代码  
port 8000  
cluster-enabled yes  
cluster-config-file nodes-8000.conf  
cluster-node-timeout 15000  
dir "/opt/soft/redis/data/"  
appendonly yes  
appendfilename "appendonly-8000.aof"  
logfile "8000.log"  
daemonize yes  
pidfile /var/run/redis-8000.pid   
dbfilename "dump-8000.rdb"  
 

    生成另外5个文件

Java代码  
sed s/8000/8001/g redis-8000.conf > redis-8001.conf  
sed s/8000/8002/g redis-8000.conf > redis-8002.conf  
sed s/8000/8003/g redis-8000.conf > redis-8003.conf  
sed s/8000/8004/g redis-8000.conf > redis-8004.conf  
sed s/8000/8005/g redis-8000.conf > redis-8005.conf  
  

 

2. 启动6个节点。

Java代码  
redis-server /opt/soft/redis/conf/redis-8000.conf  
redis-server /opt/soft/redis/conf/redis-8001.conf  
redis-server /opt/soft/redis/conf/redis-8002.conf  
redis-server /opt/soft/redis/conf/redis-8003.conf  
redis-server /opt/soft/redis/conf/redis-8004.conf  
redis-server /opt/soft/redis/conf/redis-8005.conf  
 

 

3. 查看节点是否都已经启动:

Java代码  
[@zw_53_162 conf]# ps -ef | grep redis  
root 26007 1 0 21:56 ? 00:00:00 redis-server *:8000 [cluster]   
root 26011 1 0 21:56 ? 00:00:00 redis-server *:8001 [cluster]   
root 26019 1 0 21:56 ? 00:00:00 redis-server *:8002 [cluster]   
root 26023 1 0 21:56 ? 00:00:00 redis-server *:8003 [cluster]   
root 26033 1 0 21:56 ? 00:00:00 redis-server *:8004 [cluster]   
root 26047 1 0 21:56 ? 00:00:00 redis-server *:8005 [cluster]  
 

 

4. 查看单个节点:(此时六个节点是分散的,没有形成集群,所有cluster_state=fail)

 

Java代码  
[@zw_53_162 conf]# redis-cli -c -p 8000  
127.0.0.1:8000> cluster info  
cluster_state:fail  
cluster_slots_assigned:0  
cluster_slots_ok:0  
cluster_slots_pfail:0  
cluster_slots_fail:0  
cluster_known_nodes:1  
cluster_size:0  
cluster_current_epoch:0  
cluster_my_epoch:0  
cluster_stats_messages_sent:0  
cluster_stats_messages_received:0  
 

 

四、利用redis-cluster meet命令,实现节点握手,组成集群:

 

cluster meet <ip> <port> 将 ip 和 port 所指定的节点添加到集群当中,让它成为集群的一份子。
   

 

 1. 利用redis-cli连接到8000,然后meet 8001-8005

Java代码  
redis-cli -c -p 8000 cluster meet 10.10.53.159 8001  
redis-cli -c -p 8000 cluster meet 10.10.53.159 8002  
redis-cli -c -p 8000 cluster meet 10.10.53.159 8003  
redis-cli -c -p 8000 cluster meet 10.10.53.159 8004  
redis-cli -c -p 8000 cluster meet 10.10.53.159 8005  
 

  

五、分配槽(slots)给节点:

       

     1. 分派slots

 

cluster addslots <slot> [slot ...] 将一个或多个槽(slot)指派(assign)给当前节点。
 

     redis-cli -c -p 8000 cluster addslots 0 1 2 ...

    (redis-cli 未实现0-5462这样的参数,必须一个个输入。)

    所以利用shell生成最终的命令addslots.sh:

 

Java代码  
start=$1  
end=$2  
port=$3  
for slot in `seq ${start} ${end}`  
do  
    echo "slot:${slot}"  
    redis-cli -c -p ${port} cluster addslots ${slot}   
done  
 

    执行: 

sh addslots.sh 0 5460 8000
sh addslots.sh 5461 10922 8001
sh addslots.sh 10923 16383 8002
 


   2. 确认cluster当前状态 .

 

Java代码  
127.0.0.1:8000> 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:5  
cluster_my_epoch:1  
cluster_stats_messages_sent:1650  
cluster_stats_messages_received:1650  
 

  3. 确认分配槽状态:

Java代码  
127.0.0.1:8000> cluster nodes  
6690722c4536210a231af4bdceb604e83d53403e 10.10.53.159:8001 master - 0 1440897737597 3 connected 5461-10922  
4d2842d9f846481783eafbd9135df372e2153307 10.10.53.159:8000 myself,master - 0 0 1 connected 0-5460  
caefaab1e5d01d76a728c84cb1dc285a6aab15a6 10.10.53.159:8005 master - 0 1440897736092 4 connected  
ec403c50e59460bcb0b47906e567423b7d9aed50 10.10.53.159:8003 master - 0 1440897738097 0 connected  
a52b9d211ec06190bcc4ab36eecf643fa13aa215 10.10.53.159:8004 master - 0 1440897736594 5 connected  
c05ebacbeeb6cbcf52a6ac60384891586226f131 10.10.53.159:8002 master - 0 1440897737096 2 connected 10923-16383  
  
127.0.0.1:8000> cluster slots  
1) 1) (integer) 5461  
 2) (integer) 10922  
 3) 1) "10.10.53.159"  
 2) (integer) 8001  
2) 1) (integer) 0  
 2) (integer) 5460  
 3) 1) "10.10.53.159"  
 2) (integer) 8000  
3) 1) (integer) 10923  
 2) (integer) 16383  
 3) 1) "10.10.53.159"  
 2) (integer) 8002  
  

 

六、配置主从关系(保证高可用):

 

cluster replicate <node_id> 将当前节点设置为 node_id 指定的节点的从节点。
 

    8003设置成8000的从

    8004设置成8001的从

    8005设置成8002的从

 

注意这里的命令不是slaveof
<node_id>不是ip:port的形式,需要查询cluster nodes查找自己的node_id(myself)
  

Java代码  
redis-cli -c -p 8003 cluster  replicate `redis-cli -c -p 8003 cluster nodes | grep 8000 | awk {print $1}`  
redis-cli -c -p 8004 cluster  replicate `redis-cli -c -p 8003 cluster nodes | grep 8001 | awk {print $1}`  
redis-cli -c -p 8005 cluster  replicate `redis-cli -c -p 8003 cluster nodes | grep 8002 | awk {print $1}`  
 

 

 

七、最终确认集群状态、节点状态、分配槽状态:

 

Java代码  
集群状态:  
127.0.0.1:8000> 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:5  
cluster_my_epoch:1  
cluster_stats_messages_sent:2963  
cluster_stats_messages_received:2963  
   
三主三从  
127.0.0.1:8000> cluster nodes   
6690722c4536210a231af4bdceb604e83d53403e 10.10.53.159:8001 master - 0 1440897872917 3 connected 5461-10922  
4d2842d9f846481783eafbd9135df372e2153307 10.10.53.159:8000 myself,master - 0 0 1 connected 0-5460  
caefaab1e5d01d76a728c84cb1dc285a6aab15a6 10.10.53.159:8005 slave c05ebacbeeb6cbcf52a6ac60384891586226f131 0 1440897874922 4 connected  
ec403c50e59460bcb0b47906e567423b7d9aed50 10.10.53.159:8003 slave 4d2842d9f846481783eafbd9135df372e2153307 0 1440897874922 1 connected  
a52b9d211ec06190bcc4ab36eecf643fa13aa215 10.10.53.159:8004 slave 6690722c4536210a231af4bdceb604e83d53403e 0 1440897874421 5 connected  
c05ebacbeeb6cbcf52a6ac60384891586226f131 10.10.53.159:8002 master - 0 1440897873920 2 connected 10923-16383  
   
分配槽状态:  
127.0.0.1:8000> cluster slots  
1) 1) (integer) 10923  
   2) (integer) 16383  
   3) 1) "10.10.53.159"  
      2) (integer) 8002  
   4) 1) "10.10.53.159"  
      2) (integer) 8005  
2) 1) (integer) 5461  
   2) (integer) 10922  
   3) 1) "10.10.53.159"  
      2) (integer) 8001  
   4) 1) "10.10.53.159"  
      2) (integer) 8004  
3) 1) (integer) 0  
   2) (integer) 5460  
   3) 1) "10.10.53.159"  
      2) (integer) 8000  
   4) 1) "10.10.53.159"  
      2) (integer) 8003  
  

 

 

Redis-Cluster实战--5.使用redis-cli安装