Examples of redis+keepalive high availability in CentOS

Source: Internet
Author: User
Tags hash install openssl lua openssl redis sleep centos install redis

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
Features to implement:
1. When master and slave are operating normally, Master is responsible for writing, backup is responsible for hot standby, Slave is responsible for reading
2. When master hangs up, backup is normal, backup takes over service, and the master-slave copy is turned off
3.Slave Turn off write disk, reduce IO load

First, install keepalived (Backup also installed)

The code is as follows Copy Code
################################# 1. Installing the Compatibility Pack ###########################
Yum-y Install OpenSSL openssl-devel ipvsadm
################################ 2. Download and install 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 Configuration ##################
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
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 #redis_backup changed to 90
Advert_int 1
Nopreempt #不抢占ip, set only on master
Authentication {
Auth_type Pass
Auth_pass 1111
}
virtual_ipaddress {
192.168.240.89
}
}
##################### #4. Start keepalived and view ###########################################
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

Second, install Redis (each installation)

The code is as follows Copy Code
#################### #1. Download and install 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. Write Redis startup Scripts ################################################
Vi/etc/init.d/redis

Startup script

The code is as follows Copy Code
#!/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 are 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 "$" in
Start
Rh_status_q && Exit 0
$
;;
Stop
Rh_status_q | | Exit 0
$
;;
Restart|configtest)
$
;;
Reload
Rh_status_q | | Exit 7
$
;;
Force-reload)
Force_reload
;;
Status
Rh_status
;;
Condrestart|try-restart)
Rh_status_q | | Exit 0
;;
*)
echo $ "Usage: $ {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
Exit 2


Configure Redis

The code is as follows Copy Code

Vi/etc/redis/6501.conf (Redis_master)
Daemonize Yes #启用守护进程
Port 6501 #监听的端口号
Timeout 0 #当客户端闲置多长时间后关闭连接, if specified as 0, to turn off the feature
Tcp-keepalive #表示将周期性使用SO_KEEPALIVE检测客户端是否还处于健康状态, per second
LogLevel Notice #指定日志记录级别, Redis supports a total of four levels: Debug, verbose, notice, warning
Logfile/var/log/redis_6501.log #指定日志路径
Databases 8 #设置数据库的数量
Save 900 1 #表示900秒 (15 minutes) with 1 changes, saving data to disk
Save #300秒 (5 minutes) has 10 changes
Save 10000 #60秒内有10000个更改
# slaveof 192.168.240.78 6501 This line is added to the redis_backup above
Stop-writes-on-bgsave-error Yes #当持久化出现错误之后, continue to provide write service
Rdbcompression Yes #在进行镜像备份时, whether to compress
Rdbchecksum Yes #读取和写入的时候是否支持CRC64校检
Dbfilename Dump.rdb #指定本地数据库文件名, the default value is Dump.rdb
dir/var/lib/redis/6501 #指定本地数据库存放目录
Slave-serve-stale-data Yes #当slave服务器和master服务器失去连接后, or if the value of this parameter is set to "yes" when the data is being replicated, the slave server can continue to accept requests from the client, otherwise The requested client is returned with the following information "SYNC with Master in progress"
Slave-read-only Yes #是否允许slave服务器节点只提供读服务
Repl-disable-tcp-nodelay no #指定向slave同步数据时, disable the No_delay option for the socket. If you configure Yes to disable No_delay, the TCP stack will combine packet forwarding to reduce the number of packets between the master and slave nodes and save bandwidth, but increase the time that data is synchronized to slave. If configured to "No" to indicate that No_delay is enabled, the TCP stack does not delay the sending of packets, so that the latency of data synchronization decreases, but requires greater bandwidth. In general, you should configure No to reduce synchronization latency, but you can configure it to Yes if the network load between the master and slave nodes is already high.
Slave-priority #指定slave优先级
AppendOnly Yes #开启append only mode, Redis appends each write request received to the Appendonly.aof file, and restores the previous state from the file when Redis restarts. But this will cause appendonly.aof file is too large, so Redis also support the bgrewriteaof instructions, appendonly.aof to reorganize. Default is not turned on.
Appendfilename appendonly.aof #默认为appendonly. aof
Appendfsync everysec #设置aof的同步频率, there are three choices always, everysec, no, the default is everysec means sync every second
No-appendfsync-on-rewrite no #指定是否在后台aof文件rewrite期间调用fsync, defaults to No
Auto-aof-rewrite-percentage #指定Redis重写aof文件的条件, which defaults to 100, indicates that the current AOF file growth exceeds 100% of the last Afo file size compared to the last rewrite aof file size. Will trigger background rewrite. If configured to 0, automatic rewrite is disabled
Auto-aof-rewrite-min-size 64MB #指定触发rewrite的aof文件大小. If the aof file is less than this value, automatic rewrite is not triggered even if the current file's increment ratio reaches the Auto-aof-rewrite-percentage configuration value. That is, when both of these configuration items are satisfied, the rewrite is triggered
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 The rewrite process, whether to take an incremental file synchronization policy, default to "Yes". Rewrite process, a file synchronization per 32M data, which reduces the number of aof large file writes to the disk
####################### #4. Configure Redis_slave (same as two configurations) #################################
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. Write Redis monitoring Scripts redis_master##############################
VI check_redis.sh
#!/bin/sh
While:
Todo
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
Fi
Sleep 10
Done
Nohup check_redis.sh & #后台运行
#################### #6. Write Redis_backup monitoring Scripts #####################################
# VI check_redis.sh
#!/bin/sh
While:
Todo
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
grep "^slaveof"/etc/redis/6501.conf
If [$?-eq 0]; Then
Sed-i ' s/^slaveof/#&/'/etc/redis/6501.conf
Service Redis Stop
Sleep 1
Service Redis Start
Fi
Fi
Fi
Sleep 10
Done
Nohup check_redis.sh & #后台运行

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.