搭建redis cluster集群
服务器版本
CentOS Linux release 7.5.1804 (Core)
redis cluster 规模
- 三主三从
- 192.168.24.241:6001,192.168.241.6002
- 192.168.24.242:6001,192.168.242.6002
- 192.168.24.243:6001,192.168.243.6002
下载redis
- wget http://download.redis.io/releases/redis-4.0.8.tar.gz
- 解压
- make
- make install
- 启动redis服务:redis-server
出现的错误
- zmalloc.h:50:31: 致命错误:jemalloc/jemalloc.h:没有那个文件或目录 这个错误
解决方法:make MALLOC=libc
修改redis.conf配置
- 创建redis_cluster目录(每台服务器)
mkdir redis_cluster
- 在redis_cluster目录里创建两个目录6001 6002
mkdir 6001
mkdir 6002
- 修改redis.conf配置
#配置
#redis后台运行
daemonize yes
#pidfile文件 6001~6003
pidfile /var/run/redis_6000.pid
#6001~6003
port 7000
#开启集群
cluster-enabled yes
#配置文件首次启动自动生成
cluster-config-file nodes_6001.conf
#请求超时
cluster-node-timeout 5000
#aof日志
appendonly no
#绑定地址,需要别的机器能ping通的地址
bind 192.168.21.137
- 将redis.conf 赋值到6001和6002中
cp redis.conf redis_cluster/6001
cp redis.conf redis_cluster/6002
验证redis安装成功
-
启动redis
进去每个目录cd redis_cluster/6001
启动redisredis-server redis.conf
-
查看redis进程
ps -ef |grep redis
如果看到redis进程,则代表redis启动成功
安装redis-trib.rb
redis-trib.rb 是官方提供的用于搭建集群的工具
安装ruby
redis-trib.rb是基于ruby写的
- 安装ruby
yum -y install ruby ruby-devel rubygems rpm-build
由于CentOS7 yum库中ruby的版本支持到 2.0.0,但是redis-trib.rb需要>2.2.2版本,因此需要用rvm来更新ruby - 更新ruby
- 安装rvm
gpg2 --keyserver hkp://keys.gnupg.net --recv-keys D39DC0E3
curl -L get.rvm.io | bash -s stable
find / -name rvm -print
- 更新配置
source /usr/local/rvm/scripts/rvm
- 查看rvm库中已知的ruby版本
rvm list known
- 安装一个ruby版本
rvm install 2.3.3
- 使用一个ruby版本
rvm install 2.3.3
- 设置默认版本
rvm use 2.3.3 --default
- 卸载yum下载的版本
rvm remove 2.0.0
- 安装rvm
下载redis 接口
- 利用gem下载
gem install redis
确认安装成功
- redis-trib.rb是在redis的src目录下
- /opt/redis/redis-4.0.8/src/redis-trib.rb
创建集群
用redis-trib.rb 创建集群
-
redis-trib.rb 使用
-
利用create指令来创建集群,**–replicas 1 ** 指的是每个主节点都有一个从节点,后面跟着的是redis的 ip:port
-
./redis-trib.rb create --replicas 1 192.168.24.241:6001 192.168.24.241:6002 192.168.24.242:6001 192.168.24.242:6002 192.168.24.243:6001 192.168.24.243:6002
[root@sy1 src]# ./redis-trib.rb create --replicas 1 192.168.24.241:6001 192.168.24.241:6002 192.168.24.242:6001 192.168.24.242:6002 192.168.24.243:6001 192.168.24.243:6002
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.24.241:6001
192.168.24.242:6001
192.168.24.243:6001
Adding replica 192.168.24.242:6002 to 192.168.24.241:6001
Adding replica 192.168.24.243:6002 to 192.168.24.242:6001
Adding replica 192.168.24.241:6002 to 192.168.24.243:6001
M: f829b83670df08aa0289a5ffef9efa32386e92d2 192.168.24.241:6001
slots:0-5460 (5461 slots) master
S: 184f6dc341d0d49fbf17739233b7078606b399c3 192.168.24.241:6002
replicates 130f3b8025c422820afa222f27949cf9acdc5f24
M: 63458c1953bdaa3484c482af23b1a1fa7e6f4f71 192.168.24.242:6001
slots:5461-10922 (5462 slots) master
S: 8c8a6e54ee4c50e296f95faa7cda1cda0328fbe8 192.168.24.242:6002
replicates f829b83670df08aa0289a5ffef9efa32386e92d2
M: 130f3b8025c422820afa222f27949cf9acdc5f24 192.168.24.243:6001
slots:10923-16383 (5461 slots) master
S: 3c8b17d350ffafc7bafe1a0189deac5164fa45f1 192.168.24.243:6002
replicates 63458c1953bdaa3484c482af23b1a1fa7e6f4f71
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.24.241:6001)
M: f829b83670df08aa0289a5ffef9efa32386e92d2 192.168.24.241:6001
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 130f3b8025c422820afa222f27949cf9acdc5f24 192.168.24.243:6001
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 184f6dc341d0d49fbf17739233b7078606b399c3 192.168.24.241:6002
slots: (0 slots) slave
replicates 130f3b8025c422820afa222f27949cf9acdc5f24
S: 3c8b17d350ffafc7bafe1a0189deac5164fa45f1 192.168.24.243:6002
slots: (0 slots) slave
replicates 63458c1953bdaa3484c482af23b1a1fa7e6f4f71
S: 8c8a6e54ee4c50e296f95faa7cda1cda0328fbe8 192.168.24.242:6002
slots: (0 slots) slave
replicates f829b83670df08aa0289a5ffef9efa32386e92d2
M: 63458c1953bdaa3484c482af23b1a1fa7e6f4f71 192.168.24.242:6001
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.
连接集群
- 命令:
redis-cli -h 192.168.24.241 -p 6001 -c
- -h:Ip
- -p:端口,默认6379
- -c:集群模式,访问集群必须带上
[root@sy2 6001]# redis-cli -h 192.168.24.241 -p 6001 -c
192.168.24.241:6001> set key1 key1
-> Redirected to slot [9189] located at 192.168.24.242:6001
OK
192.168.24.242:6001> get key1
"key1"
192.168.24.242:6001> set kk 241
-> Redirected to slot [2589] located at 192.168.24.241:6001
OK
192.168.24.241:6001> set kjkjkj 1231231
OK
192.168.24.241:6001> get kk
"241"