Redis的集群搭建记录

当前以redis-5.0为例,首先下载指定的包,即redis-5.0.0.tar.gz,解压后执行下述操作:

//执行完make后,会在src下生成几个可执行文件,如果执行make install,这些文件会被拷贝

//到/usr/local/bin下,最好在root下执行,否则即使你安装了gcc也会报错,我没深究,注意一下吧

//src下的可执行文件,是执行make后生成的

[root@localhost ~/redis-5.0.0]# make

[root@localhost ~/redis-5.0.0]# ll src| grep redis-

-rwxr-xr-x 1 root root 4365464 11月 6 10:43 redis-benchmark

-rw-rw-r-- 1 root root 29605 10月 17 23:31 redis-benchmark.c

-rw-r--r-- 1 root root 109120 11月 6 10:43 redis-benchmark.o

-rwxr-xr-x 1 root root 8081576 11月 6 10:43 redis-check-aof

-rw-rw-r-- 1 root root 7143 10月 17 23:31 redis-check-aof.c

-rw-r--r-- 1 root root 28776 11月 6 10:42 redis-check-aof.o

-rwxr-xr-x 1 root root 8081576 11月 6 10:43 redis-check-rdb

-rw-rw-r-- 1 root root 13541 10月 17 23:31 redis-check-rdb.c

-rw-r--r-- 1 root root 65896 11月 6 10:42 redis-check-rdb.o

-rwxr-xr-x 1 root root 4782376 11月 6 10:43 redis-cli

-rw-rw-r-- 1 root root 249486 10月 17 23:31 redis-cli.c

-rw-r--r-- 1 root root 871072 11月 6 10:42 redis-cli.o

-rwxr-xr-x 1 root root 8081576 11月 6 10:42 redis-sentinel

-rwxr-xr-x 1 root root 8081576 11月 6 10:42 redis-server

-rwxrwxr-x 1 root root 3600 10月 17 23:31 redis-trib.rb

[root@localhost ~/redis-5.0.0]#

将这些可执行文件拷贝到你新建的文件夹中,假如是/root/redis

[root@localhost ~/redis]# ll

drwxr-xr-x 2 root root 80 11月 6 19:14 7000

drwxr-xr-x 2 root root 58 11月 6 16:24 7001

drwxr-xr-x 2 root root 80 11月 6 16:34 7002

drwxr-xr-x 2 root root 80 11月 6 19:14 7003

drwxr-xr-x 2 root root 80 11月 6 16:24 7004

drwxr-xr-x 2 root root 80 11月 6 16:30 7005

-rwxr-xr-x 1 root root 4365464 11月 6 15:15 redis-benchmark

-rwxr-xr-x 1 root root 4782376 11月 6 15:15 redis-cli

-rwxr-xr-x 1 root root 8081576 11月 6 15:15 redis-server

// 重点关注一下这个文件,现在其不用了, 其功能会被redis-cli替换掉

-rwxr-xr-x 1 root root 3600 11月 6 15:54 redis-trib.rb

新建上面的这些文件夹,即7000 - 7005,里面仅需有一个redis.conf,这个文件可以从解压的文件夹里找到,需要进行如下设置:

port 7001 #当然是指定的端口

cluster-enabled yes

cluster-config-file nodes.conf

# 代表这个服务器离群后,还能用多长时间

cluster-node-timeout 5000

然后,到指定的目录下去执行,相应的命令,形如:

[root@localhost ~/redis/7001]# ../redis-server ./redis.conf

[root@localhost ~/redis/7001]# ll

-rw-r--r-- 1 root root 186 11月 6 16:24 dump.rdb

-rw-r--r-- 1 root root 1027 11月 6 19:25 nodes.conf

-rw-r--r-- 1 root root 62143 11月 6 15:25 redis.conf

这个地方,切不要在~/redis下执行命令,否则随后的7002啥的都起不来,因为nodes.conf会被生成在 执行命令的目录下,切记!

7000 - 7005的端口上的redis起来后,可以将之搭成集群了,命令如下:

[root@localhost ~/redis/7001]# redis-cli --cluster create 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 --cluster-replicas 1

如果后来又想添加节点,可以用下面这个方法:

1. 添加master节点

[root@localhost ~/redis]# redis-cli --cluster add-node 127.0.0.1:30001 127.0.0.1:7000

添加了节点后,默认不会朝里放东西,因为没给它分配slot,下面的命令就是要分给它slot

[root@localhost ~/redis]# redis-cli --cluster reshard 127.0.0.1:7000

>>> Performing Cluster Check (using node 127.0.0.1:7000)

M: 64ed63c76c8b3c090aa9764d5a50dbf79dfa3e9a 127.0.0.1:7000

slots:[800-5460] (4661 slots) master

1 additional replica(s)

S: 8eb673c75f81b0a7b7c1f190c797dd6b22cef5bd 127.0.0.1:7004

slots: (0 slots) slave

replicates 6186b8edb37cf24a7d32b93c5c0ed42d46e15618

M: 509aee674ee5fb8b3eee0a528c72ce743eb29246 127.0.0.1:30001

slots:[0-799] (800 slots) master

1 additional replica(s)

M: 0600601de0c4d5c46ef5dd0cdecbf18576830f23 127.0.0.1:7002

slots:[10923-16383] (5461 slots) master

1 additional replica(s)

S: 5a6b397b09011d13b5aa5992985db4e5a94d6750 127.0.0.1:7003

slots: (0 slots) slave

replicates 64ed63c76c8b3c090aa9764d5a50dbf79dfa3e9a

S: 76fc659895bbc355bc8a51752dc1248b76665dfb 127.0.0.1:30002

slots: (0 slots) slave

replicates 509aee674ee5fb8b3eee0a528c72ce743eb29246

M: 6186b8edb37cf24a7d32b93c5c0ed42d46e15618 127.0.0.1:7001

slots:[5461-10922] (5462 slots) master

1 additional replica(s)

S: 83c12793fe18855d7ac81c63143b7b88da8e8d30 127.0.0.1:7005

slots: (0 slots) slave

replicates 0600601de0c4d5c46ef5dd0cdecbf18576830f23

[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)? 800

What is the receiving node ID? 509aee674ee5fb8b3eee0a528c72ce743eb29246

Please enter all the source node IDs.

Type 'all' to use all the nodes as source nodes for the hash slots.

Type 'done' once you entered all the source nodes IDs.

Source node #1:

// 以上是我分配完的,虚执行一下这个命令,这个命令很简单,先让你指定分配给谁,分配多少,从哪些Master那里进行分配,然后回车,过一回输入yes即完事了,很简单。

2. 添加slave节点,执行下这个节点,将一个新启动的

[root@localhost ~/redis]# redis-cli --cluster add-node 127.0.0.1:30002 127.0.0.1:7000

[root@localhost ~/redis]# redis-cli -c -p 30002 cluster replicate 509aee674ee5fb8b3eee0a528c72ce743 eb29246

根据下面的说明,可以知道,replicate后面跟的id是master的id,登录的就是slave

127.0.0.1:7000> cluster help

1) CLUSTER <subcommand> arg arg ... arg. Subcommands are:

2) ADDSLOTS <slot> [slot ...] -- Assign slots to current node.

3) BUMPEPOCH -- Advance the cluster config epoch.

4) COUNT-failure-reports <node-id> -- Return number of failure reports for <node-id>.

5) COUNTKEYSINSLOT <slot> - Return the number of keys in <slot>.

6) DELSLOTS <slot> [slot ...] -- Delete slots information from current node.

7) FAILOVER [force|takeover] -- Promote current replica node to being a master.

8) FORGET <node-id> -- Remove a node from the cluster.

9) GETKEYSINSLOT <slot> <count> -- Return key names stored by current node in a slot.

10) FLUSHSLOTS -- Delete current node own slots information.

11) INFO - Return onformation about the cluster.

12) KEYSLOT <key> -- Return the hash slot for <key>.

13) MEET <ip> <port> [bus-port] -- Connect nodes into a working cluster.

14) MYID -- Return the node id.

15) NODES -- Return cluster configuration seen by node. Output format:

16) <id> <ip:port> <flags> <master> <pings> <pongs> <epoch> <link> <slot> ... <slot>

17) REPLICATE <node-id> -- Configure current node as replica to <node-id>.

18) RESET [hard|soft] -- Reset current node (default: soft).

19) SET-config-epoch <epoch> - Set config epoch of current node.

20) SETSLOT <slot> (importing|migrating|stable|node <node-id>) -- Set slot state.

21) REPLICAS <node-id> -- Return <node-id> replicas.

22) SLOTS -- Return information about slots range mappings. Each range is made of:

23) start, end, master and replicas IP addresses, ports and ids

进行主备切换验证:

[root@localhost ~/redis]# ps -ef |grep redis

root 1732 1 0 16:50 ? 00:00:54 ../redis-server 127.0.0.1:7000 [cluster]

root 1991 1 0 16:51 ? 00:00:53 ../redis-server 127.0.0.1:7001 [cluster]

root 2013 1 0 16:51 ? 00:00:53 ../redis-server 127.0.0.1:7002 [cluster]

root 2131 1 0 16:52 ? 00:00:53 ../redis-server 127.0.0.1:7003 [cluster]

root 2265 1 0 16:53 ? 00:00:53 ../redis-server 127.0.0.1:7004 [cluster]

root 2288 1 0 16:53 ? 00:00:53 ../redis-server 127.0.0.1:7005 [cluster]

root 5240 2298 0 17:48 pts/6 00:00:05 emacs 7000/redis.conf

root 7849 1 0 18:39 ? 00:00:24 ../../src/redis-server *:30001 [cluster]

root 7852 1 0 18:39 ? 00:00:20 ../../src/redis-server *:30002 [cluster]

root 11058 8721 0 19:36 pts/7 00:00:06 emacs -nw 7001/redis.conf

root 12173 8721 0 19:57 pts/7 00:00:00 grep --color=auto redis

[root@localhost ~/redis]# kill -9 7849

[root@localhost ~/redis]# redis-cli -c -p 7000

127.0.0.1:7000> cluster nodes

8eb673c75f81b0a7b7c1f190c797dd6b22cef5bd 127.0.0.1:7004@17004 slave 6186b8edb37cf24a7d32b93c5c0ed42d46e15618 0 1541505495000 5 connected

509aee674ee5fb8b3eee0a528c72ce743eb29246 127.0.0.1:30001@40001 master,fail - 1541505482601 1541505481076 7 disconnected

0600601de0c4d5c46ef5dd0cdecbf18576830f23 127.0.0.1:7002@17002 master - 0 1541505495574 3 connected 10923-16383

5a6b397b09011d13b5aa5992985db4e5a94d6750 127.0.0.1:7003@17003 slave 64ed63c76c8b3c090aa9764d5a50dbf79dfa3e9a 0 1541505495000 4 connected

64ed63c76c8b3c090aa9764d5a50dbf79dfa3e9a 127.0.0.1:7000@17000 myself,master - 0 1541505494000 1 connected 800-5460

76fc659895bbc355bc8a51752dc1248b76665dfb 127.0.0.1:30002@40002 master - 0 1541505494000 8 connected 0-799

6186b8edb37cf24a7d32b93c5c0ed42d46e15618 127.0.0.1:7001@17001 master - 0 1541505495574 2 connected 5461-10922

83c12793fe18855d7ac81c63143b7b88da8e8d30 127.0.0.1:7005@17005 slave 0600601de0c4d5c46ef5dd0cdecbf18576830f23 0 1541505494000 6 connected

可以看到原来30002是备机,现在变成了主机。

 

什么时候,集群会被判定为失败?

1. 有一个master挂掉,但是它又没有slave.

2. 有超过半数的master挂掉,即不能全面覆盖所有slots时,这个时候活着的还活着,但是不能再朝里写数据等操作了,即cluster down.

 

遇到的问题:

我将redis.conf中的bind置为 10.112.12.2 127.0.0.1 ::1我在create的时候,将部分主机的ip写成了127.0.0.1:7000,结果造成的原因是,通过一些机器登上去后,执行cluster nodes,会出现127.0.0.1,这个时候,如果你用诸如spring boot集成的cluster方式访问理,会出现下面的情况:

java.util.concurrent.CompletionException: io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: no further information: /127.0.0.1:7003

处理方式很简单,只要看一下哪个的node.conf中存在127改成指定的ip再重启一下就可以

ps:

我的集群怎么重启?

-》 逐个关,然后逐个再打开即可,因为你的集群配置已经被记录在了各个node.conf中

不要试图add一个有数据的node进去,会报错,想一想可以理解的,空的加进去才可以。

如果A是B的备,那么B重启后,会成为A的备,而不是抢夺A的主角色

 

总结一下:

redis大体有三种方式来处理集群,勉强可以这么说。

一种是基本的主备,即通过redis.conf中的slaveof来进行设置

一种是哨兵模式,没用过,配置好像还有点小复杂

一种就是现在说的,也是官方推荐的方式,当然里面也说了,虽说是稳定了,但还没有足够的正式环境验证,我觉的问题不大,姑且用之。主要是进行集群访问的客户端要给力,毕竟redis的集群是无中心的,且没有提供一个诸如proxy或者router一样的东西来让你集中访问,我正在试spring boot的cluter redis支持如何,希望问题不大吧  :-)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值