redis之cluster(叢集)
阿新 • • 發佈:2018-12-11
搭建redis cluster
1. 準備節點
2. 節點間的通訊
3. 分配槽位給節點
redis-cluster架構
多個服務端,負責讀寫,彼此通訊,redis指定了16384個槽。
多匹馬兒,負責運輸資料,馬兒分配16384個槽位,管理資料。
ruby的指令碼自動就把分配槽位這事做了
啟動所有redis-cluster節點
我準備了6個節點, 配置檔案如下, 除了埠不一樣, 其他都一樣
配置檔案解釋:
port 7000 daemonize yes dir "/opt/redis/data" logfile "7000.log" dbfilename "dump-7000.rdb" cluster-enabled yes #開啟叢集模式 cluster-config-file nodes-7000.conf #叢集內部的配置檔案 cluster-require-full-coverage no #redis cluster需要16384個slot都正常的時候才能對外提供服務,換句話說,只要任何一個slot異常那麼整個cluster不對外提供服務。 因此生產環境一般為no
啟動所有的節點
此時的redis雖然已經啟動了, 但是還不能使用, 因為現在執行在叢集模式下, 叢集還沒有配置完了.....
準備ruby環境
開啟redis-cluster
下載ruby安裝包
#下載ruby wget https://cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.1.tar.gz #安裝ruby tar -xvf ruby-2.3.1.tar.gz ./configure --prefix=/opt/ruby/ make && make install #準備一個ruby命令 #準備一個gem軟體包管理命令 #拷貝ruby命令到path下/usr/local/ruby cp /opt/ruby/bin/ruby /usr/local/bin/ cp bin/gem /usr/local/bin
安裝gem安裝redis模組, 類似於pip install redis
wget http://rubygems.org/downloads/redis-3.3.0.gem gem install -l redis-3.3.0.gem #檢視gem有哪些包 gem list -- check redis gem
安裝redis-trib.rb命令
在redis/src目錄下
新增到環境變數
[[email protected] 05:15 /opt/redis-4.0.10/src]# cp redis-trib.rb /usr/local/bin/
一鍵開啟redis-cluster
#每個主節點,有一個從節點,代表--replicas 1 redis-trib.rb create --replicas 1 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 #叢集自動分配主從關係 7000、7001、7002為 7003、7004、7005 主動關係
[[email protected] 05:15 /opt/redis-4.0.10/src]# redis-trib.rb create --replicas 1 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 >>> Creating cluster >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 Adding replica 127.0.0.1:7004 to 127.0.0.1:7000 Adding replica 127.0.0.1:7005 to 127.0.0.1:7001 Adding replica 127.0.0.1:7003 to 127.0.0.1:7002 >>> Trying to optimize slaves allocation for anti-affinity [WARNING] Some slaves are in the same host as their master M: fb345360da148c168f590237e0c5557815654c34 127.0.0.1:7000 slots:0-5460 (5461 slots) master M: 23875b1844fe1af2f5287877298abaf7c8eeaea3 127.0.0.1:7001 slots:5461-10922 (5462 slots) master M: 39e2b69102c684c0a4973395acb37120f60db22c 127.0.0.1:7002 slots:10923-16383 (5461 slots) master S: 390c6bfbd8fe0a94c37b9b41c45ec0a033c58e85 127.0.0.1:7003 replicates 39e2b69102c684c0a4973395acb37120f60db22c S: 115c8166b48a6b2835a5fd89b3bcb021687e64d5 127.0.0.1:7004 replicates fb345360da148c168f590237e0c5557815654c34 S: 28b7664378d6aea262bc5a9ee890d4a857bf397e 127.0.0.1:7005 replicates 23875b1844fe1af2f5287877298abaf7c8eeaea3 Can I set the above configuration? (type 'yes' to accept): yes >>> Nodes configuration updated >>> Assign a different config epoch to each node >>> Sending CLUSTER MEET messages to join the cluster Waiting for the cluster to join..... >>> Performing Cluster Check (using node 127.0.0.1:7000) M: fb345360da148c168f590237e0c5557815654c34 127.0.0.1:7000 slots:0-5460 (5461 slots) master 1 additional replica(s) S: 28b7664378d6aea262bc5a9ee890d4a857bf397e 127.0.0.1:7005 slots: (0 slots) slave replicates 23875b1844fe1af2f5287877298abaf7c8eeaea3 M: 23875b1844fe1af2f5287877298abaf7c8eeaea3 127.0.0.1:7001 slots:5461-10922 (5462 slots) master 1 additional replica(s) S: 115c8166b48a6b2835a5fd89b3bcb021687e64d5 127.0.0.1:7004 slots: (0 slots) slave replicates fb345360da148c168f590237e0c5557815654c34 S: 390c6bfbd8fe0a94c37b9b41c45ec0a033c58e85 127.0.0.1:7003 slots: (0 slots) slave replicates 39e2b69102c684c0a4973395acb37120f60db22c M: 39e2b69102c684c0a4973395acb37120f60db22c 127.0.0.1:7002 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.
檢視叢集狀態的命令
redis-cli -p 7000 cluster info redis-cli -p 7000 cluster nodes #等同於檢視nodes-7000.conf檔案節點資訊 叢集主節點狀態 redis-cli -p 7000 cluster nodes | grep master 叢集從節點狀態 redis-cli -p 7000 cluster nodes | grep slave
測試叢集
測試寫入叢集資料,登入叢集必須使用redis-cli -c -p 7000必須加上-c引數
redis叢集部署完成