配置redis 集群

2018-07-05  本文已影响46人  南京杨小兵
redis  集群搭建环境

1、使用ruby脚本搭建集群。需要ruby的运行环境。

安装ruby:

yum install ruby

yum install rubygems

2、上传redis-3.0.0.gem到 linux中
image.png image.png
3、安装ruby运行时所使用的包

[root@localhost ~]# gem install redis-3.0.0.gem

Successfully installed redis-3.0.0

1 gem installed

Installing ri documentation for redis-3.0.0...

Installing RDoc documentation for redis-3.0.0...

[root@localhost ~]#

查看redis-trib.rb 脚本所在的目录:

[root@localhost ~]# cd redis-3.0.0/src

[root@localhost src]# ll *.rb

-rwxrwxr-x. 1 root root 48141 Apr  1  2015  redis-trib.rb

7.2.2. 搭建步骤

需要6台redis服务器。搭建伪分布式。

需要6个redis实例。彻底

需要运行在不同的端口7001-7006

使用之前搭建好的redis实例 。

注意:搭建前 如果节点里有数据,需要删除(rdb文件,aof文件)。

第一步:创建6个redis实例,每个实例运行在不同的端口。需要修改redis.conf配置文件。配置文件中还需要把cluster-enabled yes前的注释去掉。

Ø 创建目录:

<colgroup><col width="568"></colgroup>
| 

[root@localhost local]# mkdir redis-cluster

 |

Ø copy 之前搭建好的redis 并改名为redis01

<colgroup><col width="568"></colgroup>
| 

[root@localhost local]# cp redis/ redis-cluster/redis01 -r

 |

Ø 进入redis-cluster目录中cd到redis01的bin目录,删除数据文件

<colgroup><col width="568"></colgroup>
| 

[root@localhost local]# cd redis-cluster/redis01/bin

[root@localhost bin]# rm -rf *.rdb *.aof

 |

Ø 修改redis.conf,取消注释,如图

<colgroup><col width="568"></colgroup>
| 

[root@localhost bin]# vim redis.conf

 |

按ESC    :wq  

保存退出。

Ø cd到redis-cluster目录

Ø copy六份并分别命名为redis02,redis03,redis04,redis05,redis06

<colgroup><col width="568"></colgroup>
| 

[root@localhost redis-cluster]# cp redis01 redis02 -r

[root@localhost redis-cluster]# cp redis01 redis03 -r

[root@localhost redis-cluster]# cp redis01 redis04 -r

[root@localhost redis-cluster]# cp redis01 redis05 -r

[root@localhost redis-cluster]# cp redis01 redis06 -r

 |

Ø cd到每一个实例的bin目录,修改每一个redis实例的端口分别改为7001-7006

<colgroup><col width="568"></colgroup>
| image.png

[root@localhost bin]# vim redis.conf

 |
image.png

第二步:启动每个redis实例。

vim  redis-cluster-start-all.sh

添加如下文字到文件中:

<colgroup><col width="568"></colgroup>
| 

cd /usr/local/redis-cluster/redis01/bin

./redis-server redis.conf

cd /usr/local/redis-cluster/redis02/bin

./redis-server redis.conf

cd /usr/local/redis-cluster/redis03/bin

./redis-server redis.conf

cd /usr/local/redis-cluster/redis04/bin

./redis-server redis.conf

cd /usr/local/redis-cluster/redis05/bin

./redis-server redis.conf

cd /usr/local/redis-cluster/redis06/bin

./redis-server redis.conf

 |

Ø 修改文件:redis-cluster-start-all.sh 的权限,让其可执行。

<colgroup><col width="568"></colgroup>
| 

chmod u+x redis-cluster-start-all.sh

 |

Ø 执行启动

[root@localhost redis-cluster]# ./redis-cluster-start-all.sh

第三步:使用ruby脚本搭建集群。

Ø 从解压目录下的src下的拷贝redis-trib.rb文件到redis-cluster目录中
image.png

Ø 执行创建:

<colgroup><col width="568"></colgroup>
| 

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

 |

<colgroup><col width="568"></colgroup>
| 

[root@localhost redis-cluster]# ./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

>>> 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 slots allocation 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 to 192.168.25.153:7001

Adding replica 192.168.25.153:7005 to 192.168.25.153:7002

Adding replica 192.168.25.153:7006 to 192.168.25.153:7003

M: 2e48ae301e9c32b04a7d4d92e15e98e78de8c1f3 192.168.25.153:7001

   slots:0-5460 (5461 slots) master

M: 8cd93a9a943b4ef851af6a03edd699a6061ace01 192.168.25.153:7002

   slots:5461-10922 (5462 slots) master

M: 2935007902d83f20b1253d7f43dae32aab9744e6 192.168.25.153:7003

   slots:10923-16383 (5461 slots) master

S: 74f9d9706f848471583929fc8bbde3c8e99e211b 192.168.25.153:7004

   replicates 2e48ae301e9c32b04a7d4d92e15e98e78de8c1f3

S: 42cc9e25ebb19dda92591364c1df4b3a518b795b 192.168.25.153:7005

   replicates 8cd93a9a943b4ef851af6a03edd699a6061ace01

S: 8b1b11d509d29659c2831e7a9f6469c060dfcd39 192.168.25.153:7006

   replicates 2935007902d83f20b1253d7f43dae32aab9744e6

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.25.153:7001)

M: 2e48ae301e9c32b04a7d4d92e15e98e78de8c1f3 192.168.25.153:7001

   slots:0-5460 (5461 slots) master

M: 8cd93a9a943b4ef851af6a03edd699a6061ace01 192.168.25.153:7002

   slots:5461-10922 (5462 slots) master

M: 2935007902d83f20b1253d7f43dae32aab9744e6 192.168.25.153:7003

   slots:10923-16383 (5461 slots) master

M: 74f9d9706f848471583929fc8bbde3c8e99e211b 192.168.25.153:7004

   slots: (0 slots) master

   replicates 2e48ae301e9c32b04a7d4d92e15e98e78de8c1f3

M: 42cc9e25ebb19dda92591364c1df4b3a518b795b 192.168.25.153:7005

   slots: (0 slots) master

   replicates 8cd93a9a943b4ef851af6a03edd699a6061ace01

M: 8b1b11d509d29659c2831e7a9f6469c060dfcd39 192.168.25.153:7006

   slots: (0 slots) master

   replicates 2935007902d83f20b1253d7f43dae32aab9744e6

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

[root@localhost redis-cluster]#

 |

第四步 创建关闭集群的脚本:(不是必须的)

首先使用:vim 命令创建一个文件 redis-cluster-stop-all.sh

编辑文件,添加如下:

<colgroup><col width="568"></colgroup>
| 

cd /usr/local/redis-cluster/redis01/bin

./redis-cli -p 7001 shutdown

cd /usr/local/redis-cluster/redis02/bin

./redis-cli -p 7002 shutdown

cd /usr/local/redis-cluster/redis03/bin

./redis-cli -p 7003 shutdown

cd /usr/local/redis-cluster/redis04/bin

./redis-cli -p 7004 shutdown

cd /usr/local/redis-cluster/redis05/bin

./redis-cli -p 7005 shutdown

cd /usr/local/redis-cluster/redis06/bin

./redis-cli -p 7006 shutdown

 |

修改文件:redis-cluster-stop-all.sh 的权限,让其可执行。

chmod u+x redis-cluster-stop-all.sh
image.png

7.3. 集群的使用方法

Redis-cli连接集群。

[root@localhost redis-cluster]# redis01/bin/redis-cli -p 7002 -c

-c:代表连接的是redis集群

使用命令操作redis是和单机版的一样。

下面是我操作的命令截图

 yum install ruby
 yum install rubygems
tar -xvf redis-3.2.12.tar.gz 

开启集群 cd /usr/local/redis-cluster/redis01
redis-server ./redis.conf

cd /usr/local/redis-cluster/redis02
redis-server ./redis.conf

cd /usr/local/redis-cluster/redis03
redis-server ./redis.conf

cd /usr/local/redis-cluster/redis04
redis-server ./redis.conf

cd /usr/local/redis-cluster/redis05
redis-server ./redis.conf

cd /usr/local/redis-cluster/redis06
redis-server ./redis.conf

进入集群  redis-cli -p 7001 -c    重点:集群一定加-c

上一篇 下一篇

猜你喜欢

热点阅读