单机redis多端口实例+keepalived高可用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了单机redis多端口实例+keepalived高可用相关的知识,希望对你有一定的参考价值。
一、实验环境说明
192.168.115.21(keepalived+redis)
192.168.115.95(keepalived+redis)
VIP:192.168.115.99
二、安装redis和keepalived
这个就不细说了,你yum安装,或者源码编译安装都可以的
三、配置redis
192.168.115.21:
redis 6379端口的配置文件
egrep -v "(^$|^#)" /usr/local/rktsapps/redis/etc/redis_6379.conf
daemonize yes pidfile /var/run/redis.pid port 6379 tcp-backlog 10000 timeout 0 tcp-keepalive 100 loglevel notice logfile "/usr/local/rktsapps/redis/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 /usr/local/rktsapps/redis/data/ slave-serve-stale-data yes slave-read-only yes 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 lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 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 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
redis 6380端口的配置文件,跟6379的主要区别的几个参数是:
pidfile /var/run/redis_6380.pid port 6380 logfile "/usr/local/rktsapps/redis/log/redis_6380.log" dbfilename dump_6380.rdb appendfilename "appendonly_6380.aof"
主要就是这5个地方。
192.168.115.95:
95的redis配置,也是配置6379和6380两个redis,配置文件内容都是一样的
四、启动redis
分别在192.168.115.21和192.168.115.95上面启动redis的两个端口:
/usr/local/rktsapps/redis/bin/redis-server /usr/local/rktsapps/redis/etc/redis_6379.conf /usr/local/rktsapps/redis/bin/redis-server /usr/local/rktsapps/redis/etc/redis_6380.conf
五、配置keepalived:
192.168.115.21:
egrep -v "(^#|^$)" /usr/local/rktsapps/keepalived/etc/keepalived/keepalived.conf
Configuration File for keepalived global_defs { router_id LVS_Redis2 } vrrp_script chk_down { script "/usr/local/rktsapps/keepalived/scripts/test_check.sh" interval 2 #weight -10 fall 2 rise 1 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 80 nopreempt advert_int 1 authentication { auth_type PASS auth_pass keepalivepass } virtual_ipaddress { 192.168.115.99/24 dev eth0 label eth0:0 } track_script { chk_down } track_interface { eth0 } notify_master "/usr/local/rktsapps/keepalived/scripts/test_master.sh" notify_backup "/usr/local/rktsapps/keepalived/scripts/test_backup.sh" notify_fault /usr/local/rktsapps/keepalived/scripts/test_fault.sh notify_stop /usr/local/rktsapps/keepalived/scripts/test_stop.sh }
192.168.115.95:
egrep -v "(^#|^$)" /usr/local/rktsapps/keepalived/etc/keepalived/keepalived.conf
! Configuration File for keepalived global_defs { router_id LVS_Redis1 } vrrp_script chk_down { script "/usr/local/rktsapps/keepalived/scripts/test_check.sh" interval 2 #weight -10 fall 2 rise 1 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 nopreempt advert_int 1 authentication { auth_type PASS auth_pass keepalivepass } virtual_ipaddress { 192.168.115.99/24 dev eth0 label eth0:0 } track_script { chk_down } track_interface { eth0 } notify_master "/usr/local/rktsapps/keepalived/scripts/test_master.sh" notify_backup "/usr/local/rktsapps/keepalived/scripts/test_backup.sh" notify_fault /usr/local/rktsapps/keepalived/scripts/test_fault.sh notify_stop /usr/local/rktsapps/keepalived/scripts/test_stop.sh }
这里先设置192.168.115.95为keepalived的master,192.168.115.21为backup
设置keepalived需要的5个脚本(两个keepalived可以使用相同的如下脚本)
cat test_check.sh (检查是否所有启动的redis端口都可以正常工作,如果都ok,返回0,表示脚本正常执行)
#!/bin/bash ports=(6379 6380) port_num=${#ports[@]} pong_num=0 for port in ${ports[*]} do ALIVE=`/usr/local/bin/redis-cli -p $port PING` LOGFILE="/var/log/keepalived-redis-check.log" echo "[CHECK]" >> $LOGFILE date >> $LOGFILE if [ $ALIVE == "PONG" ]; then : pong_num=$[$pong_num+1] echo "Success: redis-cli -p $port PING $ALIVE" >> $LOGFILE 2>&1 else echo "Failed:redis-cli -p $port PING $ALIVE " >> $LOGFILE 2>&1 fi done if [ $port_num -eq $pong_num ];then exit 0 else exit 1 fi
cat test_master.sh (成为master的时候,redis取消同步)
#!/bin/bash ports=(6379 6380) port_num=${#ports[@]} LOGFILE="/var/log/keepalived-redis-state.log" for port in ${ports[*]} do echo "[master]" >> $LOGFILE date >> $LOGFILE echo "Being master...." >> $LOGFILE 2>&1 echo "Run MASTER cmd ..." >> $LOGFILE 2>&1 echo "Run -p $port SLAVEOF NO ONE cmd ..." >> $LOGFILE /usr/local/bin/redis-cli -p $port slaveof no one >> $LOGFILE 2>&1 done
cat test_backup.sh (成为backup的时候,redis开启同步)
#!/bin/bash ports=(6379 6380) port_num=${#ports[@]} LOGFILE="/var/log/keepalived-redis-state.log" other_ip="192.168.115.95" for port in ${ports[*]} do echo "[backup]" >> $LOGFILE date >> $LOGFILE echo "Being backup...." >> $LOGFILE 2>&1 echo "Run BACKUP cmd ..." >> $LOGFILE 2>&1 echo "Run -p $port SLAVEOF $other_ip cmd ..." >> $LOGFILE /usr/local/bin/redis-cli -p $port slaveof $other_ip $port >> $LOGFILE 2>&1 done
这里要注意一下
other_ip="192.168.115.95"
是指另外一台keepalived的ip地址。
cat test_fault.sh (如果检查到有redis端口down掉,会立马重新启动,然后keepalived状态会从fault状态转成backup状态)
#!/bin/bash ports=(6379 6380) port_num=${#ports[@]} LOGFILE=/var/log/keepalived-redis-state.log echo "[fault]" >> $LOGFILE date >> $LOGFILE for port in ${ports[*]} do ALIVE=`/usr/local/bin/redis-cli -p $port PING` if [ "$ALIVE" != "PONG" ]; then netstat -tlpn | grep redis | grep $port 2>&1 if [ $? -eq 0 ]; then kill_port=`netstat -tlpn | grep redis | grep 0.0.0.0:$port | awk ‘{print $7}‘ | awk -F/ ‘{print $1}‘` kill -9 $kill_port fi /usr/local/rktsapps/redis/bin/redis-server /usr/local/rktsapps/redis/etc/redis_$port.conf fi done
cat test_stop.sh (keepalived挂掉的情况下)
#!/bin/bash LOGFILE=/var/log/keepalived-redis-state.log echo "[stop]" >> $LOGFILE date >> $LOGFILE
因为Keepalived在转换状态时会依照状态来呼叫:
notify_master 当进入Master状态时会呼叫notify_master
notify_backup 当进入Backup状态时会呼叫notify_backup
notify_fault 当发现异常情况时进入Fault状态呼叫notify_fault
notify_stop 当Keepalived程序终止时则呼叫notify_stop
六、启动keepalived
先在192.168.115.95上面启动keepalived,因为他做为master
再在192.168.115.21上面启动keepalived
/usr/local/rktsapps/keepalived/sbin/keepalived -f /usr/local/rktsapps/keepalived/etc/keepalived/keepalived.conf
如果没有启动脚本,可以使用上面的方法来启动keepalived
七、测试
这个大家可以自己手动关闭redis或者keepalived测试一下。我这边测试过了,都是ok的。
keepalived状态变化的信息主要在/var/log/messages里面,上面keepalived脚本里面也有自定义的日志文件
/var/log/keepalived-redis-state.log
和
/var/log/keepalived-redis-check.log
ok,至此结束!
本文出自 “郑小明的技术博客” 博客,请务必保留此出处http://zhengmingjing.blog.51cto.com/1587142/1863901
以上是关于单机redis多端口实例+keepalived高可用的主要内容,如果未能解决你的问题,请参考以下文章