Redis+Keepalived high availability environment deployment record

 

Keepalived implements the VRRP (Virtual Routing Redundancy) protocol and implements VIP switching from the routing level, which can completely avoid problems like heartbeat split-brain, and can well implement master-slave, master-standby, and mutual-standby solutions, especially for stateless services. The business needs a little extra effort. Since Mysql can use Keepalived to achieve master-slave switching, Redis can naturally use this method to achieve high availability.

The master-slave implementation of Redis is not as mature as Mysql. It is only available. After testing, the master-slave is not so unreliable. The main problem is that after the synchronization connection is disconnected, it needs to be re-synchronized in full. performance impact. However, in reality, the master-slave machines are often required to be placed under the same switching device in a cabinet, and the network flash is extremely low; in addition, when the master-slave synchronization has a large number of synchronizations, it is necessary to adjust the buffer area to be large enough, otherwise it is easy to cause the connection to be disconnected.
The switching logic is implemented as follows: two machines A and B
1) A and B are started in sequence, A is the master and B is a slave
2) The master A hangs up, and B takes over the business and acts as the master.
3) Machine A starts up as slave SLAVEOF B
4) Machine B hangs up, and machine A switches back to the master

There are two roles in Keepalived: Master (one) and Backup (multiple). If one is set as the Master, but the Master hangs up and then restarts, the business must be switched again, which is unacceptable for stateful services. The solution is that both machines are set to Backup, and the Backup with high priority is set to nopreemt without preemption.

Deployment record:

0) Server information

192.168.10.205 redis-master needs to install redis (version 3.2.0), keepalived (version 1.3.2)
192.168.10.206 redis-slave needs to install redis (version 3.2.0) and keepalived (version 1.3.2)
192.168.10.230    VIP

1) Install the redis service (both node machines must operate)

[root@redis-master ~]# cd /usr/local/src/
[root@redis-master src]# wget http://download.redis.io/releases/redis-3.2.0.tar.gz
[root@redis-master src]# tar -zvxf redis-3.2.0.tar.gz
[root@redis-master src]# cd redis-3.2.0
[root@redis-master redis-3.2.0]# make

Add related files and commands
[root@redis-master redis-3.2.0]# mkdir -p /usr/local/redis/bin/
[root@redis-master redis-3.2.0]# cd src
[root@redis-master src]# cp redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-server redis-sentinel /usr/local/redis/bin/
[root@redis-master src]# cd ../
[root@redis-master redis-3.2.0]# cp redis.conf /etc/

Add redis startup script
[root@redis-master redis-3.2.0]# vim /etc/init.d/redis
#!/bin/bash
#chkconfig: 2345 10 90
#description: Start and Stop redis

REDISPORT=6379

EXEC=/usr/local/redis/bin/redis-server

REDIS_CLI=/usr/local/redis/bin/redis-cli

PIDFILE=/var/run/redis.pid

CONF="/etc/redis.conf"

case "$1" in

start)

if [ -f $PIDFILE ]

then

echo "$PIDFILE exists, process is already running or crashed"

else

echo "Starting Redis server..."

$EXEC $CONF

be

if [ "$?"="0" ]

then

echo "Redis is running..."

be

;;

stop)

if [ ! -f $PIDFILE ]

then

echo "$PIDFILE does not exist, process is not running"

else

PID=$(cat $PIDFILE)

echo "Stopping ..."

$REDIS_CLI -p $REDISPORT SHUTDOWN

while [ -x ${PIDFILE} ]

do

echo "Waiting for Redis to shutdown ..."

sleep 1

done

echo "Redis stopped"

be

;;

restart|force-reload)

${0} stop

${0} start

;;

*)

echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2

exit 1

esac

Add execute permission
[root@redis-master redis-3.2.0]# chmod 755 /etc/init.d/redis

Set up to start automatically
[root@redis-master redis-3.2.0]# chkconfig --add redis
[root@redis-master redis-3.2.0]# chkconfig redis on

Create redis status log
[root@redis-master redis-3.2.0]# mkdir /var/log/redis/
[root@redis-master redis-3.2.0]# touch /var/log/redis/redis.log

Redis master-slave configuration (first look at the configuration of the redis-master master node)
[root@redis-master redis-3.2.0]# vim /etc/redis.conf
.......
port 6379
.......
daemonize yes #This is changed to yes
.......
bind 0.0.0.0 #Binding host address. It means that the local redis can only be connected through this ip address. It is best to bind 0.0.0.0; note that this cannot be configured as 127.0.0.1, otherwise the replication will fail! You can use 0.0.0.0 or the local ip address
.......
pidfile /var/run/redis.pid
.......
logfile /var/log/redis/redis.log
.......
dir /var/redis/redis #redis data directory
.......
appendonly yes #Enable AOF persistence
appendfilename "appendonly.aof" #The name of the AOF file, the default is appendonly.aof
appendfsync everysec #Force write to disk once per second, a good compromise between performance and persistence, is the recommended way.
.....
save 900 1 #Enable the RDB snapshot function, which is enabled by default
save 300 10
save 60 10000 # That is, in how many seconds, how many key changed data is added to the .rdb file
.......
slave-serve-stale-data yes #It will be enabled by default
slave-read-only yes
......
dbfilename dump.rdb # snapshot file name
......

The redis.conf configuration of another slave node redis-slave is basically the same as the above, except that the following line of configuration is added:
slaveof 192.168.10.205 6379

Then create the redis data directory
[root@redis-master redis-3.2.0]# mkdir -p /var/redis/redis

Then start the redis service of the two nodes
[root@redis-master redis-3.2.0]# /etc/init.d/redis start
Starting Redis server...
Redis is running...
[root@redis-master redis-3.2.0]# lsof -i:6379
COMMAND     PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
redis-ser 17265 root    4u  IPv4  59068      0t0  TCP *:6379 (LISTEN)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325552058&siteId=291194637