CentOS8系统Redis哨兵安装

前提条件

安装依赖包

sudo yum -y install gcc automake autoconf libtool make tcl

编辑/etc/sysctl.conf

sudo vi /etc/sysctl.conf

添加以下内容:

net.core.somaxconn = 1024
vm.overcommit_memory = 1

执行以下命令立即生效:

sudo sysctl -p

切换为root,并执行以下命令:

echo never > /sys/kernel/mm/transparent_hugepage/enabled
source /etc/rc.local

安装Redis

wget http://download.redis.io/redis-stable.tar.gz
sudo cp redis-stable.tar.gz /opt
cd /opt
sudo tar zxvf redis-stable.tar.gz
sudo mv redis-stable redis
cd /opt/redis
make
make test
sudo make install

配置Redis

创建Redis配置目录和数据目录:

sudo mkdir /etc/redis
sudo mkdir /opt/redis-data

配置Master

创建数据目录:

sudo mkdir /opt/redis-data/6381

复制启动脚本:

sudo cp utils/redis_init_script /etc/init.d/redis_6381

编辑启动脚本:

sudo vi /etc/init.d/redis_6381

修改REDISPORT,并设置redis密码:

#!/bin/sh
#
# 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=6381
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.conf"

PASSWORD=您的密码

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 -a $PASSWORD shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac

复制配置文件:

sudo cp /opt/redis/redis.conf /etc/redis/6381.conf

修改配置文件:

sudo vi /etc/redis/6381.conf

更改以下配置:

# 后台启动
daemonize yes
# 设置端口
port 6381
# 设置pidfile
pidfile /var/run/redis_6381.pid
# 注释bind,允许远程访问
# bind 127.0.0.1
# 修改密码
requirepass 您的密码
masterauth 您的密码
# 关闭protected-mode模式,允许外网访问
protected-mode no
# 设置日志级别
loglevel notice
# 设置日志文件
logfile /var/log/redis_6381.log
# 设置redis数据存储目录
dir /opt/redis-data/6381

设置开机启动:

sudo chkconfig --add redis_6381

开放端口:

sudo firewall-cmd --zone=public --add-port=6381/tcp --permanent
sudo firewall-cmd --reload

启动redis:

sudo systemctl start redis_6381

配置Slave

创建数据目录:

sudo mkdir /opt/redis-data/6382

复制启动脚本:

sudo cp utils/redis_init_script /etc/init.d/redis_6382

编辑启动脚本:

sudo vi /etc/init.d/redis_6382

修改REDISPORT,并设置redis密码:

#!/bin/sh
#
# 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=6382
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.conf"

PASSWORD=您的密码

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 -a $PASSWORD shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac

复制配置文件:

sudo cp /opt/redis/redis.conf /etc/redis/6382.conf

修改配置文件:

sudo vi /etc/redis/6382.conf

更改以下配置:

# 后台启动
daemonize yes
# 设置端口
port 6382
# 设置pidfile
pidfile /var/run/redis_6382.pid
# 注释bind,允许远程访问
# bind 127.0.0.1
# 修改密码
requirepass 您的密码
masterauth 您的密码
# 关闭protected-mode模式,允许外网访问
protected-mode no
# 设置日志级别
loglevel notice
# 设置日志文件
logfile /var/log/redis_6382.log
# 设置redis数据存储目录
dir /opt/redis-data/6382
# 设置主从关系
slaveof master的ip 6381

设置开机启动:

sudo chkconfig --add redis_6382

开放端口:

sudo firewall-cmd --zone=public --add-port=6382/tcp --permanent
sudo firewall-cmd --reload

启动redis:

sudo systemctl start redis_6382

确定主从关系

redis-cli -h master的ip -p 6381 -a 您的密码 info replication
redis-cli -h salve的ip -p 6382 -a 您的密码 info replication

部署Sentinel

3个Sentinel节点的部署方法是完全一致的(端口不同),以其中一个为例进行说明。

创建数据目录:

sudo mkdir -p /opt/redis-sentinel-data/26381

复制启动脚本:

sudo cp utils/redis_init_script /etc/init.d/redis_sentinel_26381

编辑启动脚本:

sudo vi /etc/init.d/redis_sentinel_26381

配置如下:

#!/bin/sh
#
# 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=26381
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli

PIDFILE=/var/run/redis-sentinel_${REDISPORT}.pid
CONF="/etc/redis/sentinel_${REDISPORT}.conf"

case "$1" in
    start)
        if [ -f $PIDFILE ]
        then
                echo "$PIDFILE exists, process is already running or crashed"
        else
                echo "Starting Redis server..."
                $EXEC $CONF --sentinel
        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
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac

设置开机启动:

sudo chkconfig --add redis_sentinel_26381

开放端口:

sudo firewall-cmd --zone=public --add-port=26381/tcp --permanent
sudo firewall-cmd --reload

配置Sentinel.conf:

sudo cp /opt/redis/sentinel.conf /etc/redis/sentinel_26381.conf

修改配置文件:

sudo vi /etc/redis/sentinel_26381.conf

修改以下配置:

protected-mode no
port 26381
daemonize yes
pidfile /var/run/redis-sentinel_26381.pid
logfile /var/log/redis-sentinel_26381.log
dir /opt/redis-sentinel-data/26381
sentinel monitor 请输入master名称 master的ip 6381 2
sentinel auth-pass 请输入master名称 您的密码
sentinel down-after-milliseconds 请输入master名称 30000
sentinel parallel-syncs 请输入master名称 1
sentinel failover-timeout 请输入master名称 180000
min-replicas-to-write 1
min-replicas-max-lag 10

启动Sentinel:

systemctl start redis_sentinel_26381

确定哨兵关系

redis-cli -h 哨兵1的ip -p 26381 info Sentinel
redis-cli -h 哨兵2的ip -p 26382 info Sentinel
redis-cli -h 哨兵3的ip -p 26381 info Sentinel

未经允许不得转载:君子如兰 » CentOS8系统Redis哨兵安装

赞 (0)