Redis cluster集群配置的步骤介绍

作者:袖梨 2022-06-29

redis cluster使用
redis集群方案
1,客户端分片。如:取余等
2, 代理(proxy)分片。由proxy绝对数据存取,如:twemproxy。
3,redis cluster。无中心模式,
4, codis(豌豆荚开发的解决方案)
redis cluster参考文档:http://redisdoc.com/topic/cluster-tutorial.html
通常情况下,redis集群最少需要6台,三主三从

[root@yum-down opt]# mkdir `seq 7001 7008`
[root@yum-down opt]# ls
7001  7002  7003  7004  7005  7006  7007  7008  redis  rh
[root@yum-down opt]# sed 's/6379/7001/' 6379.conf >> 7001/redis.conf
[root@yum-down opt]# sed 's/6379/7002/' 6379.conf >> 7002/redis.conf
[root@yum-down opt]# sed 's/6379/7003/' 6379.conf >> 7003/redis.conf
[root@yum-down opt]# sed 's/6379/7004/' 6379.conf >> 7004/redis.conf
[root@yum-down opt]# sed 's/6379/7005/' 6379.conf >> 7005/redis.conf
[root@yum-down opt]# sed 's/6379/7006/' 6379.conf >> 7006/redis.conf
[root@yum-down opt]# sed 's/6379/7007/' 6379.conf >> 7007/redis.conf
[root@yum-down opt]# sed 's/6379/7008/' 6379.conf >> 7008/redis.conf
[root@yum-down 7008]# for i in `seq 7001 7008`;do cd /opt/$i && /usr/local/redis/bin/redis-server redis.conf;done
[root@yum-down 7008]# netstat -lntp|grep :700*
tcp        0      0 0.0.0.0:7001                0.0.0.0:*                   LISTEN      2701/redis-server *
tcp        0      0 0.0.0.0:7002                0.0.0.0:*                   LISTEN      2740/redis-server *
tcp        0      0 0.0.0.0:7003                0.0.0.0:*                   LISTEN      2742/redis-server *
tcp        0      0 0.0.0.0:7004                0.0.0.0:*                   LISTEN      2746/redis-server *
tcp        0      0 0.0.0.0:7005                0.0.0.0:*                   LISTEN      2750/redis-server *
tcp        0      0 0.0.0.0:7006                0.0.0.0:*                   LISTEN      2754/redis-server *
tcp        0      0 0.0.0.0:7007                0.0.0.0:*                   LISTEN      2760/redis-server *
tcp        0      0 0.0.0.0:7008                0.0.0.0:*                   LISTEN      2762/redis-server *
tcp        0      0 :::7001                     :::*                        LISTEN      2701/redis-server *
tcp        0      0 :::7002                     :::*                        LISTEN      2740/redis-server *
tcp        0      0 :::7003                     :::*                        LISTEN      2742/redis-server *
tcp        0      0 :::7004                     :::*                        LISTEN      2746/redis-server *
tcp        0      0 :::7005                     :::*                        LISTEN      2750/redis-server *
tcp        0      0 :::7006                     :::*                        LISTEN      2754/redis-server *
tcp        0      0 :::7007                     :::*                        LISTEN      2760/redis-server *
tcp        0      0 :::7008                     :::*                        LISTEN      2762/redis-server *
[root@yum-down 7008]#
安装ruby工具

[root@yum-down ~]# yum install ruby rubygems
[root@yum-down ~]# gem install redis
Successfully installed redis-3.2.2
1 gem installed
Installing ri documentation for redis-3.2.2...
Installing RDoc documentation for redis-3.2.2...
[root@yum-down ~]#
从源安装目录复制一个redis-trib脚本

[root@yum-down src]# cp /usr/local/redis-3.0.7/src/redis-trib.rb /usr/local/bin/redis-trib
集群创建:主-从,前面是主后面是从

[root@yum-down src]# redis-trib create --replicas 1 10.10.0.250:7001 10.10.0.250:7002 10.10.0.250:7003 10.10.0.250:7004 10.10.0.250:7005 10.10.0.250:7006
创建主:

>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
10.10.0.250:7001
10.10.0.250:7002
10.10.0.250:7003
是否确定?

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 10.10.0.250:7001)
M: efff09f4a9fe116bfda3c4d99076b535d8262dde 10.10.0.250:7001 分配0-5460
   slots:0-5460 (5461 slots) master
M: e8bb5fcbccec005aa4c3fc4d1561b54531c586e9 10.10.0.250:7002 5461-10922
   slots:5461-10922 (5462 slots) master
M: f70394b2c10d5beab0c9b91f99be9a714cdd948f 10.10.0.250:7003
   slots:10923-16383 (5461 slots) master
M: 5ca874e2e71914cf85ca85063daa56bcec4efda4 10.10.0.250:7004
   slots: (0 slots) master
   replicates efff09f4a9fe116bfda3c4d99076b535d8262dde
M: f67f8a20942831cd0454a3d54d57fb1fe75d0904 10.10.0.250:7005
   slots: (0 slots) master
   replicates e8bb5fcbccec005aa4c3fc4d1561b54531c586e9
M: 0d8c3ef48a4ae8a493699551ab629775180219c0 10.10.0.250:7006
   slots: (0 slots) master
   replicates f70394b2c10d5beab0c9b91f99be9a714cdd948f
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered. 总共被分配的个数
redis集群创建.png
查看:
7001的从是7004

[root@yum-down ~]# redis-cli -c -h 10.10.0.250 -p 7001
10.10.0.250:7001> info
# Replication
role:master
connected_slaves:1
slave0:ip=10.10.0.250,port=7004,state=online,offset=743,lag=1
master_repl_offset:743
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:742

10.10.0.250:7001>
当set一个后便会转到其他redis(根据算法Redirected)

10.10.0.250:7001> set key1 test
-> Redirected to slot [9189] located at 10.10.0.250:7002
OK
10.10.0.250:7002>
查看状态:

10.10.0.250:7002> cluster nodes
5ca874e2e71914cf85ca85063daa56bcec4efda4 10.10.0.250:7004 slave efff09f4a9fe116bfda3c4d99076b535d8262dde 0 1456659010020 4 connected
f67f8a20942831cd0454a3d54d57fb1fe75d0904 10.10.0.250:7005 slave e8bb5fcbccec005aa4c3fc4d1561b54531c586e9 0 1456659011029 5 connected
f70394b2c10d5beab0c9b91f99be9a714cdd948f 10.10.0.250:7003 master - 0 1456659011030 3 connected 10923-16383
0d8c3ef48a4ae8a493699551ab629775180219c0 10.10.0.250:7006 slave f70394b2c10d5beab0c9b91f99be9a714cdd948f 0 1456659009515 6 connected
efff09f4a9fe116bfda3c4d99076b535d8262dde 10.10.0.250:7001 master - 0 1456659010526 1 connected 0-5460
e8bb5fcbccec005aa4c3fc4d1561b54531c586e9 10.10.0.250:7002 myself,master - 0 0 2 connected 5461-10922
10.10.0.250:7002> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:2
cluster_stats_messages_sent:3238
cluster_stats_messages_received:3238
10.10.0.250:7002>
添加新节点:10.10.0.250:7007,7004为从节点,新增节点为主

[root@yum-down ~]# redis-trib add-node 10.10.0.250:7007 10.10.0.250:7004
查看

10.10.0.250:7002> cluster nodes
5ca874e2e71914cf85ca85063daa56bcec4efda4 10.10.0.250:7004 slave efff09f4a9fe116bfda3c4d99076b535d8262dde 0 1456659211846 4 connected
f67f8a20942831cd0454a3d54d57fb1fe75d0904 10.10.0.250:7005 slave e8bb5fcbccec005aa4c3fc4d1561b54531c586e9 0 1456659210839 5 connected
29610d96ef7c5d001e22c1bea370ba6f39f62068 10.10.0.250:7007 master - 0 1456659210131 0 connected
f70394b2c10d5beab0c9b91f99be9a714cdd948f 10.10.0.250:7003 master - 0 1456659210838 3 connected 10923-16383
0d8c3ef48a4ae8a493699551ab629775180219c0 10.10.0.250:7006 slave f70394b2c10d5beab0c9b91f99be9a714cdd948f 0 1456659211846 6 connected
efff09f4a9fe116bfda3c4d99076b535d8262dde 10.10.0.250:7001 master - 0 1456659210333 1 connected 0-5460
e8bb5fcbccec005aa4c3fc4d1561b54531c586e9 10.10.0.250:7002 myself,master - 0 0 2 connected 5461-10922
添加完成7007后,重新分片

[root@yum-down ~]# redis-trib reshard 10.10.0.250:7007
How many slots do you want to move (from 1 to 16384)? 1000  分配大小槽位
What is the receiving node ID? 29610d96ef7c5d001e22c1bea370ba6f39f62068 7007ID号
Source node #1:all all即可
Do you want to proceed with the proposed reshard plan (yes/no)? yes
在添加7008作为7007的从节点:
首先添加7008

[root@yum-down ~]# redis-trib add-node 10.10.0.250:7008 10.10.0.250:7004
而后连接在7008中添加7007的cluster replicate id即可

[root@yum-down ~]# redis-cli -c -h 10.10.0.250 -p 7008
10.10.0.250:7008> cluster replicate 29610d96ef7c5d001e22c1bea370ba6f39f62068
OK
10.10.0.250:7008>
在查看:

0.10.0.250:7008> cluster nodes
29610d96ef7c5d001e22c1bea370ba6f39f62068 10.10.0.250:7007 master - 0 1456659930448 7 connected 0-665 5461-6128 10923-11588
613063078b7b128c269996369fc2838736520a2c 10.10.0.250:7008 myself,slave 29610d96ef7c5d001e22c1bea370ba6f39f62068 0 0 0 connected
10.10.0.250:7008>
在线添加从的时候,需要master bgsava

相关文章

精彩推荐