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:

    Port 6000    requirepass 123456

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
Root66171018:34?00:00:Redis-server*:6000 root6647 1 0  18: 43? 00:00:00 redis-server *:< Span class= "number" >6001 root 6653 1 0 18:43? 00:00:00 redis-server *:< Span class= "number" >6002 root 6658 6570 0 18:43 Pts/0 00:00:< Span class= "number" >00 grep redis          

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

Let's verify the master-slave replication.

Master

redis-CLI-p 6000127. 0. 0. 1auth 123456OK127. 0. 0. 1chenqmOK 

SLAVE1:

redis-CLI-p 6001127. 0. 0. 1auth 123456OK127. 0. 0. 1test "chenqm" 

Slave2:

redis-CLI-p 6002127. 0. 0. 1auth 123456OK127. 0. 0. 1test "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:

127.0. 02sentinel auth-123456  
VI sentinel.conf
127.0. 02sentinel auth-123456  

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

--sentinel

To view logs:

[7014] 11Jan 19: 42: 30.918 # +MonitorMasterMyMaster 127.0.0.1 6000Quorum 2[7014] 11Jan 19: 42:30.923 * +slave slave 127.0.0.1:6002 127.0.0.1 6002 @ mymaster 127.0.0.1 6000[7014] 11 Jan 19:42:30.925 * +slave slave  127.0.0.1:6001 127.0. 0.1 6002 @ mymaster 127.0. 0.1 6000             

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

We're going to kill the master process now.

[Root@localhost Slave1]# Ps-ef|grep Redisroot69601019:29?00:00:Redis-server*:6000 root69681019:30?00:00:Redis-server*:6001 Root69751019:30?00:00:Redis-server*:6002 Root70146570019:pts/0 00:00:01 redis-server *:26479 root 7017 6789 0 19:42 Pts/5 00:< Span class= "number" >00:01 redis-server *:26379 Root 7021 6729 0 19:46 Pts/3 00:00:00 span class= "keyword" >grep redis[root @localhost slave1]# kill-9 6960  

We observe the log:

Jan:463 # +MyMaster 127. 0. 0. 1 6000Jan: £º. 379 # +  switch-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 rear number 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.