redis集群&主从部署

redis集群&主从部署


1.安装redis,并把src目录中对应的可执行文件建立软连接到/bin目录下


2.新建目录:
mkdir redis_slave_2004
3.修改配置:
将redis.conf文件拷贝一份到redis_slave_2004,修改文件内容为:
daemonize yes
port 2004 #端口配置为对应目录的端口
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes

4.启动集群
由于在上一篇中已经配置好了集群,故先启动集群,参考上篇
启动2004端口的redis
进入目录redis_slave_2004,执行 redis-server redis.conf

5.加入集群
实例加入到集群,我们只需要执行 redis-trib.rb 命令:
第一个参数是我们刚才启动的新实例,第二个参数是集群中已有的节点。
[root@localhost redis_slave_2003]# redis-trib.rb add-node 127.0.0.1:2004 127.0.0.1:2003
>>> Adding node 127.0.0.1:2004 to cluster 127.0.0.1:2003
Connecting to node 127.0.0.1:2003: OK
Connecting to node 127.0.0.1:2001: OK
Connecting to node 127.0.0.1:2002: OK
Connecting to node 127.0.0.1:6379: OK
>>> Performing Cluster Check (using node 127.0.0.1:2003)
M: da4c546799f99b7f5654a47d5b3de5f8ba048173 127.0.0.1:2003
  slots:12288-16383 (4096 slots) master
  0 additional replica(s)
M: e24883fc78bcd42913320946e30d9f2f7052897c 127.0.0.1:2001
  slots:4096-8191 (4096 slots) master
  0 additional replica(s)
M: 0fd7e8a748886c5eca030c71679bdd718ffc7a3f 127.0.0.1:2002
  slots:8192-12287 (4096 slots) master
  0 additional replica(s)
M: ce808a08a1a85a1108c3c683142b10a09bf2416a 127.0.0.1:6379
  slots:0-4095 (4096 slots) master
  0 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
Connecting to node 127.0.0.1:2004: OK
>>> Send CLUSTER MEET to node 127.0.0.1:2004 to make it join the cluster.
[OK] New node added correctly.
打印加入成功,测试一下:
[root@localhost redis_slave_2003]# redis-cli -c -p 2004
127.0.0.1:2004> get 001
-> Redirected to slot [9794] located at 127.0.0.1:2002
(nil)
127.0.0.1:2002> 
连接2004端口的redis,可以获取到在2002端口的redis的001键值,表示2004端口已经加入redis集群
设置也是可以的,但是由于使用一致性hash分布键值,所以,测试时不一定重定向到2004,此处省略(试了五六个键都不是插入2004--/汗)

5.为集群中的主节点配置主从:
6379 2001 2003 2003 2004 任意一个节点加入主从配置,用于读写分离
此处选取2002,从上面的打印可以看出2002是有值的,方便后面测试

1.创建目录,拷贝redis.conf文件,修改配置,然后启动实例:[root@localhost redis_slave_2005]# redis-server redis.conf 
2.2002端口的redis主节点,2005端口redis配置为其从节点:
实例加入到集群,我们只需要执行 redis-trib.rb 命令:
第一个参数是我们5.1启动的新实例,--slave参数表示配置第二个参数实例的从节点,第二个参数是集群中已有的节点,并为slave的主节点
[root@localhost redis_slave_2005]# redis-trib.rb add-node --slave 127.0.0.1:2005 127.0.0.1:2002
>>> Adding node 127.0.0.1:2005 to cluster 127.0.0.1:2002
Connecting to node 127.0.0.1:2002: OK
Connecting to node 127.0.0.1:2004: OK
Connecting to node 127.0.0.1:2001: OK
Connecting to node 127.0.0.1:6379: OK
Connecting to node 127.0.0.1:2003: OK
>>> Performing Cluster Check (using node 127.0.0.1:2002)
M: 0fd7e8a748886c5eca030c71679bdd718ffc7a3f 127.0.0.1:2002
  slots:8192-12287 (4096 slots) master
  0 additional replica(s)
M: 3c8a3d017ff66642a130738a4d7bb71f383707c8 127.0.0.1:2004
  slots: (0 slots) master
  0 additional replica(s)
M: e24883fc78bcd42913320946e30d9f2f7052897c 127.0.0.1:2001
  slots:4096-8191 (4096 slots) master
  0 additional replica(s)
M: ce808a08a1a85a1108c3c683142b10a09bf2416a 127.0.0.1:6379
  slots:0-4095 (4096 slots) master
  0 additional replica(s)
M: da4c546799f99b7f5654a47d5b3de5f8ba048173 127.0.0.1:2003
  slots:12288-16383 (4096 slots) master
  0 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
Automatically selected master 127.0.0.1:2002
Connecting to node 127.0.0.1:2005: OK
>>> Send CLUSTER MEET to node 127.0.0.1:2005 to make it join the cluster.
Waiting for the cluster to join.
>>> Configure node as replica of 127.0.0.1:2002.
[OK] New node added correctly.
打印如上表示成功
3.测试集群主节点中的主从:

首先查看2002, 2003端口节点有些什么键
2002:
[root@localhost redis_slave_2004]# redis-cli -c -p 2002
127.0.0.1:2002> keys *
1) "01053511"
2) "009"
3) "005"
4) "01053"
127.0.0.1:2002> 

2003:
127.0.0.1:2003> keys *
1) "5002304033"
2) "301053511"
3) "004"
4) "008"
5) "0101"
6) "0105"
7) "010535"
127.0.0.1:2003> 
可以看到2002中没有008,有009的键,2003中有008,没有009的键

查看2005的键:
[root@localhost redis_slave_2005]# redis-cli -c -p 2005
127.0.0.1:2005> keys *
1) "009"
2) "01053511"
3) "005"
4) "01053"
127.0.0.1:2005> 
发现跟2002的键是一样的,但是我们并没有对2005进行写入的操作,说明已经从2002的主节点中同步过来了,主从模式已经生效
然后看一下里面的值是否正确:
redis-cli -c -p 2005, 连接2005节点(2002的从节点)
127.0.0.1:2005> get 009
-> Redirected to slot [10058] located at 127.0.0.1:2002
"008"
127.0.0.1:2002> 
可以获取成功,但我们可能怀疑,是不是集群重定向让他获取回来的,如果是的话,那么我们获取2003的键009,也应该是可以的得到值得
试一下:
[root@localhost redis_slave_2005]# redis-cli -c -p 2005
127.0.0.1:2005> get 008
-> Redirected to slot [14187] located at 127.0.0.1:2003
"008"
确实如此,我们一样可以通过2005节点,获取集群中的其他的主节点的值,但是2005节点只会保存2002主节点的键值
说明2005节点redis以2002从节点的身份,加入到redis的集群

整个集群关系和节点信息如下:
[root@localhost redis_slave_2005]# redis-cli -c -p 6379
127.0.0.1:6379> cluster nodes
3c8a3d017ff66642a130738a4d7bb71f383707c8 127.0.0.1:2004 master - 0 1433056761140 0 connected
e24883fc78bcd42913320946e30d9f2f7052897c 127.0.0.1:2001 master - 0 1433056759629 2 connected 4096-8191
88072bd673ff98e424336e36c6c5c687b6b35dfb 127.0.0.1:2005 slave 0fd7e8a748886c5eca030c71679bdd718ffc7a3f 0 1433056759629 3 connected
da4c546799f99b7f5654a47d5b3de5f8ba048173 127.0.0.1:2003 master - 0 1433056759629 4 connected 12288-16383
ce808a08a1a85a1108c3c683142b10a09bf2416a 127.0.0.1:6379 myself,master - 0 0 1 connected 0-4095
0fd7e8a748886c5eca030c71679bdd718ffc7a3f 127.0.0.1:2002 master - 0 1433056760635 3 connected 8192-12287
127.0.0.1:6379> 

ps:这里发现一个问题是:通过2005(从)节点,可以修改2002(主)节点的键的值,此时的从节点只读属性,因为集群的关系,已经不存在了
个人认为这是一个潜在的bug,或许这样设计可能成为需求也不一定,希望有高手解答
[root@localhost redis_slave_2005]# redis-cli -c -p 2005
127.0.0.1:2005> set 2001 2002
-> Redirected to slot [10179] located at 127.0.0.1:2002
OK
127.0.0.1:2002> get 2002
-> Redirected to slot [6048] located at 127.0.0.1:2001
"20022"
127.0.0.1:2001> 

6.删除节点
1:如果删除的节点是主节点,如删除127.0.0.1:2001节点
首先要把节点中的哈希槽转移到其他节点中,执行下面的命令
redis-trib.rb reshard 127.0.0.1:2001

Connecting to node 127.0.0.1:2001: OK
Connecting to node 127.0.0.1:2002: OK
Connecting to node 127.0.0.1:2004: OK
Connecting to node 127.0.0.1:2005: OK
Connecting to node 127.0.0.1:6379: OK
Connecting to node 127.0.0.1:2003: OK
>>> Performing Cluster Check (using node 127.0.0.1:2001)
M: e24883fc78bcd42913320946e30d9f2f7052897c 127.0.0.1:2001
  slots: (0 slots) master
  0 additional replica(s)
M: 0fd7e8a748886c5eca030c71679bdd718ffc7a3f 127.0.0.1:2002
  slots: (0 slots) master
  1 additional replica(s)
M: 3c8a3d017ff66642a130738a4d7bb71f383707c8 127.0.0.1:2004
  slots: (0 slots) master
  0 additional replica(s)
S: 88072bd673ff98e424336e36c6c5c687b6b35dfb 127.0.0.1:2005
  slots: (0 slots) slave
  replicates 0fd7e8a748886c5eca030c71679bdd718ffc7a3f
M: ce808a08a1a85a1108c3c683142b10a09bf2416a 127.0.0.1:6379
  slots: (0 slots) master
  0 additional replica(s)
M: da4c546799f99b7f5654a47d5b3de5f8ba048173 127.0.0.1:2003
  slots:0-16383 (16384 slots) master
  0 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 16384  回车
What is the receiving node ID? da4c546799f99b7f5654a47d5b3de5f8ba048173 回车
系统会提示要移动多少哈希槽,这里移动16384个,因为127.0.0.1:2001节点有16384个哈希槽
接着系统提示输入要接收这些哈希槽的节点的ID,这里使用127.0.0.1:2003的节点ID  da4c546799f99b7f5654a47d5b3de5f8ba048173
然后要我们选择从那些节点中转出哈希槽,输入127.0.0.1:2001这个节点的ID e24883fc78bcd42913320946e30d9f2f7052897c,最后输入 done  表示输入完毕
然后把这个节点删除:
第一个参数为节点的ip端口,第二个参数为节点的ID
redis-trib.rb del-node 127.0.0.1:2001 e24883fc78bcd42913320946e30d9f2f7052897c
2:如果节点是从节点的,直接使用下面的命令删除即可,无需移动哈希槽
redis-trib.rb del-node 127.0.0.1:2001 e24883fc78bcd42913320946e30d9f2f7052897c

ps:文章为本人一步一步亲测,可能存在错误,欢迎指正,转载请注明出处

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章