Redis-Build high-availability and load-balanced Redis

Source: Internet
Author: User
Tags download redis hash lua min openssl redis sleep

Reprint Please specify source: http://blog.csdn.net/l1028386804/article/details/52578080

Before, I gave you some articles about Redis, and you can refer to blog post's article on Redis. Today, we're going to learn how to build a high-availability and load-balanced redis, well, no more, let's go straight to the chase. first, the test environment 1. Machine

Machine: centos6.5-64
Virtual machine: centos6.5-64 Single-Core 1G independent IP 3
The virtual machine uses Vmware,centos as 64-bit 6.5. Specific information is as follows:

192.168.0.231   redis-master
192.168.0.231   redis-slave
192.168.0.239   Virtual IP

2. Software

Reidis
Keepalived
second, pre-installation configuration 1. Minimal installation of virtual machine

Virtual machine minimized installation, update system and installation required components

$ yum install-y OpenSSL openssl-devel kernel kernel-devel gcc wget tcl libnl-devel
$ yum upgrade
$ reboot  Rebooting the system

2, installation 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

//View successful

$ service keepalived Status

3. Installing Redis

Download Redis, the current stable version of Redis 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/

Third, the configuration 1. master Configuration

Configure 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 1 save 10000 stop-writes-on-bgsave-error Yes Rdbcompressi On Yes Rdbchecksum Yes Dbfilename dump.rdb dir/usr/local/redis/data slave-serve-stale-data Yes slave-read-only no Repl-di
Sable-tcp-nodelay no slave-priority appendonly yes appendfilename "appendonly.aof" Appendfsync everysec
No-appendfsync-on-rewrite no auto-aof-rewrite-percentage auto-aof-rewrite-min-size 64MB lua-time-limit 5000
Slowlog-log-slower-than 10000 Slowlog-max-len notify-keyspace-events "" Hash-max-ziplist-entries 512
Hash-max-ziplist-value list-max-ziplist-entries List-max-ziplist-value set-max-intset-entries 512 Zset-max-ziplist-entries zset-max-ziplist-value activerehashing Yes client-output-buffer-limit normal 0 0 0 client -output-buffer-limit slave 256mb 64mb client-output-buffer-limit pubsub 32MB8MB Hz 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
  101              # # #权重值
  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 SCP to upload to the server after you have edited it locally. 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
  echo "Being Master ">> $LOGFILE 2>&1
  echo" Run slaveof cmd ... ">> $LOGFILE
  $REDISCLI slaveof 192.168.0.232 6 379 >> $LOGFILE  2>&1
  sleep #延迟10秒以后待数据同步完成后再取消同步状态
  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 ..." >> $LOGFI LE
  $REDISCLI slaveof 192.168.0.232 6379 >> $LOGFILE  2>&1

2. Backup Configuration

Also configure the Redis first, the master-slave replication succeeds after 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 1 save 10000 stop-writes-on-bgsave-error Yes Rdbcompressi On Yes Rdbchecksum Yes Dbfilename dump.rdb dir/usr/local/redis/data slave-serve-stale-data Yes slave-read-only no Repl-di
Sable-tcp-nodelay no slave-priority appendonly yes appendfilename "appendonly.aof" Appendfsync everysec
No-appendfsync-on-rewrite no auto-aof-rewrite-percentage auto-aof-rewrite-min-size 64MB lua-time-limit 5000
Slowlog-log-slower-than 10000 Slowlog-max-len notify-keyspace-events "" Hash-max-ziplist-entries 512
Hash-max-ziplist-value list-max-ziplist-entries List-max-ziplist-value set-max-intset-entries 512 Zset-max-ziplist-entries zset-max-ziplist-value activerehashing Yes client-output-buffer-limit normal 0 0 0 client -output-buffer-limit slave 256mb 64mb client-output-buffer-limit pubsub 32MB8MB Hz 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 Wuyi 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 SCP to upload to the server after you have edited it locally. 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
  echo "Being Master ">> $LOGFILE 2>&1
  echo" Run slaveof cmd ... ">> $LOGFILE
  $REDISCLI slaveof 192.168.0.231 6  379 >> $LOGFILE  2>&1
  sleep #延迟10秒以后待数据同步完成后再取消同步状态
  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 ..." >> $LOGFI LE
  $REDISCLI slaveof 192.168.0.231 6379 >> $LOGFILE  2>&1

iv. Public Script configuration 1, redis_chekc.sh

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

Install NC, use NC to detect Redis service 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

2, redis_fault.sh

To create a script:

Script content:
#!/bin/bash

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

3, redis_stop.sh

To create a script:

Script content:
#!/bin/bash

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

v. Testing 1. Master-slave replication test

Open Redis on Master, and then open Redis on Backup to view info separately

$ REDIS-CLI Info  
Master's rule shows that Master is normal and slave is displayed as 192.168.0.232
Write data on master, read from backup
$ redis-cli Set a hello  //master write data
$ redis-cli Get a        //backup read data
"Hello"
Master-slave replication is normal.

2. Master-Slave switching test

Start the keepalived on master and backup and view the VIP on Master

$ IP A

You can see from the figure that the VIP is already tied to master, and now the data is tested by the VIP.

$ 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 on host and slave separately
$ r Edis-cli get a

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

Now turn off Redis on master to see if the VIP is drifting properly and test the data.

$ killall-9 redis-server
$ redis-cli-h 192.168.0.232 Info

Now write data to Redis and test to see if the Master recovery service can recover the data normally

$ redis-cli-h 192.168.0.239 Set a Nihao
Recover Redis on Master, view IP a
$ service redis-server start
$ IP a
$ redis-cli get a

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

At this point, a highly available and load-balanced REDIS environment has been successfully built.

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.