1:安裝redis cluster
1):安裝redis-cluster依賴:redis-cluster的依賴庫在使用時有兼容問題,在reshard時會遇到各種錯誤,請按指定版本安裝.
(1)確保系統安裝zlib,否則gem install會報(no such file to load -- zlib)
1. #download:zlib-1.2.6.tar
2. ./configure
3. make
4. make install
(1)安裝ruby:version(1.9.2)
1. # ruby1.9.2
2. cd /path/ruby
3. ./configure -prefix=/usr/local/ruby
4. make
5. make install
6. sudo cp ruby /usr/local/bin
(2)安裝rubygem:version(1.8.16)
1. # rubygems-1.8.16.tgz
2. cd /path/gem
3. sudo ruby setup.rb
4. sudo cp bin/gem /usr/local/bin
(3)安裝gem-redis:version(3.0.0)
1. gem install redis --version 3.0.0
2. #由於源的原因,可能下載失敗,就手動下載下來安裝
3. #download地址:http://rubygems.org/gems/redis/versions/3.0.0
4. gem install -l /data/soft/redis-3.0.0.gem
2)安裝redis-cluster
1. cd /path/redis
2. make
3. sudo cp /opt/redis/src/redis-server /usr/local/bin
4. sudo cp /opt/redis/src/redis-cli /usr/local/bin
5. sudo cp /opt/redis/src/redis-trib.rb /usr/local/bin
2:配置redis cluster
1)redis配置文件結構:
使用包含(include)把通用配置和特殊配置分離,方便維護.
2)redis通用配置.
1. #GENERAL
2. daemonize no
3. tcp-backlog 511
4. timeout 0
5. tcp-keepalive 0
6. loglevel notice
7. databases 16
8. dir /opt/redis/data
9. slave-serve-stale-data yes
10.#slave只讀
11.slave-read-only yes
12.#not use default
13.repl-disable-tcp-nodelay yes
14.slave-priority 100
15.#打開aof持久化
16.appendonly yes
17.#每秒一次aof寫
18.appendfsync everysec
19.#關閉在aof rewrite的時候對新的寫操作進行fsync
20.no-appendfsync-on-rewrite yes
21.auto-aof-rewrite-min-size 64mb
22.lua-time-limit 5000
23.#打開redis集羣
24.cluster-enabled yes
25.#節點互連超時的閥值
26.cluster-node-timeout 15000
27.cluster-migration-barrier 1
28.slowlog-log-slower-than 10000
29.slowlog-max-len 128
30.notify-keyspace-events ""
31.hash-max-ziplist-entries 512
32.hash-max-ziplist-value 64
33.list-max-ziplist-entries 512
34.list-max-ziplist-value 64
35.set-max-intset-entries 512
36.zset-max-ziplist-entries 128
37.zset-max-ziplist-value 64
38.activerehashing yes
39.client-output-buffer-limit normal 0 0 0
40.client-output-buffer-limit slave 256mb 64mb 60
41.client-output-buffer-limit pubsub 32mb 8mb 60
42.hz 10
43.aof-rewrite-incremental-fsync yes
3)redis特殊配置.
1. #包含通用配置
2. include /opt/redis/redis-common.conf
3. #監聽tcp端口
4. port 6379
5. #最大可用內存
6. maxmemory 100m
7. #內存耗盡時採用的淘汰策略:
8. # volatile-lru -> remove the key with an expire set using an LRU algorithm
9. # allkeys-lru -> remove any key accordingly to the LRU algorithm
10.# volatile-random -> remove a random key with an expire set
11.# allkeys-random -> remove a random key, any key
12.# volatile-ttl -> remove the key with the nearest expire time (minor TTL)
13.# noeviction -> don't expire at all, just return an error on write operations
14.maxmemory-policy allkeys-lru
15.#aof存儲文件
16.appendfilename "appendonly-6379.aof"
17.#rdb文件,只用於動態添加slave過程
18.dbfilename dump-6379.rdb
19.#cluster配置文件(啓動自動生成)
20.cluster-config-file nodes-6379.conf
21.#部署在同一機器的redis實例,把<span style="font-size: 1em; line-height: 1.5;">auto-aof-rewrite搓開,防止瞬間fork所有redis進程做rewrite,佔用大量內存</span>
22.auto-aof-rewrite-percentage 80-100
3:cluster 操作
cluster集羣相關命令,更多redis相關命令見文檔:http://redis.readthedocs.org/en/latest/
1. 集羣
2. CLUSTER INFO 打印集羣的信息
3. CLUSTER NODES 列出集羣當前已知的所有節點(node),以及這些節點的相關信息。
4. 節點
5. CLUSTER MEET <ip> <port> 將 ip 和 port 所指定的節點添加到集羣當中,讓它成爲集羣的一份子。
6. CLUSTER FORGET <node_id> 從集羣中移除 node_id 指定的節點。
7. CLUSTER REPLICATE <node_id> 將當前節點設置爲 node_id 指定的節點的從節點。
8. CLUSTER SAVECONFIG 將節點的配置文件保存到硬盤裏面。
9. 槽(slot)
10.CLUSTER ADDSLOTS <slot> [slot ...] 將一個或多個槽(slot)指派(assign)給當前節點。
11.CLUSTER DELSLOTS <slot> [slot ...] 移除一個或多個槽對當前節點的指派。
12.CLUSTER FLUSHSLOTS 移除指派給當前節點的所有槽,讓當前節點變成一個沒有指派任何槽的節點。
13.CLUSTER SETSLOT <slot> NODE <node_id> 將槽 slot 指派給 node_id 指定的節點,如果槽已經指派給另一個節點,那麼先讓另一個節點刪除該槽>,然後再進行指派。
14.CLUSTER SETSLOT <slot> MIGRATING <node_id> 將本節點的槽 slot 遷移到 node_id 指定的節點中。
15.CLUSTER SETSLOT <slot> IMPORTING <node_id> 從 node_id 指定的節點中導入槽 slot 到本節點。
16.CLUSTER SETSLOT <slot> STABLE 取消對槽 slot 的導入(import)或者遷移(migrate)。
17.鍵
18.CLUSTER KEYSLOT <key> 計算鍵 key 應該被放置在哪個槽上。
19.CLUSTER COUNTKEYSINSLOT <slot> 返回槽 slot 目前包含的鍵值對數量。
20.CLUSTER GETKEYSINSLOT <slot> <count> 返回 count 個 slot 槽中的鍵。
4:redis cluster 運維操作
1)初始化並構建集羣
(1)#啓動集羣相關節點(必須是空節點),指定配置文件和輸出日誌
1. redis-server /opt/redis/conf/redis-6380.conf > /opt/redis/logs/redis-6380.log 2>&1 &
2. redis-server /opt/redis/conf/redis-6381.conf > /opt/redis/logs/redis-6381.log 2>&1 &
3. redis-server /opt/redis/conf/redis-6382.conf > /opt/redis/logs/redis-6382.log 2>&1 &
4. redis-server /opt/redis/conf/redis-7380.conf > /opt/redis/logs/redis-7380.log 2>&1 &
5. redis-server /opt/redis/conf/redis-7381.conf > /opt/redis/logs/redis-7381.log 2>&1 &
6. redis-server /opt/redis/conf/redis-7382.conf > /opt/redis/logs/redis-7382.log 2>&1 &
(2):使用自帶的ruby工具(redis-trib.rb)構建集羣
1. #redis-trib.rb的create子命令構建
2. #--replicas 則指定了爲Redis Cluster中的每個Master節點配備幾個Slave節點
3. #節點角色由順序決定,先master之後是slave(爲方便辨認,slave的端口比master大1000)
4. redis-trib.rb create --replicas 1 10.10.34.14:6380 10.10.34.14:6381 10.10.34.14:6382 10.10.34.14:7380 10.10.34.14:7381 10.10.34.14:7382
(3):檢查集羣狀態,
1. #redis-trib.rb的check子命令構建
2. #ip:port可以是集羣的任意節點
3. redis-trib.rb check 1 10.10.34.14:6380
最後輸出如下信息,沒有任何警告或錯誤,表示集羣啓動成功並處於ok狀態
1. [OK] All nodes agree about slots configuration.
2. >>> Check for open slots...
3. >>> Check slots coverage...
4. [OK] All 16384 slots covered.
2):添加新master節點
(1)添加一個master節點:創建一個空節點(empty node),然後將某些slot移動到這個空節點上,這個過程目前需要人工干預
a):根據端口生成配置文件(ps:establish_config.sh是我自己寫的輸出配置腳本)
1. sh establish_config.sh 6386 > conf/redis-6386.conf
b):啓動節點
1. nohup redis-server /opt/redis/conf/redis-6386.conf > /opt/redis/logs/redis-6386.log 2>&1 &
c):加入空節點到集羣
add-node 將一個節點添加到集羣裏面, 第一個是新節點ip:port,
第二個是任意一個已存在節點ip:port
1. redis-trib.rb add-node 10.10.34.14:6386 10.10.34.14:6381
node:新節點沒有包含任何數據,因爲它沒有包含任何slot。新加入的加點是一個主節點,當集羣需要將某個從節點升級爲新的主節點時,這個新節點不會被選中
d):爲新節點分配slot
1. redis-trib.rb reshard 10.10.34.14:6386
2. #根據提示選擇要遷移的slot數量(ps:這裏選擇500)
3. How many slots do you want to move (from 1 to 16384)? 500
4. #選擇要接受這些slot的node-id
5. What is the receiving node ID? f51e26b5d5ff74f85341f06f28f125b7254e61bf
6. #選擇slot來源:
7. #all表示從所有的master重新分配,
8. #或者數據要提取slot的master節點id,最後用done結束
9. Please enter all the source node IDs.
10. Type 'all' to use all the nodes as source nodes for the hash slots.
11. Type 'done' once you entered all the source nodes IDs.
12.Source node #1:all
13.#打印被移動的slot後,輸入yes開始移動slot以及對應的數據.
14.#Do you want to proceed with the proposed reshard plan (yes/no)? yes
15.#結束
3):添加新的slave節點
a):前三步操作同添加master一樣
b)第四步:redis-cli連接上新節點shell,輸入命令:cluster replicate 對應master的node-id
1. cluster replicate 2b9ebcbd627ff0fd7a7bbcc5332fb09e72788835
note:在線添加slave 時,需要dump整個master進程,並傳遞到slave,再由 slave加載rdb文件到內存,rdb傳輸過程中Master可能無法提供服務,整個過程消耗大量io,小心操作.
例如本次添加slave操作產生的rdb文件
1. -rw-r--r-- 1 root root 34946 Apr 17 18:23 dump-6386.rdb
2. -rw-r--r-- 1 root root 34946 Apr 17 18:23 dump-7386.rdb
4):在線reshard 數據:
對於負載/數據均勻的情況,可以在線reshard slot來解決,方法與添加新master的reshard一樣,只是需要reshard的master節點是老節點.
5):刪除一個slave節點
1. #redis-trib del-node ip:port '<node-id>'
2. redis-trib.rb del-node 10.10.34.14:7386 'c7ee2fca17cb79fe3c9822ced1d4f6c5e169e378'
6):刪除一個master節點
a):刪除master節點之前首先要使用reshard移除master的全部slot,然後再刪除當前節點(目前只能把被刪除
master的slot遷移到一個節點上)
1. #把10.10.34.14:6386當前master遷移到10.10.34.14:6380上
2. redis-trib.rb reshard 10.10.34.14:6380
3. #根據提示選擇要遷移的slot數量(ps:這裏選擇500)
4. How many slots do you want to move (from 1 to 16384)? 500(被刪除master的所有slot數量)
5. #選擇要接受這些slot的node-id(10.10.34.14:6380)
6. What is the receiving node ID? c4a31c852f81686f6ed8bcd6d1b13accdc947fd2 (ps:10.10.34.14:6380的node-id)
7. Please enter all the source node IDs.
8. Type 'all' to use all the nodes as source nodes for the hash slots.
9. Type 'done' once you entered all the source nodes IDs.
10.Source node #1:f51e26b5d5ff74f85341f06f28f125b7254e61bf(被刪除master的node-id)
11.Source node #2:done
12.#打印被移動的slot後,輸入yes開始移動slot以及對應的數據.
13.#Do you want to proceed with the proposed reshard plan (yes/no)? yes
b):刪除空master節點
1. redis-trib.rb del-node 10.10.34.14:6386 'f51e26b5d5ff74f85341f06f28f125b7254e61bf'
三:redis cluster 客戶端(Jedis)
1:客戶端基本操作使用
1. <span style="color: #333333; font-family: Arial, sans-serif;"><span style="color: #333333; font-family: Arial, sans-serif;"> private static BinaryJedisCluster jc;
2. static {
3. //只給集羣裏一個實例就可以
4. Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
5. jedisClusterNodes.add(new HostAndPort("10.10.34.14", 6380));
6. jedisClusterNodes.add(new HostAndPort("10.10.34.14", 6381));
7. jedisClusterNodes.add(new HostAndPort("10.10.34.14", 6382));
8. jedisClusterNodes.add(new HostAndPort("10.10.34.14", 6383));
9. jedisClusterNodes.add(new HostAndPort("10.10.34.14", 6384));
10. jedisClusterNodes.add(new HostAndPort("10.10.34.14", 7380));
11. jedisClusterNodes.add(new HostAndPort("10.10.34.14", 7381));
12. jedisClusterNodes.add(new HostAndPort("10.10.34.14", 7382));
13. jedisClusterNodes.add(new HostAndPort("10.10.34.14", 7383));
14. jedisClusterNodes.add(new HostAndPort("10.10.34.14", 7384));
15. jc = new BinaryJedisCluster(jedisClusterNodes);
16. }
17.@Test
18. public void testBenchRedisSet() throws Exception {
19. final Stopwatch stopwatch = new Stopwatch();
20. List list = buildBlogVideos();
21. for (int i = 0; i < 1000; i++) {
22. String key = "key:" + i;
23. stopwatch.start();
24. byte[] bytes1 = protostuffSerializer.serialize(list);
25. jc.setex(key, 60 * 60, bytes1);
26. stopwatch.stop();
27. }
28. System.out.println("time=" + stopwatch.toString());
29. }</span></span>
redis-trib.rb具有以下功能:
1、 create:創建集羣
2、check:檢查集羣
3、info:查看集羣信息
4、fix:修復集羣
5、reshard:在線遷移slot
6、rebalance:平衡集羣節點slot數量
7、add-node:將新節點加入集羣
8、del-node:從集羣中刪除節點
9、set-timeout:設置集羣節點間心跳連接的超時時間
10、call:在集羣全部節點上執行命令
11、import:將外部redis數據導入集羣
進入安裝文件目錄,然後 使用如下命令進行安裝
./configure --prefix=/data/progam/zlib
make
make install
vi/etc/ld.so.conf.d/zlib.conf
加入如下內容後保存退出
/data/program/zlib/lib