目录
预热
上一篇我们一步步搭建了redis的主从复制集群和哨兵集群
这一篇我们我们来搭建分布式集群,为了方便,直接在一台服务器上搭建一个伪分布式集群
二话不说我们先把之前的redis进程都杀掉
[root@xinyeshuaiqi ~]# ps -ef | grep redis
root 8730 1 0 Oct29 ? 00:02:03 redis-server *:6382
root 8742 1 0 Oct29 ? 00:02:10 redis-server *:6383
root 11390 1 0 Oct29 ? 00:03:23 redis-sentinel *:26379 [sentinel]
root 11396 1 0 Oct29 ? 00:03:23 redis-sentinel *:26479 [sentinel]
root 11400 1 0 Oct29 ? 00:03:23 redis-sentinel *:26579 [sentinel]
root 13236 1 0 Oct29 ? 00:02:10 redis-server *:6381
root 17788 17767 0 12:41 pts/0 00:00:00 grep --color=auto redis
[root@xinyeshuaiqi ~]# kill -9 8730 8742 11390 11396 11400 13236
redis分布式集群
比如集群3个实例:server1,server2,server3
都启动后开始认领槽位
比如
server1:1,3,4,7….
server2:2,6,9,10,11…
server3:5,8,12,13…
然后客户端每次来一个key都会对应到一个槽位(见上面的计算公式)
集群搭建
好久没搞了,之前在虚拟机上搞过一次,搞了半天才搞成
这次在阿里云服务器上再试下
[root@xinyeshuaiqi ~]# cd redis-cluster/
[root@xinyeshuaiqi redis-cluster]#
[root@xinyeshuaiqi redis-cluster]# mkdir 700{1,2,3,4,5,6}
[root@xinyeshuaiqi redis-cluster]# ls
7001 7002 7003 7004 7005 7006
创建redis-cluster目录,并在其中创建7001-7006目录,非别对应redis集群中的三主三从
安装ruby支持 yum install ruby rubygems -y
这个默认安装的版本比较低,我们先这样走,后面遇到问题再去解决
在7001,7002,7003,7004,7005,7006中
都创建文件 vi redis.conf
文件内容如下: (port分别为7001、7002、7003、7004、7005、7006)
cluster-enabled yesport 7001daemonize yes
然后分别在各目录中启动
[root@xinyeshuaiqi 7006]# vi redis.conf
[root@xinyeshuaiqi 7006]# redis-server redis.conf
还是挺麻烦的。。。
[root@xinyeshuaiqi 7006]# ps -ef | grep redis
root 22462 1 0 14:13 ? 00:00:00 redis-server *:7001 [cluster]
root 22521 1 0 14:14 ? 00:00:00 redis-server *:7002 [cluster]
root 22577 1 0 14:15 ? 00:00:00 redis-server *:7003 [cluster]
root 22691 1 0 14:17 ? 00:00:00 redis-server *:7004 [cluster]
root 22748 1 0 14:18 ? 00:00:00 redis-server *:7005 [cluster]
root 22760 1 0 14:18 ? 00:00:00 redis-server *:7006 [cluster]
找到这个东西
./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
会说ruby的版本太低。。。
这个卡了我半天!擦
[root@xinyeshuaiqi src]# ruby -v
ruby 2.0.0p648 (2015-12-16) [x86_64-linux]
我们需要更改ruby版本
还需要安装rvm
- Install GPG keys:
gpg2 --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
In case you encounter an issues or want to know more, check security
- Install RVM:
\curl -sSL https://get.rvm.io | bash -s stable
完整步骤:
1、sudo yum install curl 安装curl
2、http://www.rvm.io/ 官网首页就可以看到
gpg –keyserver hkp://keys.gnupg.net –recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
3、下载rvm \curl -sSL https://get.rvm.io | bash -s stable
4、查找配置文件 find / -name rvm.sh
5、配置文件生效 source /etc/profile.d/rvm.sh
6、下载rvm依赖 rvm requirements
7、查看rvm库ruby版本 rvm list known
8、安装ruby指定版本 rvm install ruby-2.4.1
9、使用ruby版本默认 rvm use 2.4.1 default10、gem install redis
这一次给我整的明明白白的
其实上面的步骤我到滴3部就卡住了,rvm下不下来
换一个方法,找一个ruby新版本的压缩包,直接tar -zxvf解压再make编译
https://cache.ruby-lang.org/pub/ruby/2.3/
下载好了上传至服务器
yum install lrzsz
使centOS支持文件上传功能
tar -zxvf ruby-2.3.4.tar.gz
cd ruby-2.3.4
./configure -prefix=/usr/local/ruby
make
maek install
cp ruby /bin
覆盖原有ruby
再执行gem install redis
再重新执行一波【pwd : /root/redis-3.2.12/src】
[root@xinyeshuaiqi src]# ./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
M: 92083f8380dc90e79cd791ed7f42182b590bd0ef 127.0.0.1:7001
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 5ebd2ca34fa8c682cc7f315abd6b87ba50ae0e1b 127.0.0.1:7005
slots: (0 slots) slave
replicates 5f8de806751781fa4e977a9b29305cb7261c8238
M: acb94b688bc3b7b664b6fa13b43ecf008b9138c4 127.0.0.1:7003
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 027c7cdab4127ec76d8a45e2da335d6325c01869 127.0.0.1:7006
slots: (0 slots) slave
replicates acb94b688bc3b7b664b6fa13b43ecf008b9138c4
S: 94cb5393f3aa093fe3cbb4c0658fa71161736fcf 127.0.0.1:7004
slots: (0 slots) slave
replicates 92083f8380dc90e79cd791ed7f42182b590bd0ef
M: 5f8de806751781fa4e977a9b29305cb7261c8238 127.0.0.1:7002
slots:5461-10922 (5462 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
开始分发槽位
根据上面的信息
0-5460
属于7001的redis实例(master)
5461-10922
属于7002的redis实例(master)
10923-16383
属于7003的redis实例(master)
其他三个是slave,slave分配0个槽位,作为master的冷备
至此redis分布式集群搭建完毕,亲测哈
你就按照我的步骤一步步来,遇到问题按我提供的方法解决,应该没啥问题
过程中遇到任何问题,欢迎留言
[root@xinyeshuaiqi src]# ./redis-trib.rb check 127.0.0.1:7001
>>> Performing Cluster Check (using node 127.0.0.1:7001)
M: 92083f8380dc90e79cd791ed7f42182b590bd0ef 127.0.0.1:7001
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 5ebd2ca34fa8c682cc7f315abd6b87ba50ae0e1b 127.0.0.1:7005
slots: (0 slots) slave
replicates 5f8de806751781fa4e977a9b29305cb7261c8238
M: acb94b688bc3b7b664b6fa13b43ecf008b9138c4 127.0.0.1:7003
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 027c7cdab4127ec76d8a45e2da335d6325c01869 127.0.0.1:7006
slots: (0 slots) slave
replicates acb94b688bc3b7b664b6fa13b43ecf008b9138c4
S: 94cb5393f3aa093fe3cbb4c0658fa71161736fcf 127.0.0.1:7004
slots: (0 slots) slave
replicates 92083f8380dc90e79cd791ed7f42182b590bd0ef
M: 5f8de806751781fa4e977a9b29305cb7261c8238 127.0.0.1:7002
slots:5461-10922 (5462 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
我们知道3个master和3个slave了,具体谁是谁的slave还不知道
我们直接连接按以前的方法去看一哈
[root@xinyeshuaiqi src]# redis-cli -p 7001
127.0.0.1:7001>
127.0.0.1:7001> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=7004,state=online,offset=897,lag=1
master_repl_offset:897
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:896
127.0.0.1:7001>
7001的slave是7004
[root@xinyeshuaiqi src]# redis-cli -p 7002
127.0.0.1:7002> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=7005,state=online,offset=1037,lag=1
master_repl_offset:1037
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:1036
7002的slave是7005
[root@xinyeshuaiqi src]# redis-cli -p 7003
127.0.0.1:7003> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=7006,state=online,offset=1093,lag=1
master_repl_offset:1093
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:1092
7003的slave是7006
这就很舒服了
到现在为止我们就很清晰了
测试一下体现一下槽的概念
127.0.0.1:7003> set key1 ig
(error) MOVED 9189 127.0.0.1:7002
我们在7003做set, 提示让我们去7002
说明该key计算所得的槽不属于7003,而在7002
[root@xinyeshuaiqi src]# redis-cli -p 7002
127.0.0.1:7002> set key1 ig
OK
现在就好了
老实说搭建过程有点恶心,一会缺这个一会那不通过一会这版本太低
end…
转载请注明:汪明鑫的个人博客 » 搭建Redis分片集群
说点什么
您将是第一位评论人!