CentOS7 安裝Redis
節點數量至少為 6 個才能保證組成完整高可用的叢集
cluster ├── 9001 │ ├── data │ │ ├── appendonly.aof │ │ └── nodes-9001.conf │ ├── redis-9001.conf │ └── redis-9001.log ├── 9002 │ ├── data │ │ ├── appendonly.aof │ │ └── nodes-9002.conf │ ├── redis-9002.conf │ └── redis-9002.log ...
cd cluster mkdir -p 9001/data 9002/data 9003/data 9004/data 9005/data 9006/data cp ../redis-6.0.9/redis.conf 9001/redis-9001.conf vi 9001/redis-9001.conf port 9001 daemonize yes bind 192.168.11.40 dir /root/cluster/9001/data/ pidfile /var/run/redis_9001.pid cluster-enabled yes # 集群模式运行 cluster-config-file nodes-9001.conf cluster-node-timeout 15000 # appendonly yes logfile "/root/cluster/9001/redis-9001.log"
複製& 取代::%s/9001/9002/g
/usr/local/redis/bin/redis-server /root/cluster/9001/redis-9001.conf ... tail 9001/redis-9001.log kill `cat /var/run/redis_9001.pid`
啟動後會自動在data 目錄建立叢集設定檔nodes-9001.conf
。當叢集內節點資訊發生變化時,節點會自動保存叢集狀態到該配置文件,最好不要手動修改。如果啟動時存在叢集設定文件,節點會使用設定檔內容初始化叢集資訊
$ cat 9001/data/nodes-9001.conf 8ccdb0963411ebd05ce21952bdd4b7597825afdc :0@0 myself,master - 0 0 0 connected vars currentEpoch 0 lastVoteEpoch 0 $ /usr/local/redis/bin/redis-cli -h 192.168.11.40 -p 9001 192.168.11.40:9001> cluster nodes 8ccdb0963411ebd05ce21952bdd4b7597825afdc :9001@19001 myself,master - 0 0 0 connected
節點 ID:40 位元 16 進位字串,用於唯一標識叢集內一個節點。節點ID 只創建一次,而運行ID 每次重啟都會變化
節點握手指一批運行在集群模式下的節點通過Gossip 協議彼此通信,以互相感知
$ /usr/local/redis/bin/redis-cli -h 192.168.11.40 -p 9001 192.168.11.40:9001> cluster meet 192.168.11.40 9002 OK 192.168.11.40:9001> cluster nodes 8ccdb0963411ebd05ce21952bdd4b7597825afdc 192.168.11.40:9001@19001 myself,master - 0 0 0 connected 5786e3237c7fa413ed22465d15be721f95e72cfa 192.168.11.40:9002@19002 master - 0 1620703357871 1 connected
cluster meet 是一個異步指令,作用是節點彼此交換狀態資料訊息,執行之後立刻返回,在內部與目標節點進行握手通訊:
#節點9001 本機建立9002 節點資訊對象,並傳送meet 訊息
節點9002 接受meet 訊息後,儲存9001 節點資訊並回覆pong 訊息
之後節點9001 和9002 彼此定期透過ping/pong 訊息進行節點通訊
叢集內任意節點都可執行cluster meet 指令來加入新節點,握手狀態會透過訊息在叢集內傳播:其他節點會自動發現新節點並發起握手流程
節點建立握手之後叢集還不能正常運作,此時叢集處於下線狀態,所有資料讀寫都被禁止。因為槽沒有被分配到節點,因此叢集無法完成槽到節點的對應
192.168.11.40:9001> set hello world (error) CLUSTERDOWN Hash slot not served 192.168.11.40:9001> cluster info cluster_state:fail cluster_slots_assigned:0 cluster_slots_ok:0 cluster_slots_pfail:0 cluster_slots_fail:0 cluster_known_nodes:6 cluster_size:0 ...
Redis 叢集把所有的資料對應到 16384 個槽中。只有當槽全部分配給節點後,叢集才進入線上狀態
分配槽:
/usr/local/redis/bin/redis-cli -h 192.168.11.40 -p 9001 cluster addslots {0..5461} /usr/local/redis/bin/redis-cli -h 192.168.11.40 -p 9002 cluster addslots {5462..10922} /usr/local/redis/bin/redis-cli -h 192.168.11.40 -p 9003 cluster addslots {10923..16383}
查看叢集資訊:
192.168.11.40:9001> cluster info cluster_state:ok cluster_slots_assigned:16384 cluster_slots_ok:16384 cluster_slots_pfail:0 cluster_slots_fail:0 cluster_known_nodes:6 cluster_size:3 ... 192.168.11.40:9001> cluster nodes cebb7ed63d469748d4015ede6b4a0f5ff59c9322 192.168.11.40:9006@19006 master - 0 1620704406746 0 connected 1b7785f80c4712c6ba4abd71cc93027fa85a02f8 192.168.11.40:9005@19005 master - 0 1620704406000 4 connected 8ccdb0963411ebd05ce21952bdd4b7597825afdc 192.168.11.40:9001@19001 myself,master - 0 1620704404000 2 connected 0-5461 9408059de8b2dd712f0a9381a3b7aad561aef206 192.168.11.40:9004@19004 master - 0 1620704407753 5 connected 85ceb9826e8aa003169c46fb4ba115c72002d4f9 192.168.11.40:9003@19003 master - 0 1620704407000 3 connected 10923-16383 5786e3237c7fa413ed22465d15be721f95e72cfa 192.168.11.40:9002@19002 master - 0 1620704408763 1 connected 5462-10922
每個負責處理槽的節點應該具有從節點,保證當它發生故障時可以自動進行故障轉移
首次啟動的節點和被分配槽的節點都是主節點,從節點負責複製主節點槽資訊和相關的資料
192.168.11.40:9004> cluster replicate 8ccdb0963411ebd05ce21952bdd4b7597825afdc OK 192.168.11.40:9005> cluster replicate 5786e3237c7fa413ed22465d15be721f95e72cfa OK 192.168.11.40:9006> cluster replicate 85ceb9826e8aa003169c46fb4ba115c72002d4f9 OK
192.168.11.40:9001> cluster nodes cebb7ed63d469748d4015ede6b4a0f5ff59c9322 192.168.11.40:9006@19006 slave 85ceb9826e8aa003169c46fb4ba115c72002d4f9 0 1620704825926 3 connected 1b7785f80c4712c6ba4abd71cc93027fa85a02f8 192.168.11.40:9005@19005 slave 5786e3237c7fa413ed22465d15be721f95e72cfa 0 1620704825000 1 connected 8ccdb0963411ebd05ce21952bdd4b7597825afdc 192.168.11.40:9001@19001 myself,master - 0 1620704824000 2 connected 0-5461 9408059de8b2dd712f0a9381a3b7aad561aef206 192.168.11.40:9004@19004 slave 8ccdb0963411ebd05ce21952bdd4b7597825afdc 0 1620704824921 2 connected 85ceb9826e8aa003169c46fb4ba115c72002d4f9 192.168.11.40:9003@19003 master - 0 1620704824000 3 connected 10923-16383 5786e3237c7fa413ed22465d15be721f95e72cfa 192.168.11.40:9002@19002 master - 0 1620704823914 1 connected 5462-10922
##【Redis5.0 後使用redis-cli 搭建叢集】
redis-trib.rb 是採用Ruby 實作的Redis叢集管理工具。內部透過Cluster 相關指令幫我們簡化叢集建立、檢查、槽遷移和均衡等常見運維操作
#1. Ruby 環境安裝Rubywget https://cache.ruby-lang.org/pub/ruby/2.7/ruby-2.7.3.tar.gz tar -zxvf ruby-2.7.3.tar.gz cd ruby-2.7.3 ./configure --prefix=/usr/local/ruby make make install cd /usr/local/ruby cp bin/ruby /usr/local/bin cp bin/gem /usr/local/bin安裝rubygem redis依賴
wget http://rubygems.org/downloads/redis-4.2.5.gem gem install -l redis-4.2.5.gem gem list遇到問題
$ gem install -l redis-4.2.5.gem ERROR: Loading command: install (LoadError) cannot load such file -- zlib ERROR: While executing gem ... (NoMethodError) undefined method `invoke_with_build_args' for nil:NilClass解決(openssl 同理)
yum -y install zlib-devel cd ruby-2.7.3/ext/zlib ruby ./extconf.rb make make install安裝redis-trib.rb
cp /{redis_home}/src/redis-trib.rb /usr/local/bin redis-trib.rb2. 建立叢集
# --replicas 1:指定集群中每个主节点配备几个从节点 redis-trib.rb create --replicas 1 192.168.11.40:9001 192.168.11.40:9002 192.168.11.40:9003 192.168.11.40:9004 192.168.11.40:9005 192.168.11.40:9006 # 集群完整性检查 redis-trib.rb check 192.168.11.40:9001
redis-cli --cluster create 192.168.11.40:9001 192.168.11.40:9002 192.168.11.40:9003 192.168.11.40:9004 192.168.11.40:9005 192.168.11.40:9006 --cluster-replicas 1
以上是CentOS7如何建構Redis集群的詳細內容。更多資訊請關注PHP中文網其他相關文章!