Redis集群部署  
 准备集群环境  
 
 创建集群  
[ root@localhost  ~ ] 
[ root@host51  ~ ] 
bind 192.168 .88 .51 
cluster- enabled yes
cluster- config- file  nodes- 6379. conf
cluster- node- timeout 5000 
[ root@host51  ~ ] 
State      Recv- Q     Send- Q         Local  Address:Port          Peer Address:Port    Process                                                                               
LISTEN     0           128                   0.0 .0 .0 :22                  0.0 .0 .0 :*         users:( ( "sshd" , pid= 548 , fd= 3 ) )                                                         
LISTEN     0           128             192.168 .88 .51 :16379               0.0 .0 .0 :*         users:( ( "redis-server" , pid= 1080 , fd= 8 ) )                                                
LISTEN     0           128             192.168 .88 .51 :6379                0.0 .0 .0 :*         users:( ( "redis-server" , pid= 1080 , fd= 6 ) )     
[ root@host51  ~ ] 
192.168 .88 .51 :6379 >  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 
1 、在任意一台redis服务器上都可以执行创建集群的命令。
2 、
3 、创建集群时,会自动创建主从角色,默认把主机列表中的前3 台服务器创建为Master角色的redis服务器,剩下的均配置为slave角色服务器。
4 、创建集群时,会自动给master角色的主机分配hash 槽 ,通过hash 槽实现数据的分布式存储。
[ root@host51  ~ ] 
>> >  Performing hash  slots allocation on  6  nodes. . . 
Master[ 0 ]  - >  Slots 0  -  5460 
Master[ 1 ]  - >  Slots 5461  -  10922 
Master[ 2 ]  - >  Slots 10923  -  16383 
Adding replica 192.168 .88 .54 :6379  to  192.168 .88 .51 :6379 
Adding replica 192.168 .88 .55 :6379  to  192.168 .88 .52 :6379 
Adding replica 192.168 .88 .56 :6379  to  192.168 .88 .53 :6379 
M: 1 cbc84ecf3dbde1c3a06d1d7cd8c527da6b7fe32 192.168 .88 .51 :6379 
   slots:[ 0 - 5460 ]  ( 5461  slots)  master
M: 88 e66d4df0867bf76015e22cc480be85ed5eb5de 192.168 .88 .52 :6379 
   slots:[ 5461 - 10922 ]  ( 5462  slots)  master
M: b8968a8c613a389052a2978803dd7fd6d24ca076 192.168 .88 .53 :6379 
   slots:[ 10923 - 16383 ]  ( 5461  slots)  master
S: 07740 e8a77b462dbbb4ab55cade01b102cb3a5d8 192.168 .88 .54 :6379 
   replicates 1 cbc84ecf3dbde1c3a06d1d7cd8c527da6b7fe32
S: 8615847 c6f9ce5c65f6017efcbc8f6e4072ae9d1 192.168 .88 .55 :6379 
   replicates 88 e66d4df0867bf76015e22cc480be85ed5eb5de
S: 6 f766d3638e6e66710743a88f684bac1d252bbef 192.168 .88 .56 :6379 
   replicates b8968a8c613a389052a2978803dd7fd6d24ca076
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 .88 .51 :6379 ) 
M: 1 cbc84ecf3dbde1c3a06d1d7cd8c527da6b7fe32 192.168 .88 .51 :6379 
   slots:[ 0 - 5460 ]  ( 5461  slots)  master
   1  additional replica( s) 
S: 07740 e8a77b462dbbb4ab55cade01b102cb3a5d8 192.168 .88 .54 :6379 
   slots: ( 0  slots)  slave
   replicates 1 cbc84ecf3dbde1c3a06d1d7cd8c527da6b7fe32
S: 8615847 c6f9ce5c65f6017efcbc8f6e4072ae9d1 192.168 .88 .55 :6379 
   slots: ( 0  slots)  slave
   replicates 88 e66d4df0867bf76015e22cc480be85ed5eb5de
S: 6 f766d3638e6e66710743a88f684bac1d252bbef 192.168 .88 .56 :6379 
   slots: ( 0  slots)  slave
   replicates b8968a8c613a389052a2978803dd7fd6d24ca076
M: 88 e66d4df0867bf76015e22cc480be85ed5eb5de 192.168 .88 .52 :6379 
   slots:[ 5461 - 10922 ]  ( 5462  slots)  master
   1  additional replica( s) 
M: b8968a8c613a389052a2978803dd7fd6d24ca076 192.168 .88 .53 :6379 
   slots:[ 10923 - 16383 ]  ( 5461  slots)  master
   1  additional replica( s) 
[ OK]  All  nodes agree about slots configuration. 
>> >  Check  for  open  slots. . . 
>> >  Check  slots coverage. . . 
[ OK]  All  16384  slots covered. 
第一列:主服务器ip地址
第二列:主服务器ID
第三列:存储变量个数
第四列:hash 槽个数 (hash 槽的作用在集群存储工程过程里讲)
第五列:从服务器数量
[ root@host51  ~ ] 
192.168 .88 .51 :6379  ( 1 cbc84ec. . . )  - >  0  keys  |  5461  slots |  1  slaves. 
192.168 .88 .52 :6379  ( 88 e66d4d. . . )  - >  0  keys  |  5462  slots |  1  slaves. 
192.168 .88 .53 :6379  ( b8968a8c. . . )  - >  0  keys  |  5461  slots |  1  slaves. 
[ OK]  0  keys  in  3  masters. 
0.00  keys  per slot on  average. 
[ root@host51  ~ ] 
192.168 .88 .51 :6379 >  set  school nsd2306
- >  Redirected to  slot [ 8455 ]  located at 192.168 .88 .52 :6379 
OK
192.168 .88 .52 :6379 >  set  teacher laoyang
- >  Redirected to  slot [ 12541 ]  located at 192.168 .88 .53 :6379 
OK
192.168 .88 .53 :6379 >  set  class NSD
- >  Redirected to  slot [ 7755 ]  located at 192.168 .88 .52 :6379 
OK
[ root@host51  ~ ] 
192.168 .88 .51 :6379  ( 1 cbc84ec. . . )  - >  0  keys  |  5461  slots |  1  slaves. 
192.168 .88 .52 :6379  ( 88 e66d4d. . . )  - >  2  keys  |  5462  slots |  1  slaves. 
192.168 .88 .53 :6379  ( b8968a8c. . . )  - >  1  keys  |  5461  slots |  1  slaves. 
[ OK]  3  keys  in  3  masters. 
0.00  keys  per slot on  average. 
[ root@host51  ~ ] 
192.168 .88 .54 :6379 >  keys  * 
( empty list or  set ) 
192.168 .88 .54 :6379 >  exit 
[ root@host51  ~ ] 
192.168 .88 .55 :6379 >  keys  * 
1 )  "class" 
2 )  "school" 
192.168 .88 .55 :6379 >  exit 
[ root@host51  ~ ] 
192.168 .88 .56 :6379 >  keys  * 
1 )  "teacher" 
[ root@host53  ~ ] 
[ root@host51  ~ ] 
Could not  connect  to  Redis at 192.168 .88 .53 :6379 : Connection refused
192.168 .88 .51 :6379  ( 1 cbc84ec. . . )  - >  1  keys  |  5461  slots |  1  slaves. 
192.168 .88 .56 :6379  ( 6 f766d36. . . )  - >  2  keys  |  5461  slots |  0  slaves. 
192.168 .88 .52 :6379  ( 88 e66d4d. . . )  - >  3  keys  |  5462  slots |  1  slaves. 
[ OK]  6  keys  in  3  masters. 
0.00  keys  per slot on  average. 
[ root@host51  ~ ] 
192.168 .88 .51 :6379  ( 1 cbc84ec. . . )  - >  1  keys  |  5461  slots |  1  slaves. 
192.168 .88 .56 :6379  ( 6 f766d36. . . )  - >  2  keys  |  5461  slots |  1  slaves. 
192.168 .88 .52 :6379  ( 88 e66d4d. . . )  - >  3  keys  |  5462  slots |  1  slaves. 
[ OK]  6  keys  in  3  masters. 
0.00  keys  per slot on  average. 
[ root@redis64  ~ ] 
< ?php
$redis_list =  [ '192.168.88.51:6379' , '192.168.88.52:6379' , '192.168.88.53:6379' , '192.168.88.54:6379' , '192.168.88.55:6379' , '192.168.88.56:6379' ] ; 
$client =  new RedisCluster( NUll , $redis_list) ; 
$client- > set ( "i" , "tarenaA " ) ; 
$client- > set ( "j" , "tarenaB " ) ; 
$client- > set ( "k" , "tarenaC " ) ; 
echo "save ok\n" ; 
?> 
[ root@host51  ~ ] 
192.168 .88 .51 :6379 >  keys  * 
1 )  "j" 
192.168 .88 .51 :6379 >  exit 
[ root@host51  ~ ] 
192.168 .88 .52 :6379 >  keys  * 
1 )  "class" 
2 )  "k" 
3 )  "school" 
192.168 .88 .52 :6379 >  exit 
[ root@host51  ~ ] 
192.168 .88 .53 :6379 >  keys  * 
1 )  "teacher" 
2 )  "i" 
< ?php
$redis_list =  [ '192.168.88.51:6379' , '192.168.88.52:6379' , '192.168.88.53:6379' , '192.168.88.54:6379' , '192.168.88.55:6379' , '192.168.88.56:6379' ] ;   
$client =  new RedisCluster( NUll , $redis_list) ;  
echo $client- > get( "i" ) ;   
echo $client- > get( "j" ) ;   
echo $client- > get( "k" ) ;   
?> 
[ root@redis64  ~ ] 
tarenaA tarenaB tarenaC
[ root@host51  ~ ] 
192.168 .88 .51 :6379 >  keys  * 
1 )  "j" 
192.168 .88 .51 :6379 >  exit 
[ root@host51  ~ ] 
192.168 .88 .52 :6379 >  keys  * 
1 )  "class" 
2 )  "k" 
3 )  "school" 
192.168 .88 .52 :6379 >  exit 
[ root@host51  ~ ] 
192.168 .88 .53 :6379 >  keys  * 
1 )  "teacher" 
2 )  "i"