redis哨兵模式搭建

1.下载redis安装包

a. wget http://download.redis.io/releases/redis-5.0.5.tar.gz

b.解压 
tar -xvf redis-5.0.5.tar.gz
cd redis-5.0.5
make
make install

2.搭建一主二从三哨兵配置文件

主redis配置文件

redis_master.conf

bind 192.168.3.117
port 6379
daemonize yes
dir /data/redis_master
logfile /data/redis/redis_master.log

二从配置文件

redis_slave_one.conf

bind 192.168.3.117
port 6380
daemonize yes
dir /data/redis/redis_slave_one
logfile /data/redis/redis_slave_one.log
slaveof 192.168.3.117 6379

---------------------------------------

redis_slave_two.conf

bind 192.168.3.117
port 6381
daemonize yes
dir /data/redis/redis_slave_two
logfile /data/redis/redis_slave_two.log
slaveof 192.168.3.117 6379

三守护配置文件

master的守护者文件配置

bind 192.168.3.117
port 26379
sentinel myid f8c2ae332072ddbd62e778e5ffbb33fcc6fc786e
dir "/data/redis/master_sentinel"
logfile "/data/redis/sentinel/master_sentinel.log"
daemonize yes

-------------------------------------

slave_one的守护者配置文件

bind 192.168.3.117
port 26380
sentinel myid d47b5dc1d048e08cd400115ead7407f276ac9dbc
dir "/data/redis/slave_one_sentinel"
logfile "/data/redis/sentinel/slave_two_sentinel.log"
daemonize yes

------------------------------------

slave_two的守护者配置文件

bind 192.168.3.117
port 26381
sentinel myid bf65ebc664402c2faca875f080e1e52fd2010688
dir "/data/redis/slave_two_sentinel"
logfile "/data/redis/sentinel/slave_two_sentinel.log"
daemonize yes

3.启动

启动主redis:
/usr/local/redis-5.0.5/src/redis-server /etc/redis/redis_master.conf

启动两从:
/usr/local/redis-5.0.5/src/redis-server /etc/redis/redis_slave_one.conf

/usr/local/redis-5.05/src/redis-server /etc/redis/redis_slave_two.conf

启动主Redis的哨兵:
/user/local/redis-5.0.5/src/redis-sentinel /etc/redis/redis_master_sentinel.conf
也可
/user/local/redis-5.0.5/src/redis-server /etc/redis/redis_master_sentinel.conf --sentinel

启动两从Redis的哨兵:
/usr/local/redis-5.0.5/src/redis-sentinel /etc/redis/redis_slave_one_sentinel.conf

/usr/local/redis-5.0.5/src/redis-sentinel /etc/redis/redis_slave_two_sentinel.conf

4.结果

redis哨兵模式搭建_第1张图片

5.redis配置参数说明

bind 192.168.3.117  绑定到主机ip
port 6379 服务端口号
daemonize yes/no 是否后台运行 
pidfile /var/run/redis.pid 当以守护进程运行时,pid可以写入pid文件中
timeout 300 客户端连接多长时间关闭,如果为0则不断开
tcp-keepalive 0 设置客户端网络连接中断时间,为0则不检测
databases 16 默认有16个库
save 300 10 300秒内,有10个更改就保存一次
dir rdb aof备份文件放置位置
logfile /data/redis/redis_master.log 日志文件所在位置
slaveof 192.168.3.117 6379 从节点关联的主节点的ip地址及端口号
sentinel myid f8c2ae332072ddbd62e778e5ffbb33fcc6fc786e 守护redis id
sentinel monitor mymaster 192.168.3.117 6380 2 代表该哨兵监视192.168.3.117 6380的redis,当哨兵选举达到2个后,选举新的主节点
sentinel down-after-milliseconds mymaster 3000当哨兵之间互相ping,ttl大于3s后,确认不可达
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000

6.启动脚本

#!/bin/sh
#Configurations injected by install_server below....

EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/data/redis/redis.pid
CONF="/data/redis/redis.conf"
REDISPORT="16379"

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
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
            echo "$PIDFILE does not exist, process is not running"
        else
            PID=$(cat $PIDFILE)
            echo "Stopping ..."
            $CLIEXEC -p $REDISPORT shutdown
            while [ -x /proc/${PID} ]
            do
                echo "Waiting for Redis to shutdown ..."
                sleep 1
            done
            echo "Redis stopped"
        fi
        ;;
    status)
        PID=$(cat $PIDFILE)
        if [ ! -x /proc/${PID} ]
        then
            echo 'Redis is not running'
        else
            echo "Redis is running ($PID)"
        fi
        ;;
    restart)
        $0 stop
        $0 start
        ;;
    *)
        echo "Please use start, stop, restart or status as first argument"
        ;;
esac

哨兵的启动脚本

#!/bin/sh
PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/redis/.local/bin:/home/redis/bin

nohup /usr/local/bin/redis-sentinel /data/redis/sentinel.conf 2>&1 &

ps -ef |grep redis

你可能感兴趣的:(redis)