CentOS中搭建Redis伪分布式集群

CentOS中搭建Redis伪分布式集群

解压redis

先到官网https://redis.io/下载redis安装包,然后在CentOS操作系统中解压该安装包:

tar -zxvf redis-3.2.9.tar.gz

编译redis

cd redis-3.2.9

make && make install

此时如果报如下错误:

bash: make: command not found

说明系统没有安装make,所有要先安装make,即:

yum -y install gcc automake autoconf libtool make

如果此时再报如下错误:

Error Downloading Packages:

glibc-headers-2.12-1.107.el6.x86_64: failure: Packages/glibc-headers-2.12-1.107.el6.x86_64.rpm from c6-media: [Errno 256] No more mirrors to try.

1:perl-Module-Pluggable-3.90-129.el6.x86_64: failure: Packages/perl-Module-Pluggable-3.90-129.el6.x86_64.rpm from c6-media: [Errno 256] No more mirrors to try.

mpfr-2.4.1-6.el6.x86_64: failure: Packages/mpfr-2.4.1-6.el6.x86_64.rpm from c6-media: [Errno 256] No more mirrors to try.

1:make-3.81-20.el6.x86_64: failure: Packages/make-3.81-20.el6.x86_64.rpm from c6-media: [Errno 256] No more mirrors to try.

cloog-ppl-0.15.7-1.2.el6.x86_64: failure: Packages/cloog-ppl-0.15.7-1.2.el6.x86_64.rpm from c6-media: [Errno 256] No more mirrors to try.

gcc-4.4.7-3.el6.x86_64: failure: Packages/gcc-4.4.7-3.el6.x86_64.rpm from c6-media: [Errno 256] No more mirrors to try.

libgomp-4.4.7-3.el6.x86_64: failure: Packages/libgomp-4.4.7-3.el6.x86_64.rpm from c6-media: [Errno 256] No more mirrors to try.

kernel-headers-2.6.32-358.el6.x86_64: failure: Packages/kernel-headers-2.6.32-358.el6.x86_64.rpm from c6-media: [Errno 256] No more mirrors to try.

autoconf-2.63-5.1.el6.noarch: failure: Packages/autoconf-2.63-5.1.el6.noarch.rpm from c6-media: [Errno 256] No more mirrors to try.

automake-1.11.1-4.el6.noarch: failure: Packages/automake-1.11.1-4.el6.noarch.rpm from c6-media: [Errno 256] No more mirrors to try.

4:perl-libs-5.10.1-129.el6.x86_64: failure: Packages/perl-libs-5.10.1-129.el6.x86_64.rpm from c6-media: [Errno 256] No more mirrors to try.

3:perl-version-0.77-129.el6.x86_64: failure: Packages/perl-version-0.77-129.el6.x86_64.rpm from c6-media: [Errno 256] No more mirrors to try.

4:perl-5.10.1-129.el6.x86_64: failure: Packages/perl-5.10.1-129.el6.x86_64.rpm from c6-media: [Errno 256] No more mirrors to try.

1:perl-Pod-Simple-3.13-129.el6.x86_64: failure: Packages/perl-Pod-Simple-3.13-129.el6.x86_64.rpm from c6-media: [Errno 256] No more mirrors to try.

glibc-devel-2.12-1.107.el6.x86_64: failure: Packages/glibc-devel-2.12-1.107.el6.x86_64.rpm from c6-media: [Errno 256] No more mirrors to try.

1:perl-Pod-Escapes-1.04-129.el6.x86_64: failure: Packages/perl-Pod-Escapes-1.04-129.el6.x86_64.rpm from c6-media: [Errno 256] No more mirrors to try.

libtool-2.2.6-15.5.el6.x86_64: failure: Packages/libtool-2.2.6-15.5.el6.x86_64.rpm from c6-media: [Errno 256] No more mirrors to try.

ppl-0.10.2-11.el6.x86_64: failure: Packages/ppl-0.10.2-11.el6.x86_64.rpm from c6-media: [Errno 256] No more mirrors to try.

cpp-4.4.7-3.el6.x86_64: failure: Packages/cpp-4.4.7-3.el6.x86_64.rpm from c6-media: [Errno 256] No more mirrors to try.

说明需要配置本地yum源或者让机器可以上网即可,如果需要配置本地yum源,可以参考另一篇文章《CentOS配置本地yum源》

安装好make后,在redis-3.2.9目录下,再执行

make && make install

出现如下信息,说明执行成功了:

Hint: It's a good idea to run 'make test'

make[1]: Leaving directory `/opt/redis-3.2.9/src'

cd src && make install

make[1]: Entering directory `/opt/redis-3.2.9/src'

Hint: It's a good idea to run 'make test'

INSTALL install

INSTALL install

INSTALL install

INSTALL install

INSTALL install

make[1]: Leaving directory `/opt/redis-3.2.9/src'

此时redis-3.2.9目录下,多了一个src文件夹,里面内容如下:

CentOS中搭建Redis伪分布式集群

此时编译redis完成。

创建redis节点

由于要让redis集群正常运行,就至少需要3个主节点,所以在此就在一台主机上创建6个redis节点来演示集群配置,实际生产环境中需要每个节点一台主机。同时对应的redis节点的ip和端口对应关系如下:

192.168.242.10 6380

192.168.242.10 6381

192.168.242.10 6382

192.168.242.10 6383

192.168.242.10 6384

192.168.242.10 6385

在redis-3.2.9目录下,新建一个redis_cluster目录,用于存放各位节点的配置信息:

mkdir redis_cluster

同时在redis_cluster目录下分别新建6380、6381、6382、6383、6384、6385文件夹:

mkdir 6380

mkdir 6381

mkdir 6382

mkdir 6383

mkdir 6384

mkdir 6385

在6380文件夹下,新建redis.conf文件:

touch redis.conf

编辑如下内容:

daemonize yes

pidfile redis_6380.pid

logfile redis_6380.log

appendonly yes

bind 192.168.242.10

port 6380

cluster-enabled yes

cluster-config-file nodes-6380.conf

cluster-node-timeout 15000

cluster-slave-validity-factor 10

cluster-migration-barrier 1

cluster-require-full-coverage yes

集群相关的配置说明:

cluster-enabled 是否开启集群配置

cluster-config-file 集群节点集群信息配置文件,每个节点都有一个,由redis生成和更新,配置时避免名称冲突

cluster-node-timeout 集群节点互连超时的阀值,单位毫秒

cluster-slave-validity-factor 进行故障转移时,salve会 申请成为master。有时slave会和master失联很久导致数据较旧,这样的slave不应该成为master。这个配置用来判断slave是否和master失联时间过长。

同样在其它文件夹下,也新建redis.conf文件,里面的配置信息相应的修改即可。

启动各个节点:

首先进入redis-3.2.9目录下的,src目录,然后再执行启动各个节点:

./redis-server ../redis_cluster/6380/redis.conf

./redis-server ../redis_cluster/6381/redis.conf

./redis-server ../redis_cluster/6382/redis.conf

./redis-server ../redis_cluster/6383/redis.conf

./redis-server ../redis_cluster/6384/redis.conf

./redis-server ../redis_cluster/6385/redis.conf

检查各个节点是否正常启动了:

如果出现如下信息,说明6个节点正常启动了:

CentOS中搭建Redis伪分布式集群

创建redis集群

redis官方提供了redis-trib.rb这个工具来创建redis集群,同时这个工具就在redis-3.2.9目录下的src目录中,执行如下命令:

./redis-trib.rb create --replicas 1 192.168.242.10:6380 192.168.242.10:6381 192.168.242.10:6382 192.168.242.10:6383 192.168.242.10:6384 192.168.242.10:6385

此时如果报如下错误,说明没有安装ruby:

/usr/bin/env: ruby: No such file or directory

所以先安装ruby:

yum install ruby

安装完成ruby后,再执行上面的创建集群的命令,此时如果再报如下错误,因为缺少redis和ruby的接口,使用gem安装:

./redis-trib.rb:24:in `require': no such file to load -- rubygems (LoadError)

from ./redis-trib.rb:24

由于安装redis和ruby的接口需要用gem,所以先装gem:

yum install rubygems

然而此时如果报如下错误:

Error Downloading Packages:

rubygems-1.3.7-1.el6.noarch: failure: Packages/rubygems-1.3.7-1.el6.noarch.rpm from c6-media: [Errno 256] No more mirrors to try.

说明在镜像文件中没有rubygems的安装包,此时只能先将rubygems的安装下载下来,然后在安装。

这里将rubygems-1.3.7-5.el6.noarch.rpm文件上传到CentOS中,然后执行安装命令:

rpm -ivh rubygems-1.3.7-5.el6.noarch.rpm

此时会报如下错误:

warning: rubygems-1.3.7-5.el6.noarch.rpm: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY

error: Failed dependencies:

ruby-rdoc is needed by rubygems-1.3.7-5.el6.noarch

说明需要先将rubygems依赖的软件先安装好,即将ruby-irb和ruby-rdoc先安装:

yum install ruby-irb ruby-rdoc

然后再执行上面安装rubygems-1.3.7-5.el6.noarch.rpm的命令,出现如下信息,说明安装成功了:

CentOS中搭建Redis伪分布式集群

再来安装redis和ruby的接口:

gem install redis

出现如下信息,说明安装成功:

CentOS中搭建Redis伪分布式集群

此时再来执行创建redis集群的命令:

./redis-trib.rb create --replicas 1 192.168.242.10:6380 192.168.242.10:6381 192.168.242.10:6382 192.168.242.10:6383 192.168.242.10:6384 192.168.242.10:6385

出现如下信息,说明redis集群启动成功:

>>> Creating cluster

>>> Performing hash slots allocation on 6 nodes...

Using 3 masters:

192.168.242.10:6380

192.168.242.10:6381

192.168.242.10:6382

Adding replica 192.168.242.10:6383 to 192.168.242.10:6380

Adding replica 192.168.242.10:6384 to 192.168.242.10:6381

Adding replica 192.168.242.10:6385 to 192.168.242.10:6382

M: cd9a1cfb495fd728ebf86fe3c2ae484d33a55efa 192.168.242.10:6380

slots:0-5460 (5461 slots) master

M: 86ff5dd6c91f2b1f723d20e0611b6b000b5564b9 192.168.242.10:6381

slots:5461-10922 (5462 slots) master

M: c7124e5837d89a108d4fedce46d18d190d831355 192.168.242.10:6382

slots:10923-16383 (5461 slots) master

S: 1eae195a6d3628b7ac4c2152518cd0afd2dcc4a2 192.168.242.10:6383

replicates cd9a1cfb495fd728ebf86fe3c2ae484d33a55efa

S: 3dd781d5161ac8367cbf16b97414f7ecbb271fd1 192.168.242.10:6384

replicates 86ff5dd6c91f2b1f723d20e0611b6b000b5564b9

S: a942e13b48d63e54f22425a4694fb6ba71b7be89 192.168.242.10:6385

replicates c7124e5837d89a108d4fedce46d18d190d831355

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.242.10:6380)

M: cd9a1cfb495fd728ebf86fe3c2ae484d33a55efa 192.168.242.10:6380

slots:0-5460 (5461 slots) master

1 additional replica(s)

S: a942e13b48d63e54f22425a4694fb6ba71b7be89 192.168.242.10:6385

slots: (0 slots) slave

replicates c7124e5837d89a108d4fedce46d18d190d831355

S: 1eae195a6d3628b7ac4c2152518cd0afd2dcc4a2 192.168.242.10:6383

slots: (0 slots) slave

replicates cd9a1cfb495fd728ebf86fe3c2ae484d33a55efa

M: 86ff5dd6c91f2b1f723d20e0611b6b000b5564b9 192.168.242.10:6381

slots:5461-10922 (5462 slots) master

1 additional replica(s)

M: c7124e5837d89a108d4fedce46d18d190d831355 192.168.242.10:6382

slots:10923-16383 (5461 slots) master

1 additional replica(s)

S: 3dd781d5161ac8367cbf16b97414f7ecbb271fd1 192.168.242.10:6384

slots: (0 slots) slave

replicates 86ff5dd6c91f2b1f723d20e0611b6b000b5564b9

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

验证redis集群

直接通过redis-cli工具来连接集群,该redis-cli工具也是在src目录下的。

./redis-cli -h 192.168.242.10 -c -p 6380

注意:

需要加参数 -c 可连接到集群,-p的端口只要是集群中某个节点的端口就行。

连接成功后,测试往redis集群中存入数据和取数据:

CentOS中搭建Redis伪分布式集群

CentOS中搭建Redis伪分布式集群

相关推荐