1.安装redis (安装redis之前要安装gcc) 基于 redis5以后
解压---->进入redis目录------>make 或 make MALLOC=libc---->make PREFIX=/usr/local/redis install(执行make install 没有指定安装位置默认在/usr/local/bin )
2.创建集群文件夹,将redis.conf文件拷贝 cp /opt/redis-5.0.5/redis.conf /redisclusters/redis7006
3.修改redis.conf文件 批量修改 sed 's/7000/7001/g' redis7000/redis.conf > ./redis7001/redis.conf
7000--->7001 7000端口所在 目标路径
4.启动要参与集群的 redis
/usr/local/bin/redis-server /redisclusters/redis7000/redis.conf
.
.
/usr/local/bin/redis-server /redisclusters/redis7005/redis.conf
5.将redis添加集群
这些命令要在集群所在目录下执行
这个命令会自动分配槽位(根据crc16(hash) 算法算出key的槽位) 0-16383 只有master才有槽位
/usr/local/redis/bin/redis-cli --cluster create 主机IP:端口 主机IP:端口 主机IP:端口 主机IP:端口…… --cluster-replicas 1(1:1 一主一从)假如是 2 (1:2 一主二从)
/usr/local/redis/bin/redis-cli --cluster help 集群帮助命令
6.进入client:
/usr/local/redis/bin/redis-cli -h 主机IP -p 7006(端口) -c
7.
redis扩容 :
1.启动redis
2.修改配置文件
3.在集群目录下:
/usr/local/redis/bin/redis-cli --cluster help 集群帮助命令 可查看
/usr/local/redis/bin/redis-cli --cluster add-node 参加扩容主机IP:端口 已在集群主机IP:端口 添加redis进集群(Master)
/usr/local/redis/bin/redis-cli --cluster add-node 参加扩容主机IP:端口 已在集群主机IP:端口 --cluster-slave --cluster-master-id cead55fa89e4f7cdf1cb46f3a84c8b26918d69c1(redis Master IP) (Slave)
添加槽位:
/usr/local/redis/bin/redis-cli --cluster reshard 主机IP:端口 (随便一台已经进入集群)回车,根据提示操作。
1、docker pull redis:6.2--->mkdir /mydata/redis-cluster
2、填写配置文件:
for port in `seq 7000 7005`; do \
var="port ${port} protected-mode no daemonize no appendonly yes cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 15000 bind 0.0.0.0"
mkdir -p /mydata/redis-cluster/${port} \
&& PORT=${port} && echo ${var} > /mydata/redis-cluster/${port}/redis.conf
done
port 7000
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000
bind 0.0.0.0
docker run -d -ti \
-v /mydata/redis-cluster/${port}:/data \
--restart always \
--name redis-${port} \
--net host \
redis:6.2 redis-server /data/redis.conf;
done
redis-cli -p 6379 --cluster create \
192.168.213.129:7000 192.168.213.129:7001 192.168.213.129:7002 \
192.168.213.129:7003 192.168.213.129:7004 192.168.213.129:7005 \
--cluster-replicas 1
[root@rocketmq3 mydata]# docker exec -it redis-7000 \
> redis-cli -p 6379 --cluster create \
> 192.168.213.129:7000 192.168.213.129:7001 192.168.213.129:7002 \
> 192.168.213.129:7003 192.168.213.129:7004 192.168.213.129:7005 \
> --cluster-replicas 1
>>> 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.213.129:7004 to 192.168.213.129:7000
Adding replica 192.168.213.129:7005 to 192.168.213.129:7001
Adding replica 192.168.213.129:7003 to 192.168.213.129:7002
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: ea41ebbc750e3cdc3f5719b18cf05a951765c1f7 192.168.213.129:7000
slots:[0-5460] (5461 slots) master
M: 15ff2561f5f4d286e5e011a221379e6a6942eb75 192.168.213.129:7001
slots:[5461-10922] (5462 slots) master
M: b092670a2376e1da8dc1af03d73d27c2c85f1f22 192.168.213.129:7002
slots:[10923-16383] (5461 slots) master
S: b2d9f7ca8d186f152dac84626f9ea0eadf2c987c 192.168.213.129:7003
replicates ea41ebbc750e3cdc3f5719b18cf05a951765c1f7
S: bb63ca759be173e003dd9790a9c2122ea0b9e75a 192.168.213.129:7004
replicates 15ff2561f5f4d286e5e011a221379e6a6942eb75
S: f5fee752317525daa2ee7543ef62e42a8dd03cdd 192.168.213.129:7005
replicates b092670a2376e1da8dc1af03d73d27c2c85f1f22
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
..