Redis master-slave configuration and Redis Auto-switch high availability via keepalived

Source: Internet
Author: User
Tags haproxy redis cluster redis server

Redis master-slave configuration and Redis Auto-switch high availability via keepalived
[Date: 2014-07-23] Source: Linux Community Fuquanjun [Font: Big Small]

A: Environment introduction:

master:192.168.1.4

slave:192.168.1.5

Virtural IP Address (VIP): 192.168.1.253

Second: Design ideas:

When Master and Slave are working normally, Master is responsible for service, Slave is responsible for standby;

When master hangs up, Slave, Slave takes over the service while shutting down the master-slave copy function;

When Master returns to normal, the data is synchronized from slave, the master-slave replication function is turned off after the data is synchronized, and the master identity is restored, while slave waits for the master synchronization data to complete before resuming slave identity.

Then loop in turn.

It is important to note that this requires a localization strategy on both master and slave, otherwise, in the automatic switching process, the other party's data will be emptied by the non-open side, resulting in a complete loss of data.

Three: Preparation before installation configuration


1. Do the following on the main server 192.168.1.4

echo "192.168.1.4 test01" >>/etc/hosts

echo "192.168.1.5 test" >>/etc/hosts

2. Do the following from the server 192.168.1.5

echo "192.168.1.4 test01" >>/etc/hosts

echo "192.168.1.5 test" >>/etc/hosts


Four: master server configuration Redis

1. Download and install the Redis package

wget Http://download.redis.io/releases/redis-2.8.12.tar.gztar XF redis-2.8.12.tar.gz

CD redis-2.8.12

Make && make install

CD src/

CP redis-server REDIS-CLI Redis-benchmark redis-check-aof redis-check-dump/usr/local/bin

Cd/usr/local/bin

Ls-ll

Then copy the redis.conf from the source to the/etc/redis.conf

Cp/root/redis-2.8.12/redis.conf/etc/redis.conf

Modify/etc/redis.conf inside can change daemonize No to daemonize Yes

You can perform redis-server in the background by default.

Then make a init.d startup script:

Vim/etc/init.d/redis-server

#!/usr/bin/env Bash

#

# Redis start up the Redis server daemon

#

# chkconfig:345 99 99

# Description:redis Service In/etc/init.d/redis \

# chkconfig--add redis or chkconfig--list redis \

# service Redis start or service Redis stop

# Processname:redis-server

# config:/etc/redis.conf

Path=/usr/local/bin:/sbin:/usr/bin:/bin

redisport=6379

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

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

Pidfile=/var/run/redis.pid

conf= "/etc/redis.conf"

#make sure some dir exist

if [!-d/var/lib/redis]; then

Mkdir-p/var/lib/redis

Mkdir-p/var/log/redis

Fi

Case "$" in

Status

Ps-a|grep Redis

;;

Start

If [-F $PIDFILE]

Then

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

Else

echo "Starting Redis server ..."

$EXEC $CONF

Fi

If ["$?" = "0"]

Then

echo "Redis is running ..."

Fi

;;

Stop

if [!-F $PIDFILE]

Then

echo "$PIDFILE does not exist, process was 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"

Fi

;;

Restart|force-reload)

${0} stop

${0} start

;;

*)

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

Exit 1

Esac


chmod o+x/etc/init.d/redis-server

Chkconfig--add Redis-server

Service Redis-server Start

--------------------------------------Split Line--------------------------------------

Ubuntu 14.04 Redis installation and simple test http://www.linuxidc.com/Linux/2014-05/101544.htm

Redis Cluster Detail Document Http://www.linuxidc.com/Linux/2013-09/90118.htm

Installation of Redis under Ubuntu 12.10 (graphic) + Jedis connection Redis http://www.linuxidc.com/Linux/2013-06/85816.htm

Redis Series-Installation and deployment Maintenance Chapter Http://www.linuxidc.com/Linux/2012-12/75627.htm

CentOS 6.3 Installation Redis http://www.linuxidc.com/Linux/2012-12/75314.htm

Redis configuration file redis.conf detailed http://www.linuxidc.com/Linux/2013-11/92524.htm

CentOS 6.3 Under Haproxy+keepalived+apache configuration note http://www.linuxidc.com/Linux/2013-06/85598.htm

Haproxy + keepalived implement Web cluster on CentOS 6 http://www.linuxidc.com/Linux/2012-03/55672.htm

Keepalived+haproxy Configuring high-availability Load balancing http://www.linuxidc.com/Linux/2012-03/56748.htm

Haproxy+keepalived Building high-availability load balancing http://www.linuxidc.com/Linux/2012-03/55880.htm

--------------------------------------Split Line--------------------------------------

V: Configure Redis from the outgoing server

From the server, configuration, just modify the/etc/redis.conf

Slaveof <masterip> <masterport> revision changed to

Slaveof 192.168.1.4 6379

Then turn on the Redis service from the server.

Start Redis-server Start

VI: Perform REDIS master-slave testing

#主服务器

Redis-cli-p 6379 Set Hello World

#从服务器

Redis-cli-p 6379 Get Hello

"World"


#主服务器

Redis-cli-p 6379 Set Hello World2

#从服务器

Redis-cli-p 6379 Get Hello

"World2"

Redis-cli-p 6379 Set Hello World

(Error) READONLY you can ' t write against a read only slave.

A master-slave Redis server was successfully configured, and since one of the configurations was read-only from the server, data could not be set from the server and only the data could be read.

Redis master-slave configuration and Redis Auto-switch high availability via keepalived

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.