Redis概要 参考 :https://www.jianshu.com/p/32e171b310c4
Redis集群原理,参考:https://blog.csdn.net/qq_25484147/article/details/78641392
Redis集群搭建步骤:
redis 安装
官网下载redis
解压
tar zxf redis-4.0.11.tar.gz
安装
cd redis-4.0.11
make
make install prefix=/home/vagrant/soft/redis-cluster
注:
make,出现以下错误,说明缺少gcc,需要安装gcc,yum install gcc
cd src && make all
make[1]: Entering directory `/home/vagrant/soft/redis-4.0.11/src'
CC Makefile.dep
make[1]: Leaving directory `/home/vagrant/soft/redis-4.0.11/src'
make[1]: Entering directory `/home/vagrant/soft/redis-4.0.11/src'
CC adlist.o
/bin/sh: cc: command not found
make[1]: *** [adlist.o] Error 127
make[1]: Leaving directory `/home/vagrant/soft/redis-4.0.11/src'
make: *** [all] Error 2
make,出现以下错误,则将make改为make MALLOC=libc
In file included from adlist.c:34:0:
zmalloc.h:50:31: fatal error: jemalloc/jemalloc.h: No such file or directory
#include <jemalloc/jemalloc.h>
^
compilation terminated.
make[1]: *** [adlist.o] Error 1
make[1]: Leaving directory `/home/vagrant/soft/redis-4.0.11/src'
make: *** [all] Error 2
redis配置
进入/home/vagrant/soft/redis-cluster
把bin改成redis01
把/home/vagrant/soft/redis-4.0.11/src中的redis.conf 复制到redis01目录下
修改redis.conf的配置:
daemonize yes #后台启动
port 7001 #修改端口号,从7001到7006
cluster-enabled yes #开启cluster,去掉注释
cluster-config-file nodes.conf
cluster-node-timeout 15000
appendonly yes
redis01 复制几份 redis02,redis03,redis04,redis05,redis06,并修改redis.conf中的端口号
在redis-cluster目录中增加redis启动脚本startup.sh
#!/bin/bash
./redis01/redis-server ./redis01/redis.conf
./redis02/redis-server ./redis02/redis.conf
./redis03/redis-server ./redis03/redis.conf
./redis04/redis-server ./redis04/redis.conf
./redis05/redis-server ./redis05/redis.conf
./redis06/redis-server ./redis06/redis.conf
增加执行权限:chmod +x startup.sh,启动脚本
./startup.sh
redis集群配置
centos下默认安装ruby的版本是:2.0,执行ruby脚本时,会报一下错误:
“redis requires Ruby version >= 2.2.2”
需要安装最新的ruby
yum install centos-release-scl-rh
yum install rh-ruby23 -y
scl enable rh-ruby23 bash
ruby -v
安装redis-trib.rb运行依赖的ruby的包
gem install redis
启动集群
./redis-trib.rb create --replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006
启动成功
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
127.0.0.1:7001
127.0.0.1:7002
127.0.0.1:7003
Adding replica 127.0.0.1:7005 to 127.0.0.1:7001
Adding replica 127.0.0.1:7006 to 127.0.0.1:7002
Adding replica 127.0.0.1:7004 to 127.0.0.1:7003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: f6079eb3248ecb53558f7d9958bae6a73653684f 127.0.0.1:7001
slots:0-5460 (5461 slots) master
M: 0a2bf8374719c0c6c94759fcfaf55f2810f5ff0b 127.0.0.1:7002
slots:5461-10922 (5462 slots) master
M: 8dfa9decad334d8c10b2d54cfd0784d10d62522c 127.0.0.1:7003
slots:10923-16383 (5461 slots) master
S: 52ecf4670eb76f90d94e0f8044214d8640b48371 127.0.0.1:7004
replicates f6079eb3248ecb53558f7d9958bae6a73653684f
S: e6cc18314f4ca824fec61f1e1a5b85a7d34d7d3f 127.0.0.1:7005
replicates 0a2bf8374719c0c6c94759fcfaf55f2810f5ff0b
S: 26cacc4e6349388021b46a663842538c25a51b5b 127.0.0.1:7006
replicates 8dfa9decad334d8c10b2d54cfd0784d10d62522c
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 127.0.0.1:7001)
M: f6079eb3248ecb53558f7d9958bae6a73653684f 127.0.0.1:7001
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 0a2bf8374719c0c6c94759fcfaf55f2810f5ff0b 127.0.0.1:7002
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 52ecf4670eb76f90d94e0f8044214d8640b48371 127.0.0.1:7004
slots: (0 slots) slave
replicates f6079eb3248ecb53558f7d9958bae6a73653684f
M: 8dfa9decad334d8c10b2d54cfd0784d10d62522c 127.0.0.1:7003
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: e6cc18314f4ca824fec61f1e1a5b85a7d34d7d3f 127.0.0.1:7005
slots: (0 slots) slave
replicates 0a2bf8374719c0c6c94759fcfaf55f2810f5ff0b
S: 26cacc4e6349388021b46a663842538c25a51b5b 127.0.0.1:7006
slots: (0 slots) slave
replicates 8dfa9decad334d8c10b2d54cfd0784d10d62522c
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.