redis集群的搭建

1.1   搭建集群需要的环境


搭建集群需要使用到官方提供的ruby脚本。

需要安装ruby的环境。

 

安装ruby

yum install ruby

yum install rubygems

redis集群的搭建


redis集群管理工具redis-trib.rb

[[email protected] ~]# vim

[[email protected] redis-3.0.0]# cd src

[[email protected] src]# ll *.rb

-rwxrwxr-x. 1 root root 48141 Apr  1 07:01 redis-trib.rb

[[email protected] src]#

 

脚本需要的ruby包:

redis集群的搭建


需要上传到linux服务。

安装ruby的包:

gem install redis-3.0.0.gem

[[email protected] ~]# gem install redis-3.0.0.gem

Successfully installed redis-3.0.0

1 gem installed

Installing ri documentation forredis-3.0.0...

Installing RDoc documentation forredis-3.0.0...

redis集群的搭建


1.1   集群的搭建

第一步:创建6个redis实例,端口号从7001~7006

redis集群的搭建


redis集群的搭建

redis集群的搭建


第二步:修改redis的配置文件

1、修改端口号


redis集群的搭建


2、打开cluster-enable前面的注释。

redis集群的搭建


还有几个配置:

##修改配置文件中的下面选项

port 7000

daemonize yes

cluster-enabled yes

cluster-config-file nodes.conf 

cluster-node-timeout 5000

appendonly yes

redis集群的搭建


第三步:把创建集群的ruby脚本复制到redis-cluster目录下。

redis集群的搭建

redis集群的搭建


显示行数:

到命令模式下,输入 :set number


第四步:启动6个redis实例

 

先创建个脚本:

redis集群的搭建


redis集群的搭建


redis集群的搭建


redis集群的搭建


第五步:创建集群。

 

在脚本执行命令:


./redis-trib.rb create --replicas 1 192.168.25.153:7001 192.168.25.153:7002 192.168.25.153:7003 192.168.25.153:7004 192.168.25.153:7005  192.168.25.153:7006


[[email protected] redis-cluster]# ./redis-trib.rbcreate --replicas 1 192.168.25.153:7001 192.168.25.153:7002 192.168.25.153:7003192.168.25.153:7004 192.168.25.153:7005 192.168.25.153:7006

>>> Creating cluster

Connecting to node 192.168.25.153:7001: OK

Connecting to node 192.168.25.153:7002: OK

Connecting to node 192.168.25.153:7003: OK

Connecting to node 192.168.25.153:7004: OK

Connecting to node 192.168.25.153:7005: OK

Connecting to node 192.168.25.153:7006: OK

>>> Performing hash slotsallocation on 6 nodes...

Using 3 masters:

192.168.25.153:7001

192.168.25.153:7002

192.168.25.153:7003

Adding replica 192.168.25.153:7004 to192.168.25.153:7001

Adding replica 192.168.25.153:7005 to192.168.25.153:7002

Adding replica 192.168.25.153:7006 to192.168.25.153:7003

M: 5a8523db7e12ca600dc82901ced06741b3010076192.168.25.153:7001

  slots:0-5460 (5461 slots) master

M: bf6f0929044db485dea9b565bb51e0c917d20a53192.168.25.153:7002

  slots:5461-10922 (5462 slots) master

M: c5e334dc4a53f655cb98fa3c3bdef8a808a693ca192.168.25.153:7003

  slots:10923-16383 (5461 slots) master

S: 2a61b87b49e5b1c84092918fa2467dd70fec115f192.168.25.153:7004

  replicates 5a8523db7e12ca600dc82901ced06741b3010076

S: 14848b8c813766387cfd77229bd2d1ffd6ac8d65192.168.25.153:7005

  replicates bf6f0929044db485dea9b565bb51e0c917d20a53

S: 3192cbe437fe67bbde9062f59d5a77dabcd0d632192.168.25.153:7006

  replicates c5e334dc4a53f655cb98fa3c3bdef8a808a693ca

Can I set the above configuration? (type'yes' to accept): yes

>>> Nodes configuration updated

>>> Assign a different configepoch to each node

>>> Sending CLUSTER MEET messagesto join the cluster

Waiting for the cluster to join.....

>>> Performing Cluster Check(using node 192.168.25.153:7001)

M: 5a8523db7e12ca600dc82901ced06741b3010076192.168.25.153:7001

  slots:0-5460 (5461 slots) master

M: bf6f0929044db485dea9b565bb51e0c917d20a53192.168.25.153:7002

  slots:5461-10922 (5462 slots) master

M: c5e334dc4a53f655cb98fa3c3bdef8a808a693ca192.168.25.153:7003

  slots:10923-16383 (5461 slots) master

M: 2a61b87b49e5b1c84092918fa2467dd70fec115f192.168.25.153:7004

  slots: (0 slots) master

  replicates 5a8523db7e12ca600dc82901ced06741b3010076

M: 14848b8c813766387cfd77229bd2d1ffd6ac8d65192.168.25.153:7005

  slots: (0 slots) master

  replicates bf6f0929044db485dea9b565bb51e0c917d20a53

M: 3192cbe437fe67bbde9062f59d5a77dabcd0d632192.168.25.153:7006

  slots: (0 slots) master

  replicates c5e334dc4a53f655cb98fa3c3bdef8a808a693ca

[OK] All nodes agree about slotsconfiguration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

[[email protected] redis-cluster]# 


1.1   测试集群

redis集群的搭建


[[email protected] redis-cluster]#redis01/redis-cli -h 192.168.25.153 -p 7002 -c

 

 

[[email protected] redis-cluster]#redis01/redis-cli -h 192.168.25.153 -p 7002

192.168.25.153:7002> set a 100

(error) MOVED 15495 192.168.25.153:7003

192.168.25.153:7002>

[[email protected] redis-cluster]#redis01/redis-cli -h 192.168.25.153 -p 7002 -c

192.168.25.153:7002> set a 100

-> Redirected to slot [15495] located at192.168.25.153:7003

OK

192.168.25.153:7003> 

1.1   关闭redis

redis01/redis-cli -p 7001 shutdown

redis集群的搭建


redis集群的搭建


做个脚本:

redis集群的搭建


redis集群的搭建



redis集群的搭建