redis集群部署
环境变量添加
vim /etc/profile.d/redis.sh
内容如下
export REDIS_HOME=/home/software/redis-5.0.4/src
export PATH=$REDIS_HOME:$PATH
刷新变量
source /etc/profile.d/redis.sh
集群部署
Master: 192.168.2.160
Slave: 192.168.2.161 和192.168.2.162
Sentinel: 192.168.2.161 , 192.168.2.162和192.168.2.163
Master配置文件
bind 192.168.2.160
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis-m6379.pid
loglevel notice
logfile "/home/software/redis-5.0.4/redis-m6379.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /home/software/redis-5.0.4
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
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
aof-use-rdb-preamble 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
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
Slave配置
########从库##############
slaveof <masterip> <masterport>
#设置该数据库为其他数据库的从数据库
masterauth <master-password>
#主从复制中,设置连接master服务器的密码(前提master启用了认证)
slave-serve-stale-data yes
# 当从库同主库失去连接或者复制正在进行,从库有两种运行方式:
# 1) 如果slave-serve-stale-data设置为yes(默认设置),从库会继续相应客户端的请求
# 2) 如果slave-serve-stale-data设置为no,除了INFO和SLAVOF命令之外的任何请求都会返回一个错误"SYNC with master in progress"
slave-priority 100
#当主库发生宕机时候,哨兵会选择优先级最高的一个称为主库,从库优先级配置默认100,数值越小优先级越高
slave-read-only yes
#从节点是否只读;默认yes只读,为了保持数据一致性,应保持默认
####主库##############
repl-disable-tcp-nodelay no
#在slave和master同步后(发送psync/sync),后续的同步是否设置成TCP_NODELAY假如设置成yes,则redis会合并小的TCP包从而节省带宽,但会增加同步延迟(40ms),造成master与slave数据不一致假如设置成no,则redis master会立即发送同步数据,没有延迟
#前者关注性能,后者关注一致性
repl-ping-slave-period 10
#从库会按照一个时间间隔向主库发送PING命令来判断主服务器是否在线,默认是10秒
repl-backlog-size 1mb
#复制积压缓冲区大小设置
repl-backlog-ttl 3600
#master没有slave一段时间会释放复制缓冲区的内存,repl-backlog-ttl用来设置该时间长度。单位为秒。
min-slaves-to-write 3
min-slaves-max-lag 10
#设置某个时间断内,如果从库数量小于该某个值则不允许主机进行写操作,以上参数表示10秒内如果主库的从节点小于3个,则主库不接受写请求,min-slaves-to-write 0代表关闭此功能。
Sentinel配置文件
bind 192.168.2.163
port 26379
daemonize yes
pidfile "/var/run/redis-sentinel.pid"
logfile "/home/software/redis-5.0.4/sentinel26379.log"
dir "/home/software/redis-5.0.4"
sentinel myid 5464a132aac7f43773d311cf78c21312c0c45ae8
sentinel deny-scripts-reconfig yes
sentinel monitor mymaster 192.168.2.160 6379 2
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 0
protected-mode no
sentinel known-replica mymaster 192.168.2.162 6379
sentinel known-replica mymaster 192.168.2.161 6379
sentinel current-epoch 0
加入开机启动:vim /etc/rc.d/init.d/redis
Redis主从启动如下内容:
#!/bin/sh
#chkconfig: 2345 55 25
#
# 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
source /etc/init.d/functions
REDISPORT=6379
EXEC=/usr/bin/redis-server
CLIEXEC=/usr/bin/redis-cli
PIDFILE=/run/redis_${REDISPORT}.pid
CONF="/home/redis/conf/redis.conf"
AUTH=""
BIND_IP='192.168.2.211' #配置你所在的主机ip
start(){
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF
fi
if [ "$?"="0" ]
then
echo "Redis is running..."
else
echo "Redis not running !"
fi
}
stop(){
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
#$CLIEXEC -h $BIND_IP -a $AUTH -p $REDISPORT shutdown
$CLIEXEC -h $BIND_IP -p $REDISPORT shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped."
fi
}
status(){
ps -ef | grep redis-server | grep -v grep >/dev/null 2>&1
if [ $? -eq 0 ];then
echo "redis server is running."
else
echo "redis server is stopped."
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status
;;
*)
echo "Please use start or stop as first argument"
;;
esac
哨兵模式的启动配置文件:vim /etc/rc.d/init.d/sentinel
#!/bin/sh
#chkconfig: 2345 55 25
#
# Simple Sentinel 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: Sentinel data structure server
# Description: Sentinel data structure server. See https://redis.io
### END INIT INFO
source /etc/init.d/functions
REDISPORT=26379
EXEC=/usr/bin/redis-sentinel
CLIEXEC=/usr/bin/redis-cli
PIDFILE=/run/redis-sentinel.pid
CONF="/home/redis/conf/sentinel.conf"
AUTH=""
BIND_IP='192.168.2.211'
start(){
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Sentinel server..."
$EXEC $CONF
fi
if [ "$?"="0" ]
then
echo "Sentinel is running..."
else
echo "Sentinel not running !"
fi
}
stop(){
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
#$CLIEXEC -h $BIND_IP -a $AUTH -p $REDISPORT shutdown
$CLIEXEC -h $BIND_IP -p $REDISPORT shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Sentinel to shutdown ..."
sleep 1
done
echo "Sentinel stopped."
fi
}
status(){
ps -ef | grep redis-sentinel | grep -v grep >/dev/null 2>&1
if [ $? -eq 0 ];then
echo "Sentinel server is running."
else
echo "Sentinel server is stopped."
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status
;;
*)
echo "Please use start or stop as first argument"
;;
esac
加入开机启动:
chmod +x redis
chmod +x sentinel
chkconfig --add redis
chkconfig --add sentinel