设为首页 收藏本站
查看: 1190|回复: 0

[经验分享] redis3.0集群

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-1-5 08:28:38 | 显示全部楼层 |阅读模式
操作系统环境
1
2
3
4
5
[iyunv@62290133db0c ~]# cat /etc/issue
CentOS release 6.7 (Final)
Kernel \r on an \m
[iyunv@62290133db0c ~]# getconf LONG_BIT
64




redis安装请参考redis安装一遍博文,redis集群是在3.0中推出的集群功能...........

由于资源有限,只好在一台机器上搭建redis集群环境,redis版本3.0.6,要让集群正常运作至少需要三个主节点,不过在刚开始试用集群功能时, 强烈建议使用六个节点: 其中三个为主节点, 而其余三个则是各个主节点的从节点。

redis
dirport
6381/opt/63816381
6382/opt/63826382
6383/opt/63836383
6384/opt/63846384
6385/opt/63856385
6386/opt/63866386

创建redis相应的目录       
1
2
3
4
5
6
7
8
9
10
[iyunv@62290133db0c ~]# mkdir /opt/638{1..6}
[iyunv@62290133db0c ~]# tree /opt/
/opt/
├── 6381
├── 6382
├── 6383
├── 6384
├── 6385
└── 6386
6 directories, 0 files




拷贝配置文件以及redis启动redis-server文件
1
2
3
4
5
6
7
8
[iyunv@62290133db0c ~]# cp /usr/local/src/redis-3.0.6/redis.conf /opt/6381/
[iyunv@62290133db0c ~]# cp /usr/local/src/redis-3.0.6/src/redis-server /opt/6381/
[iyunv@62290133db0c ~]# cp /usr/local/src/redis-3.0.6/src/redis-server /opt/6382/
[iyunv@62290133db0c ~]# cp /usr/local/src/redis-3.0.6/src/redis-server /opt/6383/
[iyunv@62290133db0c ~]# cp /usr/local/src/redis-3.0.6/src/redis-server /opt/6384/
[iyunv@62290133db0c ~]# cp /usr/local/src/redis-3.0.6/src/redis-server /opt/6385/
[iyunv@62290133db0c ~]# cp /usr/local/src/redis-3.0.6/src/redis-server /opt/6386/
[iyunv@62290133db0c ~]# cd /opt/6381/




6381配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[iyunv@62290133db0c 6381]# vim redis.conf
daemonize yes
pidfile /var/run/redis-6381.pid
port 6381
tcp-keepalive 60
logfile "/var/log/redis/6381.log"
dbfilename dump.rdb
dir /opt/6381/
slave-serve-stale-data yes
appendonly yes
appendfilename "appendonly-6381.aof"
appendfsync everysec
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000




拷贝redis配置文件到其他redis对应的目录下,并修改redis的配置文件
1
2
3
4
5
6
7
8
9
10
[iyunv@62290133db0c ~]# cp /opt/6381/redis.conf /opt/6382/
[iyunv@62290133db0c ~]# cp /opt/6381/redis.conf /opt/6383/
[iyunv@62290133db0c ~]# cp /opt/6381/redis.conf /opt/6384/
[iyunv@62290133db0c ~]# cp /opt/6381/redis.conf /opt/6385/
[iyunv@62290133db0c ~]# cp /opt/6381/redis.conf /opt/6386/
[iyunv@62290133db0c ~]# sed -i "s#6381#6382#g" /opt/6382/redis.conf
[iyunv@62290133db0c ~]# sed -i "s#6381#6383#g" /opt/6383/redis.conf
[iyunv@62290133db0c ~]# sed -i "s#6381#6384#g" /opt/6384/redis.conf
[iyunv@62290133db0c ~]# sed -i "s#6381#6385#g" /opt/6385/redis.conf
[iyunv@62290133db0c ~]# sed -i "s#6381#6386#g" /opt/6386/redis.conf





分别启动6381..6386
1
2
3
4
5
6
[iyunv@62290133db0c ~]# /opt/6381/redis-server /opt/6381/redis.conf
[iyunv@62290133db0c ~]# /opt/6382/redis-server /opt/6382/redis.conf
[iyunv@62290133db0c ~]# /opt/6383/redis-server /opt/6383/redis.conf
[iyunv@62290133db0c ~]# /opt/6384/redis-server /opt/6384/redis.conf
[iyunv@62290133db0c ~]# /opt/6385/redis-server /opt/6385/redis.conf
[iyunv@62290133db0c ~]# /opt/6386/redis-server /opt/6386/redis.conf




查看redis进程
1
2
3
4
5
6
7
8
[iyunv@62290133db0c ~]# ps -ef|grep redis|grep -v grep
root        809      1  0 09:17 ?        00:00:00 /opt/6381/redis-server *:6381 [cluster]   
root        815      1  0 09:18 ?        00:00:00 /opt/6382/redis-server *:6382 [cluster]   
root        821      1  0 09:18 ?        00:00:00 /opt/6383/redis-server *:6383 [cluster]   
root        825      1  0 09:18 ?        00:00:00 /opt/6384/redis-server *:6384 [cluster]   
root        829      1  0 09:18 ?        00:00:00 /opt/6385/redis-server *:6385 [cluster]   
root        833      1  0 09:18 ?        00:00:00 /opt/6386/redis-server *:6386 [cluster]
注:以上只是把各个redis节点启动成功




创建redis集群
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
[iyunv@62290133db0c ~]# redis-trib.rb create ----replicas 1 172.17.0.1:6381 172.17.0.1:6382 172.17.0.1:6383 172.17.0.1:6384 172.17.0.1:6385 172.17.0.1:6386
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
172.17.0.1:6381
172.17.0.1:6382
172.17.0.1:6383
Adding replica 172.17.0.1:6384 to 172.17.0.1:6381
Adding replica 172.17.0.1:6385 to 172.17.0.1:6382
Adding replica 172.17.0.1:6386 to 172.17.0.1:6383
M: d3b7db6b7b2696be3e2dbb74d99d33e6a3b31658 172.17.0.1:6381
   slots:0-5460 (5461 slots) master
M: be37cf292b638d919e9e39fc3ebe3984697f6026 172.17.0.1:6382
   slots:5461-10922 (5462 slots) master
M: 5e5f54956d424ac491820f10ef0e8be443e1d98f 172.17.0.1:6383
   slots:10923-16383 (5461 slots) master
S: ddf29c1716a7b111f26cfa2376edbf84b7e4f471 172.17.0.1:6384
   replicates d3b7db6b7b2696be3e2dbb74d99d33e6a3b31658
S: 54c405412761e7714062260c7080e0f904d04cfe 172.17.0.1:6385
   replicates be37cf292b638d919e9e39fc3ebe3984697f6026
S: b0d8b47bf039f65e452a661613d97f62ba569dfb 172.17.0.1:6386
   replicates 5e5f54956d424ac491820f10ef0e8be443e1d98f
Can I set the above configuration? (type 'yes' to accept): yes //同意输入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 172.17.0.1:6381)
M: d3b7db6b7b2696be3e2dbb74d99d33e6a3b31658 172.17.0.1:6381
   slots:0-5460 (5461 slots) master
M: be37cf292b638d919e9e39fc3ebe3984697f6026 172.17.0.1:6382
   slots:5461-10922 (5462 slots) master
M: 5e5f54956d424ac491820f10ef0e8be443e1d98f 172.17.0.1:6383
   slots:10923-16383 (5461 slots) master
M: ddf29c1716a7b111f26cfa2376edbf84b7e4f471 172.17.0.1:6384
   slots: (0 slots) master
   replicates d3b7db6b7b2696be3e2dbb74d99d33e6a3b31658
M: 54c405412761e7714062260c7080e0f904d04cfe 172.17.0.1:6385
   slots: (0 slots) master
   replicates be37cf292b638d919e9e39fc3ebe3984697f6026
M: b0d8b47bf039f65e452a661613d97f62ba569dfb 172.17.0.1:6386
   slots: (0 slots) master
   replicates 5e5f54956d424ac491820f10ef0e8be443e1d98f
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.




登陆任意redis节点
1
2
3
4
5
6
7
8
[iyunv@62290133db0c ~]# redis-cli -c -p 6381
127.0.0.1:6381> CLUSTER NODES //各个节点以及主从对应的信息
54c405412761e7714062260c7080e0f904d04cfe 172.17.0.1:6385 slave be37cf292b638d919e9e39fc3ebe3984697f6026 0 1451889401026 5 connected
5e5f54956d424ac491820f10ef0e8be443e1d98f 172.17.0.1:6383 master - 0 1451889401529 3 connected 10923-16383
ddf29c1716a7b111f26cfa2376edbf84b7e4f471 172.17.0.1:6384 slave d3b7db6b7b2696be3e2dbb74d99d33e6a3b31658 0 1451889400017 4 connected
d3b7db6b7b2696be3e2dbb74d99d33e6a3b31658 172.17.0.1:6381 myself,master - 0 0 1 connected 0-5460
b0d8b47bf039f65e452a661613d97f62ba569dfb 172.17.0.1:6386 slave 5e5f54956d424ac491820f10ef0e8be443e1d98f 0 1451889402033 6 connected
be37cf292b638d919e9e39fc3ebe3984697f6026 172.17.0.1:6382 master - 0 1451889400522 2 connected 5461-10922




登陆redis 6381
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[iyunv@62290133db0c ~]# redis-cli -c -p 6381
127.0.0.1:6381> set name zxl
-> Redirected to slot [5798] located at 172.17.0.1:6382
OK
172.17.0.1:6382> set age 33
-> Redirected to slot [741] located at 172.17.0.1:6381
OK
172.17.0.1:6381> KEYS *
1) "age"
172.17.0.1:6381> GET age
"33"
172.17.0.1:6381>
[iyunv@62290133db0c ~]# redis-cli -c -p 6382
127.0.0.1:6382> KEYS *
1) "name"
127.0.0.1:6382> get name
"zxl"
127.0.0.1:6382>



注:以上就是redis集群创建




创建redis集群时,错误总结如下:
1
2
3
[iyunv@62290133db0c ~]# cp /usr/local/src/redis-3.0.6/src/redis-trib.rb /usr/local/bin/
[iyunv@62290133db0c ~]# redis-trib.rb create --replicas 1 172.17.0.1:6381 172.17.0.1:6382 172.17.0.1:6383 172.17.0.1:6384 172.17.0.1:6385 172.17.0.1:6386
/usr/bin/env: ruby: No such file or directory



注:缺少ruby环境,需安装
1
[iyunv@62290133db0c ~]# yum install ruby -y




提示如下:
1
2
3
[iyunv@62290133db0c ~]# redis-trib.rb create --replicas 1 172.17.0.1:6381 172.17.0.1:6382 172.17.0.1:6383 172.17.0.1:6384 172.17.0.1:6385 172.17.0.1:6386
/usr/local/bin/redis-trib.rb:24:in `require': no such file to load -- rubygems (LoadError)
from /usr/local/bin/redis-trib.rb:24



注:需安装rubygems
1
[iyunv@62290133db0c ~]# yum install rubygems -y




提示如下:
1
2
3
4
[iyunv@62290133db0c ~]# redis-trib.rb create --replicas 1 172.17.0.1:6381 172.17.0.1:6382 172.17.0.1:6383 172.17.0.1:6384 172.17.0.1:6385 172.17.0.1:6386
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- redis (LoadError)
from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
from /usr/local/bin/redis-trib.rb:25




注:提示不能加载redis,是因为缺少redis和ruby的接口,使用gem安装
1
2
3
4
5
[iyunv@62290133db0c ~]# gem install redis
Successfully installed redis-3.2.1
1 gem installed
Installing ri documentation for redis-3.2.1...
Installing RDoc documentation for redis-3.2.1...






运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-160391-1-1.html 上篇帖子: Redis学习 下篇帖子: redis主从同步及切换主从配置示例
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表