Keepalived+redis High-availability deployment

Source: Internet
Author: User
Tags syslog

1 Redis Introduction and Installation

Redis is an open source, advanced key-value Storage and a perfect solution for building high-performance, scalable Web applications.

The three main features that Redis inherits from its many competitions are:

Redis The database is completely in memory and the disk is used for persistence only.

Redis has a rich set of data types compared to many key-value data stores.

Redis data can be copied to any number of slave servers.

1.1 Redis Advantages

Exceptionally fast: Redis is very fast and can perform about 110,000 episodes per second, about 81000 + records per second.

Support for rich data types: Redis support Most developers already know like lists, collections, ordered collections, hash data types. This makes it very easy to solve a wide variety of problems because we know which issues are better than the data types that can be handled through it.

Operations are atomic: All Redis operations are atomic, which ensures that Redis servers accessed by two clients at the same time will get the updated values.

Multifunction utility: Redis is a versatile tool that can be used in multiple uses such as cache, message, queue (Redis native support publish/subscribe), any transient data, applications such as Web application sessions, Web page hits count etc.

1.2 Redis Installation

Download the latest source package on the Redis website

Http://download.redis.io/releases/redis-2.8.3.tar.gz

    1. Extract

# tar Xzf redis-3.0.0.tar.gz

    1. Copy Directory

# MV Redis-3.0.0/usr/local/redis

    1. Go to Catalog

# Cd/usr/local/redis

    1. Compile

# Make && make install

    1. View command run results, if 0 is successful

# echo $?

1.3 Redis Boot

# Redis-server

The first boot may have a hint of the warning information, can be ignored, but it is best to follow the instructions to modify the method.

1.4 Redis Client Boot

# REDIS-CLI

1.5 Verifying client Actions

# redis> Set IOM Dtsoft

Ok

# redis> Get IOM

"Dtsoft"

2 master-Slave automatic Switching principle 2.1 scene Description

Suppose there are two servers a,b,a the primary node, B is from the node, each platform is equipped with keepalived and Redis, and a VIP has been assigned to keepalived, the following two scenarios are discussed:

B-redis, hang it off.

VIP is still a server occupancy, users read and write without any impact, b-keepalived detect B-redis hang off after automatically turn off their services (in fact, you can not turn off the keepalived service, here is to try to maintain and a-keepalived the same configuration) ; After the problem is fixed, start B-redis with the slave configuration file, start b-keepalived, and the nodes A and b back to their initial state.

A-redis, hang it off.

A-keepalived detect A-redis hang off after the service itself, VIP is occupied by B server, B-redis set as the primary node; After the issue is fixed, A-redis restarts with the slave configuration file and automatically synchronizes B-redis data. After the synchronization is completed, the a-keepalived is turned on, the VIP returns to the A server, the A-redis is set to the master node, the B-redis is set to A-redis from the node, the A and B nodes return to the initial state.

2.1 Switching requirements
    1. The keepalived on a, B should be able to turn off the service after Redis on this server has been hung off.
    2. Whether A-redis or B-redis, it is a slave configuration file restart (the first installation, or two services are all down to restart, such as special cases);
    3. A-reids must be synchronized before the keepalived can be opened;
    4. A-redis must be in the server to obtain VIP, set the primary node;
    5. The server that lost the VIP wants to set the Redis on this computer as the slave node (if Redis is not yet hung).

1,4,5 can be resolved with keepalived configuration scripts, 2,3 can write their own master-slave startup script control.

3 keepalived Configuration 3.1 a-keepalived Configuration

Red Word for manual add notes, the original file did not.

VI/ETC/KEEPALIVED/KEEPALIVED.CNF file

Global_defs {

router_id Lvs_master

}

Vrrp_script Monitor_redis {

Script "/etc/keepalived/script/redis_monitor.sh" – Check the Redis service status script;

Interval 3

}

Vrrp_instance Vi_1 {

State MASTER

Interface eth0

VIRTUAL_ROUTER_ID 51

Priority 100

Advert_int 1

Authentication {

Auth_type PASS

Auth_pass 1111

}

Track_script {

monitor_redis– Check Redis service status script;

}

virtual_ipaddress {

192.168.10.251 #virtual Server

}

Notify_backup/etc/keepalived/script/redis_backup.sh–keepalived becomes a script executed after the node (lost VIP)

Script executed after notify_master/etc/keepalived/script/redis_master.sh-keepalived becomes the primary node (get VIP)

}

3.2 b-keepalived Configuration

VI/ETC/KEEPALIVED/KEEPALIVED.CNF file

Global_defs {

router_id Lvs_backup

}

Vrrp_script Monitor_redis {

Script "/etc/keepalived/script/redis_monitor.sh" – Check the Redis service status script;

Interval 3

}

Vrrp_instance Vi_1 {

State BACKUP

Interface eth0

VIRTUAL_ROUTER_ID 51

Priority 99

Advert_int 1

Authentication {

Auth_type PASS

Auth_pass 1111

}

Track_script {

monitor_redis– Check Redis service status script;

}

virtual_ipaddress {

192.168.10.251 #virtual Server

}

Notify_backup/etc/keepalived/script/redis_backup.sh–keepalived becomes a script executed after the node (lost VIP)

Script executed after notify_master/etc/keepalived/script/redis_master.sh-keepalived becomes the primary node (get VIP)

}

4 Redis Configuration

We are based on the principle of keepalived automatic switching VIP to achieve the Redis master-slave switch, each switch, the master and slave roles will be swapped, so the two two machines are to write master-slave configuration files.

4.1 Editing the master node configuration file

# Vim Redis.conf

Daemonize yes– Process Background boot

logfile/usr/local/redis/redis.log– log files

syslog-enabled no– log is not written into the system log

dir/usr/local/redis/data– Data File storage

Requirepass system– Authentication Password

Slave-server-stale-data no– Synchronization is not complete the slave cannot receive commands other than slaveof and info, which is quite important

4.2 Editing from a node configuration file

# Vim Redis_slave.conf

Daemonize Yes

Logfile/usr/local/redis/redis.log

Syslog-enabled No

Dir/usr/local/redis/data

Slaveof 192.168.10.3 6379– Primary server IP (two machine different) address and port

Masterauth system– The authentication password on the primary server

Slave-server-stale-data no– Synchronization is not complete the slave cannot receive commands other than slaveof and info, which is quite important

5 Scripting 5.1 A server Redis boot script:

#!/bin/bash

Export configpath=/usr/local/redis/redis_slave.conf

Export REDISPATH=/USR/LOCAL/REDIS/SRC

Export Redispass=system

Export result=123

Export result=0

Export vip=192.168.10.251

Export slaveip=192.168.10.3

Killall-9 Redis-server

#scp $REDISPATH/flag [email protected] $SLAVEIP: $REDISPATH

echo "Start redis-server with $CONFIGPATH"

Nohup $REDISPATH/redis-server $CONFIGPATH 2>nohup.out

Until ["$result" = "$RESULT"]

Do

Export result= ' redis-cli-a $REDISPASS <<eof

Get IOM

EOF '

echo "result= $RESULT, SYNC ..., wait 10s ..."

Sleep 10

Done

Killall-9 keepalived

Sleep 5

echo "Start keepalived"

Service keepalived Start

Sleep 10

echo "VIP get?"

IP a|sed-n '/inet addr/p ' |grep $VIP

#until ["$?" = "0"]

#do

#ip a|sed-n '/inet/p ' |grep $VIP

#done

If ["$?" = = "0"];then

echo $ (IP a)

Fi

echo "Set slaveof Master"

Redis-cli-a $REDISPASS <<eof

Slaveof no One

Eof

Exit 0

5.2 redis_monitor.sh

#/bin/bash

Export Logfile=/etc/keepalived/log/alive_state.log

Export alive=$ (/usr/local/redis/src/redis-cli-a system PING)

echo "alive= $ALIVE" >> $LOGFILE

If ["$ALIVE" = "PONG"]; Then

Echo $ALIVE

Exit 0

Else

echo "Killall-9 keepalived" >> $LOGFILE

Echo $ALIVE

Killall-9 keepalived

Exit 1

Fi

5.3 redis_master.sh

#!/bin/bash

Export REDISPATH=/USR/LOCAL/REDIS/SRC

Export Redispass=system

Export logfile= "/etc/keepalived/log/alive_state.log"

Date >> $LOGFILE

echo "Set master" >> $LOGFILE

echo "Run slaveof no one cmd ..."

$REDISPATH/redis-cli-a $REDISPASS slaveof no one

5.4 redis_backup.sh

#!/bin/bash

Export REDISPATH=/USR/LOCAL/REDIS/SRC

Export Redispass=system

Export redismaster= "192.168.10.3 6379″–redis primary node address, A, b Two servers different, pay attention to the difference

Export logfile= "/etc/keepalived/log/alive_state.log"

Date >> $LOGFILE

echo "Set back Up" >> $LOGFILE

echo "MASTER: $REDISMASTER" >> $LOGFILE

echo "Run slaveof cmd ..."

$REDISPATH/redis-cli-a $REDISPASS slaveof $REDISMASTER

6 System Test 6.1.1 test environment

vip:192.168.10.251

A machine ip:192.168.10.2

B Machine ip:192.168.10.2

Client ip:192.168.10.1

6.1.2 Test Process

A machine starts Redis and keepalived

# Redis-server Redis.conf

# service Keepalived Start

B machines Start Redis and keepalived

# Redis-server Redis_slave.conf

# service Keepalived Start

Test Client connection to A/b cluster

The client runs the Redis client:

# redis-cli–h 192.168.10.251–p 6379

redis:6379> auth system– need to enter the password for the first time

Ok

The client does some write (increase, delete, change) operation, see A-redis and B-redis whether data synchronization.

Test A-redis (Master node) hangs off after client business

Turn off the Redis host to see if a-keepalived stops automatically

# Ps–ef|grep Keep

b whether the machine receives VIP

# IP A

Whether the B-redis becomes a host State

# info

On the client computer do some write (increase, delete, change) operation, see whether the B-redis data synchronization.

Test A-redis client Business after reboot

Start the A-redis host with a command script

# sh./script/redis_start.sh

Check to see if the A-redis B-redis data is fully synchronized in a A, B Redis run

# keys * See if the numbers are equal

See if a machine or VIP

# IP A

See if the A-redis becomes the primary node (keepalived gets the VIP executes the command, changes the A-redis to the master node), logs A-redis, runs the command

# info

See if B-redis becomes slave node (keepalived loses VIP will execute command, change B-redis to slave node), log b-redis, Run command

# info

Client connects to Redis client to test whether the business is normal

# redis-cli–h 192.168.10.251–p 6379

6.1.3 Test Note
    • The Redis node cannot turn on from node read-only mode, which is slave-read-only to set to No.
    • If the AB server keepalived are all hung up, the VIP may be fixed on some long-term.
    • Keepalived the script in the configuration file should pay attention to execute permissions, the configuration file format is completely correct, otherwise you may not be able to bind the VIP.
    • B. The variables in the server scripts should be aware of the differences, especially the IP.
    • A-redis hangs off after starting to use the startup script, B-redis not required.

Keepalived+redis Highly Available deployment

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.