redis+keepalived 高可用搭建实践
系统环境Centos6.3拓扑图如下:一、安装keepalived(每台都安装)1.安装兼容包# yum -y install openssl openssl-devel ipvsadm2.下载并安装keepalived# wget http://www.keepalived.org/software/keepalived-1.2.7.tar.gz# tar zxvf keepalived-1.2.7.tar.gz# cd keepalived-12.7 && make && make install3.redis_master keepalived配置# cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/# mkdir /etc/keepalived# cp /usr/local/sbin/keepalived /usr/sbin/# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/# chkconfig --add keepalived# vi /etc/keepalived/keepalived.conf(redis_master 1)! Configuration File for keepalivedglobal_defs {notification_email { zhengxiaofeiccc@126.com}notification_email_from Alexandre.Cassen@firewall.locsmtp_server smtp.126.comsmtp_connect_timeout 30router_id LVS_DEVEL}vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 52 priority 100#redis_master 2 改为90 advert_int 1nopreempt#不抢占ip,只在master 1上设置 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.240.89 }}4.redis_slave keepalived配置 (redis_slave 1)# vi /etc/keepalived/keepalived.conf(redis_master 1)! Configuration File for keepalivedglobal_defs {notification_email { zhengxiaofeiccc@126.com}notification_email_from Alexandre.Cassen@firewall.locsmtp_server smtp.126.comsmtp_connect_timeout 30router_id LVS_DEVEL}vrrp_instance VI_1 { state MASTER# redis_slave 2 改为 BACKUP interface eth0 virtual_router_id 53 priority 100#redis_slave 2 改为90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.240.90 }}5.启动keepalived,并查看# service keepalived start# ip a|grep inet inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host inet 192.168.240.78/24 brd 192.168.240.255 scope global eth0 inet 192.168.240.89/32 scope global eth0inet6 fe80::250:56ff:fe98:6a56/64 scope link# ip a|grep inet inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host inet 192.168.240.80/24 brd 192.168.240.255 scope global eth0 inet 192.168.240.90/32 scope global eth0inet6 fe80::250:56ff:fe98:7c0f/64 scope link二、安装redis(每台都安装)1.下载并安装redis# wget http://redis.googlecode.com/files/redis-2.6.14.tar.gz# tar zxvf redis-2.6.14.tar.gz# cd redis-2.6.14# make && make install# mkdir -p /etc/redis/2 配置redis_master# vi /etc/redis/6501.conf(redis_master 1)daemonize yesport 6501timeout 0tcp-keepalive 0loglevel noticelogfile /var/log/redis_6501.logdatabases 8save 900 1save 300 10save 60 10000# slaveof 192.168.240.78 6501这行在redis_master 2 上面添加stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump.rdbdir /var/lib/redis/6501slaveof 192.168.240.117 6501slave-serve-stale-data yesslave-read-only yesrepl-disable-tcp-nodelay noslave-priority 100appendonly yesappendfilename appendonly.aofappendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mblua-time-limit 5000slowlog-log-slower-than 10000slowlog-max-len 128hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-entries 512list-max-ziplist-value 64set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit slave 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10aof-rewrite-incremental-fsync yes3.配置redis_slave (两台配置一样)# vi /etc/redis/6501.confdaemonize yespidfile /var/run/redis_6501.pidport 6501timeout 0tcp-keepalive 0loglevel noticelogfile /var/log/redis_6501.logdatabases 8stop-writes-on-bgsave-error yesrdbcompression yesrdbchecksum yesdbfilename dump.rdbdir /var/lib/redis/6501slaveof 192.168.240.89 6501slave-serve-stale-data yesslave-read-only yesrepl-disable-tcp-nodelay noslave-priority 100maxclients 20000appendonly noappendfsync everysecno-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mblua-time-limit 5000slowlog-log-slower-than 10000slowlog-max-len 128hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-entries 512list-max-ziplist-value 64set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64activerehashing yesclient-output-buffer-limit normal 0 0 0client-output-buffer-limit slave 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60hz 10aof-rewrite-incremental-fsync yes4.编写redis启动脚本# vi /etc/init.d/redis#!/bin/sh## redis - this script starts and stops the redis-server daemon## chkconfig: - 85 15# description:Redis is a persistent key-value database# processname: redis-server# config: /etc/redis.conf# config: /etc/sysconfig/redis# pidfile: /var/run/redis.pid# Source function library.. /etc/rc.d/init.d/functions# Source networking configuration.. /etc/sysconfig/network# Check that networking is up.[ "$NETWORKING" = "no" ] && exit 0redis="/usr/local/bin/redis-server"prog=$(basename $redis)REDIS_CONF_FILE="/etc/redis/6501.conf"[ -f /etc/sysconfig/redis ] && . /etc/sysconfig/redislockfile=/var/lock/subsys/redisstart() {[ -x $redis ] || exit 5[ -f $REDIS_CONF_FILE ] || exit 6echo -n $"Starting $prog: "daemon $redis $REDIS_CONF_FILEretval=$?echo[ $retval -eq 0 ] && touch $lockfilereturn $retval}stop() {echo -n $"Stopping $prog: "killproc $prog -QUITretval=$?echo[ $retval -eq 0 ] && rm -f $lockfilereturn $retval}restart() {stopstart}reload() {echo -n $"Reloading $prog: "killproc $redis -HUPRETVAL=$?echo}force_reload() {restart}rh_status() {status $prog}rh_status_q() {rh_status >/dev/null 2>&1}case "$1" instart)rh_status_q && exit 0$1;;stop)rh_status_q || exit 0$1;;restart|configtest)$1;;reload)rh_status_q || exit 7$1;;force-reload)force_reload;;status)rh_status;;condrestart|try-restart)rh_status_q || exit 0;;*)echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"exit 2# chmod u+x /etc/init.d/redis# chkconfig --add redis# service redis start5.编写redis监控脚本# vi check_Redis.sh(并在两台redis_slaves上面运行)#!/bin/shwhile :do redis-cli -p 6501 info &> /dev/null if [ $? -ne 0 ] ;then sleep 2 redis-cli -p 6501 info &> /dev/null if [ $? -ne 0 ] ;then service keepalived stop fifi sleep 10done# nohup check_Redis.sh & #后台运行6.编写redis_master 2 监控脚本# vi check_Redis.sh#!/bin/shwhile :do redis-cli -p 6501 info &> /dev/null if [ $? -ne 0 ] ;then sleep 2 redis-cli -p 6501 info &> /dev/null if [ $? -ne 0 ] ;then service keepalived stop fi else ip a|grep 192.168.240.89 if [ $? -eq 0 ] ;then sed -i 's/^slaveof/#&/' /etc/redis/6501.conf service redis stop sleep 1 service redis start fi fi sleep 10done# nohup check_Redis.sh & #后台运行打破老婆终身制,实行小姨股份制。引入小姐竞争制,推广情人合同制。 生,容易。活,容易。生活,不容易。 支持一下:lol 我真想亲口管你爷爷叫声:爹! 床上运动也可以减肥的,你们都不知道吗?
页:
[1]