1.安装ruby2.5.5
yum install ruby
2.安装依赖
yum -y install gcc gcc-c++ libstdc++-devel
yum install make
3.下载redis5.0.7
wget http://download.redis.io/releases/redis-5.0.7.tar.gz
4.解压安装
tar -zxvf redis-5.0.7.tar.gz
cd redis-5.0.7
#make后面的参数,是为解决报错: jemalloc/jemalloc.h: No such file or directory
make MALLOC=libc
#指定编译路径
make install PREFIX=/usr/local/redis
5.编辑master节点/etc/6379.conf
port 6379
#设置内网外网都能访问
bind 192.168.198.135
#后台运行
daemonize yes
#密码配置
requirePass 123456
从6379.conf复制两份配置文件 6380.conf,6381.conf 并修改配置文件
#端口根据实际情况配置
port 6380
#设置内网外网都能访问
bind 192.168.198.135
#后台运行
daemonize yes
#密码配置
requirePass 123456
#复制数据的节点
replicaof 192.168.198.135 6379
#master密码
masterauth 123456
6.启动
cd到/usr/local/redis/bin
#启动master节点
./redis-server /etc/6379.conf
#启动slave节点
./redis-server /etc/6380.conf
./redis-server /etc/6381.conf
查询集群:
#登录
./redis-cli -p 6379
#认证
127.0.0.1:6379> auth 123456
#查看集群信息
info replication
role:master
connected_slaves:2
slave0:ip=192.168.198.135,port=6380,state=online,offset=1792,lag=1
slave1:ip=192.168.198.135,port=6381,state=online,offset=1778,lag=1
master_replid:d4bfe3d14d362690e97ccbcd50a4d85d013c30a3
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1792
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1792
登录slave节点6380查询集群信息:
#登录
./redis-cli -p 6380
#认证
127.0.0.1:6379> auth 123456
#查看集群信息
info replication
# Replication
role:slave
master_host:192.168.198.135
master_port:6379
master_link_status:up
master_last_io_seconds_ago:8
master_sync_in_progress:0
slave_repl_offset:2198
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:d4bfe3d14d362690e97ccbcd50a4d85d013c30a3
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2198
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:2198
关闭命令
./redis-cli -a 密码 shutdown
或者强制关闭
pkill redis-server
7.设置开机启动
新建文件
vim /etc/systemd/system/redis.service
新增内容:
[Unit]
Description=redis-server
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/bin/redis.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
配置说明:
Description:描述服务
After:描述服务类别
[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3
添加开机启动:
systemctl daemon-reload
systemctl enable redis
相关命令:
systemctl start redis.service 启动
systemctl stop redis.service 停止服务
systemctl restart redis.service 重新启动服务
systemctl list-units --type=service 查看所有已启动的服务
systemctl status redis.service 查看服务当前状态
systemctl enable redis.service 设置开机自启动
systemctl disable redis.service 停止开机自启动
7.配置哨兵
复制配置文件:
cp sentinel.conf /etc/redis/
#重命名
cd /etc/redis/
mv sentinel.conf sentinel-26379.conf
编辑sentinel-26379.conf:
#端口
port 26379
#后台运行
daemonize yes
#pid文件
pidfile /var/run/redis-sentinel-26379.pid
#sentinel monitor <master-name> <ip> <redis-port> <quorum>
#master-name 名称
#ip 监控的ip
#redis-port 监控的端口
#quorum 只有当至少<quorum>哨兵同意时,监控的节点才会处于(客观上Down)状态
sentinel monitor mymaster 192.168.198.135 6379 2
#监控节点的密码
sentinel auth-pass mymaster 123456
#主服务器在30秒ping无响应,主观认为主服务器处于DOWN状态
sentinel down-after-milliseconds mymaster 30000
#故障转移超时时间
sentinel failover-timeout mymaster 180000
复制其它哨兵:
cp sentinel-26379.conf sentinel-26380.conf
cp sentinel-26379.conf sentinel-26381.conf
修改关键配置:
port 26380
pidfile /var/run/redis-sentinel-26380.pid
8.启动哨兵
cd /usr/local/redis/bin
[root@localhost bin]# ./redis-sentinel /etc/redis/sentinel-26379.conf
7033:X 05 May 2022 02:41:50.381 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
7033:X 05 May 2022 02:41:50.381 # Redis version=5.0.7, bits=64, commit=00000000, modified=0, pid=7033, just started
7033:X 05 May 2022 02:41:50.381 # Configuration loaded
[root@localhost bin]# ps aux|grep redis
root 5232 0.0 0.2 147456 9752 ? Ssl 01:21 0:03 ./redis-server 0.0.0.0:6379
root 5237 0.0 0.2 147456 9684 ? Ssl 01:22 0:03 ./redis-server 0.0.0.0:6380
root 5633 0.0 0.2 147456 9680 ? Ssl 01:38 0:02 ./redis-server 0.0.0.0:6381
root 7034 0.0 0.0 144224 2256 ? Ssl 02:41 0:00 ./redis-sentinel *:26379 [sentinel]
root 7039 0.0 0.0 112808 968 pts/1 R+ 02:42 0:00 grep --color=auto redis
[root@localhost bin]# ./redis-sentinel /etc/redis/sentinel-26380.conf
7040:X 05 May 2022 02:42:16.525 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
7040:X 05 May 2022 02:42:16.525 # Redis version=5.0.7, bits=64, commit=00000000, modified=0, pid=7040, just started
7040:X 05 May 2022 02:42:16.525 # Configuration loaded
[root@localhost bin]# ./redis-sentinel /etc/redis/sentinel-26381.conf
7045:X 05 May 2022 02:42:20.662 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
7045:X 05 May 2022 02:42:20.662 # Redis version=5.0.7, bits=64, commit=00000000, modified=0, pid=7045, just started
7045:X 05 May 2022 02:42:20.662 # Configuration loaded
[root@localhost bin]# ps aux|grep redis
root 5232 0.0 0.2 147456 9752 ? Ssl 01:21 0:03 ./redis-server 0.0.0.0:6379
root 5237 0.0 0.2 147456 9684 ? Ssl 01:22 0:03 ./redis-server 0.0.0.0:6380
root 5633 0.0 0.2 147456 9680 ? Ssl 01:38 0:02 ./redis-server 0.0.0.0:6381
root 7034 0.1 0.0 144260 2416 ? Ssl 02:41 0:00 ./redis-sentinel *:26379 [sentinel]
root 7041 0.2 0.0 144288 2452 ? Ssl 02:42 0:00 ./redis-sentinel *:26380 [sentinel]
root 7046 0.2 0.0 144292 2464 ? Ssl 02:42 0:00 ./redis-sentinel *:26381 [sentinel]
root 7051 0.0 0.0 112808 968 pts/1 R+ 02:42 0:00 grep --color=auto redis
错误:
*** FATAL CONFIG FILE ERROR ***
Reading the configuration file, at line 84
>>> 'sentinel myid 0cb49d967198d7804ac5ad21b589468b1ca9eab8'
sentinel directive while not in sentinel mode
需要重新启动所有哨兵
9.主节点故障,从节点6380成为主节点
[root@localhost bin]# ps aux|grep redis
root 4487 0.0 0.0 145380 2320 ? Ssl 20:11 0:00 ./redis-server 192.168.198.135:6379
root 4492 0.0 0.0 145360 2296 ? Ssl 20:11 0:00 ./redis-server 192.168.198.135:6380
root 4498 0.0 0.0 145360 2300 ? Ssl 20:11 0:00 ./redis-server 192.168.198.135:6381
root 4530 0.1 0.0 144284 2400 ? Ssl 20:12 0:00 ./redis-sentinel *:26379 [sentinel]
root 4535 0.1 0.0 144232 2288 ? Ssl 20:13 0:00 ./redis-sentinel *:26380 [sentinel]
root 4540 0.1 0.0 144232 2268 ? Ssl 20:13 0:00 ./redis-sentinel *:26381 [sentinel]
关闭主节点6379
kill -9 4487
查看节点信息6380成为主节点:
[root@localhost bin]# ./redis-cli -p 26379 -h 192.168.198.135
192.168.198.135:26379> info
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.198.135:6380,slaves=2,sentinels=3
10.spring boot 哨兵模式连接配置
redis:
password: 123456
sentinel:
#master节点名称
master: mymaster
#哨兵列表
nodes: 192.168.198.135:26379,192.168.198.135:26380,192.168.198.135:26381