1. 程式人生 > >快速搭建redis叢集

快速搭建redis叢集

  • 安裝redis server

下載redis原始碼

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

如果出現如下錯誤:

[email protected]:~/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 `<main>’

安裝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:[email protected] master - 0 1532570391000 1 connected 0-4095

9a3f4539dc3d3bbc2025b5f3ad9b44ff375b5518 172.17.0.3:[email protected] master - 0 1532570392358 3 connected 8192-12287

080d67e1e7e03d810ee981843acf28298efbf3f7 172.17.0.3:[email protected] master - 0 1532570392000 0 connected

9f52ff0ce75babb36bd91a49c3b4d7034ff795e5 172.17.0.3:[email protected] myself,master - 0 1532570391000 2 connected 4096-8191

8fae3dc831dac5e8cc68ba31ff3a9346a14f2586 172.17.0.3:[email protected] 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:[email protected] slave d8bb8742a8887506e0e80789e75d4e202c4e5181 0 1532591509000 2 connected

a2843bae140704d15a1f07acb961706ba3066896 172.17.0.3:[email protected] master - 0 1532591509556 5 connected 0-249 4096-4345 8192-8441 12288-12537

b8255beb2ac37ab1029bc9432a400749e1e6f705 172.17.0.3:[email protected] master - 0 1532591506000 3 connected 8442-12287

d8bb8742a8887506e0e80789e75d4e202c4e5181 172.17.0.3:[email protected] myself,master - 0 1532591508000 2 connected 4346-8191

28f5c6002f2bc504b351972af146c619fa815ce0 172.17.0.3:[email protected] master - 0 1532591510591 1 connected 250-4095

a73c7e40b10f9ed5632cd4c3e8572293399dfdde 172.17.0.3:[email protected] master - 0 1532591508000 4 connected 12538-16383

  • 雜湊槽重新分片

 ./redis-trib.rb reshard 172.17.0.3:7000  (這裡的引數可以用叢集中的任意一個主節點)

[email protected]:~/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:[email protected] master - 0 1532574368867 1 connected 250-4095

09fa43dfcd0039da6cb4c3454c470e5eedabaddd 172.17.0.3:[email protected] master - 0 1532574367841 4 connected 12538-16383

bd9fe6b5f9a6185c18d26a3811c0a4043bdbe01f 172.17.0.3:[email protected] master - 0 1532574369897 5 connected 0-249 4096-4345 8192-8441 12288-12537

ab013b981a12550f5e9c12da8b74801b53653dc9 172.17.0.3:[email protected] master - 0 1532574367000 3 connected 8442-12287

865db89d3154375de617e8d62034ca29e58d70f2 172.17.0.3:[email protected] myself,master - 0 1532574366000 2 connected 4346-8191

可以看出7004被分配了1000個雜湊槽

參考: