Redis master-slave replication, read/write separation, master-slave switching

Source: Internet
Author: User

When the amount of data becomes large, the separation of read and write is necessary. While avoiding a Redis service outage, which leads to application downtime, we enable Sentinel (Sentinel) services for master-slave switching.

Redis provides a master, multiple slave service.

Prepare three Redis services, naming the folder sub-master,slave1,slave2. Here for the test machine, without interfering with the original Redis service, our master uses 6000 ports.

Configuration file (redis.conf)

Master Configuration Modify Port:

    6000123456    

Slave1 Modify the configuration:

 

    Port 6001    slaveof 127.0.0.1 6000    masterauth 123456     requirepass 123456

  

Slave2 Modify the configuration:

     Port 6002     slaveof 127.0.0.1 6000     masterauth 123456      requirepass 123456

  

Requirepass is the authentication password, should be after the master-slave switch, so it is recommended that all passwords are consistent, Masterauth is the slave to the host when the required password. (i.e. the host's Requirepass)

Start the host

     Redis-server redis.conf

To start the slave machine:

   redis-server redis1.conf   redis-server redis2.conf

Input:

PS -ef | grep Redis
Root6617     1  0  -: the?xx:xx: onRedis-server *:6000Root6647     1  0  -: +?xx:xx:xxRedis-server *:6001Root6653     1  0  -: +?xx:xx:xxRedis-server *:6002Root6658  6570  0  -: +pts/0    xx:xx:xx grepRedis

You can see that the master-slave Redis has been started accordingly.

Let's verify the master-slave replication.

Master

[[email protected] master]# redis-cli-p 6000127.0.0.1:6000> auth 123456ok127.0.0.1:6000> set Test Chenqmok

SLAVE1:

[[email protected] slave2]# redis-cli-p 6001127.0.0.1:6001> auth 123456ok127.0.0.1:6001> get Test "CHENQM"

Slave2:

[[email protected] slave2]# redis-cli-p 6002127.0.0.1:6002> auth 123456ok127.0.0.1:6002> get Test "CHENQM"

  

Can see the host to execute the Write command, from the function of the synchronization host value, master-slave replication, read and write separation is realized.

But in case the host hangs up what to do, this is a hassle, so Redis provides a Sentinel (Sentinel) to achieve the function of master-slave switching, similar to zookeeper.

We configure two Sentinel processes:

Vi
Port 26379sentinel Monitor MyMaster 127.0.0.1 6000 2
Sentinel Auth-pass MyMaster 123456

 

VI sentinel.conf

 

Port 26479sentinel Monitor MyMaster 127.0.0.1 6000 2sentinel auth-pass MyMaster 123456

  

Start the Sentinel service (to the corresponding directory to execute the corresponding command):

Redis-server sentinel.conf--sentinel

To view logs:

[7014] OneJan +: the:30.918# +monitor Master MyMaster127.0.0.1 6000Quorum2[7014] OneJan +: the:30.923* +slave Slave127.0.0.1:6002 127.0.0.1 6002@ MyMaster127.0.0.1 6000[7014] OneJan +: the:30.925* +slave Slave127.0.0.1:6001 127.0.0.1 6002@ MyMaster127.0.0.1 6000

observed from the corresponding log, a master service, two slave services

We're going to kill the master process now.

[[email protected] slave1]# ps-ef|grep redisroot      6960     1  0 19:29?        00:00:02 redis-server *:6000    root      6968     1  0 19:30?        00:00:01 redis-server *:6001     root      6975     1  0 19:30?        00:00:01 redis-server *:6002     root      7014  6570  0 19:42 pts/0    00:00:01 redis-server *:26479                 root      7017  6789  0 19:42 pts/5    00:00:01 redis-server *:26379                 root      7021  6729  0 19:46 pts/3    00:00:00 grep redis[[email protected] slave1]# kill-9 6960

We observe the log:

[7014] Jan 19:43:41.463 # +sdown Master MyMaster 127.0.0.1 6000[7014] One Jan 19:46:42.379 # +switch-master MyMaster 127 .0.0.1 6000 127.0.0.1 6001

Master switches, and when the 6000 port service restarts, he becomes the slave of the 6001 port service.

Because Sentinel changes the configuration of the corresponding sentinel.conf and redis.conf files when switching to master.

We also need to focus on a problem: Sentinel service itself is not omnipotent, also can be down, so we have to deploy Sentinel cluster, as I do more boot several sentinel.

Follow this configuration:

Sentinel Monitor MyMaster 127.0.0.1 6000 2


This trailing Figure 2 refers to the ability to perform master-slave switching when there are two or more sentinel services that detect master downtime.

Redis master-slave replication, read/write separation, master-slave 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.