redis cluster(集群)模式的創建方式


redis常用的架構有三種,單例、哨兵、集群,其他的都說過了,這里只簡單介紹集群搭建。

        單例最簡單沒什么好說的。

        哨兵之前說過,該模式下有哨兵節點監視master和slave,若master宕機可自動將slave轉為master,但它也有一個問題,就是不能動態擴充,並且存儲大小受每個節點的內存大小限制。

        集群模式Redis-Cluster,采用無中心結構,每個節點都和集群內其他節點有連接,數據可以跨主機分布式存儲,解決了存儲大小受主機限制的問題,Redis集群預分好16384個插槽(slot),每個節點分配一部分slot,當需要在 Redis 集群中放置一個 key-value 時,根據哈希算法決定將key放到哪個slot中,進而找到對應存放數據的主機,查詢數據也一樣。

        集群模式內部同樣可以配置主從,例如集群有六個數據節點,可以設置三個主節點,每個主節點對應一個從節點,當一個主節點宕機,可以自動將從節點變成主節點,保證整個集群還能用。但是一個主節點和對應的從節點都宕機后集群將不可用。每個主節點可以配置多個從節點。

集群搭建步驟

        1、創建幾台虛機,(集群模式最少要三個主節點)例如我們搭建一個三主三從的集群,創建六台虛機,當然要測試的話也可以部署在同一台主機上,使用不同的端口模擬不同主機。

        2、在每台主機上安裝redis,注意redis配置文件需要注意cluster相關的配置,例如修改cluster-enabled  的值是yes,表示開啟集群,其他的超時時間等配置根據實際修改或默認。

        3、redis安裝完之后不會自己變成集群,還需要執行創建集群的命令,這個每個版本不太一樣,redis5使用redis-cli --cluster ,redis3使用redis-trib.rb,下面具體說。

redis5集群創建

redis5的cluster相關命令,此處只寫創建,當然還支持添加節點、刪除、對solt進行操作等

[root@fvb9wbpl-clustermanager-e0hhllme ~]# redis-cli --cluster help
Cluster Manager Commands:
  create         host1:port1 ... hostN:portN
                 --cluster-replicas <arg>
  check          host:port
                 --cluster-search-multiple-owners
  info           host:port
  fix            host:port
                 --cluster-search-multiple-owners
  reshard        host:port
                 --cluster-from <arg>
                 --cluster-to <arg>
                 --cluster-slots <arg>
                 --cluster-yes
                 --cluster-timeout <arg>
                 --cluster-pipeline <arg>
                 --cluster-replace
  rebalance      host:port
                 --cluster-weight <node1=w1...nodeN=wN>
                 --cluster-use-empty-masters
                 --cluster-timeout <arg>
                 --cluster-simulate
                 --cluster-pipeline <arg>
                 --cluster-threshold <arg>
                 --cluster-replace
  add-node       new_host:new_port existing_host:existing_port
                 --cluster-slave
                 --cluster-master-id <arg>
  del-node       host:port node_id
  call           host:port command arg arg .. arg
  set-timeout    host:port milliseconds
  import         host:port
                 --cluster-from <arg>
                 --cluster-copy
                 --cluster-replace
  help

For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.

創建主從集群命令,后面是集群的ip地址和端口,cluster-replicas 1表示一個主節點對應1個從節點,創建成功后會自動分配主從關系

redis-cli --cluster create --cluster-replicas 1 10.110.30.136:6379 10.110.30.137:6379 10.110.30.138:6379 10.110.30.144:6379 10.110.30.145:6379 10.110.30.146:6379

出現提示后再輸入yes,然后開始創建集群

創建成功后查看集群,后面的ip可以是集群中的任意一個節點,結果中的M代表主節點,當M節點宕機后集群會自動把S節點變成M節點,這個可以自行測試

[root@fvb9wbpl-clustermanager-e0hhllme ~]# redis-cli --cluster check 10.110.30.68:6379
10.110.30.70:6379 (5e49929d...) -> 0 keys | 5461 slots | 1 slaves.
10.110.30.69:6379 (6dc594c5...) -> 0 keys | 5462 slots | 1 slaves.
10.110.30.112:6379 (2b9a0210...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 10.110.30.68:6379)
S: 15a969da394187e239baab1cc57bc6ec810996fe 10.110.30.68:6379
   slots: (0 slots) slave
   replicates 2b9a0210b3223a1d865324ec32cc70f8ebcbaf65
S: 095777fb0d8ad380e420b9ef465d202a745a14e6 10.110.30.114:6379
   slots: (0 slots) slave
   replicates 6dc594c5180f655603504ab68591b2e7492c7963
S: 20a33128f5353d36cb084d6ae2bf24c5b4806538 10.110.30.113:6379
   slots: (0 slots) slave
   replicates 5e49929d47e7bd438ea2e25c3731179f460eb387
M: 5e49929d47e7bd438ea2e25c3731179f460eb387 10.110.30.70:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 6dc594c5180f655603504ab68591b2e7492c7963 10.110.30.69:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: 2b9a0210b3223a1d865324ec32cc70f8ebcbaf65 10.110.30.112:6379
   slots:[10923-16383] (5461 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.

 

redis3集群創建

redis3不支持cluster,3版本創建集群使用redis-trib.rb,當然還需要ruby等不在這里介紹。

redis-trib.rb的參數跟cluster類似,了解了上面的下面這個一看就懂,基本一樣的

[root@mucuijwb-clustermanager-o9sprluh bin]# /usr/local/bin/redis-trib.rb help
Usage: redis-trib <command> <options> <arguments ...>

  create          host1:port1 ... hostN:portN
                  --replicas <arg>
  check           host:port
  info            host:port
  fix             host:port
                  --timeout <arg>
  reshard         host:port
                  --from <arg>
                  --to <arg>
                  --slots <arg>
                  --yes
                  --timeout <arg>
                  --pipeline <arg>
  rebalance       host:port
                  --weight <arg>
                  --auto-weights
                  --use-empty-masters
                  --timeout <arg>
                  --simulate
                  --pipeline <arg>
                  --threshold <arg>
  add-node        new_host:new_port existing_host:existing_port
                  --slave
                  --master-id <arg>
  del-node        host:port node_id
  set-timeout     host:port milliseconds
  call            host:port command arg arg .. arg
  import          host:port
                  --from <arg>
                  --copy
                  --replace
  help            (show this help)

For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.

創建主從集群命令,參數含義跟cluster的一樣

/usr/local/bin/redis-trib.rb create --replicas 1 10.110.30.74:6379 10.110.30.137:6379 10.110.30.138:6379 10.110.30.144:6379 10.110.30.145:6379 10.110.30.146:6379

檢查集群

[root@mucuijwb-clustermanager-o9sprluh bin]# /usr/local/bin/redis-trib.rb check 10.110.30.74:6379
>>> Performing Cluster Check (using node 10.110.30.74:6379)
M: 7483d09dadda2ecebd9fdbd6b1f186769e00904d 10.110.30.74:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: 76ecc2cf2ddc07fe7f2fda53f2f5318198321761 10.110.30.72:6379
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: 0f60c9c6595726d391805f5acfaf5be13496346a 10.110.30.73:6379
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 6f56bbe811be2ff97ef654777ae48c3ae5d83824 10.110.30.111:6379
   slots: (0 slots) slave
   replicates 76ecc2cf2ddc07fe7f2fda53f2f5318198321761
S: 8c3bb1ed5e531665dfedf126df5121b76dc604d4 10.110.30.110:6379
   slots: (0 slots) slave
   replicates 7483d09dadda2ecebd9fdbd6b1f186769e00904d
S: 543c3b42c93ccac1728eaa5d5c9a43938f922890 10.110.30.108:6379
   slots: (0 slots) slave
   replicates 0f60c9c6595726d391805f5acfaf5be13496346a
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

 

集群連接

客戶端連接集群模式可以直接連接redis節點,不需要再經過中間節點轉發,客戶端連接集群,需要加 -c 參數,否則插入數據會報錯

[root@fvb9wbpl-rediscluster-vddntq84 ~]# redis-cli -c
127.0.0.1:6379> dbsize
(integer) 0
127.0.0.1:6379>

連接其他主機

[root@fvb9wbpl-rediscluster-vddntq84 ~]# redis-cli -c -h 10.110.30.113 -p 6379
10.110.30.113:6379> dbsize
(integer) 0
10.110.30.113:6379>

插入數據和查詢數據,注意對插槽slot的分配操作,可以看出來是分布式存儲的

[root@fvb9wbpl-rediscluster-vddntq84 ~]# redis-cli -c -h 10.110.30.113 -p 6379
10.110.30.113:6379> set foo bar
-> Redirected to slot [12182] located at 10.110.30.112:6379
OK10.110.30.112:6379> set c d
-> Redirected to slot [7365] located at 10.110.30.69:6379
OK
10.110.30.69:6379>
10.110.30.69:6379> get foo
-> Redirected to slot [12182] located at 10.110.30.112:6379
"bar"
10.110.30.112:6379> get c
-> Redirected to slot [7365] located at 10.110.30.69:6379
"d"
10.110.30.69:6379>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM