快速搭建redis集群

 

  • 安装redis server

(参考https://github.com/antirez/redis)

下载redis源码

Wget https://github.com/antirez/redis/archive/5.0-rc3.tar.gz

 

Tar -xzvf xxxxx.gz

编译安装:

 make

 

启动redis server

 

Cd src && ./redis-server —port 7000

 

 

  • 搭建redis集群

 

启动4台redis服务, 分配不同的端口

 

redis-server --port 7000 --protected-mode no --cluster-enabled yes --daemonize yes

 

--protected-mode no: 这个参数没加的话会报错

 DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.

 

--cluster-enabled yes 开启集群模式,这个参数没加的话会报错

[ERR] Node 172.17.0.3:7000 is not configured as a cluster node.

 

--daemonize yes 以守护进程启动

 

 

构建集群:

redis源码包src文件夹下有个ruby脚本redis-trib.rb 用来方便的构建redis集群 

 

./redis-trib.rb create 172.17.0.3:7000 172.17.0.3:7002 172.17.0.3:7003 172.17.0.3:7004

 

看到如下输出说明集群构建成功

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

M: 5c7598031400e0ffe78adb9cb7b4234b7a765acb 172.17.0.2:7000

   slots:0-4095 (4096 slots) master

   0 additional replica(s)

M: f70e6f058537309c1540aefe82ada1ce6905473a 172.17.0.2:7003

   slots:12288-16383 (4096 slots) master

   0 additional replica(s)

M: b6de0da0560006d37dfb2818b2d7b6884b9e185d 172.17.0.2:7001

   slots:4096-8191 (4096 slots) master

   0 additional replica(s)

M: 800d9a3c4732376d1f47d73a8e63d203daee6a5f 172.17.0.2:7002

   slots:8192-12287 (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.

 

可以查看下集群中的节点:

./redis-cli -h 172.17.0.2 -p 7000  cluster nodes

 

 

如果出现如下错误:

root@19045d002ed0:~/redis-5.0-rc3/src# ./redis-trib.rb create 172.17.0.3:7000

/usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- redis (LoadError)

from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'

from ./redis-trib.rb:25:in `

 

安装gems可解决:

apt search gems

 

PS: 需要安装ruby , apt install ruby

 

  • 构建redis集群的docker镜像

Dockerfile :

 

FROM redis-dev:v5

CMD ~/7000/src/redis-server "--port" "7000" "--protected-mode" "no" "--cluster-config-file" "node-7000.conf" "--cluster-enabled" "yes" "--daemonize" "yes" \

&& ~/7001/src/redis-server "--port" "7001" "--protected-mode" "no" "--cluster-config-file" "node-7001.conf" "--cluster-enabled" "yes" "--daemonize" "yes" \

&& ~/7002/src/redis-server "--port" "7002" "--protected-mode" "no" "--cluster-config-file" "node-7002.conf" "--cluster-enabled" "yes" "--daemonize" "yes" \

&& ~/7003/src/redis-server "--port" "7003" "--protected-mode" "no" "--cluster-config-file" "node-7003.conf" "--cluster-enabled" "yes" "--daemonize" "yes" \

&& ~/7000/src/redis-trib.rb create 172.17.0.3:7000 172.17.0.3:7001 172.17.0.3:7002 172.17.0.3:7003 \

&& bash

 

 

 

 

  • 哈希槽(hash slots)

redis集群并没有使用一致性哈希, 而是引入了哈希槽的概念。

redis集群一共有16384个哈希槽,每个节点平均负责一部分哈希槽,

比如有4个节点,每个节点负责16384/4=4096 个哈希槽:

Node 1 : 0~4095

Node 2 : 4096 ~ 8191

Node 3 : 8192 ~ 12287

Node 4 : 12287 ~ 16383

 

 

  • 如何计算key属于哪个哈希槽

redis通过对key进行crc16校验和然后再与16384取模来算出key属于哪个哈希槽

计算公式:  

            Crc16(key) % 16384

 

PS: php没有crc16()函数, 用php.net中copy的一个crc16函数取做运算得出的值与预期不符

 

  • 使用redis集群

 

redis-cli -h 172.17.0.3 -c -p 7000 

 

-c : 加上-c参数redis集群会自动把key移动到相对应的节点的哈希槽。

 

172.17.0.3:7000> set name xxx

-> Redirected to slot [5798] located at 172.17.0.3:7001

OK

 

如果没有-c参数, 只是会有错误提示:

172.17.0.3:7000> set name xxx

(error) MOVED 5798 172.17.0.3:7001

 

 

 

 

  • 添加主节点

./redis-trib.rb add-node 172.17.0.3:7004 172.17.0.3:7000

 

第一个参数是准备要添加的主节点, 第二个参数是任意一个在集群中的主节点

 

查看集群节点列表:

172.17.0.3:7001> cluster nodes

cfb5188e45c74afa60216e16fb3310d3a0a08e67 172.17.0.3:7000@17000 master - 0 1532570391000 1 connected 0-4095

9a3f4539dc3d3bbc2025b5f3ad9b44ff375b5518 172.17.0.3:7002@17002 master - 0 1532570392358 3 connected 8192-12287

080d67e1e7e03d810ee981843acf28298efbf3f7 172.17.0.3:7004@17004 master - 0 1532570392000 0 connected

9f52ff0ce75babb36bd91a49c3b4d7034ff795e5 172.17.0.3:7001@17001 myself,master - 0 1532570391000 2 connected 4096-8191

8fae3dc831dac5e8cc68ba31ff3a9346a14f2586 172.17.0.3:7003@17003 master - 0 1532570393380 4 connected 12288-16383

 

可以看到7004这个节点已经加入集群, 但并没有分配哈希槽, 也就是说他还不能被使用。

接下来就要对哈希槽重新分片

 

 

  • 添加从节点

 

./redis-trib.rb add-node --slave --master-id d8bb8742a8887506e0e80789e75d4e202c4e5181  172.17.0.3:7005 172.17.0.3:7000

 

参数:

—slave  告诉集群该节点是从节点

172.17.0.3:7005 是准备添加的从节点

172.17.0.3:7000 是集群中任意一个节点

--master-id 是主节点的ID , 如果缺省会从集群中随机选择一个主节点作为该从节点的主节点

 

 

查看集群节点列表:

172.17.0.3:7001> cluster nodes

fca08c40b76357b4a5f43aedef4a8f34a920368e 172.17.0.3:7005@17005 slave d8bb8742a8887506e0e80789e75d4e202c4e5181 0 1532591509000 2 connected

a2843bae140704d15a1f07acb961706ba3066896 172.17.0.3:7004@17004 master - 0 1532591509556 5 connected 0-249 4096-4345 8192-8441 12288-12537

b8255beb2ac37ab1029bc9432a400749e1e6f705 172.17.0.3:7002@17002 master - 0 1532591506000 3 connected 8442-12287

d8bb8742a8887506e0e80789e75d4e202c4e5181 172.17.0.3:7001@17001 myself,master - 0 1532591508000 2 connected 4346-8191

28f5c6002f2bc504b351972af146c619fa815ce0 172.17.0.3:7000@17000 master - 0 1532591510591 1 connected 250-4095

a73c7e40b10f9ed5632cd4c3e8572293399dfdde 172.17.0.3:7003@17003 master - 0 1532591508000 4 connected 12538-16383

 

 

  • 哈希槽重新分片

 

 ./redis-trib.rb reshard 172.17.0.3:7000  (这里的参数可以用集群中的任意一个主节点)

 

root@04f1db7c2aa6:~/7004/src# ./redis-trib.rb reshard 172.17.0.3:7000

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

M: 722b1ad24942bf63b998b95337a8141b70a1d075 172.17.0.3:7000

   slots:0-4095 (4096 slots) master

   0 additional replica(s)

M: 865db89d3154375de617e8d62034ca29e58d70f2 172.17.0.3:7001

   slots:4096-8191 (4096 slots) master

   0 additional replica(s)

M: bd9fe6b5f9a6185c18d26a3811c0a4043bdbe01f 172.17.0.3:7004

   slots: (0 slots) master

   0 additional replica(s)

M: ab013b981a12550f5e9c12da8b74801b53653dc9 172.17.0.3:7002

   slots:8192-12287 (4096 slots) master

   0 additional replica(s)

M: 09fa43dfcd0039da6cb4c3454c470e5eedabaddd 172.17.0.3:7003

   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.

How many slots do you want to move (from 1 to 16384)? 1000         (要重新分配的哈希槽数量,这里输入1000)

What is the receiving node ID? bd9fe6b5f9a6185c18d26a3811c0a4043bdbe01f       (要接受这1000个哈希槽的节点ID)

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:all              (输入all会从集群的其他4个主节点每个拿250个哈希槽分配给7004这个节点)

Ready to move 1000 slots.

 

 

查看重新分片情况:

172.17.0.3:7001> cluster nodes

722b1ad24942bf63b998b95337a8141b70a1d075 172.17.0.3:7000@17000 master - 0 1532574368867 1 connected 250-4095

09fa43dfcd0039da6cb4c3454c470e5eedabaddd 172.17.0.3:7003@17003 master - 0 1532574367841 4 connected 12538-16383

bd9fe6b5f9a6185c18d26a3811c0a4043bdbe01f 172.17.0.3:7004@17004 master - 0 1532574369897 5 connected 0-249 4096-4345 8192-8441 12288-12537

ab013b981a12550f5e9c12da8b74801b53653dc9 172.17.0.3:7002@17002 master - 0 1532574367000 3 connected 8442-12287

865db89d3154375de617e8d62034ca29e58d70f2 172.17.0.3:7001@17001 myself,master - 0 1532574366000 2 connected 4346-8191

 

 

可以看出7004被分配了1000个哈希槽

 

 

 

参考:

 

http://www.redis.cn/topics/cluster-tutorial.html

你可能感兴趣的:(Nosql)