为什么要redis集群
参考: https://blog.youkuaiyun.com/shenjianxz/article/details/59775212
记录redis集群安装步骤
其实很简单
我这里在两台机子上 分别装了两个redis redis安装简单 下载 解压即可
这里不用说了 参考 https://www.cnblogs.com/linjiqin/p/7451353.html
这里只想说下遇到的坑
创建集群就一个命令即可 这里说至少要3个主节点 3主3从
[root@localhost src]# ./redis-trib.rb create --replicas 1 192.168.56.102:7000 192.168.56.102:7001
>>> Creating cluster
*** ERROR: Invalid configuration for cluster creation.
*** Redis Cluster requires at least 3 master nodes.
*** This is not possible with 2 nodes and 1 replicas per node.
*** At least 6 nodes are required.
[2]+ 完成 ./redis-server ../../redis_cluster/7001/redis.conf
其中报ERROR:Error installing redis:redis requires Ruby version >= 2.2.2异常
这个可以参考 https://blog.youkuaiyun.com/qq_37595946/article/details/77800147 搞定
成功如下
[root@localhost src]# ./redis-trib.rb create --replicas 1 192.168.56.102:7000 192.168.56.102:7001 192.168.56.102:7002 192.168.56.103:7000 192.168.56.103:7001 192.168.56.103:7002
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.56.102:7000
192.168.56.103:7000
192.168.56.102:7001
Adding replica 192.168.56.103:7001 to 192.168.56.102:7000
Adding replica 192.168.56.102:7002 to 192.168.56.103:7000
Adding replica 192.168.56.103:7002 to 192.168.56.102:7001
M: cc8b991c85f2982bf9ad99e22494a4fd6695edd1 192.168.56.102:7000
slots:0-5460 (5461 slots) master
M: ccea4ee6dc0e5739c099c131292b0d007f6d3c82 192.168.56.102:7001
slots:10923-16383 (5461 slots) master
S: cb1ad176e100f80926f67df1e44e16bd0b73a497 192.168.56.102:7002
replicates e14229d0227bc32aa20db283af4394f65284d70a
M: e14229d0227bc32aa20db283af4394f65284d70a 192.168.56.103:7000
slots:5461-10922 (5462 slots) master
S: 583ce7b5449aae2878a0b0028e99072a9bb116b9 192.168.56.103:7001
replicates cc8b991c85f2982bf9ad99e22494a4fd6695edd1
S: c14c17037d1e3f9a69ba8e98a0375380ab88ea1f 192.168.56.103:7002
replicates ccea4ee6dc0e5739c099c131292b0d007f6d3c82
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.56.102:7000)
M: cc8b991c85f2982bf9ad99e22494a4fd6695edd1 192.168.56.102:7000
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: ccea4ee6dc0e5739c099c131292b0d007f6d3c82 192.168.56.102:7001
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: c14c17037d1e3f9a69ba8e98a0375380ab88ea1f 192.168.56.103:7002
slots: (0 slots) slave
replicates ccea4ee6dc0e5739c099c131292b0d007f6d3c82
S: 583ce7b5449aae2878a0b0028e99072a9bb116b9 192.168.56.103:7001
slots: (0 slots) slave
replicates cc8b991c85f2982bf9ad99e22494a4fd6695edd1
S: cb1ad176e100f80926f67df1e44e16bd0b73a497 192.168.56.102:7002
slots: (0 slots) slave
replicates e14229d0227bc32aa20db283af4394f65284d70a
M: e14229d0227bc32aa20db283af4394f65284d70a 192.168.56.103:7000
slots:5461-10922 (5462 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.
看到这个就成功了
这里说下 通过client添加数据,如果key正好落在client对应的节点,就没有转发消息。否则,你会看到有一个转发消息。
[root@localhost src]# redis-cli -c -h 192.168.56.103 -p 7001
192.168.56.103:7001> set kkk kkkkkk
-> Redirected to slot [8583] located at 192.168.56.103:7000
OK
192.168.56.103:7000>
由于103:7001是slave 会转到它对应的master上去。
整个集群中 key 具有唯一性 比如 在一个master上 set k kkk 在另外一个master上 set k kkkkk 那么 这时会转到刚才第一个master那里去。
master down掉了后 slave自动切为master
记录下,仅此而已。