Linux环境安装Redis-哨兵模式( sentinel mode)
环境信息
系统版本信息
cat /proc/version
Linux version 4.18.0-348.el8.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 8.5.0 20210514 (Red Hat 8.5.0-3) (GCC)) #1 SMP Tue Oct 19 15:14:17 UTC 2021
redis软件信息
下载地址:https://download.redis.io/releases/redis-6.0.18.tar.gz?_gl=1*1r1mldx*_ga*MTQ2NDM5NTI5Ny4xNjc3OTAzNDM5*_ga_8BKGRQKRPV*MTY3NzkwMzQzOS4xLjEuMTY3NzkwMzYzMi42MC4wLjA.
安装Redis
1.上传安装包到服务器。
2.解压 tar -xzvf redis-6.0.18.tar.gz
3. 进入到redis的解压目录下,编译redis,默认是"/usr/local/bin"
//1.修改redis安装路径
cd redis-6.0.18/src
vi Makefile
#PREFIX?=/usr/local
PREFIX?=$HOME/redis 修改为自己想要安装的目录
//2. 编译redis
make install
- 安装校验
cd $HOME/redis/bin
./redis-server 启动服务段
./redis-cli 启动客户端
sentinel-mode配置
- 拷贝配置文件
[ysp@localhost ~]$ cd redis/bin/config/
[ysp@localhost config]$ cp /home/ysp/Sources/redis-6.0.18/*.conf .
[ysp@localhost config]$ ls -ltr
total 96
-rw-rw-r--. 1 ysp ysp 10744 Mar 4 13:19 sentinel.conf
-rw-rw-r--. 1 ysp ysp 85560 Mar 4 13:19 redis.conf
[ysp@localhost config]$
- 构建sentinel集群模式目录
[ysp@localhost redis]$ ls -ltr
total 0
drwxrwxr-x. 3 ysp ysp 164 Mar 4 13:18 bin
drwxrwxr-x. 2 ysp ysp 6 Mar 4 13:21 redis-sentinel
[ysp@localhost redis]$ mkdir -p redis-sentinel/master-node
[ysp@localhost redis]$ mkdir -p redis-sentinel/slave-node1
[ysp@localhost redis]$ mkdir -p redis-sentinel/slave-node2
[ysp@localhost redis]$ cp ./bin/* redis-sentinel/master-node
cp: -r not specified; omitting directory './bin/config'
[ysp@localhost redis]$ cp -rf ./bin/* redis-sentinel/master-node
[ysp@localhost redis]$ cp -rf ./bin/* redis-sentinel/slave-node1
[ysp@localhost redis]$ cp -rf ./bin/* redis-sentinel/slave-node2
[ysp@localhost redis]$ cd redis-sentinel/
[ysp@localhost redis-sentinel]$ ls
master-node slave-node1 slave-node2
[ysp@localhost redis-sentinel]$ pwd
/home/ysp/redis/redis-sentinel
[ysp@localhost redis-sentinel]$
- 配置master
[ysp@localhost config]$ pwd
/home/ysp/redis/redis-sentinel/master-node/config
[ysp@localhost config]$ vi redis.conf
#注释掉,让外界机器可以访问
#bind 127.0.0.1
protected-mode no
#修改PID文件存储路径
pidfile /home/ysp/redis/redis-sentinel/master-node/redis_6379.pid
#修改日志存放路径
logfile "/home/ysp/redis/redis-sentinel/master-node/redis_6379.log"
- 配置哨兵,(多个哨兵在同一台机器需要修改端口)
[ysp@localhost config]$ pwd
/home/ysp/redis/redis-sentinel/master-node/config
[ysp@localhost config]$ vi sentinel.conf
[ysp@localhost config]$
daemonize yes
logfile "/home/ysp/redis/redis-sentinel/master-node/redis_6379.log"
pidfile /home/ysp/redis/redis-sentinel/master-node/redis-sentinel.pid
logfile "/home/ysp/redis/redis-sentinel/master-node/redis-sentinel.log"
#主节点
sentinel monitor mymaster 127.0.0.1 6379 2
- 从节点,(多个从节点在同一台机器需要修改端口)
#bind 127.0.0.1
protected-mode no
slaveof 127.0.0.1 6379
port 6380
logfile "/home/ysp/redis/redis-sentinel/slave-node1/redis_6380.log"
pidfile "/home/ysp/redis/redis-sentinel/master-node/redis_6380.log"
6.启动redis服务
# 1.启动主节点
[ysp@localhost master-node]$ nohup ./redis-server config/redis.conf 2>&1 >/dev/null &
[ysp@localhost master-node]$ nohup: ignoring input and redirecting stderr to stdout
#2.启动从节点1
[ysp@localhost slave-node1]$ nohup ./redis-server config/redis.conf 2>&1 >/dev/null &
[ysp@localhost slave-node1]$ nohup: ignoring input and redirecting stderr to stdout
#3.启动从节点2
[ysp@localhost slave-node2]$ nohup ./redis-server config/redis.conf 2>&1 >/dev/null &
#4. 启动哨兵1
[ysp@localhost slave-node2]$ nohup ./redis-sentinel config/sentinel.conf 2>&1 >/dev/null &
#5.启动哨兵2
[ysp@localhost slave-node2]$ nohup ./redis-sentinel config/sentinel.conf 2>&1 >/dev/null &
#6.启动哨兵3
[ysp@localhost master-node]$ nohup ./redis-sentinel config/sentinel.conf 2>&1 >/dev/null &
#7. 启动检查
[ysp@localhost master-node]$ ps -ef|grep redis
ysp 40260 40036 0 14:05 pts/1 00:00:00 ./redis-server *:6379
ysp 40270 40036 0 14:07 pts/1 00:00:00 ./redis-server *:6380
ysp 40282 40036 0 14:08 pts/1 00:00:00 ./redis-server *:6381
ysp 40291 1 0 14:11 ? 00:00:00 ./redis-sentinel *:26381 [sentinel]
ysp 40303 1 0 14:13 ? 00:00:00 ./redis-sentinel *:26379 [sentinel]
ysp 40314 1 0 14:14 ? 00:00:00 ./redis-sentinel *:26380 [sentinel]
ysp 40320 40036 0 14:14 pts/1 00:00:00 grep --color=auto redis
哨兵模式检查
- 主节点即可写又可读,从节点只能读。