1.概要
- 伸缩原理
- 扩容原理
- 缩容原理
2.伸缩原理
3.扩容集群
- 加入节点
- 加入集群
- 迁移槽和数据
-
槽迁移计划
-
迁移数据
-
添加从节点
-
4.集群扩容-操作
[root@master redis-cluster-config]# sed 's/7000/7006/g' redis-7000.conf > redis-7006.conf
[root@master redis-cluster-config]# sed 's/7000/7007/g' redis-7000.conf > redis-7007.conf
[root@master redis-cluster-config]# ll
总用量 36
-rw-r--r--. 1 root root 0 9月 8 22:16 7000.log
-rw-r--r--. 1 root root 0 9月 8 22:16 7001.log
-rw-r--r--. 1 root root 0 9月 8 22:16 7002.log
-rw-r--r--. 1 root root 0 9月 8 22:16 7003.log
-rw-r--r--. 1 root root 0 9月 8 22:16 7004.log
-rw-r--r--. 1 root root 0 9月 8 22:16 7005.log
-rw-r--r--. 1 root root 153 9月 8 22:34 addslots.sh
-rw-r--r--. 1 root root 225 9月 8 22:14 redis-7000.conf
-rw-r--r--. 1 root root 225 9月 8 22:15 redis-7001.conf
-rw-r--r--. 1 root root 225 9月 8 22:15 redis-7002.conf
-rw-r--r--. 1 root root 225 9月 8 22:15 redis-7003.conf
-rw-r--r--. 1 root root 225 9月 8 22:15 redis-7004.conf
-rw-r--r--. 1 root root 225 9月 8 22:15 redis-7005.conf
-rw-r--r--. 1 root root 225 9月 9 21:31 redis-7006.conf
-rw-r--r--. 1 root root 225 9月 9 21:31 redis-7007.conf
[root@master redis-cluster-config]# cat redis-7006.conf
port 7006
daemonize yes
pidfile /var/run/redis-7006.pid
dbfilename "dump-7006.rdb"
logfile "7006.log"
dir "/opt/soft/redis/redis/data/"
cluster-enabled yes
cluster-config-file nodes-7006.conf
cluster-require-full-coverage no
[root@master redis-cluster-config]# redis-server redis-7006.conf
[root@master redis-cluster-config]# redis-server redis-7007.conf
[root@master redis-cluster-config]# redis-cli -p 7000 cluster meet 127.0.0.1 7006
OK
[root@master redis-cluster-config]# redis-cli -p 7000 cluster meet 127.0.0.1 7007
OK
[root@master redis-cluster-config]# redis-cli -p 7007 cluster replicate 755f7e38f4cb85d1937aac01993090e4dfa10cb7
OK
[root@master redis-cluster-config]# redis-cli -p 7000 cluster nodes
755f7e38f4cb85d1937aac01993090e4dfa10cb7 127.0.0.1:7006@17006 master - 0 1568036081012 6 connected
0fee682eb0247d98bcffe694fe745af194f708dd 127.0.0.1:7005@17005 slave 80602baaf52498f27be91a8b9169dc78de6e6e07 0 1568036079000 5 connected
df740a3f64985bb4645c1f9fff276d29ad1bcb25 127.0.0.1:7000@17000 myself,master - 0 1568036080000 0 connected 0-5461
2e6827954a8dd1b7a1455e9aec057cb89fd56fa4 127.0.0.1:7003@17003 slave df740a3f64985bb4645c1f9fff276d29ad1bcb25 0 1568036081000 3 connected
80602baaf52498f27be91a8b9169dc78de6e6e07 127.0.0.1:7002@17002 master - 0 1568036079005 2 connected 10923-16383
7a233b8a7cfd6369302d4d1f073b342c8088a388 127.0.0.1:7007@17007 slave 755f7e38f4cb85d1937aac01993090e4dfa10cb7 0 1568036083021 7 connected
c09e9379e37ee282219a07399bfb0dcbb933159a 127.0.0.1:7001@17001 master - 0 1568036082017 1 connected 5462-10922
97969f534d03b6c7c346df035e35b676c79e2d51 127.0.0.1:7004@17004 slave c09e9379e37ee282219a07399bfb0dcbb933159a 0 1568036082000 4 connected
[root@master ~]# redis-cli --cluster reshard 127.0.0.1:7000 --cluster-from df740a3f64985bb4645c1f9fff276d29ad1bcb25,80602baaf52498f27be91a8b9169dc78de6e6e07,c09e9379e37ee282219a07399bfb0dcbb933159a --cluster-to 755f7e38f4cb85d1937aac01993090e4dfa10cb7 --cluster-slots 4096
[root@master ~]# redis-cli -p 7000 cluster nodes | grep master
755f7e38f4cb85d1937aac01993090e4dfa10cb7 127.0.0.1:7006@17006 master - 0 1568039684000 8 connected 0-1365 5462-6826 10923-12287
df740a3f64985bb4645c1f9fff276d29ad1bcb25 127.0.0.1:7000@17000 myself,master - 0 1568039679000 0 connected 1366-5461
80602baaf52498f27be91a8b9169dc78de6e6e07 127.0.0.1:7002@17002 master - 0 1568039683000 2 connected 12288-16383
c09e9379e37ee282219a07399bfb0dcbb933159a 127.0.0.1:7001@17001 master - 0 1568039683000 1 connected 6827-10922
4.收缩集群
[root@master ~]# redis-cli --cluster reshard 127.0.0.1:7000 --cluster-from df740a3f64985bb4645c1f9fff276d29ad1bcb25,80602baaf52498f27be91a8b9169dc78de6e6e07,c09e9379e37ee282219a07399bfb0dcbb933159a --cluster-to 755f7e38f4cb85d1937aac01993090e4dfa10cb7 --cluster-slots 4096
[root@master ~]# redis-cli --cluster del-node 127.0.0.1:7000 755f7e38f4cb85d1937aac01993090e4dfa10cb7
5.客户端路由
-
moved重定向
[root@master redis-cluster-config]# redis-cli -p 7000 127.0.0.1:7000> set hello world (error) MOVED 866 127.0.0.1:7006 127.0.0.1:7000> cluster keyslot hello (integer) 866 127.0.0.1:7000> exit [root@master redis-cluster-config]# redis-cli -c -p 7000 127.0.0.1:7000> set hello world -> Redirected to slot [866] located at 127.0.0.1:7006 OK 127.0.0.1:7006>
-
ask重定向
-
smart客户端
6.JedisCluster
-
JedisCluster基本使用
-
整合spring
package com.ctgu.juc_project; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import redis.clients.jedis.HostAndPort; import redis.clients.jedis.JedisCluster; import redis.clients.jedis.JedisPoolConfig; import java.util.HashSet; import java.util.List; import java.util.Set; @Component public class JedisClusterFactory { private JedisCluster jedisCluster; private List
hostPortList; private int timeout; private Logger logger = LoggerFactory.getLogger(JedisClusterFactory.class); public void init(){ JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); Set nodeSet = new HashSet<>(); for (String hostPort : hostPortList) { String[] arr = hostPort.split(":"); if (arr.length != 2){ continue; } nodeSet.add(new HostAndPort(arr[0], Integer.parseInt(arr[1]))); } try { jedisCluster = new JedisCluster(nodeSet, timeout, jedisPoolConfig); } catch (Exception e) { logger.error(e.getMessage(), e); } } public void destroy(){ if (jedisCluster != null){ try { jedisCluster.close(); } catch (Exception e) { logger.error(e.getMessage(), e); } } } public JedisCluster getJedisCluster() { return jedisCluster; } public void setHostPortList(List hostPortList) { this.hostPortList = hostPortList; } public void setTimeout(int timeout) { this.timeout = timeout; } } package com.ctgu.juc_project.config; import com.ctgu.juc_project.JedisClusterFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.ArrayList; import java.util.List; @Configuration public class RedisClusterConfig { private List
hostPortList = new ArrayList (){{ add("127.0.0.1:7000"); add("127.0.0.1:7001"); add("127.0.0.1:7002"); add("127.0.0.1:7003"); add("127.0.0.1:7004"); add("127.0.0.1:7005"); }}; @Bean(initMethod = "init", destroyMethod = "destroy") public JedisClusterFactory jedisClusterFactory(){ JedisClusterFactory jedisClusterFactory = new JedisClusterFactory(); jedisClusterFactory.setHostPortList(hostPortList); jedisClusterFactory.setTimeout(1000); return jedisClusterFactory; } } -
多节点命令实现
-
批量命令实现
7.故障转移
-
故障发现
-
故障恢复
-
故障转移演练
8.Redis Cluster开发运维常见问题
-
集群完整性
-
带宽消耗
-
Pub/Sub广播
-
数据倾斜
-
读写分离
-
数据迁移
-
集群vs单机