搭建Redis Sentinel集群
博客专区 > shelltea 的博客 > 博客详情
搭建Redis Sentinel集群
shelltea 发表于2年前
搭建Redis Sentinel集群
  • 发表于 2年前
  • 阅读 485
  • 收藏 19
  • 点赞 0
  • 评论 0

标题:腾讯云 新注册用户域名抢购1元起>>>   

下载Redis

当前最新版本为3.2.1

cd /opt
wget http://download.redis.io/releases/redis-3.2.1.tar.gz
tar zxf redis-3.2.1.tar.gz
ln -s redis-3.2.1 redis
cd redis
make
yum install tcl
make test

服务器

  • 100.31 master
  • 100.32 slave0
  • 100.33 slave1

配置

redis.conf

复制一份配置:cp redis.conf redis-100.31-6379.conf 修改以下配置:

修改前修改后
bind 127.0.0.1#bind 127.0.0.1
protected-mode yesprotected-mode no
daemonize nodaemonize yes
logfile ""logfile "/var/log/redis-100.31-6379.log"
dir ./dir "/usr/local/redis-data"
appendonly noappendonly yes

100.32和100.33的配置需要增加以下配置:

修改前修改后
slaveof 192.168.100.31 6379

并注意修改日志文件名

sentinel.conf

复制一份配置:cp sentinel.conf sentinel-100.31-26379.conf 修改或增加以下配置:

修改前修改后
sentinel monitor mymaster 127.0.0.1 6379 2sentinel monitor mymaster ${master ip} 6379 2
sentinel down-after-milliseconds mymaster 30000sentinel down-after-milliseconds mymaster 3000
protected-mode no
daemonize yes
logfile "/var/log/redis-sentinel-100.31-26379.log"

100.32和100.33的配置同上,但需要注意修改日志文件名

启动

启动Redis

在三台服务器上分别启动Redis

/usr/local/redis-3.2.1/src/redis-server /usr/local/redis-3.2.1/redis-100.31-6379.conf
/usr/local/redis-3.2.1/src/redis-server /usr/local/redis-3.2.1/redis-100.32-6379.conf
/usr/local/redis-3.2.1/src/redis-server /usr/local/redis-3.2.1/redis-100.33-6379.conf

查看日志:

tail -100f /var/log/redis-100.31-6379.log
tail -100f /var/log/redis-100.32-6379.log
tail -100f /var/log/redis-100.33-6379.log

启动Redis Sentinel

在三台服务器上分别启动Redis Sentinel

/usr/local/redis-3.2.1/src/redis-sentinel /usr/local/redis-3.2.1/sentinel-100.31-26379.conf
/usr/local/redis-3.2.1/src/redis-sentinel /usr/local/redis-3.2.1/sentinel-100.32-26379.conf
/usr/local/redis-3.2.1/src/redis-sentinel /usr/local/redis-3.2.1/sentinel-100.33-26379.conf

查看日志:

tail -100f /var/log/redis-sentinel-100.31-26379.log
tail -100f /var/log/redis-sentinel-100.32-26379.log
tail -100f /var/log/redis-sentinel-100.33-26379.log

自启动

Redis自启动

将下面的脚步写入/etc/rc.d/init.d/redis并执行chmod +x /etc/rc.d/init.d/redis

#!/bin/sh
# chkconfig: 2345 90 10
# description: Redis is a persistent key-value database
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

REDISPORT=6379
EXEC=/usr/local/redis-3.2.1/src/redis-server
CLIEXEC=/usr/local/redis-3.2.1/src/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/usr/local/redis-3.2.1/redis-100.31-${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
        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

注意脚本中的配置文件路径

然后执行

chkconfig redis on
chkconfig --add redis
chkconfig --list redis

之后就可以用service redis start/stop对Redis进行控制

Redis Sentinel自启动

将下面的脚步写入/etc/rc.d/init.d/redis-sentinel并执行chmod +x /etc/rc.d/init.d/redis-sentinel

#!/bin/sh
# chkconfig: 2345 90 10
# description: Redis is a persistent key-value database
# Redis Sentinel startup script

REDISPORT=26379
EXEC=/usr/local/redis-3.2.1/src/redis-sentinel
CLIEXEC=/usr/local/redis-3.2.1/src/redis-cli

PIDFILE=/var/run/redis.pid
CONF="/usr/local/redis-3.2.1/sentinel-100.31-${REDISPORT}.conf"

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

注意脚本中的配置文件路径

然后执行

chkconfig redis-sentinel on
chkconfig --add redis-sentinel
chkconfig --list redis-sentinel

之后就可以用service redis-sentinel start/stop对Redis Sentinel进行控制

问题汇总

The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

To fix this warning you have to set a new config to /etc/rc.local so that the setting will persist upon reboot

$~: sudo nano /etc/rc.local
Add this:

sysctl -w net.core.somaxconn=65535
When you reboot the next time, the new setting will be to allow 65535 connections instead of 128 as before.

WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.

echo 'vm.overcommit_memory = 1' >> /etc/sysctl.conf

WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.

This is also simple to fix by just running the recommended command as stated in the warning.

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

$~: sudo nano /etc/rc.local
Add this:

echo never > /sys/kernel/mm/transparent_hugepage/enabled
Now this will be persistent upon reboot as well.

参考

标签: redis sentinel
共有 人打赏支持
shelltea
粉丝 5
博文 4
码字总数 2039
×
shelltea
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: