Redis配置哨兵開機自啟動


  • Redis哨兵

啟停命令

# 啟動3台redis
cd /opt/redis
./redis-server redis.conf

# 啟動3台sentinel
cd /opt/redis
./redis-sentinel sentinel.conf

測試sentinel模式

cd /opt/redis
./redis-cli -h master的ip

192.168.0.205:6379> auth 設置的密碼
192.168.0.205:6379> info
  • 方案一
    腳本放到/etc/init.d/redisSentinel
    添加開機自啟動:
# 1.編輯rc.local文件
vi /etc/rc.local
# 2.添加redisSentinel 啟動命令
/etc/init.d/redisSentinel restart
# 3.最后修改rc.local文件的執行權限--一定要執行
chmod +x  /etc/rc.local
chmod 755 /etc/rc.local
vim /etc/init.d/redisSentinel
#!/bin/sh
#
# chkconfig: 2345 10 90
# description: Start and Stop redis
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

### BEGIN INIT INFO
# Provides:     redis_6379
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    Redis data structure server
# Description:          Redis data structure server. See https://redis.io
### END INIT INFO


REDISPORT=6379
REDIS_PORT=26379
EXEC=/opt/redis/redis-server
CLIEXEC=/opt/redis/redis-cli
EXEC_SENTINEL=/opt/redis/redis-sentinel
PIDFILE=/run/redis_${REDISPORT}.pid
#Redis配置文件位置
CONF="/opt/redis/redis.conf"
#sentinel 配置位置
#SlCONF="/opt/redis/sentinel.conf"
SlCONF="/opt/redis/sentinel.conf --sentinel"

case "$1" in
    start)
        if [ -f $PIDFILE ]
        then
                echo "$PIDFILE exists, process is already running or crashed"
        else
                echo "Starting Redis server..."
                $EXEC $CONF
                $EXEC $SlCONF
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                # -a '123' 這里是該redis服務密碼 關閉redis服務須要該密碼支持⚠️其他2台修改服務器IP
                $CLIEXEC -h "172.16.xx.xx" -a "123" -p $REDISPORT shutdown
                $CLIEXEC -h "172.16.xx.xx" -p $REDIS_PORT 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 Sentinel is not running'
    #    else
    #        echo "Redis Sentinel is running ($PID)"
    #    fi
    #    ;;
    restart)
        $0 stop
        $0 start
        ;;
    *)
        echo "Please use start, stop, restart or status as first argument"
        ;;
esac
chmod 777 redisSentinel
systemctl daemon-reload

# 啟動
service redisSentinel start
# 停止
service redisSentinel stop
# 重啟
service redisSentinel restart
### 查看狀態
### service redisSentinel status

配置 redis 啟動、停止腳步 redis.sh

#!/bin/bash
case $1 in
    "start")
        if(ps -ef | grep redis-server | grep -v grep > /dev/null); then
            echo "Redis is running!"
        else
            src/redis-server redis.conf
        fi
    ;;
    "stop")
        if(ps -ef | grep redis-server | grep -v grep > /dev/null); then
            kill -9 `ps -ef | grep redis-server | grep -v grep | awk '{print $2}'`
        else
            echo "Redis is not running!"
        fi
    ;;
    "restart")
        if(ps -ef | grep redis-server | grep -v grep > /dev/null); then
            kill -9 `ps -ef | grep redis-server | grep -v grep | awk '{print $2}'`
            src/redis-server redis.conf
        else
            echo "Redis is not running!"
            src/redis-server redis.conf
        fi
    ;;
    *)
        echo "Input Error! Example: sh redis.sh start | stop | restart"
    ;;
esac

配置 sentinel 啟動、停止腳步 sentinel.sh

#!/bin/bash
case $1 in
    "start")
        if(ps -ef | grep redis-sentinel | grep -v grep > /dev/null); then
            echo "Redis sentinel is running!"
        else
            src/redis-sentinel sentinel.conf
        fi
    ;;
    "stop")
        if(ps -ef | grep redis-sentinel | grep -v grep > /dev/null); then
            kill -9 `ps -ef | grep redis-sentinel | grep -v grep | awk '{print $2}'`
        else
            echo "Redis sentinel is not running!"
        fi
    ;;
    "restart")
        if(ps -ef | grep redis-sentinel | grep -v grep > /dev/null); then
            kill -9 `ps -ef | grep redis-sentinel | grep -v grep | awk '{print $2}'`
            src/redis-sentinel sentinel.conf
        else
            echo "Redis sentinel is not running!"
            src/redis-sentinel sentinel.conf
        fi
    ;;
    *)
        echo "Input Error! Example: sh redis.sh start | stop | restart"
    ;;
esac
#啟動redis實例
sh redis.sh start

#啟動sentinel實例
sh sentinel.sh start


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM