一、首先创建redis文件夹,下载redis源码(通过yum下载无法指定文件夹,比较乱)
[root@dxh redis]# mkdir /redis
[root@dxh redis]# cd /redis/
[root@dxh redis]# wget http://download.redis.io/releases/redis-6.2.6.tar.gz
二、解压源码,并下载工具进行编译
[root@dxh redis]# tar -xf ./redis-6.2.6.tar.gz
[root@dxh redis]# cd ./redis-6.2.6/
[root@dxh redis-6.2.6]# yum update
[root@dxh redis-6.2.6]# yum groupinstall "Development Tools"
[root@dxh redis-6.2.6]# make
[root@dxh redis-6.2.6]# make install
三、创建多个配置文件(我这里用一台机器搭配集群,所以需要创建多个端口的redis服务,如果是多台机器搭建集群则不需要)
[root@dxh redis-6.2.6]# mkdir conf
[root@dxh redis-6.2.6]# mkdir cluster-node
[root@dxh redis-6.2.6]# mv ./redis.conf ./conf/
[root@dxh redis-6.2.6]# cd ./conf/
[root@dxh conf]# cp ./redis.conf ./redis-6380.conf
[root@dxh conf]# cp ./redis.conf ./redis-6381.conf
[root@dxh conf]# cp ./redis.conf ./redis-6382.conf
创建三个配置文件后,修改配置文件内容如下
//redis-6380.conf
port 6380
cluster-enabled yes//是否开启集群
cluster-config-file nodes-6380.conf//集群节点配置文件,创建集群时自动生成,只需指定名称和路径
cluster-node-timeout 5000//超时事件
appendonly yes//是否开始持久化存储
dir /redis/redis-6.2.6/cluster-node//集群节点配置文件路径
//redis-6381.conf
port 6380
cluster-enabled yes
cluster-config-file nodes-6381.conf
cluster-node-timeout 5000
appendonly yes
dir /redis/redis-6.2.6/cluster-node
//redis-6382.conf
port 6380
cluster-enabled yes
cluster-config-file nodes-6382.conf
cluster-node-timeout 5000
appendonly yes
dir /redis/redis-6.2.6/cluster-node
四、启动所有服务,启动集群
[root@dxh conf]# cd ../bin/
[root@dxh bin]# ./redis-server ../conf/redis-6380.conf &
//ctrl+c退出
[root@dxh bin]# ./redis-server ../conf/redis-6381.conf &
#ctrl+c退出
[root@dxh bin]# ./redis-server ../conf/redis-6382.conf &
#ctrl+c退出
#--cluster-replicas参数是指定一个主机有几个从机,redis规定最少三个主机,所以我这里只能全当主机,不要从机,如果有更多节点,并且这个值不为0的话,前面的节点当主机,后面的节点当从机
[root@dxh bin]# ./redis-cli --cluster create 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 --cluster-replicas 0
五、测试集群
通过redis-cli链接一个redis服务,使用cluster nodes命令查看,发现三个节点,集群搭建成功
[root@dxh bin]# ./redis-cli -c -p 6380
127.0.0.1:6380> cluster nodes
bc2b8b7dac060bbb97a24d0f81a31fc4c608f721 127.0.0.1:6380@16380 myself,master - 0 1722347028000 1 connected 0-5460
be6f99958c433cabe692bfd0455bd7acaf68d4b2 127.0.0.1:6382@16382 master - 0 1722347028420 3 connected 10923-16383
24387541d23d63a7674d2d1c3ead36d896de0091 127.0.0.1:6381@16381 master - 0 1722347029430 2 connected 5461-10922
六、搭建哨兵集群,监控节点(我这里依旧是一个机器,多个端口的哨兵服务)
[root@dxh redis-6.2.6]# mkdir sconf
[root@dxh redis-6.2.6]# mv ./sentinel.conf ./sconf/
[root@dxh sconf]# cp sentinel.conf 80sentinel.conf
[root@dxh sconf]# cp sentinel.conf 81sentinel.conf
[root@dxh sconf]# cp sentinel.conf 82sentinel.conf
修改哨兵配置不需要主动声明集群,只需要多个节点监听同一节点则就是集群结构,一个节点也能监听多个主机,只需要配置时多配置一个sentinel monitor即可(主机名不能重复),具体配置如下,如果看不懂注释可以看我的另一篇文章redis哨兵原理
//80sentinel.conf
port 26380
sentinel monitor mymaster 127.0.0.1 6380 2 //分别是主机的ip和端口,以及多少哨兵发现主机主观下线后,认为主机客观下线
sentinel down-after-milliseconds mymaster 5000//多少毫秒没有收到主机回应则认为主机主观下限
sentinel failover-timeout mymaster 10000//选举新主机的超时事件
sentinel parallel-syncs mymaster 1//最多有多少个从节点同时与新的主节点进行数据同步
//81sentinel.conf
port 26381
sentinel monitor mymaster 127.0.0.1 6380 2 //分别是主机的ip和端口,以及多少哨兵发现主机主观下线后,认为主机客观下线
sentinel down-after-milliseconds mymaster 5000//多少毫秒没有收到主机回应则认为主机主观下限
sentinel failover-timeout mymaster 10000//选举新主机的超时事件
sentinel parallel-syncs mymaster 1//最多有多少个从节点同时与新的主节点进行数据同步
//82sentinel.conf
port 26382
sentinel monitor mymaster 127.0.0.1 6380 2 //分别是主机的ip和端口,以及多少哨兵发现主机主观下线后,认为主机客观下线
sentinel down-after-milliseconds mymaster 5000//多少毫秒没有收到主机回应则认为主机主观下限
sentinel failover-timeout mymaster 10000//选举新主机的超时事件
sentinel parallel-syncs mymaster 1//最多有多少个从节点同时与新的主节点进行数据同步
全部启动
[root@dxh bin]# ./redis-sentinel ../sconf/80sentinel.conf &
#ctrl+c
[root@dxh bin]# ./redis-sentinel ../sconf/81sentinel.conf &
#ctrl+c
[root@dxh bin]# ./redis-sentinel ../sconf/82sentinel.conf &
#ctrl+c
通过redis-cli链接哨兵节点,执行指令测试
[root@dxh bin]# ./redis-cli -p 26380
127.0.0.1:26380> SENTINEL masters
1) "name"
2) "mymaster"
3) "ip"
4) "127.0.0.1"
5) "port"
6) "6380"
7) "runid"
8) "6fca86fda8a75951355b337dc3aaa846fcf0addf"
9) "flags"
10) "master"
11) "link-pending-commands"
12) "0"
13) "link-refcount"
14) "1"
15) "last-ping-sent"
16) "0"
17) "last-ok-ping-reply"
18) "242"
19) "last-ping-reply"
20) "242"
21) "down-after-milliseconds"
22) "30000"
23) "info-refresh"
24) "7060"
25) "role-reported"
26) "master"
27) "role-reported-time"
28) "77177"
29) "config-epoch"
30) "0"
31) "num-slaves"
32) "0"
33) "num-other-sentinels"
34) "0"
35) "quorum"
36) "2"
37) "failover-timeout"
38) "180000"
39) "parallel-syncs"
40) "1"
127.0.0.1:26380> SENTINEL sentinels mymaster
(empty array)
127.0.0.1:26380> exit
[root@dxh bin]# ./redis-cli -p 26381
127.0.0.1:26381> SENTINEL masters
标签:redis,cluster,集群,conf,dxh,sentinel,root,搭建
From: https://blog.csdn.net/dxh9231028/article/details/140801366