安装redis
tar zxvf redis-3.0.5.tar.gzcd redis-3.0.5make && make install
集群配置
redis 集群是一个提供在多个redis节点间共享数据的程序集redis集群不支持处理多个keys的命令,因为这需要在不同节点间移动数据,从而达不到像redis那样的性能,在高负载情况下可能导致不可预料的错误redis集群通过分区来提供一定程度的可用性,在实际环境中当某个节点宕机或者不可达的情况下继续处理命令,redis集群的优势在于: 自动分割数据到不同的节点上 整个集群的部分节点失败或不可达情况下继续处理命令
将redis-trib.rb 复制到/usr/local/bin
cd src/cp redis-trib.rb /usr/local/binmkdir -p /usr/local/cluster-testcd /usr/local/cluster-test/
新建6个节点 并将redis.conf 分别拷贝到这6个文件夹中
for i in {7001..7005};do mkdir $i && cp /etc/redis/6379.conf $i;done
修改成对应的端口号
for j in {7001..7005};do sed -i "s/7000/$j/g" /usr/local/cluster-test/$j/6379.conf;done
make sure that different nodes use different cluster configuration files.
for j in {7001..7005};do sed -i "s/nodes-6379.conf/nodes-$j.conf/g" /usr/local/cluster-test/$j/6379.conf;done
批量启动
for k in {7000..7005};do redis-server ./$k/6379.conf;done
批量关闭
ps -ef|grep redis |grep -v grep|awk '{print $2}' |xargs -i kill -9 {}
查看6个节点的启动进程情况
ps -ef|grep redis
安装ruby2.2以上
https://segmentfault.com/n/1330000014166676#articleHeader6
因为我们要新建集群, 所以这里使用create命令.
--replicas 1 参数表示为每个主节点创建一个从节点. 其他参数是实例的地址集合
redis-trib.rb create --replicas 1 \127.0.0.1:7000 \127.0.0.1:7001 \127.0.0.1:7002 \127.0.0.1:7003 \127.0.0.1:7004 \127.0.0.1:7005 \
测试集群的状态
redis-trib.rb check 127.0.0.1:7000>>> Creating clusterConnecting to node 127.0.0.1:7000: OKConnecting to node 127.0.0.1:7001: OKConnecting to node 127.0.0.1:7002: OKConnecting to node 127.0.0.1:7003: OKConnecting to node 127.0.0.1:7004: OKConnecting to node 127.0.0.1:7005: OK>>> Performing hash slots allocation on 6 nodes...Using 3 masters:127.0.0.1:7000127.0.0.1:7001127.0.0.1:7002Adding replica 127.0.0.1:7003 to 127.0.0.1:7000Adding replica 127.0.0.1:7004 to 127.0.0.1:7001Adding replica 127.0.0.1:7005 to 127.0.0.1:7002M: 1f2a4d15b18a3215f1061ea3c710a086baa328d1 127.0.0.1:7000 slots:0-5460 (5461 slots) masterM: 70b04bac6e56084a1ed5cdc5a98f63154bd409ba 127.0.0.1:7001 slots:5461-10922 (5462 slots) masterM: c3c753177d9a79eb77091c28a71d7ba0418bdaaf 127.0.0.1:7002 slots:10923-16383 (5461 slots) masterS: 403fa0e59f7cb338442cf2f5f6c2d27d231dff81 127.0.0.1:7003 replicates 1f2a4d15b18a3215f1061ea3c710a086baa328d1S: e9657dd6211f637ea1898d7cfbe57471d3aede3c 127.0.0.1:7004 replicates 70b04bac6e56084a1ed5cdc5a98f63154bd409baS: 1a6bf68a26d83d97b4c72acb417d8a0c833d8e00 127.0.0.1:7005 replicates c3c753177d9a79eb77091c28a71d7ba0418bdaafCan 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 clusterWaiting for the cluster to join...>>> Performing Cluster Check (using node 127.0.0.1:7000)M: 1f2a4d15b18a3215f1061ea3c710a086baa328d1 127.0.0.1:7000 slots:0-5460 (5461 slots) masterM: 70b04bac6e56084a1ed5cdc5a98f63154bd409ba 127.0.0.1:7001 slots:5461-10922 (5462 slots) masterM: c3c753177d9a79eb77091c28a71d7ba0418bdaaf 127.0.0.1:7002 slots:10923-16383 (5461 slots) masterM: 403fa0e59f7cb338442cf2f5f6c2d27d231dff81 127.0.0.1:7003 slots: (0 slots) master replicates 1f2a4d15b18a3215f1061ea3c710a086baa328d1M: e9657dd6211f637ea1898d7cfbe57471d3aede3c 127.0.0.1:7004 slots: (0 slots) master replicates 70b04bac6e56084a1ed5cdc5a98f63154bd409baM: 1a6bf68a26d83d97b4c72acb417d8a0c833d8e00 127.0.0.1:7005 slots: (0 slots) master replicates c3c753177d9a79eb77091c28a71d7ba0418bdaaf[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.
集群功能测试
redis cluster的特点是去中心化,每个节点都是对等的,所以,你连接哪个节点都可以获取和设置数据
redis-cli是redis默认的客户端工具,启动时加上`-c`参数,就可以连接到集群
redis-cli -c -p 7000127.0.0.1:7000> set node 7000-> Redirected to slot [14143] located at 127.0.0.1:7002OK127.0.0.1:7002> get node"7000"127.0.0.1:7002> set name lyon-> Redirected to slot [5798] located at 127.0.0.1:7001OK127.0.0.1:7001> get name"lyon"
分配key的时候,它会使用CRC16('name')%16384算法,来计算,将这个key 放到哪个节点
数据都会在7000-7002 这3个主节点来跳转存储
测试集群中的节点挂掉
redis-cli -c -p 7000127.0.0.1:7000> set age 18OK127.0.0.1:7000> get age"18"127.0.0.1:7000> set cpy cpy-> Redirected to slot [9692] located at 127.0.0.1:7001OK127.0.0.1:7001> get cpy"eichong"
kill掉redis-7000节点
redis-trib.rb check 127.0.0.1:7000Connecting to node 127.0.0.1:7000: [ERR] Sorry, can't connect to node 127.0.0.1:7000
7003节点接任
redis-trib.rb check 127.0.0.1:7001Connecting to node 127.0.0.1:7001: OKConnecting to node 127.0.0.1:7002: OKConnecting to node 127.0.0.1:7003: OKConnecting to node 127.0.0.1:7004: OKConnecting to node 127.0.0.1:7005: OK>>> Performing Cluster Check (using node 127.0.0.1:7001)M: 70b04bac6e56084a1ed5cdc5a98f63154bd409ba 127.0.0.1:7001 slots:5461-10922 (5462 slots) master 1 additional replica(s)M: c3c753177d9a79eb77091c28a71d7ba0418bdaaf 127.0.0.1:7002 slots:10923-16383 (5461 slots) master 1 additional replica(s)M: 403fa0e59f7cb338442cf2f5f6c2d27d231dff81 127.0.0.1:7003 slots:0-5460 (5461 slots) master 0 additional replica(s)S: e9657dd6211f637ea1898d7cfbe57471d3aede3c 127.0.0.1:7004 slots: (0 slots) slave replicates 70b04bac6e56084a1ed5cdc5a98f63154bd409baS: 1a6bf68a26d83d97b4c72acb417d8a0c833d8e00 127.0.0.1:7005 slots: (0 slots) slave replicates c3c753177d9a79eb77091c28a71d7ba0418bdaaf[OK] All nodes agree about slots configuration.>>> Check for open slots...[WARNING] Node 127.0.0.1:7002 has slots in importing state (5798,10576).[WARNING] The following slots are open: 5798,10576>>> Check slots coverage...[OK] All 16384 slots covered.
测试是否可以获取到数据
redis-cli -c -p 7001127.0.0.1:7001> get age-> Redirected to slot [741] located at 127.0.0.1:7003"18"127.0.0.1:7003> get name-> Redirected to slot [5798] located at 127.0.0.1:7001"www"127.0.0.1:7001> get cpy"cpy"
重启7000节点
[root@huawei-test ~ ]$ redis-trib.rb check 127.0.0.1:7000Connecting to node 127.0.0.1:7000: OKConnecting to node 127.0.0.1:7004: OKConnecting to node 127.0.0.1:7005: OKConnecting to node 127.0.0.1:7002: OKConnecting to node 127.0.0.1:7001: OKConnecting to node 127.0.0.1:7003: OK>>> Performing Cluster Check (using node 127.0.0.1:7000)S: 1f2a4d15b18a3215f1061ea3c710a086baa328d1 127.0.0.1:7000 slots: (0 slots) slave replicates 403fa0e59f7cb338442cf2f5f6c2d27d231dff81S: e9657dd6211f637ea1898d7cfbe57471d3aede3c 127.0.0.1:7004 slots: (0 slots) slave replicates 70b04bac6e56084a1ed5cdc5a98f63154bd409baS: 1a6bf68a26d83d97b4c72acb417d8a0c833d8e00 127.0.0.1:7005 slots: (0 slots) slave replicates c3c753177d9a79eb77091c28a71d7ba0418bdaafM: c3c753177d9a79eb77091c28a71d7ba0418bdaaf 127.0.0.1:7002 slots:10923-16383 (5461 slots) master 1 additional replica(s)M: 70b04bac6e56084a1ed5cdc5a98f63154bd409ba 127.0.0.1:7001 slots:5461-10922 (5462 slots) master 1 additional replica(s)M: 403fa0e59f7cb338442cf2f5f6c2d27d231dff81 127.0.0.1:7003 slots:0-5460 (5461 slots) master 1 additional replica(s)[OK] All nodes agree about slots configuration.>>> Check for open slots...[WARNING] Node 127.0.0.1:7002 has slots in importing state (5798,10576).[WARNING] The following slots are open: 5798,10576>>> Check slots coverage...[OK] All 16384 slots covered
可以看到节点状态为激活态,但是作为7003的从节点
集群中新加入节点
分2种情况,1是作为主节点,2是作为一个节点的从节点
1 新建一个 7006 节点 作为一个新的主节点加入cd /usr/local/cluster-test/mkdir 7006cp 7005/6379.conf 7006/6379.confsed -i "s/7005/7006/g" /usr/local/cluster-test/7006/6379.conf重启集群节点for k in {7000..7006};do redis-server ./$k/6379.conf;done添加7006节点redis-trib.rb add-node 127.0.0.1:7006 127.0.0.1:7000检查7006节点redis-trib.rb check 127.0.0.1:7006Connecting to node 127.0.0.1:7006: OKConnecting to node 127.0.0.1:7004: OKConnecting to node 127.0.0.1:7002: OKConnecting to node 127.0.0.1:7000: OKConnecting to node 127.0.0.1:7001: OKConnecting to node 127.0.0.1:7005: OKConnecting to node 127.0.0.1:7003: OK>>> Performing Cluster Check (using node 127.0.0.1:7006)S: 173f43306bcfac4319a9556e2d49f246880cbbf4 127.0.0.1:7006 slots: (0 slots) slave replicates 70b04bac6e56084a1ed5cdc5a98f63154bd409baS: e9657dd6211f637ea1898d7cfbe57471d3aede3c 127.0.0.1:7004 slots: (0 slots) slave replicates 70b04bac6e56084a1ed5cdc5a98f63154bd409baM: c3c753177d9a79eb77091c28a71d7ba0418bdaaf 127.0.0.1:7002 slots:10923-16383 (5461 slots) master 1 additional replica(s)S: 1f2a4d15b18a3215f1061ea3c710a086baa328d1 127.0.0.1:7000 slots: (0 slots) slave replicates 403fa0e59f7cb338442cf2f5f6c2d27d231dff81M: 70b04bac6e56084a1ed5cdc5a98f63154bd409ba 127.0.0.1:7001 slots:5461-10922 (5462 slots) master 2 additional replica(s)S: 1a6bf68a26d83d97b4c72acb417d8a0c833d8e00 127.0.0.1:7005 slots: (0 slots) slave replicates c3c753177d9a79eb77091c28a71d7ba0418bdaafM: 403fa0e59f7cb338442cf2f5f6c2d27d231dff81 127.0.0.1:7003 slots:0-5460 (5461 slots) master 1 additional replica(s)[OK] All nodes agree about slots configuration.>>> Check for open slots...[WARNING] Node 127.0.0.1:7002 has slots in importing state (5798,10576).[WARNING] The following slots are open: 5798,10576>>> Check slots coverage...[OK] All 16384 slots covered.redis-cli -c -p 7006127.0.0.1:7006> cluster nodese9657dd6211f637ea1898d7cfbe57471d3aede3c 127.0.0.1:7004 slave 70b04bac6e56084a1ed5cdc5a98f63154bd409ba 0 1537170999876 2 connectedc3c753177d9a79eb77091c28a71d7ba0418bdaaf 127.0.0.1:7002 master - 0 1537170999366 3 connected 10923-163831f2a4d15b18a3215f1061ea3c710a086baa328d1 127.0.0.1:7000 slave 403fa0e59f7cb338442cf2f5f6c2d27d231dff81 0 1537170998346 7 connected70b04bac6e56084a1ed5cdc5a98f63154bd409ba 127.0.0.1:7001 master - 0 1537170997836 2 connected 5461-109221a6bf68a26d83d97b4c72acb417d8a0c833d8e00 127.0.0.1:7005 slave c3c753177d9a79eb77091c28a71d7ba0418bdaaf 0 1537170998855 3 connected173f43306bcfac4319a9556e2d49f246880cbbf4 127.0.0.1:7006 myself,slave 70b04bac6e56084a1ed5cdc5a98f63154bd409ba 0 0 0 connected403fa0e59f7cb338442cf2f5f6c2d27d231dff81 127.0.0.1:7003 master - 0 1537170999884 7 connected 0-5460可以看到7006成为了从节点
参考文档