Redis master-Slave, Sentinel fault auto-switching

Source: Internet
Author: User
Tags allkeys


First, what is Redis master-slave replication?

Master-slave replication, when the user writes data to the master, sending the data file to Slave,slave via the Redis Sync mechanism also performs the same operation to ensure consistent data, and the master-slave replication of Redis is straightforward.

Second, Redis master-slave replication features

1, the same master can have more than one slaves.

2, master under the Slave can also accept the same schema other Slave link and synchronization request, to achieve cascade replication of data, that is, Master->slave->slave mode;

3. Master synchronizes data to slave in a non-blocking manner, which will mean that master will continue to process one or more slave read and write requests;

4, slave-side synchronization data can also be modified to non-blocking is the way, when the slave is performing a new synchronization, it can still use the old data information to provide the query; otherwise, when slave loses contact with master, slave returns an error to the client;

5, master-slave replication has scalability, that is, multiple slave dedicated to provide read-only query and data redundancy, master dedicated to provide write operations;

6, through the configuration disables the master data persistence mechanism, the data persistence operation to the slaves completes, avoids in the master to have the independent process to complete this operation.

Third,RedisMaster-slave replication principle

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/38/6A/wKiom1Ozz5OThc6NAAGUIzDDlQs366.jpg "title=" 13.png "alt=" wkiom1ozz5othc6naaguizddlqs366.jpg "width=" 650 "style=" Padding:0px;margin:0px;vertical-align:top;border: none; "/>when you start aSlaveprocess, it willMasterSend aSYNC Command, request a synchronous connection. Whether it's the first connection or the reconnection,Masterstarts a background process, saves a snapshot of the data to the data file, andMasterAll commands that modify the data are logged and cached in the data file. After the background process completes the cache operation,Mastersend a data file toSlave,SlaveSave the data file to your hard disk, then load it into memory, and thenMasterall actions that modify the data are sent toSlaveend. IfSlavefailure causes downtime and automatically re-connects when it returns to normal.MasterreceivedSlaveconnection, send its full data file toSlave, ifMateralso receive multipleSlavethe synchronization request that was sentMasteronly a process is started in the background to save the data file and then send it to allSlaveto ensureSlaveNormal.


iv.. Configuration Process

Jemalloc Installation

#yum install-y tcl gcc gcc-c++ make bzip2

#wgethttp://www.canonware.com/download/jemalloc/jemalloc-4.2.0.tar.bz2

#tar-XJF jemalloc-4.2.0.tar.bz2

#cd jemalloc-4.2.0

#./configure--prefix=/usr/local/jemalloc--libdir=/usr/local/lib

#make && make Install

#make Clean

#echo "/usr/local/lib" >/etc/ld.so.conf.d/usr_local_lib.conf

#/sbin/ldconfig

Redis Installation

#cd: /

#wget http://download.redis.io/releases/redis-3.2.0.tar.gz

#cd redis-3.2.0

#make Malloc=jemalloc

#make Install

#cd utils/

#./install_server.sh

The installation configuration and data path can be defined as:

Selected Config:

port:6379

Config File:/etc/redis/6379.conf

Log File:/var/log/redis_6379.log

Data dir:/data/redis

Executable:/usr/local/bin/redis-server

Cli executable:/USR/LOCAL/BIN/REDIS-CLI

Is this OK? Then press ENTER to go on or ctrl-c to abort.

Copied/tmp/6379.conf =>/etc/init.d/redis_6379

Redis set Memory size

Open redis configuration file

#vim/etc/redis/6379.conf

Add the following two lines

MaxMemory 12g
Maxmemory-policy VOLATILE-LRU

First line: Redis Sets the maximum memory footprint, which is generally recommended for Redis to set memory to three-fourths of the maximum physical memory.

Second line: If you set the MaxMemory , you typically set an expiration policy.

There are six kinds of expiration policies for Redis:

1, Volatile-lru,is deleted according to the expiration time generated by the LRU algorithm.

2, ALLKEYS-LRU, Remove any key according to the LRU algorithm.

3. volatile-random-> randomly deletes key according to expiration settings .

4 . Random deletion of allkeys->random-> without difference.

5,Volatile-ttl, according to the most recent expiration time to delete (supplemented with ttl)

6,noeviction , who also do not delete, directly in the write operation returned error.

Configuration Complete Restart Redis

To see if the configuration takes effect

#redis-CLI Info

Master :

192.168.1.76 Master

192.168.1.77 from

two sets of Redis installed separately

after installation, modify bind and add a single line of parameters in the Redis configuration file from 192.168.1.77

#vim/etc/redis/6379.conf

Bind 0.0.0.0

Slaveof 192.168.1.76 6379

start two Redis

in the main 192.168.1.76 execution

#redis-CLI

>set name Ceshi

>get Name

in the execution from 192.168.1.77

#redis-CLI

>get Name

if can get to Ceshi, represent master and slave success

Sentinel Sentinel:

On one of the Redis or just a server installation , if you install only one Sentinel and install it on a redis server, it is recommended to build Sentinel, which ensures that the main outage does not affect master-slave switching, and that downtime does not affect the main operation

#vim/etc/redis/sentinel.conf

Port 26379
Daemonize Yes
Sentinel Monitor MyMaster 192.168.1.76 6379 1
Sentinel Down-after-milliseconds MyMaster 30000
Sentinel Parallel-syncs MyMaster 1
Sentinel Failover-timeout MyMaster 900000
LogFile "/var/log/sentinel.log"

Comments:

First line: Specify the Sentinel port number

second line: Specify Sentinel for background boot

line Three: Specify Sentinel to monitor a master named MyMaster ,with the IP address of Master 192.168.1.76,

The port is 6379, and the last 1 indicates that when 1 Sentinel detects a Master exception, it will be invalidated if it is 2, That is , only if 2 Sentinel is determined to fail the Master is automatically migrated, if the number of Sentinel is not met, automatic failover will not be performed.

Line Fourth: Specify Sentinel determines when Master is disconnected. (Units in milliseconds, judged as subjective downline sdown)

Line Fifth: Specifies the maximum number of times that a failover can be performed Slave Synchronize The new Master at the same time . This number is set to 1, although the time required to complete the failover is longer, but it is guaranteed that only 1 Slave are in a state that cannot handle the command request at a time.

line Sixth: Ifthe failover isnot completed within that time (ms)(i.e. , master/slave automatically switch on failure

) operation, the failover is considered to be unsuccessful.

line seventh: Specify Sentinel log file.

Start

#redis-sentinel/etc/redis/sentinel.conf

Test:

Use the following command to view

#redis-cli-h 192.168.1.76-p 6379 Info

The Master 192.168.1.76 's Redis will be stopped and the 192.168.1.77 switch from the main

Booting the master 192.168.1.76 Redis will find that it is automatically added as a slave server.

Redis Data Migration

1. In the old redis execution redis-clisave Save the data to the disk file Dump.rdb

2. Stop new Redis

3. Copy theold redis saved disk file Dump.rdb to the new redis Data Directory

4. Start a new Redis


Redis master-Slave, Sentinel fault auto-switching

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.