redis主从+哨兵模式搭建

redis安装

rpm或deb包安装

源码安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#加压缩
tar -zxvf redis-5.0.10.tar.gz

#编译和编译安装
cd redis-5.0.10
make && make install

#移动编译结果和配置文件到/usr/local/redis目录下
mkdir -p /usr/local/redis
cp src/mkreleasehdr.sh /usr/local/redis/
cp src/redis-benchmark /usr/local/redis/
cp src/redis-check-aof /usr/local/redis/
cp src/redis-check-rdb /usr/local/redis/
cp src/redis-cli /usr/local/redis/
cp src/redis-sentinel /usr/local/redis/
cp src/redis-server /usr/local/redis/
cp src/redis-trib.rb /usr/local/redis/
cp src/redis-check-rdb /usr/local/redis/
cp redis.conf /usr/local/redis/
cp sentinel.conf /usr/local/redis/

注册为服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
vi /etc/init.d/redis

#!/bin/sh
# chkconfig: 2345 10 90
# description: Start and Stop redis

PORT=6379
REDISDIR=/usr/local/redis
EXEC=$REDISDIR/redis-server
REDIS_CLI=$REDISDIR/redis-cli
CONF=$REDISDIR/redis.conf
PASSWD="***"
PIDFILE=/var/run/redis_6379.pid

case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed."
else
echo "Starting Redis server..."
$EXEC $CONF
fi
if [ "$?"="0" ]
then
echo "Redis is running..."
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE exists, process is not running."
else
PID=$(cat $PIDFILE)
echo "Stopping..."
$REDIS_CLI -p $PORT -a $PASSWD SHUTDOWN
sleep 2
while [ -x $PIDFILE ]
do
echo "Waiting for Redis to shutdown..."
sleep 1
done
echo "Redis stopped"
fi
;;
restart|force-reload)
${0} stop
${0} start
;;
*)
echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2
exit 1
esac

赋权

1
chmod +x /etc/init.d/redis

开机自启

1
chkconfig redis on

修改redis.conf

  1. 主从服务器redis.conf文件都需要修改以下配置:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    #后台运行
    - daemonize no
    + daemonize yes

    #关闭保护模式,允许远程访问
    - protected-mode yes
    + protected-mode no

    #注释ip绑定
    - bind 127.0.0.1
    + #bind 127.0.0.1

    #连接密码(可不填
    + requirepass ***

    #master密码(若填了连接密码时需要填写
    + masterauth ***
  2. 从服务器redis.conf文件在上面的修改基础上还需要修改以下配置:

    1
    + replicaof 主服务器ip 6379

修改sentinel.conf

备份sentinel.conf文件

1
mv sentinel.conf sentinel.conf.bak

新建sentinel.conf文件

1
vi sentinel.conf

编辑主从服务器上的sentinel.conf文件

1
2
3
4
5
6
7
8
9
daemonize yes
port 26379
protected-mode no
sentinel monitor redis-master 主服务器ip 6379 1
sentinel down-after-milliseconds redis-master 30000
sentinel failover-timeout redis-master 180000
sentinel parallel-syncs redis-master 1
#若redis设置了密码则进行下面的设置
sentinel auth-pass redis-master ***

启动主从服务器

主从服务器上分别执行:

1
2
./redis-server redis.conf
./redis-sentinel sentinel.conf

检查主服务器集群状态:

1
2
3
4
5
6
./redis-cli 
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=10.10.10.5,port=6379,state=online,offset=17460264,lag=1

检查从服务器集群状态:

1
2
3
4
5
6
7
./redis-cli 
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:10.10.10.4
master_port:6379
master_link_status:up

redis主从+哨兵模式搭建
https://blog.kedr.cc/posts/1518701644/
作者
zhuweitung
发布于
2021年4月25日
许可协议