Linux环境redis集群搭建

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux环境redis集群搭建相关的知识,希望对你有一定的参考价值。

原文:http://blog.csdn.net/yj327243832a/article/details/52785100

原文:http://www.linuxidc.com/Linux/2015-08/121845.htm

 


logfile "/usr/local/redis_cluster/log/"

dir /usr/local/redis_cluster

在redis_cluster目录下创建log文件夹


./redis-server /usr/local/redis_cluster/7004/redis.conf

 

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


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


./redis-trib.rb create --replicas 1 127.0.0.1:7000 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

 

 

(要让集群正常工作至少需要3个主节点,在这里我们要创建6个redis节点,其中三个为主节点,三个为从节点,对应的redis节点的ip和端口对应关系如下)

127.0.0.1:7000
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

 

1、安装redis

上传服务器,解压,编译

tar -zxvf redis-3.2.1.tar.gz

cd redis-3.2.1

make

编译后

cd src

复制

redis-server

redis-cli

两个文件

2、创建集群目录

建立集群对应文件夹redisCluster

在这个文件夹中建6个子文件夹分别为r7000、r7001、r7002、r7003、r7004、r7005

将第一步复制的文件粘贴在上述6个子文件夹中,并授权

chmod 775 redis-cli

chmod 775 redis-server

再每个子文件夹中创建文件redis.conf

文件内容

port 7000

cluster-enabled yes

cluster-config-file nodes.conf

cluster-node-timeout 5000

appendonly yes

daemonize yes

bind 192.168.*.*
protected-mode no

#这两个配置不加上,其他机器无法访问

每个文件的端口号都不一样,分别是7000、7001、7002、7003、7004、7005

3、分别启动这六个redis实例

到对应文件夹中执行

./redis-server redis.conf

查询redis进程如下

[[email protected] redis]# ps -ef|grep redis

root     11092     1  0 14:35 ?        00:00:00 ./redis-server *:7000 [cluster]

root     11296     1  0 14:46 ?        00:00:00 ./redis-server *:7001 [cluster]

root     11403     1  0 14:53 ?        00:00:00 ./redis-server *:7002 [cluster]

root     11441     1  0 14:55 ?        00:00:00 ./redis-server *:7003 [cluster]

root     11486     1  0 14:57 ?        00:00:00 ./redis-server *:7004 [cluster]

root     11532     1  0 14:59 ?        00:00:00 ./redis-server *:7005 [cluster]

root     11545 11135  0 15:00 pts/2    00:00:00 grep --color=auto redis

 

4、创建redis集群

在安装路径的src文件夹下执行

./redis-trib.rb  create --replicas 1 127.0.0.1:7000 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

[[email protected] src]# ./redis-trib.rb create --replicas 1 127.0.0.1:7000 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

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

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

因为是执行的ruby的脚本,需要ruby的环境

安装ruby环境

[[email protected] src]# yum install ruby

再次执行创建集群命令

[[email protected] src]# ./redis-trib.rb create --replicas 1 127.0.0.1:7000 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

/usr/share/rubygems/rubygems/core_ext/kernel_require.rb:55:in `require‘: cannot load such file -- redis (LoadError)

        from /usr/share/rubygems/rubygems/core_ext/kernel_require.rb:55:in `require‘

        from ./redis-trib.rb:25:in `<main>‘

报错cannot load such file -- redis (LoadError)...

缺少rubygems组件,使用yum安装

[[email protected] src]# yum install rubygems

再次执行创建集群命令

 

[[email protected] src]# ./redis-trib.rb create --replicas 1 127.0.0.1:7000 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

/usr/share/rubygems/rubygems/core_ext/kernel_require.rb:55:in `require‘: cannot load such file -- redis (LoadError)

        from /usr/share/rubygems/rubygems/core_ext/kernel_require.rb:55:in `require‘

        from ./redis-trib.rb:25:in `<main>‘

缺少redis和ruby的接口,使用gem 安装

gem install redis

再次执行创建集群命令后,根据提示输入yes后集群创建成功

[[email protected] src]# ./redis-trib.rb create --replicas 1 192.168.8.21:7000 192.168.8.21:7001 192.168.8.21:7002 192.168.8.21:7003 192.168.8.21:7004 192.168.8.21:7005

>>> Creating cluster

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

Using 3 masters:

192.168.8.21:7000

192.168.8.21:7001

192.168.8.21:7002

Adding replica 192.168.8.21:7003 to 192.168.8.21:7000

Adding replica 192.168.8.21:7004 to 192.168.8.21:7001

Adding replica 192.168.8.21:7005 to 192.168.8.21:7002

M: ef86d6276bb1a8bbab07a1bd4ff8dc3f54ea73a1 192.168.8.21:7000

   slots:0-5460 (5461 slots) master

M: 087d6d2f129f00262444d5bfc012b51239ee1636 192.168.8.21:7001

   slots:5461-10922 (5462 slots) master

M: 67bc3416a311770682abaffde395fbfa34d7b5b0 192.168.8.21:7002

   slots:10923-16383 (5461 slots) master

S: 22dceae7e42ff41e098854c476ec0af27a2cbfbc 192.168.8.21:7003

   replicates ef86d6276bb1a8bbab07a1bd4ff8dc3f54ea73a1

S: 3231ff554f46b0310db5ece8a589be96b3098bed 192.168.8.21:7004

   replicates 087d6d2f129f00262444d5bfc012b51239ee1636

S: 167b9645c6f1f347363c4f397c84eca666bd0456 192.168.8.21:7005

   replicates 67bc3416a311770682abaffde395fbfa34d7b5b0

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.8.21:7000)

M: ef86d6276bb1a8bbab07a1bd4ff8dc3f54ea73a1 192.168.8.21:7000

   slots:0-5460 (5461 slots) master

M: 087d6d2f129f00262444d5bfc012b51239ee1636 192.168.8.21:7001

   slots:5461-10922 (5462 slots) master

M: 67bc3416a311770682abaffde395fbfa34d7b5b0 192.168.8.21:7002

   slots:10923-16383 (5461 slots) master

M: 22dceae7e42ff41e098854c476ec0af27a2cbfbc 192.168.8.21:7003

   slots: (0 slots) master

   replicates ef86d6276bb1a8bbab07a1bd4ff8dc3f54ea73a1

M: 3231ff554f46b0310db5ece8a589be96b3098bed 192.168.8.21:7004

   slots: (0 slots) master

   replicates 087d6d2f129f00262444d5bfc012b51239ee1636

M: 167b9645c6f1f347363c4f397c84eca666bd0456 192.168.8.21:7005

   slots: (0 slots) master

   replicates 67bc3416a311770682abaffde395fbfa34d7b5b0

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

5、测试

使用redis-cli命令进入集群环境

./redis-cli -c -p 7000

 

 nodes.conf文件内容

5e21c3a40f5b875d1f46724febc91059ccf3f36c 127.0.0.1:7004 slave 00158021c8af59f49ce83605601d79ef69dfb515 0 1476083335844 5 connected

bebc7ad0769b63d5dad22f59cf7e11e8eff8142e 127.0.0.1:7003 slave 2102328b7c064ece181bf431ae983f936770aef6 0 1476083334841 4 connected

d264a13fb4968cd93c3b700050e38c16000ee0ed 127.0.0.1:7005 slave 086e344b0e6fdee5e0e896c040aadd2cef32f6d9 0 1476083333841 6 connected

086e344b0e6fdee5e0e896c040aadd2cef32f6d9 127.0.0.1:7002 master - 0 1476083335744 3 connected 10923-16383

2102328b7c064ece181bf431ae983f936770aef6 127.0.0.1:7000 myself,master - 0 0 1 connected 0-5460

00158021c8af59f49ce83605601d79ef69dfb515 127.0.0.1:7001 master - 0 1476083334341 2 connected 5461-10922

vars currentEpoch 6 lastVoteEpoch 0

以上是关于Linux环境redis集群搭建的主要内容,如果未能解决你的问题,请参考以下文章

Windows 环境搭建Redis集群

linux下redis集群搭建

Linux环境redis集群搭建

Linux下Redis集群环境的搭建

Linux(ubuntu)安装redis集群,redis集群搭建

Linux(CentOS7) 环境搭建 redis5.0.8 集群