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

[经验分享] Redis 集群安装

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-1-12 09:09:31 | 显示全部楼层 |阅读模式
我们使用一台机器,模拟6个redis实例来创建redis集群,其中3主3从。
  • 创建目录,copy配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[iyunv@localhost bin]# cd /application/redis-3.2/
[iyunv@localhost redis-3.2]# ls
bin  conf  dump.rdb
[iyunv@localhost redis-3.2]# mkdir cluster
[iyunv@localhost redis-3.2]# ls
bin  cluster  conf  dump.rdb
[iyunv@localhost redis-3.2]# cd cluster/
[iyunv@localhost cluster]# ls
[iyunv@localhost cluster]# ls
[iyunv@localhost cluster]# mkdir {7000,7001,7002,7003,7004,7005}
[iyunv@localhost cluster]# cp ../conf/redis.conf 7000
[iyunv@localhost cluster]# cp ../conf/redis.conf 7001
[iyunv@localhost cluster]# cp ../conf/redis.conf 7002
[iyunv@localhost cluster]# cp ../conf/redis.conf 7003
[iyunv@localhost cluster]# cp ../conf/redis.conf 7004
[iyunv@localhost cluster]# cp ../conf/redis.conf 7005




2. 修改配置文件
1
2
3
4
5
6
7
8
9
10
[iyunv@localhost cluster]# cd 7000
[iyunv@localhost 7000]# vi redis.conf
#做如下修改
port 7000
daemonize yes
cluster-enabled yes
cluster-config-file nodes-7000.conf
cluster-node-timeout 5000
appendonly yes
dir /application/redis-3.2/cluster/7000/



按照此方式修改7001~7005的配置文件,注意修改端口号。

3. 启动各个实例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[iyunv@localhost cluster]# ls
7000  7001  7002  7003  7004  7005
[iyunv@localhost cluster]# ../bin/redis-server 7000/redis.conf
[iyunv@localhost cluster]# ../bin/redis-server 7001/redis.conf
[iyunv@localhost cluster]# ../bin/redis-server 7002/redis.conf
[iyunv@localhost cluster]# ../bin/redis-server 7003/redis.conf
[iyunv@localhost cluster]# ../bin/redis-server 7004/redis.conf
[iyunv@localhost cluster]# ../bin/redis-server 7005/redis.conf
[iyunv@localhost cluster]# ps -ef | grep redis
root     31949     1  0 12:00 ?        00:00:00 ../bin/redis-server 127.0.0.1:7000 [cluster]
root     31954     1  0 12:00 ?        00:00:00 ../bin/redis-server 127.0.0.1:7001 [cluster]
root     31977     1  0 12:01 ?        00:00:00 ../bin/redis-server 127.0.0.1:7002 [cluster]
root     31982     1  0 12:01 ?        00:00:00 ../bin/redis-server 127.0.0.1:7003 [cluster]
root     31986     1  0 12:01 ?        00:00:00 ../bin/redis-server 127.0.0.1:7004 [cluster]
root     31991     1  0 12:01 ?        00:00:00 ../bin/redis-server 127.0.0.1:7005 [cluster]
root     31996 31349  0 12:01 pts/2    00:00:00 grep redis




4. 创建集群
  现在我们已经有了六个正在运行中的 Redis 实例, 接下来我们需要使用这些实例来创建集群, 并为每个节点编写配置文件。
通过使用 Redis 集群命令行工具redis-trib,编写节点配置文件的工作可以非常容易地完成redis-trib位于Redis 源码的src文件夹中,它是一个 Ruby 程序,这个程序通过向实例发送特殊命令来完成创建新集群,检查集群,或者对集群进行重新分片(reshared)等工作。
我们需要执行以下命令来创建集群:
1
2
[iyunv@localhost 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



系统中没有安装ruby,所以报上面的错误。
先安装ruby
[iyunv@localhost yum.repos.d]# yum install ruby
[iyunv@localhost yum.repos.d]# yum install rubygems
[iyunv@localhost yum.repos.d]# gem install redis
Successfully installed redis-3.2.2
1 gem installed
Installing ri documentation for redis-3.2.2...
Installing RDoc documentation for redis-3.2.2...

再次创建集群
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@localhost 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
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
127.0.0.1:7000
127.0.0.1:7001
127.0.0.1:7002
Adding replica 127.0.0.1:7003 to 127.0.0.1:7000
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
M: ac733323e529323b04b57fb13ddcdeb7c7db23fd 127.0.0.1:7000
   slots:0-5460 (5461 slots) master
M: 8e96fae16b8d92911e579ade8050829567427c8b 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
M: 3c0f95416608ce682fa7a18ee782111a7c4effda 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
S: 855bdb49452a2be3a3ab0a72b5270d057a7f2c9f 127.0.0.1:7003
   replicates ac733323e529323b04b57fb13ddcdeb7c7db23fd
S: 7ab4da5d4c822ab55a1dff1d24a1ff83a0c98260 127.0.0.1:7004
   replicates 8e96fae16b8d92911e579ade8050829567427c8b
S: fbe78c944af98f9cfd75c4dae87201b9c2572c7a 127.0.0.1:7005
   replicates 3c0f95416608ce682fa7a18ee782111a7c4effda
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:7000)
M: ac733323e529323b04b57fb13ddcdeb7c7db23fd 127.0.0.1:7000
   slots:0-5460 (5461 slots) master
M: 8e96fae16b8d92911e579ade8050829567427c8b 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
M: 3c0f95416608ce682fa7a18ee782111a7c4effda 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
M: 855bdb49452a2be3a3ab0a72b5270d057a7f2c9f 127.0.0.1:7003
   slots: (0 slots) master
   replicates ac733323e529323b04b57fb13ddcdeb7c7db23fd
M: 7ab4da5d4c822ab55a1dff1d24a1ff83a0c98260 127.0.0.1:7004
   slots: (0 slots) master
   replicates 8e96fae16b8d92911e579ade8050829567427c8b
M: fbe78c944af98f9cfd75c4dae87201b9c2572c7a 127.0.0.1:7005
   slots: (0 slots) master
   replicates 3c0f95416608ce682fa7a18ee782111a7c4effda
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.



Redis自动选择主从。

5. 连接集群

redis-cli 也可以作为集群的客户端工具,要想访问集群,只需连接任意一个redis实例即可。使用-c参数
1
2
[iyunv@localhost bin]# ./redis-cli -c -p 7000
127.0.0.1:7000>



1
2
3
4
5
127.0.0.1:7000> set name zhangsanfeng
-> Redirected to slot [5798] located at 127.0.0.1:7001
OK
127.0.0.1:7001> get name
"zhangsanfeng"



set 命令写数据,集群将数据写到7001实例上,当你使用get命令获取数据时,客户端即自动切换到7001端口。
redis-cli对集群的支持是非常基本的, 所以它总是依靠 Redis 集群节点来将它转向(redirect)至正确的节点。一个真正的(serious)集群客户端应该做得比这更好: 它应该用缓存记录起哈希槽与节点地址之间的映射(map), 从而直接将命令发送到正确的节点上面。


运维网声明 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-163299-1-1.html 上篇帖子: CentOS 6.5下Redis安装配置记录 下篇帖子: Redis的安装和使用之二------phpredis与phpRedisAdmin
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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