Master ip: 192.168.240.78
Backup ip: 192.168.240.79
Virtual ip: 192.168.240.89
Slave ip: 192.168.240.80
Slave2 ip: 192.168.240.81
实现的功能:
1.当Master与Slave均运作正常时,Master负责写,Backup负责热备,Slave负责读
2.当Master挂掉,Backup正常时,Backup接管服务,同时关闭主从复制功能
3.Slave关闭写入磁盘,减少IO负载
一、安装keepalived(backup也安装)
代码如下 | 复制代码 |
################################# 1.安装兼容包 ########################### yum -y install openssl openssl-devel ipvsadm ################################ 2.下载并安装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 install ###############################3.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配置 Configuration File for keepalived global_defs { notification_email { zhengxiaofeiccc@126.com } notification_email_from zhengxiaofeiccc@126.com smtp_server smtp.126.com smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 52 priority 100 #redis_backup 改为90 advert_int 1 nopreempt #不抢占ip,只在master上设置 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.240.89 } } ######################4.启动keepalived,并查看########################################### service keepalived start [root@redis_master ~]# 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 eth0 inet6 fe80::250:56ff:fe98:6a56/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启动脚本################################################ 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/6501.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 0 redis="/usr/local/bin/redis-server" prog=$(basename $redis) REDIS_CONF_FILE="/etc/redis/6501.conf" [ -f /etc/sysconfig/redis ] && . /etc/sysconfig/redis lockfile=/var/lock/subsys/redis start() { [ -x $redis ] || exit 5 [ -f $REDIS_CONF_FILE ] || exit 6 echo -n $"Starting $prog: " daemon $redis $REDIS_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { stop start } reload() { echo -n $"Reloading $prog: " killproc $redis -HUP RETVAL=$? echo } force_reload() { restart } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) 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 |
配置Redis
代码如下 | 复制代码 |
vi /etc/redis/6501.conf (redis_master ) daemonize yes #启用守护进程 port 6501 #监听的端口号 timeout 0 #当客户端闲置多长时间后关闭连接,如果指定为0,表示关闭该功能 tcp-keepalive 60 #表示将周期性使用SO_KEEPALIVE检测客户端是否还处于健康状态,单位秒 loglevel notice #指定日志记录级别,Redis总共支持四个级别:debug、verbose、notice、warning logfile /var/log/redis_6501.log #指定日志路径 databases 8 #设置数据库的数量 save 900 1 #表示900秒(15分钟)内有1个更改,保存数据到磁盘 save 300 10 #300秒(5分钟)内有10个更改 save 60 10000 #60秒内有10000个更改 # slaveof 192.168.240.78 6501 这行在redis_backup 上面添加 stop-writes-on-bgsave-error yes #当持久化出现错误之后,是否继续提供写服务 rdbcompression yes #在进行镜像备份时,是否进行压缩 rdbchecksum yes #读取和写入的时候是否支持CRC64校检 dbfilename dump.rdb #指定本地数据库文件名,默认值为dump.rdb dir /var/lib/redis/6501 #指定本地数据库存放目录 slave-serve-stale-data yes #当slave服务器和master服务器失去连接后,或者当数据正在复制传输的时候,如果此参数值设置“yes”,slave服务器可以继续接受客户端的请求,否则,会返回给请求的客户端如下信息“SYNC with master in progress” slave-read-only yes #是否允许slave服务器节点只提供读服务 repl-disable-tcp-nodelay no #指定向slave同步数据时,是否禁用socket的NO_DELAY选 项。若配置为“yes”,则禁用NO_DELAY,则TCP协议栈会合并小包统一发送,这样可以减少主从节点间的包数量并节省带宽,但会增加数据同步到 slave的时间。若配置为“no”,表明启用NO_DELAY,则TCP协议栈不会延迟小包的发送时机,这样数据同步的延时会减少,但需要更大的带宽。 通常情况下,应该配置为no以降低同步延时,但在主从节点间网络负载已经很高的情况下,可以配置为yes。 slave-priority 100 #指定slave优先级 appendonly yes #开启append only 模式之后,redis 会把所接收到的每一次写操作请求都追加到appendonly.aof 文件中,当redis 重新启动时,会从该文件恢复出之前的状态。但是这样会造成appendonly.aof 文件过大,所以redis 还支持了BGREWRITEAOF 指令,对appendonly.aof 进行重新整理。默认是不开启的。 appendfilename appendonly.aof #默认为appendonly.aof appendfsync everysec #设置aof的同步频率,有三种选择always、everysec、no,默认是everysec表示每秒同步一次 no-appendfsync-on-rewrite no #指定是否在后台aof文件rewrite期间调用fsync,默认为no auto-aof-rewrite-percentage 100 #指定Redis重写aof文件的条件,默认为100,表示与上次rewrite的aof文件大小相比,当前aof文件增长量超过上次afo文件大小的100%时,就会触发background rewrite。若配置为0,则会禁用自动rewrite auto-aof-rewrite-min-size 64mb #指定触发rewrite的aof文件大小。若aof文件小于该值,即使当前文件的增量比例达到auto-aof-rewrite-percentage的配置值,也不会触发自动rewrite。即这两个配置项同时满足时,才会触发rewrite lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 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 #aof rewrite过程中,是否采取增量文件同步策略,默认为“yes”。 rewrite过程中,每32M数据进行一次文件同步,这样可以减少aof大文件写入对磁盘的操作次数 ########################4.配置redis_slave (两台配置一样)################################# vi /etc/redis/6501.conf daemonize yes pidfile /var/run/redis_6501.pid port 6501 timeout 0 tcp-keepalive 60 loglevel notice logfile /var/log/redis_6501.log databases 8 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb dir /var/lib/redis/6501 slaveof 192.168.240.89 6501 slave-serve-stale-data yes slave-read-only yes repl-disable-tcp-nodelay no slave-priority 100 maxclients 20000 appendonly no #slave不写入到磁盘 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 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 #####################5.编写redis监控脚本 redis_master############################## |