Redis集群的搭建
本來是想搞一主二從的犬第,但是一主二從不好擴展啊锦积,高并發(fā)的時候性能肯定也不如集群啊,redis3.0開始都用集群了歉嗓,一主二從加哨兵的模式感覺要被淘汰了丰介。所以還是直接搞集群吧。
- 先研究一下redis集群的架構(gòu)以及原理
- redis集群搭建
- 運維(遷移,擴容哮幢,縮容等)
寫的比較多了带膀,可以直接去看您感興趣的那部分,有錯誤的地方歡迎指出橙垢。
1redis集群的架構(gòu)和原理
- [x] 簡單的說一下:
在redis官方未推出其集群化解決方案時垛叨,很多大公司自己出了些解決方案,這些方案的核心思想是把數(shù)據(jù)分片(sharding)存儲在多個Redis實例中柜某,每一片就是一個Redis實例嗽元,但是都需要加入一些中間件比如zookeeper。
Redis 3.0集群采用了P2P的模式喂击,完全去中心化剂癌。Redis把所有的Key分成了16384個slot,每個Redis實例負責其中一部分slot翰绊。集群中的所有信息(節(jié)點佩谷、端口、slot等)监嗜,都通過節(jié)點之間定期的數(shù)據(jù)交換而更新谐檀。Redis客戶端在任意一個Redis實例發(fā)出請求,如果所需數(shù)據(jù)不在該實例中裁奇,通過重定向命令引導客戶端訪問所需的實例稚补。
總的來說就是多個實例組成了集群,數(shù)據(jù)分散在不同的實例上框喳,然后你可以隨便去哪個實例都能讀到數(shù)據(jù)。另外為了保證高可用厦坛,還要配置從節(jié)點五垮。
- [x] 我的疑問:
現(xiàn)在我配好了六個實例作為一個集群,其中有三個master杜秸,三個slave,那么問題來了
1 在集群中有多個實例放仗,而且針對這個集群沒有統(tǒng)一的端口(比如我下文配置好的集群就有7000-70005,六個實例六個端口)撬碟,那我寫的時候往哪個端口寫暗ぁ? 有三個master呢呢蛤?
答:
你可以隨便找一個節(jié)點寫惶傻,即便是從節(jié)點也可以,然后集群內(nèi)部的機制其障,會把你的指令分散到各個主節(jié)點上银室,
下面我在從節(jié)點(7002)寫,然后他會重定向到主節(jié)點7000 7001 7003 上面。
[root@node1 src]# ./redis-cli -h master -c -p 7002
master:7002> set k1 v1
-> Redirected to slot [12706] located at 192.168.0.66:7001
OK
192.168.0.66:7001> set k2 v2
-> Redirected to slot [449] located at 192.168.0.66:7000
OK
192.168.0.66:7000> set k3 v3
OK
192.168.0.66:7000> set k4 v4
-> Redirected to slot [8455] located at 192.168.0.67:7003
OK
192.168.0.67:7003> set k5 v5
-> Redirected to slot [12582] located at 192.168.0.66:7001
OK
192.168.0.66:7001> set k6 v6
-> Redirected to slot [325] located at 192.168.0.66:7000
OK
客戶端方面蜈敢,舉2個例子:Java的Jedis 和C++的 ACL
public static void main(String[] args) {
JedisPoolConfig poolConfig = new JedisPoolConfig();
// 最大連接數(shù)
poolConfig.setMaxTotal(1);
// 最大空閑數(shù)
poolConfig.setMaxIdle(1);
// 最大允許等待時間辜荠,如果超過這個時間還未獲取到連接,則會報JedisException異常:
// Could not get a resource from the pool
poolConfig.setMaxWaitMillis(1000);
Set<HostAndPort> nodes = new LinkedHashSet<HostAndPort>();
nodes.add(new HostAndPort("192.168.83.128", 6379));
nodes.add(new HostAndPort("192.168.83.128", 6380));
nodes.add(new HostAndPort("192.168.83.128", 6381));
nodes.add(new HostAndPort("192.168.83.128", 6382));
nodes.add(new HostAndPort("192.168.83.128", 6383));
nodes.add(new HostAndPort("192.168.83.128", 6384));
JedisCluster cluster = new JedisCluster(nodes, poolConfig);
String name = cluster.get("name");
System.out.println(name);
cluster.set("age", "18");
System.out.println(cluster.get("age"));
try {
cluster.close();
} catch (IOException e) {
e.printStackTrace();
}
}
int main(void)
{
const char* redis_addr = "127.0.0.1:6379";
int conn_timeout = 10, rw_timeout = 10, max_conns = 100;
// 定義 redis 客戶端集群管理對象
acl::redis_client_cluster cluster;
// 添加一個 redis 服務結(jié)點抓狭,可以多次調(diào)用此函數(shù)添加多個服務結(jié)點伯病,
// 因為 acl redis 模塊支持 redis 服務結(jié)點的自動發(fā)現(xiàn)及動態(tài)添加
// 功能,所以添加一個服務結(jié)點即可
cluster.set(redis_addr, max_conns);
// redis 字符串類 (STRING) 操作對象
acl::redis_string cmd_string;
// redis 鍵值類(KEY) 操作對象
acl::redis_key cmd_key;
// 給 redis 操作對象綁定 redis 客戶端集群對象
cmd_string.set_cluster(&cluster, max_conns);
cmd_key.set_cluster(&cluster, max_conns);
const char* key = "test_key";
// redis 集群命令操作的測試過程
test_redis_string(cmd_string, key);
test_redis_key(cmd_key, key);
return 0;
}
看了下例子否过,不用再糾結(jié)用哪個實例了午笛,答案是定義一個集群對象,然后把所有實例注冊進來叠纹,(jedis要手動寫季研,acl比較6,可以自動發(fā)現(xiàn)注冊)用的時候你就用這個集群的對象就好了誉察,具體用哪個實例与涡,這個就是這個集群對象內(nèi)部的事情了,有興趣的可以研究下源碼持偏。
2 現(xiàn)在一個master配置了一個slave,如果master掛了驼卖,會像哨兵模式那樣,slave反客為主變成master嗎鸿秆?那端口不一樣咋辦白眯蟆?
答:
端口這個問題其實第一問里邊就有答案了卿叽,把實例都注冊到集群對象里邊桥胞,如果某個端口不可用了,集群對象肯定會用其他的考婴,master掛了贩虾,slave會不會反客為主,看下實驗
[root@master ~]# redis-trib.rb check master:7001
>>> Performing Cluster Check (using node master:7001)
M: 4aa20bd4e759c302484c3e39dae9f875dfae1f35 master:7001
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: b0668184fd15a8bb891a0d98b7d6c2c7293b22c6 192.168.0.67:7005
slots: (0 slots) slave
replicates 4aa20bd4e759c302484c3e39dae9f875dfae1f35
M: 1783142fb99cda09ebd103fe169134871106bcdc 192.168.0.67:7003
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 92a5d77c8abd7fbf8de6d66eed92e324d1cbbf08 192.168.0.66:7002
slots: (0 slots) slave
replicates 1783142fb99cda09ebd103fe169134871106bcdc
M: aae8c65a7595586befa7e2071bd385723f3eee96 192.168.0.66:7000
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 77dd396290fb2bf1bdb63a2ae23f7bd5ab28c392 192.168.0.67:7004
slots: (0 slots) slave
replicates aae8c65a7595586befa7e2071bd385723f3eee96
Mater 7001 對應的slave 是7005沥阱,現(xiàn)在把7001干掉缎罢,看下7005會不會變成master
s順便驗證下keys
1看下70001 下的keys
[root@master src]# ./redis-cli -h master -c -p 7001
master:7001> keys *
1) "k1"
2) "a2"
3) "a3"
4) "k5"
5) "k9"
master:7001> exit
2把7001干掉
[root@master src]# lsof -i tcp:7001
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
redis-ser 101142 root 4u IPv4 6227467 0t0 TCP master:afs3-callback (LISTEN)
redis-ser 101142 root 8u IPv4 6226915 0t0 TCP master:afs3-callback->node1:48580 (ESTABLISHED)
[root@master src]# kill -9 101142
3檢查狀態(tài),發(fā)現(xiàn)報錯考杉,7005沒有變成master,變成master肯定需要時間的策精,所以等幾秒再試下
[root@master src]# redis-trib.rb check master:7001
[ERR] Sorry, can't connect to node master:7001
[root@master src]# redis-trib.rb check master:7002
[ERR] Sorry, can't connect to node 192.168.0.66:7001
*** WARNING: 192.168.0.67:7005 claims to be slave of unknown node ID 4aa20bd4e759c302484c3e39dae9f875dfae1f35.
>>> Performing Cluster Check (using node master:7002)
S: 92a5d77c8abd7fbf8de6d66eed92e324d1cbbf08 master:7002
slots: (0 slots) slave
replicates 1783142fb99cda09ebd103fe169134871106bcdc
M: 1783142fb99cda09ebd103fe169134871106bcdc 192.168.0.67:7003
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: b0668184fd15a8bb891a0d98b7d6c2c7293b22c6 192.168.0.67:7005
slots: (0 slots) slave
replicates 4aa20bd4e759c302484c3e39dae9f875dfae1f35
S: 77dd396290fb2bf1bdb63a2ae23f7bd5ab28c392 192.168.0.67:7004
slots: (0 slots) slave
replicates aae8c65a7595586befa7e2071bd385723f3eee96
M: aae8c65a7595586befa7e2071bd385723f3eee96 192.168.0.66:7000
slots:0-5460 (5461 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[ERR] Not all 16384 slots are covered by nodes.
4再試一下,發(fā)現(xiàn)7005 已結(jié)變成主節(jié)點崇棠,現(xiàn)在有三個主節(jié)點咽袜,2個從節(jié)點
[root@master src]# redis-trib.rb check master:7002
>>> Performing Cluster Check (using node master:7002)
S: 92a5d77c8abd7fbf8de6d66eed92e324d1cbbf08 master:7002
slots: (0 slots) slave
replicates 1783142fb99cda09ebd103fe169134871106bcdc
M: 1783142fb99cda09ebd103fe169134871106bcdc 192.168.0.67:7003
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: b0668184fd15a8bb891a0d98b7d6c2c7293b22c6 192.168.0.67:7005
slots:10923-16383 (5461 slots) master
0 additional replica(s)
S: 77dd396290fb2bf1bdb63a2ae23f7bd5ab28c392 192.168.0.67:7004
slots: (0 slots) slave
replicates aae8c65a7595586befa7e2071bd385723f3eee96
M: aae8c65a7595586befa7e2071bd385723f3eee96 192.168.0.66:7000
slots:0-5460 (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.
5驗證下7005的keys,和7001 一樣
[root@master src]# ./redis-cli -h node1 -c -p 7005
node1:7005> keys *
1) "k9"
2) "a2"
3) "a3"
4) "k5"
5) "k1"
6現(xiàn)在在把7001 啟動易茬,看下會不會變成從節(jié)點
[root@master src]# ./redis-server ../redis_cluster/7001/redis.conf
[root@master src]# redis-trib.rb check master:7002
>>> Performing Cluster Check (using node master:7002)
S: 92a5d77c8abd7fbf8de6d66eed92e324d1cbbf08 master:7002
slots: (0 slots) slave
replicates 1783142fb99cda09ebd103fe169134871106bcdc
M: 1783142fb99cda09ebd103fe169134871106bcdc 192.168.0.67:7003
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: b0668184fd15a8bb891a0d98b7d6c2c7293b22c6 192.168.0.67:7005
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 4aa20bd4e759c302484c3e39dae9f875dfae1f35 192.168.0.66:7001
slots: (0 slots) slave
replicates b0668184fd15a8bb891a0d98b7d6c2c7293b22c6
S: 77dd396290fb2bf1bdb63a2ae23f7bd5ab28c392 192.168.0.67:7004
slots: (0 slots) slave
replicates aae8c65a7595586befa7e2071bd385723f3eee96
M: aae8c65a7595586befa7e2071bd385723f3eee96 192.168.0.66:7000
slots:0-5460 (5461 slots) master
1 additional replica(s)
果然酬蹋,7001 現(xiàn)在變成了7005 的從節(jié)點及老。
總結(jié)一下:
集群中的主節(jié)點掛了以后,從節(jié)點會反客為主變成主節(jié)點范抓,待如果掛掉了的
主機點再次啟動之后骄恶,就會變成現(xiàn)在主節(jié)點的從節(jié)點。
2redis集群搭建
Redis支持集群最小的單位為6個實例匕垫,3個主節(jié)點僧鲁,3個從節(jié)點
集群搭建:至少要三個master
2個節(jié)點:
master 192.168.0.66
node1 192.168.0.67
(master node1 隨便起的名字,并不是說master這個機器上的就是主節(jié)點)
第一步安裝GCC
不然待會make時會報錯make cc Command not found象泵,make: *** [adlist.o] Error 127有的話就不用裝了
yum install gcc
第二步 編譯安裝redis
cd redis-3.2.8
make && make install
(報錯zmalloc.h:50:31: error: jemalloc/jemalloc.h: No such file or directory 關(guān)于分配器allocator寞秃, 如果有MALLOC 這個 環(huán)境變量, 會有用這個環(huán)境變量的 去建立Redis偶惠。
而且libc 并不是默認的 分配器春寿, 默認的是 jemalloc, 因為 jemalloc 被證明 有更少的 fragmentation problems 比libc。
但是如果你又沒有jemalloc 而只有 libc 當然 make 出錯忽孽。 所以加這么一個參數(shù)绑改。
解決辦法
make MALLOC=libc)
第三步將 redis-trib.rb 復制到 /usr/local/bin 目錄下
redis-trib.rb是redis官方推出的管理redis集群的工具,集成在redis的源碼src目錄下兄一,是基于redis提供的集群命令封裝成簡單厘线、便捷、實用的操作工具出革。redis-trib.rb是redis作者用ruby完成的
cd src
cp redis-trib.rb /usr/local/bin/
第四步創(chuàng)建redis 節(jié)點
在master(192.168.0.66)節(jié)點創(chuàng)建redis節(jié)點
1創(chuàng)建redis_cluster目錄
cd /usr/local/software/redis-3.2.8
mkdir redis_cluster
2在 redis_cluster 目錄下造壮,創(chuàng)建名為7000、7001骂束、7002的目錄耳璧,并將 redis.conf 拷貝到這三個目錄中
cd redis_cluster
mkdir 7000 7001 7002
cp /usr/local/software/redis-3.2.8/redis.conf 7000/redis.conf
cp /usr/local/software/redis-3.2.8/redis.conf 70001/redis.conf
cp /usr/local/software/redis-3.2.8/redis.conf 70002/redis.conf
3分別修改配置文件
port 7000 //端口7000,7001,7002
bind 本機ip //默認ip為127.0.0.1 需要改為其他節(jié)點機器可訪問的ip 否則創(chuàng)建集群時無法訪問對應的端口,無法創(chuàng)建集群
daemonize yes //redis后臺運行
pidfile /var/run/redis_7000.pid //pidfile文件對應7000,7001,7002
cluster-enabled yes //開啟集群 把注釋#去掉
cluster-config-file nodes_7000.conf //集群的配置 配置文件首次啟動自動生成 7000,7001,7002
cluster-node-timeout 15000 //請求超時 默認15秒展箱,可自行設(shè)置
appendonly yes //aof日志開啟 有需要就開啟楞抡,它會每次寫操作都記錄一條日志
接著在另外一臺機器node1上(192.168.0.267),的操作重復以上三步析藕,只是把目錄改為7003、7004凳厢、7005账胧,對應的配置文件也按照這個規(guī)則修改即可
第五步啟動redis
[root@master src]# ./redis-server ../redis_cluster/7000/redis.conf
[root@master src]# ./redis-server ../redis_cluster/7001/redis.conf
[root@master src]# ./redis-server ../redis_cluster/7002/redis.conf
[root@node1 src]# ./redis-server ../redis_cluster/7003/redis.conf
[root@node1 src]# ./redis-server ../redis_cluster/7004/redis.conf
[root@node1 src]# ./redis-server ../redis_cluster/7005/redis.conf
第六步檢查狀態(tài)
[root@node1 src]# ps -ef | grep redis
root 22641 1 0 15:52 ? 00:00:00 ./redis-server node1:7003 [cluster]
root 22648 1 0 15:52 ? 00:00:00 ./redis-server node1:7004 [cluster]
root 22653 1 0 15:52 ? 00:00:00 ./redis-server node1:7005 [cluster]
root 22733 20035 0 15:54 pts/0 00:00:00 grep --color=auto redis
[root@node1 src]# netstat -tlnp | grep redis
tcp 0 0 192.168.0.67:17003 0.0.0.0:* LISTEN 22641/./redis-serve
tcp 0 0 192.168.0.67:17004 0.0.0.0:* LISTEN 22648/./redis-serve
tcp 0 0 192.168.0.67:17005 0.0.0.0:* LISTEN 22653/./redis-serve
tcp 0 0 192.168.0.67:7003 0.0.0.0:* LISTEN 22641/./redis-serve
tcp 0 0 192.168.0.67:7004 0.0.0.0:* LISTEN 22648/./redis-serve
tcp 0 0 192.168.0.67:7005 0.0.0.0:* LISTEN 22653/./redis-serve
另一節(jié)點類似
至此,其實還不是集群先紫,只是6個redis 實例治泥,下一步我們來創(chuàng)建集群
第七步創(chuàng)建集群
首先要安裝ruby
yum -y install ruby ruby-devel rubygems rpm-build
gem install redis
創(chuàng)建集群:
這種寫法是錯誤的:
redis-trib.rb create --replicas 1 master:7000 master:7001 master:7002 node1:7003 node1:7004 node1:7005
會報錯:ERR Invalid node address specified: master:7000 (Redis::CommandError)
由于Redis-trib.rb 對域名或主機名支持不好,故在創(chuàng)建集群的時候要使用ip:port的方式
正確的寫法:
redis-trib.rb create --replicas 1 192.168.0.66:7000 192.168.0.66:7001 192.168.0.66:7002 192.168.0.67:7003 192.168.0.67:7004 192.168.0.67:7005
但是由于剛才配錯了,所以現(xiàn)在不能重新配了遮精,1要刪除cluster-config-file 文件居夹,重啟redis
cd /usr/local/software/redis-3.2.8/src/
[root@node1 src]# rm nodes_*
rm: remove regular file ‘nodes_7003.conf’? y
rm: remove regular file ‘nodes_7004.conf’? y
rm: remove regular file ‘nodes_7005.conf’? y
[root@node1 src]# lsof -i tcp:7003
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
redis-ser 76282 root 4u IPv4 761222 0t0 TCP node1:afs3-vlserver (LISTEN)
[root@node1 src]# lsof -i tcp:7004
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
redis-ser 76275 root 4u IPv4 761196 0t0 TCP node1:afs3-kaserver (LISTEN)
[root@node1 src]# lsof -i tcp:7005
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
redis-ser 76255 root 4u IPv4 761166 0t0 TCP node1:afs3-volser (LISTEN)
[root@node1 src]# kill -9 76282
[root@node1 src]# kill -9 76275
[root@node1 src]# kill -9 76255
[root@node1 src]# ./redis-server ../redis_cluster/7003/redis.conf
[root@node1 src]# ./redis-server ../redis_cluster/7004/redis.conf
[root@node1 src]# ./redis-server ../redis_cluster/7005/redis.conf
7000 7001 7002 同理
[root@master src]# redis-trib.rb create --replicas 1 192.168.0.66:7000 192.168.0.66:7001 192.168.0.66:7002 192.168.0.67:7003 192.168.0.67:7004 192.168.0.67:7005
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.0.66:7000
192.168.0.67:7003
192.168.0.66:7001
Adding replica 192.168.0.67:7004 to 192.168.0.66:7000
Adding replica 192.168.0.66:7002 to 192.168.0.67:7003
Adding replica 192.168.0.67:7005 to 192.168.0.66:7001
M: aae8c65a7595586befa7e2071bd385723f3eee96 192.168.0.66:7000
slots:0-5460 (5461 slots) master
M: 4aa20bd4e759c302484c3e39dae9f875dfae1f35 192.168.0.66:7001
slots:10923-16383 (5461 slots) master
S: 92a5d77c8abd7fbf8de6d66eed92e324d1cbbf08 192.168.0.66:7002
replicates 1783142fb99cda09ebd103fe169134871106bcdc
M: 1783142fb99cda09ebd103fe169134871106bcdc 192.168.0.67:7003
slots:5461-10922 (5462 slots) master
S: 77dd396290fb2bf1bdb63a2ae23f7bd5ab28c392 192.168.0.67:7004
replicates aae8c65a7595586befa7e2071bd385723f3eee96
S: b0668184fd15a8bb891a0d98b7d6c2c7293b22c6 192.168.0.67:7005
replicates 4aa20bd4e759c302484c3e39dae9f875dfae1f35
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 192.168.0.66:7000)
M: aae8c65a7595586befa7e2071bd385723f3eee96 192.168.0.66:7000
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 77dd396290fb2bf1bdb63a2ae23f7bd5ab28c392 192.168.0.67:7004
slots: (0 slots) slave
replicates aae8c65a7595586befa7e2071bd385723f3eee96
S: b0668184fd15a8bb891a0d98b7d6c2c7293b22c6 192.168.0.67:7005
slots: (0 slots) slave
replicates 4aa20bd4e759c302484c3e39dae9f875dfae1f35
S: 92a5d77c8abd7fbf8de6d66eed92e324d1cbbf08 192.168.0.66:7002
slots: (0 slots) slave
replicates 1783142fb99cda09ebd103fe169134871106bcdc
M: 1783142fb99cda09ebd103fe169134871106bcdc 192.168.0.67:7003
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: 4aa20bd4e759c302484c3e39dae9f875dfae1f35 192.168.0.66:7001
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-trib.rb check master:7001(隨便某個節(jié)點)
[root@master src]# redis-trib.rb check master:7001
>>> Performing Cluster Check (using node master:7001)
M: 4aa20bd4e759c302484c3e39dae9f875dfae1f35 master:7001
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: b0668184fd15a8bb891a0d98b7d6c2c7293b22c6 192.168.0.67:7005
slots: (0 slots) slave
replicates 4aa20bd4e759c302484c3e39dae9f875dfae1f35
M: 1783142fb99cda09ebd103fe169134871106bcdc 192.168.0.67:7003
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 92a5d77c8abd7fbf8de6d66eed92e324d1cbbf08 192.168.0.66:7002
slots: (0 slots) slave
replicates 1783142fb99cda09ebd103fe169134871106bcdc
M: aae8c65a7595586befa7e2071bd385723f3eee96 192.168.0.66:7000
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 77dd396290fb2bf1bdb63a2ae23f7bd5ab28c392 192.168.0.67:7004
slots: (0 slots) slave
replicates aae8c65a7595586befa7e2071bd385723f3eee96
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
現(xiàn)在在7001上寫败潦,驗證在7005上是否可以讀到
寫:
[root@master src]# ./redis-cli -h master -c -p 7001
master:7001> set k1 v1
讀:
[root@node1 src]# ./redis-cli -h node1 -c -p 7005
node1:7005> get k1
-> Redirected to slot [12706] located at 192.168.0.66:7001
"v1"
這里看到重定向到了7001,也驗證了第一部分所說的准脂,redis集群把數(shù)據(jù)分散在不同的節(jié)點劫扒,你可以在任意節(jié)點讀,讀不到的時候他會重定向去其他節(jié)點狸膏。
運維(遷移沟饥,擴容,縮容等)
redis-trib.rb 湾戳,前面在創(chuàng)建redis集群的時候已經(jīng)用過了贤旷,這是redis官方給出的一個
集群管理工具。它具有以下功能
1create:創(chuàng)建集群
2check:檢查集群
3info:查看集群信息
4fix:修復集群
5reshard:在線遷移slot
6rebalance:平衡集群節(jié)點slot數(shù)量
7add-node:將新節(jié)點加入集群
8del-node:從集群中刪除節(jié)點
9set-timeout:設(shè)置集群節(jié)點間心跳連接的超時時間
10call:在集群全部節(jié)點上執(zhí)行命令
11import:將外部redis數(shù)據(jù)導入集群
`