Reids集群使用的是投票机制,并且需要主备模式的支持,要让集群正常工作至少需要3个主节点。
所以我们搭建redis最小的一个集群需要6个redis实例。
官网下载3.x版本,之前2.x的版本不支持集群模式
- 安装集群所需单机版
redis安装
在六台服务器上安装redis,需要修改默认设置,集群支持默认是关闭的。
vim redis.conf
将cluster-enabled no改为cluster-enabled yes - 安装ruby支持
yum install ruby -y
yum install rubygems -y
- 安装集群依赖包
将redis-3.0.0.gem拷贝到linux系统中,通过gem命令安装ruby的资源包
gem install redis-3.0.0.gem
- 创建集群
redis在构建集群时,要求参与到集群中的所有的节点必须是开启状态的。
通过执行redis源码包的src目录下的redis-trib.rb程序完成集群的创建
./redis-trib.rb create --replicas 1 192.168.10.160:6379 192.168.10.161:6379 192.168.10.162:6379 192.168.10.163:6379 192.168.10.164:6379 192.168.10.165:6379
如果看到如下信息,则表示集群创建成功
>>> Creating cluster Connecting to node 192.168.10.160:6379: OK Connecting to node 192.168.10.161:6379: OK Connecting to node 192.168.10.162:6379: OK Connecting to node 192.168.10.163:6379: OK Connecting to node 192.168.10.164:6379: OK Connecting to node 192.168.10.165:6379: OK >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 192.168.10.160:6379 192.168.10.161:6379 192.168.10.162:6379 Adding replica 192.168.10.165:8004 to 192.168.10.163:6379 Adding replica 192.168.10.165:8005 to 192.168.10.164:6379 Adding replica 192.168.10.165:8006 to 192.168.10.165:6379 M: eb16b47f0a29f9b6110afd51af6ff4838c92139c 192.168.10.160:6379 slots:0-5460 (5461 slots) master M: 3e1297407b99789d7954009a34a3062a1c4ad596 192.168.10.161:6379 slots:5461-10922 (5462 slots) master M: 56443aa25681668781a1fd4ce797a096128a9193 192.168.10.162:6379 slots:10923-16383 (5461 slots) master S: 7631793959189ca7546a152849919281b3e9b2b4 192.168.10.163:6379
replicates eb16b47f0a29f9b6110afd51af6ff4838c92139c S: 63504b8921be39a2867375abfb49b565f2b6e58c 192.168.10.164:6379 replicates 3e1297407b99789d7954009a34a3062a1c4ad596 S: 71726e0483b989bc0b4354370d3ee9100dd7ad18 192.168.10.165:6379 replicates 56443aa25681668781a1fd4ce797a096128a9193 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.10.160:6379) M: eb16b47f0a29f9b6110afd51af6ff4838c92139c 192.168.10.160:6379 slots:0-5460 (5461 slots) master M: 3e1297407b99789d7954009a34a3062a1c4ad596 192.168.10.161:6379 slots:5461-10922 (5462 slots) master M: 56443aa25681668781a1fd4ce797a096128a9193 192.168.10.162:6379 slots:10923-16383 (5461 slots) master M: 7631793959189ca7546a152849919281b3e9b2b4 192.168.10.163:6379 slots: (0 slots) master replicates eb16b47f0a29f9b6110afd51af6ff4838c92139c M: 63504b8921be39a2867375abfb49b565f2b6e58c 192.168.10.164:6379 slots: (0 slots) master replicates 3e1297407b99789d7954009a34a3062a1c4ad596 M: 71726e0483b989bc0b4354370d3ee9100dd7ad18 192.168.10.165:6379 slots: (0 slots) master replicates 56443aa25681668781a1fd4ce797a096128a9193 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered
当集群创建成功后。会在每个实例中添加一个名称为Nodes.conf的文件。该文件中保存的就是当前集群中的信息。所以,下次启动集群时,不需要在次创建集群了,直接启动所有的集群节点即可
- 连接集群
可以使用任意节点实例中的客户端来连接集群中的任意节点。在连接集群时,需要给定两个参数分别为-p表示连接redis的所在的端口。-h表示当前redis所在的主机的ip地址。注意:一定要在连接后添加-c参数,表示当前连接的是一个集群。
/usr/locle/redis/bin/redis-cli -p6379 -h192.168.10.160 -c
- 关闭集群。
其实关闭集群就是当所有的redis实例关闭掉就可以。我们可以使用任意一个redis实例中的客户端(redis-cli)来关闭集群中的任意节点。
可以编写一个批量关闭redis节点的脚本。
vim shutdown.sh
/usr/locle/redis/bin/redis-cli -p6379 -h192.168.10.160 shutdown /usr/locle/redis/bin/redis-cli -p6379 -h192.168.10.161 shutdown /usr/locle/redis/bin/redis-cli -p6379 -h192.168.10.162 shutdown /usr/locle/redis/bin/redis-cli -p6379 -h192.168.10.163 shutdown /usr/locle/redis/bin/redis-cli -p6379 -h192.168.10.164 shutdown /usr/locle/redis/bin/redis-cli -p6379 -h192.168.10.165 shutdown
为该脚本分配执行权限
chmod +x shutdown.sh
网友评论