sentinel redis 集群部署+zabbix监控配置+性能测试+多实例自动配置脚本
一、集群架构:
二、服务器配置:
cpu:4核
内存:16G
硬盘:50G
三、安装命令:
cd /opt/redis/
ls
yum install tcl -y
tar xf redis-3.2.9.tar.gz
yum install gcc -y
ls
cd redis-3.2.9
ls
make
make test
ls
mkdir /etc/redis #存放redis配置文件
ln -s /opt/redis/redis-3.2.9/src/redis-cli /usr/sbin/redis-cli
ln -s /opt/redis/redis-3.2.9/src/redis-server /usr/sbin/redis-server
ln -s /opt/redis/redis-3.2.9/src/redis-sentinel /usr/sbin/redis-sentinel
四、配置文件信息:
1、redis 从节点配置文件信息,将该文件保存到/etc/redis目录下
bind 0.0.0.0
protected-mode no
port 4379
tcp-backlog 511
timeout 300
tcp-keepalive 300
#requirepass redis
daemonize yes
supervised no
pidfile "/var/run/redis_4379.pid"
loglevel notice
logfile ""
databases 16
#save 900 1
#save 300 10
#save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
dir "/opt/redis-3.2.1"
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
# Generated by CONFIG REWRITE
maxmemory 200mb
maxmemory-policy noeviction
slaveof xx.xx.xx.xx 4379 #主节点将这一行注释掉
2、sentinel 配置文件
port 14379
daemonize yes
protected-mode no
dir "/opt/redis-3.2.1"
logfile "/var/log/sentinel.log"
sentinel monitor r1 xx.xx.xx.xx 4379 2
sentinel down-after-milliseconds r1 20000
sentinel parallel-syncs r1 1
sentinel failover-timeout r1 30000
# Generated by CONFIG REWRITE
3、redis 启动、关闭脚本,将该文件放到/etc/init.d/目录下,并给与执行权限
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
REDISPORT=4379
EXEC=/usr/sbin/redis-server
CLIEXEC=/usr/sbin/redis-cli
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/redis.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
五、启动redis和sentinel:
/etc/init.d/redis start
/usr/sbin/redis-sentinel /etc/redis/sentinel.conf
六、redis多实例自动配置脚本
#!/bin/bash
set -e
#获取端口号
[ $# -ne 1 ] && echo "Please enter the port number (For example: ./redis-add.sh 4379)" && exit 1
#RP=`echo $1`
RP=$1
#查看配置文件是否存在
if [ -f /etc/redis/redis_$RP.conf ];then
echo "Warning:There are already exists /etc/redis/redis_$RP.conf,Please check" && exit 1
elif [ -f /etc/redis/sentinel_$RP.conf ];then
echo "Warning:There are already exists /etc/redis/sentinel_$RP.conf,Please check" && exit 1
elif [ -f /etc/init.d/redis_$RP ];then
echo "Warning:There are already exists /etc/init.d/redis_$RP,Please check" && exit 1
fi
#生成配置文件
/bin/cp /etc/redis/bak/redis.conf.bak /etc/redis/redis_$RP.conf
/bin/cp /etc/redis/bak/sentinel.conf.bak /etc/redis/sentinel_$RP.conf
/bin/cp /etc/init.d/redis /etc/init.d/redis_$RP
#修改配置文件
SRP=$(( $RP + 10000 ))
#echo $SRP
sed -i s/4379/$RP/ /etc/redis/redis_$RP.conf
sed -i s/4379/$RP/ /etc/redis/sentinel_$RP.conf
sed -i s/r1/r$RP/ /etc/redis/sentinel_$RP.conf
sed -i s/sentinel.log/sentinel_$SRP.log/ /etc/redis/sentinel_$RP.conf
sed -i s/14379/$SRP/ /etc/redis/sentinel_$RP.conf
sed -i s/4379/$RP/ /etc/init.d/redis_$RP
sed -i s/redis.conf/redis_$RP.conf/ /etc/init.d/redis_$RP
#启动redis和sentinel
echo "/etc/init.d/redis_$RP start" >> /etc/rc.d/rc.local
echo "/usr/sbin/redis-sentinel /etc/redis/sentinel_$RP.conf" >> /etc/rc.d/rc.local
/etc/init.d/redis_$RP start
wait
/usr/sbin/redis-sentinel /etc/redis/sentinel_$RP.conf
七、zabbix监控脚本:
#!/bin/bash
HOST="127.0.0.1"
PORT=$1
# 检测redis性能
function Clients {
/usr/sbin/redis-cli -h $HOST -p $PORT info Clients | awk -F':' 'NR==2{print $2}'
}
function Memory {
used_memory=`/usr/sbin/redis-cli -h $HOST -p $PORT info Memory | awk -F':' 'NR==2{print $2}'| grep -o "[0-9]\+"`
max_memory=`/usr/sbin/redis-cli -h $HOST -p $PORT info Memory | awk -F':' 'NR==12{print $2}'| grep -o "[0-9]\+"`
usage_memory=`expr $used_memory \* 100 / $max_memory`
echo $usage_memory
}
function Slowlog {
/usr/sbin/redis-cli -h $HOST -p $PORT slowlog len | awk '{print $1}'
}
function Info {
/usr/sbin/redis-cli -h $HOST -p $PORT info Server &>/tmp/info.txt
INFO=`cat /tmp/info.txt | awk 'NR==1{print $2}' | grep -o -i "[a-z]\+"`
if [ "$INFO" == "Server" ];then
INFO1=1
else
INFO1=0
fi
echo $INFO1
}
# 执行function
$2
八、性能测试:
cd /opt/redis/redis-3.2.9/src/
./redis-benchmark -h xx.xx.xx.xx -p 6379 -c 30 -n 100000 -q > /opt/redis/log/30.log
九、参考文档:
http://www.cnblogs.com/janehoo/p/6118961.html