环境准备
-
VM准备3台机器
centos7环境: 192.168.217.132 root root 192.168.217.133 root root 192.168.217.134 root root
单机安装
-
下载源码包(192.168.217.132)
cd /opt wget https://download.redis.io/releases/redis-5.0.12.tar.gz
-
安装编译器(如已安装请忽略)
在安装redis之前先安装C++编译环境,查看目前服务器上gcc的版本:gcc -v yum -y install gcc-c++
-
解压编译
tar -zxvf redis-5.0.12.tar.gz 指定redis安装目录为/usr/local/redis cd redis-5.0.12 && make && make install PREFIX=/usr/local/redis cp /opt/redis-5.0.12/*.conf /usr/local/redis
-
尝试运行
cd /usr/local/redis/bin ./redis-server ../redis.conf
-
建立软连接
添加到PATH ln -s /usr/local/redis/bin/redis-cli /usr/local/bin/redis-cli ln -s /usr/local/redis/bin/redis-server /usr/local/bin/redis-server ln -s /usr/local/redis/bin/redis-sentinel /usr/local/bin/redis-sentinel 软连接建立路径必须都是绝对路径
-
启动服务
在任意路径都可以直接使用redis-cli和redis-server redis-server /usr/local/redis/redis.conf
主从复制
-
节点非配
master 192.168.217.132 replica 192.168.217.133 replica 192.168.217.134
-
复制安装文件
scp -r /usr/local/redis root@192.168.217.133:/usr/local scp -r /usr/local/redis root@192.168.217.134:/usr/local # 需输入密码
-
修改
redis.conf
文件# 后台启动 daemonize yes protected-mode no 注释 bind 127.0.0.1 # 5.0版本以上使用的是replicaof,之前版本用savleof # 配置从节点 从哪里复制数据,主节点不需要配置 replicaof 192.168.217.132 6379
-
如果主节点有密码校验,从节点需要配置
masterauth
masterauth 123456
哨兵模式
-
修改三台机器上的
sentinel.conf
文件daemonize yes # 最后的2表示:至少有两个哨兵认为master节点挂了才进行选举 sentinel monitor mymaster 192.168.217.132 6379 2
-
启动哨兵
redis-sentinel /usr/local/redis/sentinel.conf
Cluster模式
-
准备配置文件(192.168.217.132)
mkdir /usr/local/redis/cluster cp /opt/redis-5.0.12/redis.conf /usr/local/redis/cluster/redis_6379.conf cp /opt/redis-5.0.12/redis.conf /usr/local/redis/cluster/redis_7379.conf
-
修改
redis_6379.conf和redis_7379.conf
# 端口 port 6379 和 port 7379 # 后台运行 daemonize yes # 进程号 pidfile /var/run/redis_6379.pid 和 pidfile /var/run/redis_7379.pid # 工作目录 dir /usr/local/redis/cluster # 开启集群 cluster-enabled yes cluster-config-file nodes-6379.conf 和 cluster-config-file nodes-7379.conf
-
编写脚本
/usr/local/redis/cluster/start.sh
#!/bin/bash pid=`ps -ef | grep redis | grep -v grep | awk '{print $2}'` for item in $pid do kill -9 $item done redis-server /usr/local/redis/cluster/redis_6379.conf redis-server /usr/local/redis/cluster/redis_7379.conf
-
远程复制
scp -r /usr/local/redis/cluster root@192.168.217.133:/usr/local/redis scp -r /usr/local/redis/cluster root@192.168.217.134:/usr/local/redis
-
运行
cd /usr/local/redis/cluster ./start.sh
-
节点关联
任意一台机器 redis-cli --cluster create 192.168.217.132:6379 192.168.217.132:7379 192.168.217.133:6379 192.168.217.133:7379 192.168.217.134:6379 192.168.217.134:7379 --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.217.133:7379 to 192.168.217.132:6379 Adding replica 192.168.217.134:7379 to 192.168.217.133:6379 Adding replica 192.168.217.132:7379 to 192.168.217.134:6379 M: 6eb1b7c72b9bb0cfab13002156308cccbfedc723 192.168.217.132:6379 slots:[0-5460] (5461 slots) master S: 99c97e97efe3f4183aa9b2ccdc3f37602bfa4ce7 192.168.217.132:7379 replicates 0d140c1a61c3588a453855ff60726d80adac1265 M: 640f4beb94a9d7f83a7f3efb0b6d9442db85e361 192.168.217.133:6379 slots:[5461-10922] (5462 slots) master S: c04ca1b7cf03c1bb574f63e9c766f1b6e233ad2e 192.168.217.133:7379 replicates 6eb1b7c72b9bb0cfab13002156308cccbfedc723 M: 0d140c1a61c3588a453855ff60726d80adac1265 192.168.217.134:6379 slots:[10923-16383] (5461 slots) master S: dfbdf83174bd57ac615e62adec6006295932a9f0 192.168.217.134:7379 replicates 640f4beb94a9d7f83a7f3efb0b6d9442db85e361 Can I set the above configuration? (type 'yes' to accept): yes # 输入 yes
-
上面是随机分配主从节点、自动平均分配插槽,也可以自己定义
[root@node1 redis]# redis-cli -h 192.168.217.132 192.168.217.132:6379> cluster meet 192.168.217.133 6379 OK 192.168.217.132:6379> cluster meet 192.168.217.134 6379 OK 192.168.217.132:6379> cluster meet 192.168.217.132 7379 OK 192.168.217.132:6379> cluster meet 192.168.217.133 7379 OK 192.168.217.132:6379> cluster meet 192.168.217.134 7379 OK
-
查看集群节点
192.168.217.132:6379> cluster nodes 09ed6cdb5cb0f5ef706f18a488a8b23477f44e77 192.168.217.132:7379@17379 master - 0 1617472288407 1 connected 5cbc8237c517adafcb9d60c43dad34674cc54d1f 192.168.217.133:7379@17379 master - 0 1617472291496 0 connected 0e3808182116196c356bf8b79a605edc40156276 192.168.217.133:6379@16379 master - 0 1617472290464 4 connected 447b30e9ae3ba076e90e9080abb55f981d23dddb 192.168.217.132:6379@16379 myself,master - 0 1617472289000 2 connected 11c97e9c4db702681537cb02ede2bc4dab8a6ef4 192.168.217.134:7379@17379 master - 1617472293545 1617472289439 3 connected 1b968e634619001f65a807a47ec27cbb2a38480e 192.168.217.134:6379@16379 master - 0 1617472292521 5 connected # 此时集群是不可用的,因为还没有分配槽
-
查看集群状态
192.168.217.132:6379> cluster info cluster_state:fail cluster_slots_assigned:0 cluster_slots_ok:0 cluster_slots_pfail:0 cluster_slots_fail:0 cluster_known_nodes:6 cluster_size:0 cluster_current_epoch:5 cluster_my_epoch:2 cluster_stats_messages_ping_sent:660 cluster_stats_messages_pong_sent:727 cluster_stats_messages_meet_sent:10 cluster_stats_messages_sent:1397 cluster_stats_messages_ping_received:727 cluster_stats_messages_pong_received:670 cluster_stats_messages_received:1397
-
指定副本节点
# 通过cluster replicate <master node_id> 指定从节点必须在从节点客户端进行,比如我现在需要把192.168.217.132:7379 实例指定为192.168.217.132:6379的从节点,我就必须在 redis-cli -h 192.168.217.132 -p 7379 去执行cluster replicate <master node_id>命令 192.168.217.132:7379> cluster replicate 447b30e9ae3ba076e90e9080abb55f981d23dddb 192.168.217.133:7379> cluster replicate 0e3808182116196c356bf8b79a605edc40156276 192.168.217.134:7379> cluster replicate 1b968e634619001f65a807a47ec27cbb2a38480e # 结果 192.168.217.134:7379> cluster nodes 11c97e9c4db702681537cb02ede2bc4dab8a6ef4 192.168.217.134:7379@17379 myself,slave 1b968e634619001f65a807a47ec27cbb2a38480e 0 1616926898000 3 connected 0e3808182116196c356bf8b79a605edc40156276 192.168.217.133:6379@16379 master - 0 1616926905411 4 connected 1b968e634619001f65a807a47ec27cbb2a38480e 192.168.217.134:6379@16379 master - 0 1616926902357 5 connected 447b30e9ae3ba076e90e9080abb55f981d23dddb 192.168.217.132:6379@16379 master - 0 1616926903373 2 connected 09ed6cdb5cb0f5ef706f18a488a8b23477f44e77 192.168.217.132:7379@17379 slave 447b30e9ae3ba076e90e9080abb55f981d23dddb 0 1616926904393 2 connected 5cbc8237c517adafcb9d60c43dad34674cc54d1f 192.168.217.133:7379@17379 slave 0e3808182116196c356bf8b79a605edc40156276 0 1616926901337 4 connected
-
分配槽
[root@node1 redis]# redis-cli -h 192.168.217.132 -p 6379 cluster addslots {0..5460} OK [root@node1 redis]# redis-cli -h 192.168.217.133 -p 6379 cluster addslots {5461..10922} OK # 可以给副本节点分配槽 [root@node1 redis]# redis-cli -h 192.168.217.134 -p 7379 cluster addslots {10923..16383} OK # cluster nodes查看集群节点,发现134节点并没有成功分配槽 127.0.0.1:6379> cluster nodes 09ed6cdb5cb0f5ef706f18a488a8b23477f44e77 192.168.217.132:7379@17379 slave 447b30e9ae3ba076e90e9080abb55f981d23dddb 0 1617473587214 2 connected 5cbc8237c517adafcb9d60c43dad34674cc54d1f 192.168.217.133:7379@17379 slave 0e3808182116196c356bf8b79a605edc40156276 0 1617473586000 4 connected 0e3808182116196c356bf8b79a605edc40156276 192.168.217.133:6379@16379 master - 0 1617473588231 4 connected 5461-10922 447b30e9ae3ba076e90e9080abb55f981d23dddb 192.168.217.132:6379@16379 myself,master - 0 1617473587000 2 connected 0-5460 11c97e9c4db702681537cb02ede2bc4dab8a6ef4 192.168.217.134:7379@17379 slave 1b968e634619001f65a807a47ec27cbb2a38480e 0 1617473589251 5 connected 1b968e634619001f65a807a47ec27cbb2a38480e 192.168.217.134:6379@16379 master - 0 1617473590270 5 connected # cluster info 查看集群状态,发现成功分配的槽数是10923个 127.0.0.1:6379> cluster info cluster_state:fail cluster_slots_assigned:10923 cluster_slots_ok:10923 cluster_slots_pfail:0 cluster_slots_fail:0 cluster_known_nodes:6 cluster_size:2 cluster_current_epoch:5 cluster_my_epoch:2 # 所以我个人猜测在从节点分配槽无效,没有论证 # 修改134分配槽 [root@node1 redis]# redis-cli -h 192.168.217.134 -p 6379 cluster addslots {10923..16383}
-
测试
# -c的作用是如果根据你的key计算出的hash slot不在0-5460间,会自动重定向到对应的redis-cli redis-cli -c -h 192.168.217.132 -p 6379 192.168.217.132:6379> set name tom -> Redirected to slot [5798] located at 192.168.217.133:6379 OK 192.168.217.133:6379>