Build high availability and load balanced Redis Linux

Source: Internet
Author: User
Tags download redis lua openssl redis install redis

The project decides that the data store uses Redis primarily, and MySQL only saves log information. A deployment test is required for high availability and load balancing of the Redis. The test uses a virtual machine.

Test environment

Machine

Machine tool: Ubuntu 4 core 16G

Virtual machine: centos6.5-64 Single core 1G Independent IP 3

Virtual Box,centos is used for 64-bit 6.5. The details are as follows

192.168.0.231 Redis-master

192.168.0.231 Redis-slave

192.168.0.239 Virtual IP
Software

Reidis
Keepalived
Pre-installation configuration

Virtual machine minimize installation, update system, and install required components

$ yum install-y OpenSSL openssl-devel kernel kernel-devel gcc wget tcl libnl-devel

$ yum Upgrade

$ reboot//reboot system
Install keepalived

Install keepalived Download Address: http://www.keepalived.org/software/keepalived-1.2.13.tar.gz Installation:

$ wget http://www.keepalived.org/software/keepalived-1.2.13.tar.gz

$ TAR-ZXVF keepalived-1.2.13.tar.gz

$ CD keepalived-1.2.13

$ ln-s/usr/src/kernels/' uname-r '-' uname-m '//usr/src/linux

$./configure--prefix=/usr/local/keepalived--sysconf=/etc

$ chkconfig--add keepalived

See if success

$ Service keepalived Status
Install Redis

Download Redis, the current Redis stable version is 2.8.17, download address Redis-2.8.17

$ wget http://download.redis.io/releases/redis-2.8.17.tar.gz

$ TAR-ZXVF redis-2.8.17.tar.gz

$ CD redis-2.8.17

$ make && make test

$ mkdir-p/usr/local/redis/{bin,var,etc}

$ CP./src/{redis-benchmark,redis-cli,redis-server,redis-check-dump,redis-check-aof}/usr/local/redis/bin/

$ cp/usr/local/redis/bin/*/usr/local/bin/
Configuration

Master configuration

Configure the Redis first, and then configure keepalived after the master-slave replication succeeds.

Master-redis Configuration

$ vim/usr/local/redis/etc/redis.conf
Modify the following values

Daemonize Yes
Pidfile/var/run/redis.pid
Port 6379
Tcp-backlog 511
Timeout 0
Tcp-keepalive 0
LogLevel Notice
LogFile "/usr/local/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/redis/data
Slave-serve-stale-data Yes
Slave-read-only No
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
master-keepalived Configuration

Global_defs {
router_id Redis
}
Vrrp_script Chk_redis {
Script "/etc/keepalived/scripts/redis_check.sh" # # #监控脚本
Interval 2 # # #监控时间
Timeout 2 # # #超时时间
Fall 3 # # #重试次数
}
Vrrp_instance Redis {
State MASTER # # #设置为MASTER
Interface Eth0 # # #监控网卡
VIRTUAL_ROUTER_ID 52
Priority # # # #权重值
Authentication {
Auth_type Pass # # # #加密
Auth_pass Redis # # #密码
}
Track_script {
Chk_redis # # #执行上面定义的chk_redis
}
virtual_ipaddress {
192.168.0.239 # # #VIP
}
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/redsi_stop.sh
}
You can also use the SCP to upload to the server after local editing. master-keepalived Script redis_master.sh:

  #!/bin/bash
  rediscli= "/usr/local/redis/bin/redis-cli"
  logfile= "/usr/local/redis/var /keepalived-redis-state.log "
  echo" [Master] ">> $LOGFILE
  Date >> $LOGFILE
  EC Ho "Being master ..." >> $LOGFILE 2>&1
  echo "Run slaveof cmd ..." >> $LOGFILE
  $RE DISCLI slaveof 192.168.0.232 6379 >> $LOGFILE   2>&1
  sleep #延迟10秒以后待数据同步完成后再取消同步状态
&NB Sp echo "Run slaveof no one cmd ..." >> $LOGFILE
  $REDISCLI slaveof NO one >> $LOGFILE 2>&1
master-keepalived Script redis_backup.sh:

#!/bin/bash
Rediscli= "/usr/local/redis/bin/redis-cli"
Logfile= "/usr/local/redis/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.168.0.232 6379 >> $LOGFILE 2>&1
Backup configuration

Also configure the Redis first, the master-slave replication after the success of the configuration keepalived.

Backup-redis Configuration

$ vim/usr/local/redis/etc/redis.conf
Modify the following values

Daemonize Yes
Pidfile/var/run/redis.pid
Port 6379
Tcp-backlog 511
Timeout 0
Tcp-keepalive 0
LogLevel Notice
LogFile "/usr/local/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/redis/data
Slave-serve-stale-data Yes
Slave-read-only No
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
Slaveof 192.168.0.231 6379
backup-keepalived Configuration

Global_defs {
router_id Redis
}
Vrrp_script Chk_redis {
Script "/etc/keepalived/scripts/redis_check.sh" # # #监控脚本
Interval 2 # # #监控时间
Timeout 2 # # #超时时间
Fall 3 # # #重试次数
}
Vrrp_instance Redis {
State BACKUP # # #设置为MASTER
Interface Eth0 # # #监控网卡
VIRTUAL_ROUTER_ID 51
Priority # # #权重值
Authentication {
Auth_type Pass # # # #加密
Auth_pass Redis # # #密码
}
Track_script {
Chk_redis # # #执行上面定义的chk_redis
}
virtual_ipaddress {
192.168.0.239 # # #VIP
}
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/redsi_stop.sh
}
You can also use the SCP to upload to the server after local editing. master-keepalived Script redis_master.sh:

  #!/bin/bash
  rediscli= "/usr/local/redis/bin/redis-cli"
  logfile= "/usr/local/redis/var /keepalived-redis-state.log "
  echo" [Master] ">> $LOGFILE
  Date >> $LOGFILE
  EC Ho "Being master ..." >> $LOGFILE 2>&1
  echo "Run slaveof cmd ..." >> $LOGFILE
  $RE DISCLI slaveof 192.168.0.231 6379 >> $LOGFILE   2>&1
  sleep #延迟10秒以后待数据同步完成后再取消同步状态
&NB Sp echo "Run slaveof no one cmd ..." >> $LOGFILE
  $REDISCLI slaveof NO one >> $LOGFILE 2>&1
master-keepalived Script redis_backup.sh:

#!/bin/bash
Rediscli= "/usr/local/redis/bin/redis-cli"
Logfile= "/usr/local/redis/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.168.0.231 6379 >> $LOGFILE 2>&1
Common script Configuration

redis_chekc.sh

You can use Netcat to detect the status of the Redis service.

Install NC, use NC to detect Redis services instead of ping
$ wget http://sourceforge.net/projects/netcat/files/netcat/0.7.1/netcat-0.7.1.tar.gz
$ TAR-ZXVF netcat-0.7.1.tar.gz
$./configure
$ make && make install

To create a script:

$ vim/etc/keepalived/scripts/redis_check.sh
Script content:

#!/bin/bash
Alive= '/usr/local/redis/bin/redis-cli PING '

If ["$ALIVE" = = "PONG"]; then
Echo $ALIVE
Exit 0
Else
Echo $ALIVE
Exit 1
Fi
redis_fault.sh

To create a script:

$ vim/etc/keepalived/scripts/redis_fault.sh
Script content:

#!/bin/bash

Logfile=/usr/local/redis/var/keepalived-redis-state.log
echo "[Faule]" >> $LOGFILE
Date >> $LOGFILE
redis_stop.sh

To create a script:

$ vim/etc/keepalived/scripts/redis_stop.sh
Script content:

#!/bin/bash

Logfile=/usr/local/redis/var/keepalived-redis-state.log
echo "[Stop]" >> $LOGFILE
Date >> $LOGFILE
Test

Master-slave replication test

First open the Redis on master, and then open the Redis on backup to view info separately

$ REDIS-CLI Info
Master's rule shows Master as normal, and slave will appear as 192.168.0.232

Write data on master, read in Backup

$ redis-cli Set a hello//master write data
$ redis-cli Get a//backup read data
"Hello"
Master copy Normal.

Master-Slave Switching test
Start Master and backup on keepalived, view VIP on Master

$ IP A
Enter image description here

It can be seen from the figure that the VIP has been bound to master, now through the VIP to test the data

$ redis-cli-h 192.168.0.239 set a test
$ redis-cli-h 192.168.0.239 Get A//test virtual IP data read
Data read separately on host and from machine
$ redis-cli Get a
Enter image description here

You can see from the diagram that there is no problem with the test.

Now close the Redis on master to see if the VIP can drift normally and test the data.

$ killall-9 Redis-server
$ redis-cli-h 192.168.0.232 Info
Enter image description here

Now write the data to Redis and test to see if you can recover the data normally after the master recovery service

$ redis-cli-h 192.168.0.239 Set a Nihao
Restore Redis on master, view IP a

$ service Redis-server Start
$ IP A
$ redis-cli Get a
Enter image description here

Data recovery, VIP switch back to Master,redis high Availability environment to build success.

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.