redis集群搭建 Installing Redis Cluster
简介:
Redis的集群模式实现了数据的分布式存储,每个节点存储不同的数据,实现数据动态扩容。
优点:无中心节点架构,数据按照slot分布在多个节点上
缺点:不支持多数据库
1、安装包准备
1、redis-4.0.1.tar.gz redis源文件
下载地址:https://download.redis.io/releases/redis-4.0.1.tar.gz
2、ruby-2.5.9.tar.gz ruby安装包
2、安装机器准备
三台机器:192.168.43.120 192.168.43.130 192.168.43.140 3主3从,每台机器两个节点
3、目录创建和redis编译
a、在120机器上创建目录:/opt/soft
b、进入soft目录,执行 wget https://download.redis.io/releases/redis-4.0.1.tar.gz 下载redis源文件
c、执行 tar -zxvf redis-4.0.1.tar.gz 解压源文件包,解压后得到redis-4.0.1目录
d、cd redis-4.0.1 目录,编译源文件,执行命令:make
如果出现以下错误:
说明没有gcc,运行 yum install gcc
再执行make命令,如果出现以下错误:
则执行命令:make MALLOC=libc
4、节点创建
每台机器配置两个节点 7000和7001
创建目录 mkdir cluster
进入cluster目录,分别创建7000和7001目录
复制配置文件,把redis.conf分别复制到7000和7001下
增加配置:
分别在7000和7001下的redis.conf添加如下配置
bind 192.168.43.140 port 7000 #masterauth 123456 #requirepass 123456 cluster-enabled yes cluster-config-file nodes-7000.conf cluster-node-timeout 5000 appendonly yes
保存退出
5、启动节点
执行命令分别启动7000和7001节点:
./src/redis-server /opt/soft/redis-4.0.1/cluster/7000/redis.conf &
./src/redis-server /opt/soft/redis-4.0.1/cluster/7001/redis.conf &
启动成功如下:
其它两台机器一样操作
6、创建集群(只需要在一台机器上执行)
a、安装ruby
tar -zxvf ruby-2.5.9.tar.gz
cd ruby-2.5.9/
./configure --prefix=/usr/local/ruby
make && make install
查看安装后的版本
/usr/local/ruby/bin/ruby -v
设置环境变量
vi /etc/profile
export PATH=$PATH:/usr/local/ruby/bin:
source /etc/profile
b、安装gem
运行命令gem install redis
出现如下错误说明确实zlib库
yum install zlib-devel
集成zlib库到ruby环境
cd /opt/soft/ruby-2.5.9/ext/zlib/
ruby extconf.rb
操作之前修改Makefile里的文件
vi Makefile
在文件最后找到:zlib.o: $(top_srcdir)/include/ruby.h 修改为 zlib.o: ../../include/ruby.h 保存退出
执行命令 make && make install
执行命令gem install redis 如果报一下错误
安装 openssl-devel
yum install openssl-devel
集成openssl库到ruby
cd /opt/soft/ruby-2.5.9/ext/openssl/
ruby extconf.rb
修改Makefile 里面的
ossl.o: $(top_srcdir)/include/ruby.h 修改为 ossl.o: ../../include/ruby.h
ossl_asn1.o: $(top_srcdir)/include/ruby.h 修改为 ossl_asn1.o: ../../include/ruby.h
ossl_bio.o: $(top_srcdir)/include/ruby.h
ossl_bn.o: $(top_srcdir)/include/ruby.h
ossl_cipher.o: $(top_srcdir)/include/ruby.h
等等全部修改
保存退出
执行命令 make && make install
最后 运行命令gem install redis
如果卡死 执行 gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/
c、创建集群
./redis-trib.rb create --replicas 1 192.168.43.120:7000 192.168.43.120:7001 192.168.43.130:7000 192.168.43.130:7001 192.168.43.140:7000 192.168.43.140:7001
(2023.10.25修改)
如果redis为6.2.7版本,创建集群方式为:
./redis-cli --cluster create 172.17.46.133:7000 172.17.46.133:7001 172.17.46.134:7000 172.17.46.134:7001 172.17.46.135:7000 172.17.46.135:7001
[root@centos3 src]# ./redis-trib.rb create --replicas 1 192.168.43.120:7000 192.168.43.120:7001 192.168.43.130:7000 192.168.43.130:7001 192.168.43.140:7000 192.168.43.140:7001 >>> Creating cluster >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 192.168.43.120:7000 192.168.43.130:7000 192.168.43.140:7000 Adding replica 192.168.43.130:7001 to 192.168.43.120:7000 Adding replica 192.168.43.120:7001 to 192.168.43.130:7000 Adding replica 192.168.43.140:7001 to 192.168.43.140:7000 M: 5b341137f0fa22573b34a14206b3b4c625986f42 192.168.43.120:7000 slots:0-5460 (5461 slots) master S: 5e162d8d5dad7e6e4fa9ecefac9b3d27e8659e33 192.168.43.120:7001 replicates 6cbcdc0f773f5c1d895aeb33b29da449890e3064 M: 6cbcdc0f773f5c1d895aeb33b29da449890e3064 192.168.43.130:7000 slots:5461-10922 (5462 slots) master S: 6ac36c5449c77caca707b67c8d01dcc9efe3f0fa 192.168.43.130:7001 replicates 5b341137f0fa22573b34a14206b3b4c625986f42 M: f8c1c69d67ebecb3e6092e901fe1ca66bd097381 192.168.43.140:7000 slots:10923-16383 (5461 slots) master S: 4e632fd278a48016c62255746f5128c121a71dfe 192.168.43.140:7001 replicates f8c1c69d67ebecb3e6092e901fe1ca66bd097381 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.43.120:7000) M: 5b341137f0fa22573b34a14206b3b4c625986f42 192.168.43.120:7000 slots:0-5460 (5461 slots) master 1 additional replica(s) S: 6ac36c5449c77caca707b67c8d01dcc9efe3f0fa 192.168.43.130:7001 slots: (0 slots) slave replicates 5b341137f0fa22573b34a14206b3b4c625986f42 M: 6cbcdc0f773f5c1d895aeb33b29da449890e3064 192.168.43.130:7000 slots:5461-10922 (5462 slots) master 1 additional replica(s) M: f8c1c69d67ebecb3e6092e901fe1ca66bd097381 192.168.43.140:7000 slots:10923-16383 (5461 slots) master 1 additional replica(s) S: 4e632fd278a48016c62255746f5128c121a71dfe 192.168.43.140:7001 slots: (0 slots) slave replicates f8c1c69d67ebecb3e6092e901fe1ca66bd097381 S: 5e162d8d5dad7e6e4fa9ecefac9b3d27e8659e33 192.168.43.120:7001 slots: (0 slots) slave replicates 6cbcdc0f773f5c1d895aeb33b29da449890e3064 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
最后客户端测试