Redis系列--8、集群配置方式

一:關(guān)于redis cluster

 1:redis cluster的現(xiàn)狀

目前redis支持的cluster特性

成都創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比翁牛特網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式翁牛特網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋翁牛特地區(qū)。費(fèi)用合理售后完善,十余年實(shí)體公司更值得信賴。

1):節(jié)點(diǎn)自動(dòng)發(fā)現(xiàn)

2):slave->master 選舉,集群容錯(cuò)

3):Hot resharding:在線分片

4):進(jìn)群管理:cluster xxx

5):基于配置(nodes-port.conf)的集群管理

6):ASK 轉(zhuǎn)向/MOVED 轉(zhuǎn)向機(jī)制.

 2:redis cluster 架構(gòu)

1)redis-cluster架構(gòu)圖

 Redis系列--8、集群配置方式

架構(gòu)細(xì)節(jié):

(1)所有的redis節(jié)點(diǎn)彼此互聯(lián)(PING-PONG機(jī)制),內(nèi)部使用二進(jìn)制協(xié)議優(yōu)化傳輸速度和帶寬.

(2)節(jié)點(diǎn)的fail是通過集群中超過半數(shù)的節(jié)點(diǎn)檢測(cè)失效時(shí)才生效.

(3)客戶端與redis節(jié)點(diǎn)直連,不需要中間proxy層.客戶端不需要連接集群所有節(jié)點(diǎn),連接集群中任何一個(gè)可用節(jié)點(diǎn)即可

(4)redis-cluster把所有的物理節(jié)點(diǎn)映射到[0-16383]slot上,cluster 負(fù)責(zé)維護(hù)node<->slot<->value

2) redis-cluster選舉:容錯(cuò)

 Redis系列--8、集群配置方式

(1)領(lǐng)著選舉過程是集群中所有master參與,如果半數(shù)以上master節(jié)點(diǎn)與master節(jié)點(diǎn)通信超過(cluster-node-timeout),認(rèn)為當(dāng)前master節(jié)點(diǎn)掛掉.

(2):什么時(shí)候整個(gè)集群不可用(cluster_state:fail),當(dāng)集群不可用時(shí),所有對(duì)集群的操作做都不可用,收到((error) CLUSTERDOWN The cluster is down)錯(cuò)誤

    a:如果集群任意master掛掉,且當(dāng)前master沒有slave.集群進(jìn)入fail狀態(tài),也可以理解成進(jìn)群的slot映射[0-16383]不完成時(shí)進(jìn)入fail狀態(tài).

    b:如果進(jìn)群超過半數(shù)以上master掛掉,無論是否有slave集群進(jìn)入fail狀態(tài).

 二:redis cluster的使用

 1:安裝redis cluster

1):安裝redis-cluster依賴:redis-cluster的依賴庫在使用時(shí)有兼容問題,在reshard時(shí)會(huì)遇到各種錯(cuò)誤,請(qǐng)按指定版本安裝.

(1)確保系統(tǒng)安裝zlib,否則gem install會(huì)報(bào)(no such file to load -- zlib)

#download:zlib-1.2.6.tar  
./configure  
make  
make install

 (2)安裝ruby:version(1.9.2)

# ruby1.9.2   
cd /path/ruby  
./configure -prefix=/usr/local/ruby  
make  
make install  
sudo cp ruby /usr/local/bin

(3)安裝rubygem:version(1.8.16)

# rubygems-1.8.16.tgz  
cd /path/gem  
sudo ruby setup.rb  
sudo cp bin/gem /usr/local/bin

(4)安裝gem-redis:version(3.0.0)

gem install redis --version 3.0.0  
#由于源的原因,可能下載失敗,就手動(dòng)下載下來安裝  
#download地址:http://rubygems.org/gems/redis/versions/3.0.0  
gem install -l /data/soft/redis-3.0.0.gem

(5)安裝redis-cluster

cd /path/redis  
make  
sudo cp /opt/redis/src/redis-server /usr/local/bin  
sudo cp /opt/redis/src/redis-cli /usr/local/bin  
sudo cp /opt/redis/src/redis-trib.rb /usr/local/bin

2:配置redis cluster

1)redis配置文件結(jié)構(gòu):

Redis系列--8、集群配置方式

 使用包含(include)把通用配置和特殊配置分離,方便維護(hù).

2)redis通用配置.

#GENERAL  
daemonize no  
tcp-backlog 511  
timeout 0  
tcp-keepalive 0  
loglevel notice  
databases 16  
dir /opt/redis/data  
slave-serve-stale-data yes  
#slave只讀  
slave-read-only yes  
#not use default  
repl-disable-tcp-nodelay yes  
slave-priority 100  
#打開aof持久化  
appendonly yes  
#每秒一次aof寫  
appendfsync everysec  
#關(guān)閉在aof rewrite的時(shí)候?qū)π碌膶懖僮鬟M(jìn)行fsync  
no-appendfsync-on-rewrite yes  
auto-aof-rewrite-min-size 64mb  
lua-time-limit 5000  
#打開redis集群  
cluster-enabled yes  
#節(jié)點(diǎn)互連超時(shí)的閥值  
cluster-node-timeout 15000  
cluster-migration-barrier 1  
slowlog-log-slower-than 10000  
slowlog-max-len 128  
notify-keyspace-events ""  
hash-max-ziplist-entries 512  
hash-max-ziplist-value 64  
list-max-ziplist-entries 512  
list-max-ziplist-value 64  
set-max-intset-entries 512  
zset-max-ziplist-entries 128  
zset-max-ziplist-value 64  
activerehashing yes  
client-output-buffer-limit normal 0 0 0  
client-output-buffer-limit slave 256mb 64mb 60  
client-output-buffer-limit pubsub 32mb 8mb 60  
hz 10  
aof-rewrite-incremental-fsync yes

3)redis特殊配置.

#包含通用配置  
include /opt/redis/redis-common.conf  
#監(jiān)聽tcp端口  
port 6379  
#最大可用內(nèi)存  
maxmemory 100m  
#內(nèi)存耗盡時(shí)采用的淘汰策略:  
# volatile-lru -> remove the key with an expire set using an LRU algorithm  
# allkeys-lru -> remove any key accordingly to the LRU algorithm  
# volatile-random -> remove a random key with an expire set  
# allkeys-random -> remove a random key, any key  
# volatile-ttl -> remove the key with the nearest expire time (minor TTL)  
# noeviction -> don't expire at all, just return an error on write operations  
maxmemory-policy allkeys-lru  
#aof存儲(chǔ)文件  
appendfilename "appendonly-6379.aof"  
#rdb文件,只用于動(dòng)態(tài)添加slave過程  
dbfilename dump-6379.rdb  
#cluster配置文件(啟動(dòng)自動(dòng)生成)  
cluster-config-file nodes-6379.conf  
#部署在同一機(jī)器的redis實(shí)例,把<span >auto-aof-rewrite搓開,防止瞬間fork所有redis進(jìn)程做rewrite,占用大量?jī)?nèi)存</span>  
auto-aof-rewrite-percentage 80-100

3:cluster 操作

cluster集群相關(guān)命令,更多redis相關(guān)命令見文檔:http://redis.readthedocs.org/en/latest/

集群  
CLUSTER INFO 打印集群的信息  
CLUSTER NODES 列出集群當(dāng)前已知的所有節(jié)點(diǎn)(node),以及這些節(jié)點(diǎn)的相關(guān)信息。  
節(jié)點(diǎn)  
CLUSTER MEET <ip> <port> 將 ip 和 port 所指定的節(jié)點(diǎn)添加到集群當(dāng)中,讓它成為集群的一份子。  
CLUSTER FORGET <node_id> 從集群中移除 node_id 指定的節(jié)點(diǎn)。  
CLUSTER REPLICATE <node_id> 將當(dāng)前節(jié)點(diǎn)設(shè)置為 node_id 指定的節(jié)點(diǎn)的從節(jié)點(diǎn)。  
CLUSTER SAVECONFIG 將節(jié)點(diǎn)的配置文件保存到硬盤里面。  
槽(slot)  
CLUSTER ADDSLOTS <slot> [slot ...] 將一個(gè)或多個(gè)槽(slot)指派(assign)給當(dāng)前節(jié)點(diǎn)。  
CLUSTER DELSLOTS <slot> [slot ...] 移除一個(gè)或多個(gè)槽對(duì)當(dāng)前節(jié)點(diǎn)的指派。  
CLUSTER FLUSHSLOTS 移除指派給當(dāng)前節(jié)點(diǎn)的所有槽,讓當(dāng)前節(jié)點(diǎn)變成一個(gè)沒有指派任何槽的節(jié)點(diǎn)。  
CLUSTER SETSLOT <slot> NODE <node_id> 將槽 slot 指派給 node_id 指定的節(jié)點(diǎn),如果槽已經(jīng)指派給另一個(gè)節(jié)點(diǎn),那么先讓另一個(gè)節(jié)點(diǎn)刪除該槽>,然后再進(jìn)行指派。  
CLUSTER SETSLOT <slot> MIGRATING <node_id> 將本節(jié)點(diǎn)的槽 slot 遷移到 node_id 指定的節(jié)點(diǎn)中。  
CLUSTER SETSLOT <slot> IMPORTING <node_id> 從 node_id 指定的節(jié)點(diǎn)中導(dǎo)入槽 slot 到本節(jié)點(diǎn)。  
CLUSTER SETSLOT <slot> STABLE 取消對(duì)槽 slot 的導(dǎo)入(import)或者遷移(migrate)。  
鍵  
CLUSTER KEYSLOT <key> 計(jì)算鍵 key 應(yīng)該被放置在哪個(gè)槽上。  
CLUSTER COUNTKEYSINSLOT <slot> 返回槽 slot 目前包含的鍵值對(duì)數(shù)量。  
CLUSTER GETKEYSINSLOT <slot> <count> 返回 count 個(gè) slot 槽中的鍵。

4:redis cluster 運(yùn)維操作

 1)初始化并構(gòu)建集群

(1)#啟動(dòng)集群相關(guān)節(jié)點(diǎn)(必須是空節(jié)點(diǎn)),指定配置文件和輸出日志

redis-server /opt/redis/conf/redis-6380.conf > /opt/redis/logs/redis-6380.log 2>&1 &  
redis-server /opt/redis/conf/redis-6381.conf > /opt/redis/logs/redis-6381.log 2>&1 &  
redis-server /opt/redis/conf/redis-6382.conf > /opt/redis/logs/redis-6382.log 2>&1 &  
redis-server /opt/redis/conf/redis-7380.conf > /opt/redis/logs/redis-7380.log 2>&1 &  
redis-server /opt/redis/conf/redis-7381.conf > /opt/redis/logs/redis-7381.log 2>&1 &  
redis-server /opt/redis/conf/redis-7382.conf > /opt/redis/logs/redis-7382.log 2>&1 &

(2):使用自帶的ruby工具(redis-trib.rb)構(gòu)建集群

#redis-trib.rb的create子命令構(gòu)建  
#--replicas 則指定了為Redis Cluster中的每個(gè)Master節(jié)點(diǎn)配備幾個(gè)Slave節(jié)點(diǎn)  
#節(jié)點(diǎn)角色由順序決定,先master之后是slave(為方便辨認(rèn),slave的端口比master大1000)  
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):檢查集群狀態(tài)

#redis-trib.rb的check子命令構(gòu)建  
#ip:port可以是集群的任意節(jié)點(diǎn)  
redis-trib.rb check 1 10.10.34.14:6380

最后輸出如下信息,沒有任何警告或錯(cuò)誤,表示集群?jiǎn)?dòng)成功并處于ok狀態(tài)

[OK] All nodes agree about slots configuration.  
>>> Check for open slots...  
>>> Check slots coverage...  
[OK] All 16384 slots covered.

(4):使用redis-cli命令進(jìn)入集群環(huán)境

redis-cli -c -p 7001

2):添加新master節(jié)點(diǎn)

(1)添加一個(gè)master節(jié)點(diǎn):創(chuàng)建一個(gè)空節(jié)點(diǎn)(empty node),然后將某些slot移動(dòng)到這個(gè)空節(jié)點(diǎn)上,這個(gè)過程目前需要人工干預(yù)

a):根據(jù)端口生成配置文件(ps:establish_config.sh是我自己寫的輸出配置腳本)

sh establish_config.sh 6386 > conf/redis-6386.conf

b):啟動(dòng)節(jié)點(diǎn)

nohup redis-server /opt/redis/conf/redis-6386.conf > /opt/redis/logs/redis-6386.log 2>&1 &

c):加入空節(jié)點(diǎn)到集群

add-node  將一個(gè)節(jié)點(diǎn)添加到集群里面, 第一個(gè)是新節(jié)點(diǎn)ip:port, 第二個(gè)是任意一個(gè)已存在節(jié)點(diǎn)ip:port

redis-trib.rb add-node 10.10.34.14:6386 10.10.34.14:6381

node:新節(jié)點(diǎn)沒有包含任何數(shù)據(jù), 因?yàn)樗鼪]有包含任何slot。新加入的加點(diǎn)是一個(gè)主節(jié)點(diǎn), 當(dāng)集群需要將某個(gè)從節(jié)點(diǎn)升級(jí)為新的主節(jié)點(diǎn)時(shí), 這個(gè)新節(jié)點(diǎn)不會(huì)被選中

d):為新節(jié)點(diǎn)分配slot

redis-trib.rb reshard 10.10.34.14:6386  
#根據(jù)提示選擇要遷移的slot數(shù)量(ps:這里選擇500)  
How many slots do you want to move (from 1 to 16384)? 500  
#選擇要接受這些slot的node-id  
What is the receiving node ID? f51e26b5d5ff74f85341f06f28f125b7254e61bf  
#選擇slot來源:  
#all表示從所有的master重新分配,  
#或者數(shù)據(jù)要提取slot的master節(jié)點(diǎn)id,最后用done結(jié)束  
Please enter all the source node IDs.  
  Type 'all' to use all the nodes as source nodes for the hash slots.  
  Type 'done' once you entered all the source nodes IDs.  
Source node #1:all  
#打印被移動(dòng)的slot后,輸入yes開始移動(dòng)slot以及對(duì)應(yīng)的數(shù)據(jù).  
#Do you want to proceed with the proposed reshard plan (yes/no)? yes  
#結(jié)束

 3):添加新的slave節(jié)點(diǎn)

a):前三步操作同添加master一樣

b)第四步:redis-cli連接上新節(jié)點(diǎn)shell,輸入命令:cluster replicate 對(duì)應(yīng)master的node-id

cluster replicate 2b9ebcbd627ff0fd7a7bbcc5332fb09e72788835

note:在線添加slave 時(shí),需要dump整個(gè)master進(jìn)程,并傳遞到slave,再由 slave加載rdb文件到內(nèi)存,rdb傳輸過程中Master可能無法提供服務(wù),整個(gè)過程消耗大量io,小心操作.

例如本次添加slave操作產(chǎn)生的rdb文件

-rw-r--r-- 1 root root  34946 Apr 17 18:23 dump-6386.rdb  
-rw-r--r-- 1 root root  34946 Apr 17 18:23 dump-7386.rdb

4):在線reshard 數(shù)據(jù):

對(duì)于負(fù)載/數(shù)據(jù)均勻的情況,可以在線reshard slot來解決,方法與添加新master的reshard一樣,只是需要reshard的master節(jié)點(diǎn)是老節(jié)點(diǎn).

 5):刪除一個(gè)slave節(jié)點(diǎn)

#redis-trib del-node ip:port '<node-id>'  
redis-trib.rb del-node 10.10.34.14:7386 'c7ee2fca17cb79fe3c9822ced1d4f6c5e169e378'

6):刪除一個(gè)master節(jié)點(diǎn)

a):刪除master節(jié)點(diǎn)之前首先要使用reshard移除master的全部slot,然后再刪除當(dāng)前節(jié)點(diǎn)(目前只能把被刪除master的slot遷移到一個(gè)節(jié)點(diǎn)上)

#把10.10.34.14:6386當(dāng)前master遷移到10.10.34.14:6380上  
redis-trib.rb reshard 10.10.34.14:6380  
#根據(jù)提示選擇要遷移的slot數(shù)量(ps:這里選擇500)  
How many slots do you want to move (from 1 to 16384)? 500(被刪除master的所有slot數(shù)量)  
#選擇要接受這些slot的node-id(10.10.34.14:6380)  
What is the receiving node ID? c4a31c852f81686f6ed8bcd6d1b13accdc947fd2 (ps:10.10.34.14:6380的node-id)  
Please enter all the source node IDs.  
  Type 'all' to use all the nodes as source nodes for the hash slots.  
  Type 'done' once you entered all the source nodes IDs.  
Source node #1:f51e26b5d5ff74f85341f06f28f125b7254e61bf(被刪除master的node-id)  
Source node #2:done  
#打印被移動(dòng)的slot后,輸入yes開始移動(dòng)slot以及對(duì)應(yīng)的數(shù)據(jù).  
#Do you want to proceed with the proposed reshard plan (yes/no)? yes

b):刪除空master節(jié)點(diǎn)

redis-trib.rb del-node 10.10.34.14:6386 'f51e26b5d5ff74f85341f06f28f125b7254e61bf'

三:redis cluster 客戶端(Jedis)

 1:客戶端基本操作使用

public static void main(String[] args) {
           JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxTotal(20);
            config.setMaxIdle(2);
           HostAndPort hp0 = new HostAndPort( "10.8.24.112", 7001);
           HostAndPort hp1 = new HostAndPort( "10.8.24.112", 7002);
           HostAndPort hp2 = new HostAndPort( "10.8.24.112", 7003);
           HostAndPort hp3 = new HostAndPort( "10.8.24.112", 7004);
           HostAndPort hp4 = new HostAndPort( "10.8.24.112", 7005);
           HostAndPort hp5 = new HostAndPort( "10.8.24.112", 7006);
           Set<HostAndPort> hps = new HashSet<HostAndPort>();
            hps.add( hp0);
            hps.add( hp1);
            hps.add( hp2);
            hps.add( hp3);
            hps.add( hp4);
            hps.add( hp5);
            // 超時(shí),最大的轉(zhuǎn)發(fā)數(shù),最大鏈接數(shù),最小鏈接數(shù)都會(huì)影響到集群
           JedisCluster jedisCluster = new JedisCluster( hps, 5000, 10, config);
            long start = System. currentTimeMillis();
            for ( int i = 0; i < 100; i++) {
                 jedisCluster.set( "sn" + i, "n" + i);
           }
            long end = System. currentTimeMillis();
           System. out.println( "Simple  @ Sharding Set : " + (end - start) / 10000);
            for ( int i = 0; i < 1000; i++) {
                System. out.println( jedisCluster.get( "sn" + i));
           }
            try {
                 jedisCluster.close();
           } catch (IOException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
           }
     }

網(wǎng)站題目:Redis系列--8、集群配置方式
標(biāo)題URL:http://bm7419.com/article20/jdijjo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、網(wǎng)站改版、動(dòng)態(tài)網(wǎng)站響應(yīng)式網(wǎng)站、網(wǎng)站建設(shè)微信公眾號(hào)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化