Redis
下载
yum -y install gcc
wget http://download.redis.io/releases/redis-4.0.6.tar.gz
tar xzf redis-4.0.6.tar.gz
cd redis-4.0.6
make
创建6个节点
mkdir -p redis0{1,2,3,4,5,6}
cp /root/soft/redisCluster/redis-cluster/redis.conf redis01/
cp /root/soft/redisCluster/redis-cluster/redis.conf redis02/
cp /root/soft/redisCluster/redis-cluster/redis.conf redis03/
cp /root/soft/redisCluster/redis-cluster/redis.conf redis04/
cp /root/soft/redisCluster/redis-cluster/redis.conf redis05/
cp /root/soft/redisCluster/redis-cluster/redis.conf redis06/
修改配置
#//设置Master密码
masterauth redispassword
#//设置密码
requirepass redispassword
#//设置对完连接IP
bind x.x.x.x
#//六个节点配置文件分别是7001-7006
port 7001~7006
#//守护进程方式运行
daemonize yes
#//pid文件对应7001-7006
pidfile /var/run/redis-(7001~7006).pid
#//开启集群
cluster-enabled yes
#//集群节点配置,自动创建,自动更新对应7001-7006
cluster-config-file nodes-(7001~7006.conf
#//集群超时时间,节点超过这个时间没反应就断定是宕机
cluster-node-timeout 5000
#存储方式,aof,将写操作记录保存到日志中
appendonly yes
RUBY
yum下载
yum install rubygems
#////安装redis插件
gem install redis
修改client.rb配置
client.rb路径可以通过find命令查找:find / -name 'client.rb'
class Client
DEFAULTS = {
:url => lambda { ENV["REDIS_URL"] },
:scheme => "redis",
:host => "127.0.0.1",
:port => 6379,
:path => nil,
:timeout => 5.0,
:password => "redispassword",
:db => 0,
:driver => nil,
:id => nil,
:tcp_keepalive => 0,
:reconnect_attempts => 1,
:inherit_socket => false
}
搭建集群
拷贝Ruby脚本
cp /root/soft/redisCluster/redis-cluster/src/redis-trib.rb /root/soft/redisCluster/redis-cluster/redis_cluster/
启动脚本
redisOps.sh
#!/bin/bash
# author : Mood
# 启动监控当前单机伪redis集群进程。如果存活个数=集群机器个数,则不做处理,如果小于集群机器总的的一半,集群还可以提供访问,需要打印集群机器进程状态,如果当前集群机器活跃总数等于0,则需要启动集群内部的所有机器,如果为RED,集群无法对外提供服务, 需要打印集群机器进程状态,
# redis集群 路径
REDIS_CLUSTER_PATH=$1
# redis集群内节点个数
CLUSTER_NODE_NUMBER=6
# redis集群做小可用个数
CLUSTER_NODE_NUMBER_MIN=3
# 当前活跃节点个数
RUNNING_NODE_NUMBER=0
THIS_RUNNING=`ps -ef | grep redis | grep -v grep | awk '{print $9}' | grep -Po '\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}' | awk '{sum[$1]+=1} END {for(k in sum) print sum[k]}'`
REDIS_O1=`ps -ef |grep redis |egrep -v "grep"|grep ${REDIS_CLUSTER_PATH} |grep "7001" |awk '{print $2}'`
REDIS_O2=`ps -ef |grep redis |egrep -v "grep"|grep ${REDIS_CLUSTER_PATH} |grep "7002" |awk '{print $2}'`
REDIS_O3=`ps -ef |grep redis |egrep -v "grep"|grep ${REDIS_CLUSTER_PATH} |grep "7003" |awk '{print $2}'`
REDIS_O4=`ps -ef |grep redis |egrep -v "grep"|grep ${REDIS_CLUSTER_PATH} |grep "7004" |awk '{print $2}'`
REDIS_O5=`ps -ef |grep redis |egrep -v "grep"|grep ${REDIS_CLUSTER_PATH} |grep "7005" |awk '{print $2}'`
REDIS_O6=`ps -ef |grep redis |egrep -v "grep"|grep ${REDIS_CLUSTER_PATH} |grep "7006" |awk '{print $2}'`
# 启动redis节点
startAllRedis(){
if [[ ! -n ${REDIS_CLUSTER_PATH} ]]; then
echo -e "REDIS_CLUSTER_PATH IS NULL : \033[31m NULL \033[0m"
exit ;
else
if [[ ${CLUSTER_NODE_NUMBER} == ${THIS_RUNNING} ]]; then
echo -e "REDIS_CLUSTER STATUS : \033[32m GREEN \033[0m"
elif [[ ${THIS_RUNNING} > ${CLUSTER_NODE_NUMBER_MIN} && ${THIS_RUNNING} < ${CLUSTER_NODE_NUMBER} ]]; then
echo -e "REDIS CLUSTER STATUS : \033[33m YELLOW \033[0m"
if [[ ! -n ${REDIS_O1} ]]; then
echo -e "REDIS_O1 STATUS : \033[31m UNSTART \033[0m"
else
echo -e "REDIS_O1 STATUS : \033[32m RUNNING \033[0m PID IS \033[31m ${REDIS_O1} \033[0m"
fi
if [[ ! -n ${REDIS_O2} ]]; then
echo -e "REDIS_O2 STATUS : \033[31m UNSTART \033[0m"
else
echo -e "REDIS_O2 STATUS : \033[32m RUNNING \033[0m PID IS \033[31m ${REDIS_O2} \033[0m"
fi
if [[ ! -n ${REDIS_O3} ]]; then
echo -e "REDIS_O3 STATUS : \033[31m UNSTART \033[0m"
else
echo -e "REDIS_O3 STATUS : \033[32m RUNNING \033[0m PID IS \033[31m ${REDIS_O3} \033[0m"
fi
if [[ ! -n ${REDIS_O4} ]]; then
echo -e "REDIS_O4 STATUS : \033[31m UNSTART \033[0m"
else
echo -e "REDIS_O4 STATUS : \033[32m RUNNING \033[0m PID IS \033[31m ${REDIS_O4} \033[0m"
fi
if [[ ! -n ${REDIS_O5} ]]; then
echo -e "REDIS_O5 STATUS : \033[31m UNSTART \033[0m"
else
echo -e "REDIS_O5 STATUS : \033[32m RUNNING \033[0m PID IS \033[31m ${REDIS_O5} \033[0m"
fi
if [[ ! -n ${REDIS_O6} ]]; then
echo -e "REDIS_O6 STATUS : \033[31m UNSTART \033[0m"
else
echo -e "REDIS_O6 STATUS : \033[32m RUNNING \033[0m PID IS \033[31m ${REDIS_O6} \033[0m"
fi
elif [[ ${THIS_RUNNING} -eq 0 ]]; then
echo -e "REDIS CLUSTER STATUS : \033[33m UNSTART \033[0m YOU NEED STAET REDIS CLUSTER ..."
${REDIS_CLUSTER_PATH}/redis-cluster/src/redis-server ${REDIS_CLUSTER_PATH}/redis01/redis.conf >> ${REDIS_CLUSTER_PATH}/redis01/redis01.log
${REDIS_CLUSTER_PATH}/redis-cluster/src/redis-server ${REDIS_CLUSTER_PATH}/redis02/redis.conf >> ${REDIS_CLUSTER_PATH}/redis02/redis02.log
${REDIS_CLUSTER_PATH}/redis-cluster/src/redis-server ${REDIS_CLUSTER_PATH}/redis03/redis.conf >> ${REDIS_CLUSTER_PATH}/redis03/redis03.log
${REDIS_CLUSTER_PATH}/redis-cluster/src/redis-server ${REDIS_CLUSTER_PATH}/redis04/redis.conf >> ${REDIS_CLUSTER_PATH}/redis04/redis04.log
${REDIS_CLUSTER_PATH}/redis-cluster/src/redis-server ${REDIS_CLUSTER_PATH}/redis05/redis.conf >> ${REDIS_CLUSTER_PATH}/redis05/redis05.log
${REDIS_CLUSTER_PATH}/redis-cluster/src/redis-server ${REDIS_CLUSTER_PATH}/redis06/redis.conf >> ${REDIS_CLUSTER_PATH}/redis06/redis06.log
else
echo -e "REDIS CLUSTER STATUS : \033[31m RED \033[0m"
if [[ ! -n ${REDIS_O1} ]]; then
echo -e "REDIS_O1 STATUS : \033[31m UNSTART \033[0m"
else
echo -e "REDIS_O1 STATUS : \033[32m RUNNING \033[0m PID IS \033[31m ${REDIS_O1} \033[0m"
fi
if [[ ! -n ${REDIS_O2} ]]; then
echo -e "REDIS_O2 STATUS : \033[31m UNSTART \033[0m"
else
echo -e "REDIS_O2 STATUS : \033[32m RUNNING \033[0m PID IS \033[31m ${REDIS_O2} \033[0m"
fi
if [[ ! -n ${REDIS_O3} ]]; then
echo -e "REDIS_O3 STATUS : \033[31m UNSTART \033[0m"
else
echo -e "REDIS_O3 STATUS : \033[32m RUNNING \033[0m PID IS \033[31m ${REDIS_O3} \033[0m"
fi
if [[ ! -n ${REDIS_O4} ]]; then
echo -e "REDIS_O4 STATUS : \033[31m UNSTART \033[0m"
else
echo -e "REDIS_O4 STATUS : \033[32m RUNNING \033[0m PID IS \033[31m ${REDIS_O4} \033[0m"
fi
if [[ ! -n ${REDIS_O5} ]]; then
echo -e "REDIS_O5 STATUS : \033[31m UNSTART \033[0m"
else
echo -e "REDIS_O5 STATUS : \033[32m RUNNING \033[0m PID IS \033[31m ${REDIS_O5} \033[0m"
fi
if [[ ! -n ${REDIS_O6} ]]; then
echo -e "REDIS_O6 STATUS : \033[31m UNSTART \033[0m"
else
echo -e "REDIS_O6 STATUS : \033[32m RUNNING \033[0m PID IS \033[31m ${REDIS_O6} \033[0m"
fi
fi
fi
}
# 启动redis节点
stopAllredis(){
if [[ ! -n ${REDIS_CLUSTER_PATH} ]]; then
echo -e "REDIS_CLUSTER_PATH IS NULL : \033[31m NULL \033[0m"
exit ;
else
if [[ ${CLUSTER_NODE_NUMBER} == ${THIS_RUNNING} ]]; then
echo -e "REDIS_CLUSTER STATUS : \033[32m GREEN \033[0m"
kill -9 ${REDIS_O1} ${REDIS_O2} ${REDIS_O3} ${REDIS_O4} ${REDIS_O5} ${REDIS_O6}
elif [[ ${THIS_RUNNING} > ${CLUSTER_NODE_NUMBER_MIN} && ${THIS_RUNNING} < ${CLUSTER_NODE_NUMBER} ]]; then
echo -e "REDIS CLUSTER STATUS : \033[33m YELLOW \033[0m"
if [[ ! -n ${REDIS_O1} ]]; then
echo -e "REDIS_O1 STATUS : \033[31m UNSTART \033[0m"
else
echo -e "REDIS_O1 STATUS : \033[32m RUNNING \033[0m PID IS \033[31m ${REDIS_O1} \033[0m"
kill -9 ${REDIS_O1}
fi
if [[ ! -n ${REDIS_O2} ]]; then
echo -e "REDIS_O2 STATUS : \033[31m UNSTART \033[0m"
else
echo -e "REDIS_O2 STATUS : \033[32m RUNNING \033[0m PID IS \033[31m ${REDIS_O2} \033[0m"
kill -9 ${REDIS_O2}
fi
if [[ ! -n ${REDIS_O3} ]]; then
echo -e "REDIS_O3 STATUS : \033[31m UNSTART \033[0m"
else
echo -e "REDIS_O3 STATUS : \033[32m RUNNING \033[0m PID IS \033[31m ${REDIS_O3} \033[0m"
kill -9 ${REDIS_O3}
fi
if [[ ! -n ${REDIS_O4} ]]; then
echo -e "REDIS_O4 STATUS : \033[31m UNSTART \033[0m"
else
echo -e "REDIS_O4 STATUS : \033[32m RUNNING \033[0m PID IS \033[31m ${REDIS_O4} \033[0m"
kill -9 ${REDIS_O4}
fi
if [[ ! -n ${REDIS_O5} ]]; then
echo -e "REDIS_O5 STATUS : \033[31m UNSTART \033[0m"
else
echo -e "REDIS_O5 STATUS : \033[32m RUNNING \033[0m PID IS \033[31m ${REDIS_O5} \033[0m"
kill -9 ${REDIS_O5}
fi
if [[ ! -n ${REDIS_O6} ]]; then
echo -e "REDIS_O6 STATUS : \033[31m UNSTART \033[0m"
else
echo -e "REDIS_O6 STATUS : \033[32m RUNNING \033[0m PID IS \033[31m ${REDIS_O6} \033[0m"
kill -9 ${REDIS_O6}
fi
elif [[ ${THIS_RUNNING} -eq 0 ]]; then
echo -e "REDIS CLUSTER STATUS : \033[33m UNSTART \033[0m REDIS CLUSTER ALREADY STOP"
else
echo -e "REDIS CLUSTER STATUS : \033[31m RED \033[0m"
if [[ ! -n ${REDIS_O1} ]]; then
echo -e "REDIS_O1 STATUS : \033[31m UNSTART \033[0m"
else
echo -e "REDIS_O1 STATUS : \033[32m RUNNING \033[0m PID IS \033[31m ${REDIS_O1} \033[0m"
kill -9 ${REDIS_O1}
fi
if [[ ! -n ${REDIS_O2} ]]; then
echo -e "REDIS_O2 STATUS : \033[31m UNSTART \033[0m"
else
echo -e "REDIS_O2 STATUS : \033[32m RUNNING \033[0m PID IS \033[31m ${REDIS_O2} \033[0m"
kill -9 ${REDIS_O2}
fi
if [[ ! -n ${REDIS_O3} ]]; then
echo -e "REDIS_O3 STATUS : \033[31m UNSTART \033[0m"
else
echo -e "REDIS_O3 STATUS : \033[32m RUNNING \033[0m PID IS \033[31m ${REDIS_O3} \033[0m"
kill -9 ${REDIS_O3}
fi
if [[ ! -n ${REDIS_O4} ]]; then
echo -e "REDIS_O4 STATUS : \033[31m UNSTART \033[0m"
else
echo -e "REDIS_O4 STATUS : \033[32m RUNNING \033[0m PID IS \033[31m ${REDIS_O4} \033[0m"
kill -9 ${REDIS_O4}
fi
if [[ ! -n ${REDIS_O5} ]]; then
echo -e "REDIS_O5 STATUS : \033[31m UNSTART \033[0m"
else
echo -e "REDIS_O5 STATUS : \033[32m RUNNING \033[0m PID IS \033[31m ${REDIS_O5} \033[0m"
kill -9 ${REDIS_O5}
fi
if [[ ! -n ${REDIS_O6} ]]; then
echo -e "REDIS_O6 STATUS : \033[31m UNSTART \033[0m"
else
echo -e "REDIS_O6 STATUS : \033[32m RUNNING \033[0m PID IS \033[31m ${REDIS_O6} \033[0m"
kill -9 ${REDIS_O6}
fi
fi
fi
}
# 启动redis节点
createCluster(){
if [[ ! -n ${REDIS_CLUSTER_PATH} ]]; then
echo -e "REDIS_CLUSTER_PATH IS NULL : \033[31m NULL \033[0m"
exit ;
else
if [[ ${CLUSTER_NODE_NUMBER} == ${THIS_RUNNING} ]]; then
echo -e "REDIS_CLUSTER STATUS : \033[32m GREEN \033[0m"
${REDIS_CLUSTER_PATH}/redis-trib.rb create --replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006
else
echo -e "REDIS CLUSTER STATUS : \033[31m RED \033[0m IS ABOUT TO QUIT"
exit ;
fi
fi
}
cat <<eof
####################
1. startAllredis
2. stopAllredis
3. createCluster
####################
eof
read -p "please enter [1|2]:" re
case $re in
1|startAllredis)
startAllRedis ;;
2|stopAllredis)
stopAllredis ;;
3|createCluster)
createCluster ;;
*)
echo "attention your input!!!"
echo "USAGE: $0 {1|2|3}"
esac
redisClusterOps.sh
#!/bin/bash
# author : Mood
# 启动监控当前单机伪redis集群进程。如果存活个数=集群机器个数,则不做处理,如果小于集群机器总的的一半,集群还可以提供访问,需要打印集群机器进程状态,如果当前集群机器活跃总数等于0,则需要启动集群内部的所有机器,如果为RED,集群无法对外提供服务, 需要打印集群机器进程状态,
# redis集群 路径
REDIS_CLUSTER_PATH=$1
# 修复完成后再用check命令检查下是否正确
checkCluster(){
if [[ ! -n ${REDIS_CLUSTER_PATH} ]]; then
echo -e "REDIS_CLUSTER_PATH IS NULL : \033[31m NULL \033[0m"
exit ;
else
${REDIS_CLUSTER_PATH}/redis-trib.rb check 127.0.0.1:7001
fi
}
# 官方是推荐使用redis-trib.rb fix 来修复集群
fixCluster(){
if [[ ! -n ${REDIS_CLUSTER_PATH} ]]; then
echo -e "REDIS_CLUSTER_PATH IS NULL : \033[31m NULL \033[0m"
exit ;
else
${REDIS_CLUSTER_PATH}/redis-trib.rb fix 127.0.0.1:7001
fi
}
# 输入任意集群中节点即可,会自动检查所有相关节点。可以查看相应的输出看下是否是每个Master都有了slots,如果分布不均匀那可以使用下面的方式重新分配slot:
reshardCluster(){
if [[ ! -n ${REDIS_CLUSTER_PATH} ]]; then
echo -e "REDIS_CLUSTER_PATH IS NULL : \033[31m NULL \033[0m"
exit ;
else
${REDIS_CLUSTER_PATH}/redis-trib.rb reshard 127.0.0.1:7001
fi
}
cat <<eof
####################
1. checkCluster
2. reshardCluster
3. fixCluster
####################
eof
read -p "please enter [1|2]:" re
case $re in
1|checkCluster)
checkCluster ;;
2|reshardCluster)
reshardCluster ;;
3|fixCluster)
fixCluster ;;
*)
echo "attention your input!!!"
echo "USAGE: $0 {1|2|3}"
esac