搭建 redis 主备环境记录,通过 keepalived 实现 HA。
redis master |
192.30.20.13 |
redis slave |
192.30.20.14 |
vip |
192.30.20.5 |
yum -y install gcc tcl tar xzvf redis-2.8.20.tar.gz cd redis-2.8.20 make PREFIX=/wls/redis MALLOC=libc make test make install cd utils/ ./install_server.sh #这一步可以自定义配置文件目录及名称、日志文件目录及名称、数据存放目录、可执行文件目录等。 cd /etc/init.d/ mv redis_6379 redis service redis status redis-cli --如下是 ./install_server.sh 的输出日志 Welcome to the redis service installer This script will help you easily set up a running redis server Please select the redis port for this instance: [6379] Selecting default: 6379 Please select the redis config file name [/etc/redis/6379.conf] /etc/redis/redis.conf Please select the redis log file name [/var/log/redis_6379.log] /var/log/redis.log Please select the data directory for this instance [/var/lib/redis/6379] /wls/redis Please select the redis executable path [/usr/local/bin/redis-server] Selected config: Port : 6379 Config file : /etc/redis/redis.conf Log file : /var/log/redis.log Data dir : /wls/redis Executable : /usr/local/bin/redis-server Cli Executable : /usr/local/bin/redis-cli Is this ok? Then press ENTER to go on or Ctrl-C to abort. Copied /tmp/6379.conf => /etc/init.d/redis_6379 Installing service... Successfully added to chkconfig! Successfully added to runlevels 345! Starting Redis server... Installation successful!
yum -y install openssl-devel tar xzvf keepalived-1.2.13.tar.gz cd keepalived-1.2.13 ./configure --sysconf=/etc --with-kernel-dir=/usr/src/linux make && make install ln -s /usr/local/sbin/keepalived /sbin/keepalived chkconfig keepalived on service keepalived status
vrrp_script代码块是用来定义监控脚本,脚本执行时间间隔以及脚本的执行结果导致优先级变更幅度的,VRRP脚本(vrrp_script)和VRRP实例(vrrp_instance)属于同一个级别,定义好vrrp_script代码块之后,就可以在instance中使用了。
vrrp_script chk_redis { script "/etc/keepalived/scripts/redis_check.sh" #指定执行脚本的路径 interval 1 #指定脚本的执行时间间隔 weight 10 #脚本结果导致的优先级变更:10表示优先级+10;-10则表示优先级-10 }
notify_master keepalived切换到master时执行的脚本
notify_backup keepalived切换到backup时执行的脚本
notify_fault keepalived出现故障时执行的脚本
notify_stop keepalived停止运行前执行的脚本
! Configuration File for keepalived vrrp_script chk_redis { script "/etc/keepalived/scripts/redis_check.sh" interval 2 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 101 authentication { auth_type PASS auth_pass redis } track_script { chk_redis } virtual_ipaddress { 192.30.20.5 } notify_master /etc/keepalived/scripts/redis_master.sh notify_backup /etc/keepalived/scripts/redis_backup.sh notify_fault /etc/keepalived/scripts/redis_fault.sh notify_stop /etc/keepalived/scripts/redis_stop.sh }
sleep 的时间要根据具体情况调整。
#for file in `ls`; do echo "-----------"$file;cat $file; echo -e "\n"; done -----------redis_backup.sh #!/bin/bash REDISCLI="/usr/local/bin/redis-cli" LOGFILE="/var/log/keepalived-redis-state.log" echo "[backup]" >> $LOGFILE date >> $LOGFILE echo "Being slave...">> $LOGFILE 2>&1 sleep 15 echo "Run SLAVEOF cmd..." >> $LOGFILE $REDISCLI SLAVEOF 192.30.20.14 6379 >> $LOGFILE 2>&1 -----------redis_check.sh #!/bin/bash ALIVE=`/usr/local/bin/redis-cli PING` if [ "$ALIVE" == "PONG" ]; then echo $ALIVE exit 0 else echo $ALIVE exit 1 fi -----------redis_fault.sh #!/bin/bash LOGFILE=/var/log/keepalived-redis-state.log echo "[fault]" >> $LOGFILE date >> $LOGFILE -----------redis_master.sh #!/bin/bash REDISCLI="/usr/local/bin/redis-cli" LOGFILE="/var/log/keepalived-redis-state.log" echo "[master]" >> $LOGFILE date >> $LOGFILE echo "Being master..." >> $LOGFILE 2>&1 echo "Run SLAVEOF cmd..." >> $LOGFILE $REDISCLI SLAVEOF 192.30.20.14 6379 >> $LOGFILE 2>&1 sleep 10 echo "Run SLAVEOF NO ONE cmd..." >> $LOGFILE $REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1 -----------redis_stop.sh #!/bin/bash LOGFILE=/var/log/keepalived-redis-state.log echo "[stop]" >> $LOGFILE date >> $LOGFILE
除了 “appendonly yes”和 "pidfile /var/run/redis.pid" 这两个配置项之外,其它都默认了,master 和 slave 节点一样。
#cat /etc/redis/redis.conf | grep -v ^# | grep -v ^$ daemonize yes pidfile /var/run/redis.pid port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 0 loglevel notice logfile /var/log/redis.log 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 /wls/redis 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 yes 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-entries 512 list-max-ziplist-value 64 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
! Configuration File for keepalived vrrp_script chk_redis { script "/etc/keepalived/scripts/redis_check.sh" interval 2 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 100 authentication { auth_type PASS auth_pass redis } track_script { chk_redis } virtual_ipaddress { 192.30.20.5 } notify_master /etc/keepalived/scripts/redis_master.sh notify_backup /etc/keepalived/scripts/redis_backup.sh notify_fault /etc/keepalived/scripts/redis_fault.sh notify_stop /etc/keepalived/scripts/redis_stop.sh }
#for file in `ls`; do echo "-----------"$file;cat $file; echo -e "\n"; done -----------redis_backup.sh #!/bin/bash REDISCLI="/usr/local/bin/redis-cli" LOGFILE="/var/log/keepalived-redis-state.log" echo "[backup]" >> $LOGFILE date >> $LOGFILE echo "Being slave..." >> $LOGFILE 2>&1 sleep 15 echo "Run SLAVEOF cmd..." >> $LOGFILE $REDISCLI SLAVEOF 192.30.20.13 6379 >> $LOGFILE 2>&1 -----------redis_check.sh #!/bin/bash ALIVE=`/usr/local/bin/redis-cli PING` if [ "$ALIVE" == "PONG" ]; then echo $ALIVE exit 0 else echo $ALIVE exit 1 fi -----------redis_fault.sh #!/bin/bash LOGFILE=/var/log/keepalived-redis-state.log echo "[fault]" >> $LOGFILE date >> $LOGFILE -----------redis_master.sh #!/bin/bash REDISCLI="/usr/local/bin/redis-cli" LOGFILE="/var/log/keepalived-redis-state.log" echo "[master]" >> $LOGFILE date >> $LOGFILE echo "Being master..." >> $LOGFILE 2>&1 echo "Run SLAVEOF cmd..." >> $LOGFILE $REDISCLI SLAVEOF 192.30.20.13 6379 >> $LOGFILE 2>&1 sleep 10 echo "Run SLAVEOF NO ONE cmd..." >> $LOGFILE $REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1 -----------redis_stop.sh #!/bin/bash LOGFILE=/var/log/keepalived-redis-state.log echo "[stop]" >> $LOGFILE date >> $LOGFILE
#cat /etc/redis/redis.conf | grep -v ^# | grep -v ^$ daemonize yes pidfile /var/run/redis.pid port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 0 loglevel notice logfile /var/log/redis.log 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 /wls/redis 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 yes 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-entries 512 list-max-ziplist-value 64 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
添加密码认证后需要修改的地方
1. redis 命令 # 如下添加了 -a password cat /etc/init.d/redis #!/bin/sh #Configurations injected by install_server below.... EXEC=/usr/local/bin/redis-server CLIEXEC=/usr/local/bin/redis-cli PIDFILE=/var/run/redis.pid CONF="/etc/redis/redis.conf" REDISPORT="6379" ############### # SysV Init Information # chkconfig: - 58 74 # description: redis_6379 is the redis daemon. ### BEGIN INIT INFO # Provides: redis_6379 # Required-Start: $network $local_fs $remote_fs # Required-Stop: $network $local_fs $remote_fs # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Should-Start: $syslog $named # Should-Stop: $syslog $named # Short-Description: start and stop redis_6379 # Description: Redis daemon ### END INIT INFO 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 -a password -p $REDISPORT shutdown while [ -x /proc/${PID} ] do echo "Waiting for Redis to shutdown ..." sleep 1 done echo "Redis stopped" fi ;; status) PID=$(cat $PIDFILE) if [ ! -x /proc/${PID} ] then echo 'Redis is not running' else echo "Redis is running ($PID)" fi ;; restart) $0 stop $0 start ;; *) echo "Please use start, stop, restart or status as first argument" ;; esac 2. 配置文件 more /etc/redis/redis.conf |grep -E "masterauth|requirepass" # If the master is password protected (using the "requirepass" configuration masterauth password requirepass password 3. keepalived 脚本 [root@payun scripts]# ls -l total 20 -rwxr-xr-x 1 root root 295 Oct 20 11:12 redis_backup.sh -rwxr-xr-x 1 root root 151 Oct 20 11:48 redis_check.sh -rwxr-xr-x 1 root root 102 Sep 19 12:13 redis_fault.sh -rwxr-xr-x 1 root root 396 Oct 20 11:12 redis_master.sh -rwxr-xr-x 1 root root 99 Sep 18 21:05 redis_stop.sh [root@payun scripts]# [root@payun scripts]# cat *.sh | grep -E "REDISCLI|redis-cli" | grep -v ^REDISCLI $REDISCLI -a password SLAVEOF 172.30.20.13 6379 >> $LOGFILE 2>&1 ALIVE=`/usr/local/bin/redis-cli -a password PING` $REDISCLI -a password SLAVEOF 172.30.20.13 6379 >> $LOGFILE 2>&1 $REDISCLI -a password SLAVEOF NO ONE >> $LOGFILE 2>&1 [root@payun scripts]#