1、下载redis
cd /var/soft/
wget http://download.redis.io/releases/redis-5.0.4.tar.gz
2、 解压文件
tar -xzvf redis-5.0.4.tar.gz
3、执行安装
cd redis-5.0.4
make
make install PREFIX=/data/redis/redis
4、复制配置文件
cd redis-5.0.4
cp redis.conf /data/redis/redis/bin
5、启动redis
cd /data/redis/redis
./redis-server ./redis.conf
6、如果执行redis-cli提示未找到错误时,需要把配置文件复制到/usr/bin/local
cd /data/redis/redis/bin
cp redis.conf /usr/bin/local
7、防火墙放行端口
firewall-cmd --zone=public --add-port=6379/tcp --permanent
8、创建cluster
mkdir /data/redis/cluster
cd /data/redis/cluster
mkdir 6380
cp -r /var/soft/redis-5.0.4/src 6380
cp /data/redis/redis/bin/redis.conf 6380
9 、修改配置文件
cd /data/redis/cluster/6380
vim redis.conf
#修改以下地方
daemonize yes //redis后台运行
pidfile /var/run/redis_6380.pid //pidfile文件对应6380
port 6380 //端口6380
cluster-enabled yes //开启集群 把注释#去掉
cluster-config-file nodes_6380.conf //集群的配置 配置文件首次启动自动生成 6380
cluster-node-timeout 5000 //请求超时 设置5秒够了
appendonly yes //aof日志开启 有需要就开启,它会每次写操作都记录一条日志
10、然后把6380复制几份 6381、6382、6383,同时把配置文件也修改成对应的端口
11、防火墙放行端口
12、启动节点
cd /data/redis/cluster
redis-server 6380/redis.conf
redis-server 6381/redis.conf
redis-server 6382/redis.conf
redis-server 6383/redis.conf
13、查看启动的节点
ps -ef |grep redis
14、创建集群
因为安装的是redis5.0.4版本,redis在5.0之后的版本取消了ruby脚本 redis-trib.rb的支持,
./redis-trib.rb create --replicas 1 192.168.X.X:6380 192.168.X.X:6381 192.168.X.X:6382 192.168.X.X:6383
WARNING: redis-trib.rb is not longer available!
You should use redis-cli instead.
All commands and features belonging to redis-trib.rb have been moved
to redis-cli.
In order to use them you should call redis-cli with the --cluster
option followed by the subcommand name, arguments and options.
Use the following syntax:
redis-cli --cluster SUBCOMMAND [ARGUMENTS] [OPTIONS]
Example:
redis-cli --cluster create 192.168.X.X:6380 192.168.X.X:6381 192.168.X.X:6382 192.168.X.X:6383 --cluster-replicas 1
To get help about all subcommands, type:
redis-cli --cluster help
采用以下方法即可
redis-cli --cluster create 192.168.X.X:6380 192.168.X.X:6381 192.168.X.X:6382 192.168.X.X:6383 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Adding replica 192.168.X.X:6383 to 192.168.X.X:6380
Adding replica 192.168.X.X:6382 to 192.168.X.X:6381
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 522f0dca780073ab668bc80ded5a1489e2febe3e 192.168.X.X:6380
slots:[0-5460] (5461 slots) master
M: f65edec341cdc39ded3ecd5298c92f3192c8afcd 192.168.X.X:6381
slots:[5461-10922] (5462 slots)
S: 3f9ff65caf28e79864f14f1996e8b5ecd136959a 192.168.X.X:6382
replicates f65edec341cdc39ded3ecd5298c92f3192c8afcd
S: 5a595b9da405a27a097f9756008b47414e2da1f7 192.168.X.X:6383
replicates 1b55615b201a68db64db210381d9a4dbaaf6b17f
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.X.X:6380)
M: 522f0dca780073ab668bc80ded5a1489e2febe3e 192.168.X.X:6380
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: f65edec341cdc39ded3ecd5298c92f3192c8afcd 192.168.X.X:6381
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 204bc647266c4c369ebe45d4ba0755c28543494e 192.168.X.X:6382
slots: (0 slots) slave
replicates 522f0dca780073ab668bc80ded5a1489e2febe3e
S: 3f9ff65caf28e79864f14f1996e8b5ecd136959a 192.168.X.X:
slots: (0 slots) slave
replicates f65edec341cdc39ded3ecd5298c92f3192c8afcd
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
15、复制的执行文件在创建集群时候报错,错误信息如下
./redis-cli --cluster create 192.168.X.X:6380 192.168.X.X:6381 192.168.X.X:6382 192.168.X.X:6383 --cluster-replicas 1
[ERR] Node 192.168.X.X:6380 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
解决方法:删除每个节点中的appendonly.aof、dump.rdb、node_xxx.conf文件,redis-cli -c -h -p登录每个redis节点,执行以下命令,再创建集群就可以了
./redis0/redis-cli -c -h 192.168.x.x -p 6380
192.168.x.x:6380> flushdb
OK
192.168.x.x:6380> cluster reset
OK
192.168.x.x:6380> exit