2018年十月 Redis 发布了稳定版本的 5.0 版本,推出了各种新特性,其中一点是放弃 Ruby的集群方式,改为 使用 C语言编写的 redis-cli的方式,是集群的构建方式复杂度大大降低。下面进行集群搭建说明
0,安装gcc
yum install gcc-c++ -y
1,首先下载redis
wget http://download.redis.io/releases/redis-5.0.0.tar.gz
tar xzf redis-5.0.0.tar.gz
cd redis-5.0.0
make
2,安装redis
[root@hadoop12 redis]# pwd
/opt/module/redis
[root@hadoop11 redis]# make install PREFIX=/home/hui/software/redis
3,创建集群文件夹
mkdir redis-cluster
cd redis-cluster/
mkdir 7001
mkdir 7002
mkdir 7003
mkdir 7004
mkdir 7005
mkdir 7006
4,拷贝redis配置文件
cp ./redis.conf /bin
cp -r ./bin/* ./redis-cluster/700[1-6]
5,修改配置文件(redis.conf)
port 7001 #端口
bind 192.168.43.11 #主机ip
cluster-enabled yes #启用集群模式
cluster-config-file nodes.conf #自动生成的节点配置
cluster-node-timeout 5000 #超时时间
appendonly yes
daemonize yes #后台运行
protected-mode no #非保护模式
pidfile /home/hui/software/redis/redis-cluster/7001/7001.pid
其中 port 和 pidfile 需要随着 文件夹的不同调增
6,编写启动脚本
vim start-all.sh,内容如下
cd /home/hui/software/redis/redis-cluster/7001
./redis-server ./redis.conf
cd /home/hui/software/redis/redis-cluster/7002
./redis-server ./redis.conf
cd /home/hui/software/redis/redis-cluster/7003
./redis-server ./redis.conf
cd /home/hui/software/redis/redis-cluster/7004
./redis-server ./redis.conf
cd /home/hui/software/redis/redis-cluster/7005
./redis-server ./redis.conf
cd /home/hui/software/redis/redis-cluster/7006
./redis-server ./redis.conf
7,启动实例
sh start-all.sh
8,编写启动集群脚本
vim start-cluster.sh ,内容如下
/home/hui/software/redis/src/redis-cli --cluster create 192.168.43.11:7001 192.168.43.11:7002 192.168.43.11:7003 192.168.43.11:7004 192.168.43.11:7005 192.168.43.11:7006 --cluster-replicas 1
启动 集群:sh start-cluster.sh,结果如下:
[hui@hadoop11 redis-cluster]$ sh start-cluster.sh
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.43.11:7004 to 192.168.43.11:7001
Adding replica 192.168.43.11:7005 to 192.168.43.11:7002
Adding replica 192.168.43.11:7006 to 192.168.43.11:7003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 2a4e1320781105790a61051b84efc640dff19fed 192.168.43.11:7001
slots:[0-5460] (5461 slots) master
M: 1bca19101b9556bf688ff35ce7b7905083474c3e 192.168.43.11:7002
slots:[5461-10922] (5462 slots) master
M: 69f1bca60c7833b4bf6866b40334c1cacc24d0f2 192.168.43.11:7003
slots:[10923-16383] (5461 slots) master
S: 8dfcdee38afceabf0e0bdad56f51b2cd4be07ba8 192.168.43.11:7004
replicates 1bca19101b9556bf688ff35ce7b7905083474c3e
S: 26985f449573ea6bd67e97046d14fcb354494f1c 192.168.43.11:7005
replicates 69f1bca60c7833b4bf6866b40334c1cacc24d0f2
S: 5b7b86078ae011aa64ecf1590083358ea80a6295 192.168.43.11:7006
replicates 2a4e1320781105790a61051b84efc640dff19fed
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.11:7001)
M: 2a4e1320781105790a61051b84efc640dff19fed 192.168.43.11:7001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 69f1bca60c7833b4bf6866b40334c1cacc24d0f2 192.168.43.11:7003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 5b7b86078ae011aa64ecf1590083358ea80a6295 192.168.43.11:7006
slots: (0 slots) slave
replicates 2a4e1320781105790a61051b84efc640dff19fed
S: 8dfcdee38afceabf0e0bdad56f51b2cd4be07ba8 192.168.43.11:7004
slots: (0 slots) slave
replicates 1bca19101b9556bf688ff35ce7b7905083474c3e
S: 26985f449573ea6bd67e97046d14fcb354494f1c 192.168.43.11:7005
slots: (0 slots) slave
replicates 69f1bca60c7833b4bf6866b40334c1cacc24d0f2
M: 1bca19101b9556bf688ff35ce7b7905083474c3e 192.168.43.11:7002
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.
查看集群状态
cd 7001
./redis-cli -c -p 7001 -h 192.168.43.11
cluster nodes #查看节点情况
参考:https://my.oschina.net/ruoli/blog/2252393
低版本redis集群搭建:https://blog.youkuaiyun.com/yerenyuan_pku/article/details/72860432
低版本redis单机安装:https://blog.youkuaiyun.com/yerenyuan_pku/article/details/72849612