怎样搭建Redis群集
发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,前言Redis 集群是一个分布式(distributed)、容错(fault-tolerant)的 Redis 实现, 集群可以使用的功能是普通单机 Redis 所能使用的功能的一个子集(subset
千家信息网最后更新 2025年12月02日怎样搭建Redis群集
前言
Redis 集群是一个分布式(distributed)、容错(fault-tolerant)的 Redis 实现, 集群可以使用的功能是普通单机 Redis 所能使用的功能的一个子集(subset)。
Redis 集群中不存在中心(central)节点或者代理(proxy)节点, 集群的其中一个主要设计目标是达到线性可扩展性(linear scalability)。
Redis 集群提供了一种运行 Redis 的方式,其中数据在多个 Redis 节点间自动分区。Redis 集群还在分区期间提供一定程度的可用性,即在实际情况下能够在某些节点发生故障或无法通信时继续运行。但是,如果发生较大故障(例如,大多数主站不可用时),集群会停止运行。
实验环境
| 两台Centos 7虚拟机,均添加三块网卡用以模拟六台服务器实景 | 服务器角色 | IP地址 |
|---|---|---|
| centos7-1 | 主节点M1 | 192.168.142.130 |
| centos7-2 | 主节点M2 | 192.168.142.145 |
| centos7-3 | 主节点M3 | 192.168.142.146 |
| centos7-4 | 从节点S1 | 192.168.142.143 |
| centos7-5 | 从节点S2 | 192.168.142.147 |
| centos7-min | 从节点S3 | 192.168.142.148 |
实验步骤
第一步:安装Redis
#安装编译环境[root@localhost ~]# yum install gcc gcc-c++ make -y#远程挂载源码包[root@localhost ~]# mount.cifs //192.168.142.1/redis /mntPassword for root@//192.168.142.1/redis: #解压源码包[root@localhost ~]# cd /mnt[root@localhost mnt]# tar zxvf redis-5.0.7.tar.gz -C /opt#编译与安装[root@localhost mnt]# cd /opt/redis-5.0.7/[root@localhost redis-5.0.7]# make[root@localhost redis-5.0.7]# make PREFIX=/usr/local/redis/ install#建立服务命令软链接到系统[root@localhost redis-5.0.7]# ln -s /usr/redis/bin/* /usr/local/bin/#切入utils目录[root@localhost redis-5.0.7]# cd /opt/redis-5.0.7/utils/#执行启动脚本[root@localhost utils]# ./install_server.sh#以下内容,默认回车即可Welcome to the redis service installerThis script will help you easily set up a running redis serverPlease select the redis port for this instance: [6379] Selecting default: 6379Please select the redis config file name [/etc/redis/6379.conf] Selected default - /etc/redis/6379.confPlease select the redis log file name [/var/log/redis_6379.log] Selected default - /var/log/redis_6379.logPlease select the data directory for this instance [/var/lib/redis/6379] Selected default - /var/lib/redis/6379Please select the redis executable path [] /usr/local/redis/bin/redis-server#此处需手动指定扩展目录路径/usr/local/redis/bin/redis-server#使用进程控制启动服务[root@localhost utils]# /etc/init.d/redis_6379 startStarting Redis server...#配置redis的6379.conf文件,追加监听地址[root@localhost utils]# vim /etc/redis/6379.conf #注释第70行的监听127地址,已监听所有地址#bind 127.0.0.1 #去掉第89行注释关闭安全保护protected-mode no#去掉第93行注释,开启端口6379port 6379#去掉第137行注释,以独立进程启动daemonize yes#去掉第833行注释,开启群集功能cluster-enabled yes#去掉第841行注释,群集名称文件设置cluster-config-file nodes-6379.conf#去掉第847行注释,群集超时时间设置cluster-node-timeout 15000#去掉第700行注释,开启aof持久化appendonly yes#重启服务[root@localhost utils]# /etc/init.d/redis_6379 restartStopping ...Redis stoppedStarting Redis server...第二步:主服务器安装rvm组件
#导入key文件[root@localhost utils]# gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3#安装rvm[root@localhost utils]# curl -sSL https://get.rvm.io | bash -s stable#执行环境变量[root@localhost utils]# source /etc/profile.d/rvm.sh#列出ruby可以安装的版本[root@localhost utils]# rvm list known#安装2.4.1 版本[root@localhost utils]# rvm install 2.4.1#使用rubyruby2.4.1 版本[root@localhost utils]# rvm use 2.4.1Using /usr/local/rvm/gems/ruby-2.4.1#查看当前ruby2.4.1 版本[root@localhost utils]# ruby -vruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux]#再次安装Redis[root@localhost utils]# gem install redis第三步:创建Redis集群
#加入所有节点地址,并加6379端口号[root@localhost utils]# redis-cli --cluster create 192.168.142.130:6379 192.168.142.145:6379 192.168.142.146:6379 192.168.142.143:6379 192.168.142.147:6379 192.168.142.148:6379 --cluster-replicas 1 >>> Performing hash slots allocation on 6 nodes...Master[0] -> Slots 0 - 5460Master[1] -> Slots 5461 - 10922Master[2] -> Slots 10923 - 16383Adding replica 192.168.142.147:6379 to 192.168.142.130:6379Adding replica 192.168.142.148:6379 to 192.168.142.145:6379Adding replica 192.168.142.143:6379 to 192.168.142.146:6379M: a27b43ec695099b36a5c79beae70cb0364f27338 192.168.142.130:6379 slots:[0-5460] (5461 slots) masterM: a27b43ec695099b36a5c79beae70cb0364f27338 192.168.142.145:6379 slots:[5461-10922] (5462 slots) masterM: a27b43ec695099b36a5c79beae70cb0364f27338 192.168.142.146:6379 slots:[10923-16383] (5461 slots) masterS: b6d317c5f192bf84e8f464ffbf6481529cd0708a 192.168.142.143:6379 replicates a27b43ec695099b36a5c79beae70cb0364f27338S: b6d317c5f192bf84e8f464ffbf6481529cd0708a 192.168.142.147:6379 replicates a27b43ec695099b36a5c79beae70cb0364f27338S: b6d317c5f192bf84e8f464ffbf6481529cd0708a 192.168.142.148:6379 replicates a27b43ec695099b36a5c79beae70cb0364f27338Can 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 192.168.142.130:6379)M: a27b43ec695099b36a5c79beae70cb0364f27338 192.168.142.130:6379 slots:[0-16383] (16384 slots) master 1 additional replica(s)S: b6d317c5f192bf84e8f464ffbf6481529cd0708a 192.168.142.148:6379 slots: (0 slots) slave replicates a27b43ec695099b36a5c79beae70cb0364f27338[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.第四步:验证群集读写
#从主节点计入数据库写数据[root@localhost ~]# redis-cli -h 192.168.142.130 -p 6379192.168.142.130:6379> set name yangjiaOK192.168.142.130:6379> get name"yangjia"#从另一节点查看数据信息[root@localhost ~]# redis-cli -h 192.168.142.146 -p 6379192.168.142.146:6379> keys *1) "name"192.168.142.146:6379> get name"yangjia"#在从节点无法查看数据,提示切换主节点服务器地址[root@localhost ~]# redis-cli -h 192.168.142.143 -p 6379192.168.142.143:6379> keys *1) "name"192.168.142.143:6379> get name(error) MOVED 5798 192.168.142.146:6379
节点
注释
集群
服务
地址
数据
服务器
版本
功能
文件
环境
监听
运行
故障
源码
目录
进程
实验
编译
较大
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
苏州推广软件开发供应商
菜单管理系统 数据库
软件开发一年以后发现问题
数据库标准要素代码命名
服务器机箱家用
软件开发设备支撑
南邮网络技术与应用五次作业
家庭教育网络安全直播回放
做好管理的服务器
数据库原理课程学习感悟
政治新闻网络安全
电信公司制作网络安全群发内容
数据库阶乘相加计算
服务器乘以2 2
长城虚拟网络技术
数据库去年同期值怎么取
国网南瑞网络安全
洛阳华睿网络技术有限公司
网络安全教育新闻稿
软件开发中需要写哪些代码
第一章 数据库基础
安卓软件开发个人中心界面
长沙县妇幼保健院网络安全设备
白银市网络安全和信息化工作会议
32位串口管理服务器
美国遇到网络安全问题
网络技术人员职位描述
当前无法连接服务器正在恢复连接
陕西中迪丽天互联网科技有限公司
我的世界服务器权限刷新