下载Redis
当前最新版本为3.2.1
cd /optwget http://download.redis.io/releases/redis-3.2.1.tar.gztar zxf redis-3.2.1.tar.gzln -s redis-3.2.1 rediscd redismakeyum install tclmake 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 yes | protected-mode no |
daemonize no | daemonize yes |
logfile "" | logfile "/var/log/redis-100.31-6379.log" |
dir ./ | dir "/usr/local/redis-data" |
appendonly no | appendonly 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 2 | sentinel monitor mymaster ${master ip} 6379 2 |
sentinel down-after-milliseconds mymaster 30000 | sentinel 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.logtail -100f /var/log/redis-100.32-6379.logtail -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.logtail -100f /var/log/redis-sentinel-100.32-26379.logtail -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=6379EXEC=/usr/local/redis-3.2.1/src/redis-serverCLIEXEC=/usr/local/redis-3.2.1/src/redis-cliPIDFILE=/var/run/redis_${REDISPORT}.pidCONF="/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 onchkconfig --add redischkconfig --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 scriptREDISPORT=26379EXEC=/usr/local/redis-3.2.1/src/redis-sentinelCLIEXEC=/usr/local/redis-3.2.1/src/redis-cliPIDFILE=/var/run/redis.pidCONF="/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 onchkconfig --add redis-sentinelchkconfig --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.localAdd this:sysctl -w net.core.somaxconn=65535When 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/enabledGo to /etc/rc.local$~: sudo nano /etc/rc.localAdd this:echo never > /sys/kernel/mm/transparent_hugepage/enabledNow this will be persistent upon reboot as well.