Redis的基础介绍与安装使用步骤:https://www.jianshu.com/p/2a23257af57b
Redis的基础数据结构与使用:https://www.jianshu.com/p/c95c8450c5b6
Redis核心原理:https://www.jianshu.com/p/4e6b7809e10a
Redis 5 之后版本的高可用集群搭建:https://www.jianshu.com/p/8045b92fafb2
Redis 5 版本的高可用集群的水平扩展:https://www.jianshu.com/p/6355d0827aea
Redis 5 集群选举原理分析:https://www.jianshu.com/p/e6894713a6d5
Redis 5 通信协议解析以及手写一个Jedis客户端:https://www.jianshu.com/p/575544f68615
在redis3.0以前的版本要实现集群一般是借助哨兵sentinel工具来监控master节点的状态,如果master节点异常,则会做主从切换,将某一台slave作为master,哨兵的配置略微复杂,并且性能和高可用性等各方面表现一般,特别是在主从切换的瞬间存在访问瞬断的情况,而且哨兵模式只有一个主节点对外提供服务,没法支持很高的并发,且单个主节点内存也不宜设置得过大,否则会导致持久化文件过大,影响数据恢复或主从同步的效率。
redis集群是一个由多个主从节点群组成的分布式服务器群,它具有复制、高可用和分片特性。Redis集群不需要sentinel哨兵也能完成节点移除和故障转移的功能。需要将每个节点设置成集群模式,这种集群模式没有中心节点,可水平扩展,据官方文档称可以线性扩展到上万个节点(官方推荐不超过1000个节点)。redis集群的性能和高可用性均优于之前版本的哨兵模式,且集群配置非常简单。
参考之前博客:Redis的基础介绍与安装使用步骤:https://www.jianshu.com/p/2a23257af57b
(注意要使用后台启动,所以修改redis.conf里的daemonize改为yes)
redis集群需要至少要三个master节点,我们这里搭建三个master节点,并且给每个master再搭建一个slave节点,总共6个redis节点,这里用一台机器(可以多台机器部署,修改一下ip地址就可以了)部署6个redis实例,三主三从,搭建集群的步骤如下:
1)daemonize yes
2)port 8001(分别对每个机器的端口号进行设置)
3)dir /usr/local/redis-cluster/8001/(指定数据文件存放位置,必须要指定不同的目录位置,不然会丢失数据)
4)cluster-enabled yes(启动集群模式)
5)cluster-config-file nodes-8001.conf(集群节点信息文件,这里800x最好和port对应上)
6)cluster-node-timeout 5000
bind 127.0.0.1(去掉bind绑定访问ip信息)
protected-mode no (关闭保护模式)
9)appendonly yes
10)requirepass xxx (设置redis访问密码)
11)masterauth xxx (设置集群节点间访问密码,跟上面一致)
代表为每个创建的主服务器节点创建一个从服务器节点
1)连接任意一个客户端即可:
./redis-cli -c -a xxx -h 192.168.5.100 -p 8001提示:-a访问服务端密码,-c表示集群模式,指定ip地址和端口号
例如:
/usr/local/redis-5.0.2/src/redis-cli -a xxx -c -h 192.168.5.100 -p 8001注意这里进入到8002了,redirected。
2)进行验证: cluster info(查看集群信息)、cluster nodes(查看节点列表)
3)进行数据操作验证
4)关闭集群则需要逐个进行关闭,使用命令:
/usr/local/redis/src/redis-cli -a xxx -c -h 192.168.0.60 -p 8001 shutdown
作者:挂机的啊洋zzZ
链接:https://www.jianshu.com/p/8045b92fafb2
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
IP | master节点 | salve节点 |
---|---|---|
192.168.0.166 | redis-166:7001 | redis-166:7002 |
192.168.0.167 | redis-167:7001 | redis-167:7002 |
192.168.0.168 | redis-168:7001 | redis-168:7002 |
[root@localhost tools]# vim /etc/hosts192.168.0.166 redis-166192.168.0.167 redis-167192.168.0.168 redis-168
[root@localhost tools]# yum install -y gcc
[root@localhost tools]# tar -zxvf redis-5.0.5.tar.gz
[root@localhost tools]# cd redis-5.0.5/[root@localhost redis-5.0.5]# make && make PREFIX=/usr/local/redis install
[root@localhost redis-5.0.5]# echo $?0
[root@localhost tools]# vim /etc/profile###### redis env ######export REDIS_HOME=/usr/local/redisexport PATH=$PATH:$REDIS_HOME/bin
mkdir -p /usr/local/redis/redis_cluster/7001/conf/mkdir -p /usr/local/redis/redis_cluster/7001/logs/mkdir -p /usr/local/redis/redis_cluster/7001/data/mkdir -p /usr/local/redis/redis_cluster/7002/conf/mkdir -p /usr/local/redis/redis_cluster/7002/logs/mkdir -p /usr/local/redis/redis_cluster/7002/data/
vim /usr/local/redis/redis_cluster/7001/conf/redis.conf# 绑定服务器域名或IP地址bind redis-166# 设置端口,区分集群中Redis的实例port 7001# 后台运行daemonize yes# pid进程文件名,以端口号命名pidfile /var/run/redis-7001.pid# 日志文件名称,以端口号为目录来区分logfile /usr/local/redis/redis_cluster/7001/logs/redis.log# 数据文件存放地址,以端口号为目录名来区分dir /usr/local/redis/redis_cluster/7001/data# 启用集群cluster-enabled yes# 配置每个节点的配置文件,同样以端口号为名称cluster-config-file nodes_7001.conf# 配置集群节点的超时时间cluster-node-timeout 15000# 启动AOF增量持久化策略appendonly yes# 发生改变,则记录日志appendfsync always
vim /usr/local/redis/redis_cluster/7002/conf/redis.conf# 绑定服务器域名或IP地址bind redis-166# 设置端口,区分集群中Redis的实例port 7002# 后台运行daemonize yes# pid进程文件名,以端口号命名pidfile /var/run/redis-7002.pid# 日志文件名称,以端口号为目录来区分logfile /usr/local/redis/redis_cluster/7002/logs/redis.log# 数据文件存放地址,以端口号为目录名来区分dir /usr/local/redis/redis_cluster/7002/data# 启用集群cluster-enabled yes# 配置每个节点的配置文件,同样以端口号为名称cluster-config-file nodes_7002.conf# 配置集群节点的超时时间cluster-node-timeout 15000# 启动AOF增量持久化策略appendonly yes# 发生改变,则记录日志appendfsync always
redis-server /usr/local/redis/redis_cluster/7001/conf/redis.confredis-server /usr/local/redis/redis_cluster/7002/conf/redis.conf
[root@localhost redis-5.0.5]# ps -ef | grep redisroot 6460 1 0 14:14 ? 00:00:10 /usr/local/redis/bin/redis-server redis-166:7001 [cluster]root 6471 1 0 14:14 ? 00:00:10 /usr/local/redis/bin/redis-server redis-166:7002 [cluster]
[root@localhost redis-5.0.5]# ps -ef | grep redisroot 6532 1 0 14:14 ? 00:00:10 /usr/local/redis/bin/redis-server redis-167:7001 [cluster]root 6548 1 0 14:14 ? 00:00:10 /usr/local/redis/bin/redis-server redis-167:7002 [cluster]
[root@localhost redis-5.0.5]# ps -ef | grep redisroot 6486 1 0 14:14 ? 00:00:10 /usr/local/redis/bin/redis-server redis-168:7001 [cluster]root 6498 1 0 14:14 ? 00:00:10 /usr/local/redis/bin/redis-server redis-168:7002 [cluster]
redis-cli --cluster create 192.168.0.166:7001 192.168.0.167:7001 192.168.0.168:7001 192.168.0.166:7002 192.168.0.167:7002 192.168.0.168:7002 --cluster-replicas 1
[root@localhost redis-5.0.5]# redis-cli --cluster check 192.168.0.167:7001192.168.0.167:7001 (b909c05c...) -> 0 keys | 5462 slots | 1 slaves.192.168.0.166:7001 (e136a43b...) -> 0 keys | 5461 slots | 1 slaves.192.168.0.168:7001 (4bf0b7df...) -> 1 keys | 5461 slots | 1 slaves.[OK] 1 keys in 3 masters.0.00 keys per slot on average.>>> Performing Cluster Check (using node 192.168.0.167:7001)M: b909c05ca4c89695fee7b4799050312ed20c989e 192.168.0.167:7001 slots:[5461-10922] (5462 slots) master 1 additional replica(s)M: e136a43b8dbfd7612f48fe2c17e33203d5329eed 192.168.0.166:7001 slots:[0-5460] (5461 slots) master 1 additional replica(s)M: 4bf0b7df6ce34ad2d3ee87ab500b200f3ae64cee 192.168.0.168:7001 slots:[10923-16383] (5461 slots) master 1 additional replica(s)S: 84ada651baf494cbcdbfe26232b0061146260a3d 192.168.0.167:7002 slots: (0 slots) slave replicates e136a43b8dbfd7612f48fe2c17e33203d5329eedS: 4b9a181365ba49dde3def4d9d562c5a4d4ef657e 192.168.0.166:7002 slots: (0 slots) slave replicates 4bf0b7df6ce34ad2d3ee87ab500b200f3ae64ceeS: 02c3fc75bc6e6d4e070563f2b42a41f2616f66a5 192.168.0.168:7002 slots: (0 slots) slave replicates b909c05ca4c89695fee7b4799050312ed20c989e[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.
[root@localhost redis-5.0.5]# redis-cli -c -h 192.168.0.167 -p 7002192.168.0.167:7002> set key001 helloRredis-> Redirected to slot [274] located at 192.168.0.166:7001OK192.168.0.166:7001> get key001"helloRredis"192.168.0.166:7001>
再去167上连接7001节点和168上连接7001或者7002看是否可以查询到数据
[root@localhost redis-5.0.5]# redis-cli -c -h 192.168.0.167 -p 7001192.168.0.167:7001> get key001-> Redirected to slot [274] located at 192.168.0.166:7001"helloRredis"192.168.0.166:7001>
[root@localhost redis-5.0.5]# redis-cli -c -h 192.168.0.168 -p 7002192.168.0.168:7002> get key001-> Redirected to slot [274] located at 192.168.0.166:7001"helloRredis"192.168.0.166:7001>
[root@localhost redis-5.0.5]# vim /etc/rc.local/usr/local/redis/bin/redis-server /usr/local/redis/redis_cluster/7001/conf/redis.conf/usr/local/redis/bin/redis-server /usr/local/redis/redis_cluster/7002/conf/redis.conf
[root@localhost redis-5.0.5]# chmod +x /etc/rc.d/rc.local