Redis-3.x Cluster安裝配置

Redis-3.x Cluster安裝配置

官方文檔: 
phpredis擴展

環境:
CentOS6.5 x64
redis-3.0.6
master node1: 192.168.192.10
master node2: 192.168.192.11
master node3: 192.168.192.12
slave node1: 192.168.192.20
slave node2: 192.168.192.21
slave node3: 192.168.192.22
3主3輔
有興趣的朋友能夠參看我的以前寫的拙篇 Redis-sentinel集羣

一.安裝編譯依賴庫
yum -y install gcc gcc-c++ make tcl-devel 

二.安裝
tar -xvf redis-3.0.6.tar.gz -C /usr/local/src
cd /usr/local/src/redis-3.0.6
make -j4 &&  make PREFIX=/opt/redis install
cp /usr/local/src/redis-3.0.6/src/redis-trib.rb /opt/redis/bin
echo 'export PATH=$PATH:/opt/redis/bin' >>/etc/profile
source /etc/profile

三.sysv腳本
能夠藉助源碼包自帶的utils交互式工具來生成,不過還要略加修改
a.交互式
root@master:redis-3.0.6#/usr/local/src/redis-3.0.6/utils/ install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] /opt/redis/conf/redis.conf
Please select the redis log file name [/var/log/redis_6379.log] /opt/redis/log/redis.log
Please select the data directory for this instance [/var/lib/redis/6379] /opt/redis/data
Please select the redis executable path [] /opt/redis/bin/redis-server
Selected config:
Port           : 6379
Config file     : /opt/redis/conf/redis.conf
Log file       : /opt/redis/log/redis.log
Data dir       : /opt/redis/data
Executable     : /opt/redis/bin/redis-server
Cli Executable : /opt/redis/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
能夠看到, 腳本幫咱們自動配置好了相關路徑和文件
b.靜默
bash /usr/local/src/redis-3.0.7/utils/install_server.sh <<EOF
6379
/opt/redis/conf/redis.conf
/opt/redis/log/redis.log
/opt/redis/data
/opt/redis/bin/redis-server
EOF

Redis-3.x <wbr>Cluster安裝配置
不過init腳本的名字沒能幫咱們自定義,有強迫症的朋友能夠修改下
ln -s /etc/init.d/redis_6379 /etc/init.d/redis


四.內核參數優化
echo 'net.core.somaxconn = 511' >>  /etc/sysctl.conf
echo 'vm.overcommit_memory = 1'   >> /etc/sysctl.conf
cat >>/etc/rc.d/rc.local <<HERE
echo never > /sys/kernel/mm/transparent_hugepage/enabled
HERE


五.配置集羣
簡述
Redis 集羣的鍵空間被分割爲 16384 (2^14)個槽(slot), 集羣的最大節點數量也是 16384 個(推薦的最大節點數量爲 1000 個),同理每一個主節點能夠負責處理1到16384個槽位。
每一個節點在集羣中由一個獨一無二的 ID標識, 該 ID 是一個十六進制表示的 160 位隨機數,在節點第一次啓動時由 /dev/urandom 生成。節點會將它的 ID 保存到配置文件, 只要這個配置文件不被刪除, 節點就會一直沿用這個 ID 。一個節點能夠改變它的 IP 和端口號, 而不改變節點 ID 。 集羣能夠自動識別出IP/端口號的變化, 並將這一信息經過 Gossip協議廣播給其餘節點知道。

準備
master node1: 192.168.192.10
master node2: 192.168.192.11
master node3: 192.168.192.12
slave node1: 192.168.192.20
slave node2: 192.168.192.21
slave node3: 192.168.192.22
確保以上節點都己成功安裝redis, 安裝方法同上
Note that the  minimal cluster  that works as expected requires to contain at least three master nodes. For your first tests it is strongly suggested to start a six nodes cluster with three masters and three slaves.

A.集羣配置文件
master主配置文件
啓用cluster模式,須要在原配置文件的基礎上增長(或修改)以下幾行
cluster-enabled yes #是否啓用集羣模式
cluster-config-file nodes-6379.conf #集羣節點配置文件,自動生成並經過 Gossip協議同步到各節點
cluster-node-timeout 15000 
cluster-slave-validity-factor 10
cluster-migration-barrier 1
cluster-require-full-coverage yes
slave 主配置文件
直接沿用master的主配置文件,最後手動指定給某臺master來做爲slave
注意:只有全部節點都運行在cluster模式redis cluster才能生效

B.添加master節點
redis-cli cluster meet 192.168.192.10 6379
redis-cli cluster meet 192.168.192.11 6379
redis-cli cluster meet 192.168.192.12 6379
Redis-3.x <wbr>Cluster安裝配置
注意:3節點集羣,默認都爲master,節點己添加成功但處於 fail狀態


C.啓用集羣
master node1: 192.168.192.10
redis-cli cluster addslots {0..5500}
master node2: 192.168.192.11
redis-cli cluster addslots {5501..11000}
master node3: 192.168.192.12
redis-cli cluster addslots {11001..16383}
Redis-3.x <wbr>Cluster安裝配置
各字段對應含義 
node id, address:port, flags, last ping sent, last pong received, configuration epoch, link state, slots
集羣配置信息會寫入到主配置文件中定義的節點配置文件(/opt/redis/data/nodes-6379.conf), 所以,也能夠直接在配置文件裏修改好slots區間後同步到各節點,再重啓redis
注意:集羣可用的必備條件
1.有slots分配到node
2.集羣成員選舉後認爲集羣可用


D.slots在線分片
須要藉助ruby工具包---redis-trib.rb
yum -y install  ruby rubygems
gem install redis
redis-trib.rb reshard --from ca5fb0605fa3efbf62d1c8367489101cccfe0883 --to c16ba7f364b038e532c398d92b24d35ad5e23369 --slots 5 --yes 192.168.192.10:6379
Redis-3.x <wbr>Cluster安裝配置
若是不代詳細參數,redis-trib.rb會交互式地問"移什麼","怎麼移",做爲測試,這裏我只從192.168.192.11上移了5個slots到192.168.192.12這臺主機, 經過slots欄位能夠很清楚的看到

E.添加slave節點
redis-trib.rb add-node --slave --master-id 19bcc3b19b1325fac5c7647c 1431f46299609079 192.168.192.20:6379 192.168.192.10:6379
redis-trib.rb add-node --slave --master-id ca5fb0605fa3efbf62d1c836 7489101cccfe0883 192.168.192.21:6379 192.168.192.11:6379
redis-trib.rb add-node --slave --master-id c16ba7f364b038e532c398d9 2b24d35ad5e23369 192.168.192.22:6379 192.168.192.12:6379
Redis-3.x <wbr>Cluster安裝配置
爲確保主輔一致,請在各slave節點上執行同步操做
redis-cli cluster replicate 19bcc3b19b1325fac5c7647c 1431f46299609079
redis-cli cluster replicate ca5fb0605fa3efbf62d1c8367489101cccfe0883
redis-cli cluster replicate  c16ba7f364b038e532c398d9 2b24d35ad5e23369 


F.failover測試
1.一臺master宕機
redis-cli -h 192.168.192.12 -p 6379 debug segfault
redis-cli cluster info
redis-cli cluster nodes
Redis-3.x <wbr>Cluster安裝配置
能夠看到,當其中一臺master宕機時,該master的slave在很短的時間內就提高本身爲master,並將原master的全部slots所有接管過來
如下是slave切換爲master的詳細日誌
2782:S 25 Dec 11:29:29.384 # Connection with master lost.
2782:S 25 Dec 11:29:29.384 * Caching the disconnected master state.
2782:S 25 Dec 11:29:29.384 * Discarding previously cached master state.
2782:S 25 Dec 11:29:29.649 * Connecting to MASTER 192.168.192.12:6379
2782:S 25 Dec 11:29:29.650 * MASTER <-> SLAVE sync started
2782:S 25 Dec 11:29:29.650 * Non blocking connect for SYNC fired the event.
2782:S 25 Dec 11:29:29.650 * Master replied to PING, replication can continue...
2782:S 25 Dec 11:29:29.651 * Partial resynchronization not possible (no cached master)
2782:S 25 Dec 11:29:29.651 * Full resync from master: cbb2f04d66e95d799f8dabbeaa90c3a293ca9e28:1121
2782:S 25 Dec 11:29:29.758 * MASTER <-> SLAVE sync: receiving 18 bytes from master
2782:S 25 Dec 11:29:29.758 * MASTER <-> SLAVE sync: Flushing old data
2782:S 25 Dec 11:29:29.758 * MASTER <-> SLAVE sync: Loading DB in memory
2782:S 25 Dec 11:29:29.758 * MASTER <-> SLAVE sync: Finished with success
2782:S 25 Dec 11:30:08.012 # Connection with master lost.
2782:S 25 Dec 11:30:08.012 * Caching the disconnected master state.
2782:S 25 Dec 11:30:08.193 * Connecting to MASTER 192.168.192.12:6379
2782:S 25 Dec 11:30:08.193 * MASTER <-> SLAVE sync started
2782:S 25 Dec 11:30:08.193 # Error condition on socket for SYNC: Connection refused
2782:S 25 Dec 11:30:09.212 * Connecting to MASTER 192.168.192.12:6379
2782:S 25 Dec 11:30:09.212 * MASTER <-> SLAVE sync started
2782:S 25 Dec 11:30:09.212 # Error condition on socket for SYNC: Connection refused
2782:S 25 Dec 11:30:10.222 * Connecting to MASTER 192.168.192.12:6379
2782:S 25 Dec 11:30:10.222 * MASTER <-> SLAVE sync started
2782:S 25 Dec 11:30:10.222 # Error condition on socket for SYNC: Connection refused
2782:S 25 Dec 11:30:11.232 * Connecting to MASTER 192.168.192.12:6379
... ...
2782:S 25 Dec 11:30:15.284 # Error condition on socket for SYNC: Connection refused
2782:S 25 Dec 11:30:16.296 * Connecting to MASTER 192.168.192.12:6379
2782:S 25 Dec 11:30:16.296 * MASTER <-> SLAVE sync started
2782:S 25 Dec 11:30:19.329 # Error condition on socket for SYNC: Connection refused
2782:S 25 Dec 11:30:20.349 * Connecting to MASTER 192.168.192.12:6379
2782:S 25 Dec 11:30:20.349 * MASTER <-> SLAVE sync started
2782:S 25 Dec 11:30:20.350 # Error condition on socket for SYNC: Connection refused
2782:S 25 Dec 11:30:21.370 * Connecting to MASTER 192.168.192.12:6379
2782:S 25 Dec 11:30:21.370 * MASTER <-> SLAVE sync started
2782:S 25 Dec 11:30:21.371 # Error condition on socket for SYNC: Connection refused
2782:S 25 Dec 11:30:22.396 * Connecting to MASTER 192.168.192.12:6379
2782:S 25 Dec 11:30:22.396 * MASTER <-> SLAVE sync started
2782:S 25 Dec 11:30:22.396 # Error condition on socket for SYNC: Connection refused
2782:S 25 Dec 11:30:23.411 * Connecting to MASTER 192.168.192.12:6379
2782:S 25 Dec 11:30:23.412 * MASTER <-> SLAVE sync started
2782:S 25 Dec 11:30:23.412 # Error condition on socket for SYNC: Connection refused
2782:S 25 Dec 11:30:23.674 * FAIL message received from ca5fb0605fa3efbf62d1c8367489101cccfe0883 about c16ba7f364b038e532c398d92b24d35ad5e23369
2782:S 25 Dec 11:30:23.674 # Cluster state changed: fail
2782:S 25 Dec 11:30:23.715 # Start of election delayed for 690 milliseconds (rank #0, offset 1163).
2782:S 25 Dec 11:30:24.425 * Connecting to MASTER 192.168.192.12:6379
2782:S 25 Dec 11:30:24.425 * MASTER <-> SLAVE sync started
2782:S 25 Dec 11:30:24.425 # Starting a failover election for epoch 7.
2782:S 25 Dec 11:30:24.469 # Error condition on socket for SYNC: Connection refused
2782:S 25 Dec 11:30:24.470 # Failover election won: I'm the new master.
2782:S 25 Dec 11:30:24.471 # configEpoch set to 7 after successful failover
2782:M 25 Dec 11:30:24.471 * Discarding previously cached master state.
2782:M 25 Dec 11:30:24.471 # Cluster state changed: ok
2.宕機的master修復後從新啓動
Redis-3.x <wbr>Cluster安裝配置
這裏,集羣會自動將原master(192.168.192.12)變爲新master(192.168.192.22)的slave
2411:M 25 Dec 11:44:44.139 # Server started, Redis version 3.0.6
2411:M 25 Dec 11:44:44.139 * DB loaded from disk: 0.000 seconds
2411:M 25 Dec 11:44:44.140 * The server is now ready to accept connections on port 6379
2411:M 25 Dec 11:44:44.194 # Configuration change detected. Reconfiguring myself as a replica of e3bacaf98d2eee3259275c8751cd4757f8ca0b64
2411:S 25 Dec 11:44:44.195 # Cluster state changed: ok
2411:S 25 Dec 11:44:45.215 * Connecting to MASTER 192.168.192.22:6379
2411:S 25 Dec 11:44:45.216 * MASTER <-> SLAVE sync started
2411:S 25 Dec 11:44:45.216 * Non blocking connect for SYNC fired the event.
2411:S 25 Dec 11:44:45.216 * Master replied to PING, replication can continue...
2411:S 25 Dec 11:44:45.217 * Partial resynchronization not possible (no cached master)
2411:S 25 Dec 11:44:45.217 * Full resync from master: 163f91176e281346fefb0935221343b174b40843:1
2411:S 25 Dec 11:44:45.239 * MASTER <-> SLAVE sync: receiving 18 bytes from master
2411:S 25 Dec 11:44:45.239 * MASTER <-> SLAVE sync: Flushing old data
2411:S 25 Dec 11:44:45.240 * MASTER <-> SLAVE sync: Loading DB in memory
2411:S 25 Dec 11:44:45.240 * MASTER <-> SLAVE sync: Finished with success


補充:經常使用管理命令
集羣(cluster)
cluster info 打印集羣的信息  
cluster nodes 列出集羣當前已知的全部節點(node),以及這些節點的相關信息。      
節點 (node)
cluster meet 將IP和PORT所指定的節點添加到集羣當中,讓它成爲集羣的一份子。      
cluster forget 從集羣中移除node_id指定的節點。  
cluster replicate 將當前節點設置爲node_id指定的節點的從節點。  
cluster saveconfig 將節點的配置文件保存到硬盤裏面。
槽(slot)  
cluster addslots [slot ...] 將一個或多個槽(slot)指派(assign)給當前節點。      
cluster delslots [slot ...] 移除一個或多個槽對當前節點的指派。
cluster flushslots 移除指派給當前節點的全部槽,讓當前節點變成一個沒有指派任何槽的節點。 
cluster setslot node 將槽slot指派給node_id指定的節點,若是槽已經指派給另外一個節點,那麼先讓另外一個節點刪除該槽,而後再進行指派。        
cluster setslot migrating 將本節點的槽slot遷移到node_id指定的節點中。    
cluster setslot importing 從node_id指定的節點中導入槽slot到本節點。    
cluster setslot stable 取消對槽slot的導入(import)或者遷移(migrate)。      
鍵 (key)
cluster keyslot 計算鍵key應該被放置在哪一個槽上。  
cluster countkeysinslot 返回槽slot 目前包含的鍵值對數量。 
cluster getkeysinslot 返回count個slot 槽中的鍵。
相關文章
相關標籤/搜索