首页 » Ruby » 服务器上redis集群搭建

服务器上redis集群搭建

原文 http://blog.csdn.net/zhaoyahui_666/article/details/78717872

2017-12-06 02:01:08阅读(197)

我是用一台机器6个节点,创建出3 master、3 salve。

redis 采用 redis-3.2.4 版本。

安装过程

1、创建安装目录

mkdir /home/user/redis/download
2、下载并解压
mkdir /home/user/redis/download
wget http://download.redis.io/releases/redis-3.2.4.tar.gz
tar -zxvf redis-3.2.4.tar.gz
3、编译安装

cd redis-3.2.4
make && make install
4、创建redis节点

mkdir /home/user/redis/redis-cluster
cd /home/user/redis/redis-cluster
mkdir 7001 7002 7003 7004 7005 7006
cd /home/user/redis/download
cp redis-3.2.4.tar.gz /home/user/redis/redis-cluster/7001
cp redis-3.2.4.tar.gz /home/user/redis/redis-cluster/7002
cp redis-3.2.4.tar.gz /home/user/redis/redis-cluster/7003
cp redis-3.2.4.tar.gz /home/user/redis/redis-cluster/7004
cp redis-3.2.4.tar.gz /home/user/redis/redis-cluster/7005
cp redis-3.2.4.tar.gz /home/user/redis/redis-cluster/7006

配置各个节点

cd /home/user/redis/redis-cluster/7001
tar -zxvf redis-3.2.4.tar.gz
cd redis-3.2.4
vim redis.conf
port  7001                                        //端口7000,7002,7003        
bind 本机ip                                       //默认ip为127.0.0.1 需要改为其他节点机器可访问的ip 否则创建集群时无法访问对应的端口,无法创建集群
daemonize    yes                               //redis后台运行
pidfile  /var/run/redis_7001.pid          //pidfile文件对应7000,7001,7002
cluster-enabled  yes                           //开启集群  把注释#去掉
cluster-config-file  nodes_7001.conf   //集群的配置  配置文件首次启动自动生成 7000,7001,7002
cluster-node-timeout  15000                //请求超时  默认15秒,可自行设置
appendonly  yes                           //aof日志开启  有需要就开启,它会每次写操作都记录一条日志
重复此步骤,配置其他节点

5、启动各节点

cd /home/user/redis/redis-cluster/7001/
redis-server redis.conf
6、检查 redis 启动情况

[root@iZ2zeheg4tqt4xca0kdo1kZ /]# ps -ef | grep redis
root     10878     1  0 Dec04 ?        00:00:12 redis-server 127.0.0.1:7001 [cluster]
root     10903     1  0 Dec04 ?        00:00:12 redis-server 127.0.0.1:7002 [cluster]
root     10923     1  0 Dec04 ?        00:00:12 redis-server 127.0.0.1:7004 [cluster]
root     10930     1  0 Dec04 ?        00:00:12 redis-server 127.0.0.1:7003 [cluster]
root     10938     1  0 Dec04 ?        00:00:12 redis-server 127.0.0.1:7005 [cluster]
root     10951     1  0 Dec04 ?        00:00:12 redis-server 127.0.0.1:7006 [cluster]
root     17848 17695  0 11:26 pts/1    00:00:00 redis-cli -h 127.0.0.1 -c -p 7002
root     17862  1883  0 11:49 pts/0    00:00:00 grep redis
[root@iZ2zeheg4tqt4xca0kdo1kZ /]# netstat -tnlp | grep redis
tcp        0      0 127.0.0.1:17001             0.0.0.0:*                   LISTEN      10878/redis-server  
tcp        0      0 127.0.0.1:17002             0.0.0.0:*                   LISTEN      10903/redis-server  
tcp        0      0 127.0.0.1:17003             0.0.0.0:*                   LISTEN      10930/redis-server  
tcp        0      0 127.0.0.1:17004             0.0.0.0:*                   LISTEN      10923/redis-server  
tcp        0      0 127.0.0.1:17005             0.0.0.0:*                   LISTEN      10938/redis-server  
tcp        0      0 127.0.0.1:17006             0.0.0.0:*                   LISTEN      10951/redis-server  
tcp        0      0 127.0.0.1:7001              0.0.0.0:*                   LISTEN      10878/redis-server  
tcp        0      0 127.0.0.1:7002              0.0.0.0:*                   LISTEN      10903/redis-server  
tcp        0      0 127.0.0.1:7003              0.0.0.0:*                   LISTEN      10930/redis-server  
tcp        0      0 127.0.0.1:7004              0.0.0.0:*                   LISTEN      10923/redis-server  
tcp        0      0 127.0.0.1:7005              0.0.0.0:*                   LISTEN      10938/redis-server  
tcp        0      0 127.0.0.1:7006              0.0.0.0:*                   LISTEN      10951/redis-server
7、创建集群

创建集群需要适用ruby环境提供的redis接口,所以首先安装ruby环境和接口。这个可以参考http://blog.csdn.net/zhaoyahui_666/article/details/78716511。

安装好环境后,执行创建集群操作。

Redis 官方提供了 redis-trib.rb 这个工具,就在解压目录的 src 目录中,第三步中已将它复制到 /usr/local/bin 目录中,可以直接在命令行中使用了。使用下面这个命令即可完成安装。

[root@iZ2zeheg4tqt4xca0kdo1kZ bin]# 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
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
127.0.0.1:7001
127.0.0.1:7002
127.0.0.1:7003
Adding replica 127.0.0.1:7004 to 127.0.0.1:7001
Adding replica 127.0.0.1:7005 to 127.0.0.1:7002
Adding replica 127.0.0.1:7006 to 127.0.0.1:7003
M: 241c84a7dead6708296fda6929c6408193c00880 127.0.0.1:7001
   slots:0-5460 (5461 slots) master
M: 68052f0fe49468d376eaada2280ea1a0a6b48065 127.0.0.1:7002
   slots:5461-10922 (5462 slots) master
M: 48f57f10f1cb5e5b3601546630f759869146f904 127.0.0.1:7003
   slots:10923-16383 (5461 slots) master
S: 3b7df0c92cf16d346ae90ebea41692ae0283228e 127.0.0.1:7004
   replicates 241c84a7dead6708296fda6929c6408193c00880
S: 2cd82cb7fc136382ff165a36c713d3644b83c21f 127.0.0.1:7005
   replicates 68052f0fe49468d376eaada2280ea1a0a6b48065
S: 060829dc91dab9b850fe994aff0eea8a9de06b9f 127.0.0.1:7006
   replicates 48f57f10f1cb5e5b3601546630f759869146f904
Can I set the above configuration? (type 'yes' to accept):
输入yes

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 127.0.0.1:7001)
M: 241c84a7dead6708296fda6929c6408193c00880 127.0.0.1:7001
   slots:0-5460 (5461 slots) master
M: 68052f0fe49468d376eaada2280ea1a0a6b48065 127.0.0.1:7002
   slots:5461-10922 (5462 slots) master
M: 48f57f10f1cb5e5b3601546630f759869146f904 127.0.0.1:7003
   slots:10923-16383 (5461 slots) master
M: 3b7df0c92cf16d346ae90ebea41692ae0283228e 127.0.0.1:7004
   slots: (0 slots) master
   replicates 241c84a7dead6708296fda6929c6408193c00880
M: 2cd82cb7fc136382ff165a36c713d3644b83c21f 127.0.0.1:7005
   slots: (0 slots) master
   replicates 68052f0fe49468d376eaada2280ea1a0a6b48065
M: 060829dc91dab9b850fe994aff0eea8a9de06b9f 127.0.0.1:7006
   slots: (0 slots) master
   replicates 48f57f10f1cb5e5b3601546630f759869146f904
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
8、验证

登录7001客户端存储值,登录7002客户端取值

[root@iZ2zeheg4tqt4xca0kdo1kZ redis-3.2.1]# redis-cli -h 127.0.0.1 -c -p 7001
127.0.0.1:7001> set hello world
OK
127.0.0.1:7001> get hello
"world"

[root@iZ2zeheg4tqt4xca0kdo1kZ redis-3.2.1]# redis-cli -h 127.0.0.1 -c -p 7002
127.0.0.1:7002> get hello
-> Redirected to slot [866] located at 127.0.0.1:7001
"world"
127.0.0.1:7001> keys *
1) "hello"

redis集群搭建结束



最新发布

CentOS专题

关于本站

5ibc.net旗下博客站精品博文小部分原创、大部分从互联网收集整理。尊重作者版权、传播精品博文,让更多编程爱好者知晓!

小提示

按 Ctrl+D 键,
把本文加入收藏夹