Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Configure redis master-slave replication and highly available clusters

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

First, install redis

First download the redis installation package

[root@mymaster yasuobao] # pwd/root/yasuobao [root@mymaster yasuobao] # wget http://download.redis.io/releases/redis-5.0.5.tar.gz

Decompression

[root@mymaster yasuobao] # tar-zxvf redis-5.0.5.tar.gz

Download gcc, c

[root@mymaster yasuobao] # yum install gcc c-y

Compile

[root@mymaster yasuobao] # cd redis-5.0.5 [root@mymaster redis-5.0.5] # ls00-RELEASENOTES CONTRIBUTING deps Makefile README.md runtest runtest-moduleapi sentinel.conf testsBUGS COPYING INSTALL MANIFESTO redis.conf runtest-cluster runtest-sentinel src utils [root@mymaster redis-5.0.5] # make & & make install [root@mymaster redis-5.0.5] # make install PREFIX=/usr/local/redis

Copy configuration fil

[root@mymaster redis-5.0.5] # cp. / redis.conf / usr/local/redis/bin [root@mymaster redis-5.0.5] # cd / usr/local/redis/bin

Test redis

[root@mymaster bin] #. / redis-server

Go back to the decompressed directory utils

[root@mymaster bin] # cd / root/yasuobao/redis-5.0.5/utils [root@mymaster utils] #. / install-server.sh

Second, master-slave replication of redis

Note bind 127.0.0.1

In the configuration file redis.conf, the default bind interface is 127.0.0.1, which is the local loopback address. In this way, the redis service can only be accessed through the local client connection, not through the remote connection, which can avoid exposing the redis service to a dangerous network environment and prevent some insecure people from casually passing through the remote, but we do master-slave replication.

However, to build master-slave replication, you need another redise to connect to the local machine, so annotate it.

[root@mymaster utils] # cd / etc/redis/ [root@mymaster redis] # ls6379.conf sentinel.conf [root@mymaster redis] # vim 6379.conf

Turn off protection mode

The other Redis side (slave) does the same in its configuration file, and needs to specify the masterside ip and port

As shown in the above operation, the master-slave copy of redis has been completed. Test: view it in the master set name lxc,slave.

Slave:

At this point, the master-slave replication is completed.

III. High availability of redis

High availability principle:

Automatic failover (Automatic failover): when a master server does not work properly, Sentinel starts an automatic failover operation, which upgrades one of the slaves of the failed master server to the new master server and allows the other slave servers of the failed master server to replicate the new master server When the client tries to connect to the failed primary server, the cluster also returns the address of the new primary server to the client, so that the cluster can use the new primary server instead of the failed server.

Configuration environment: three virtual machines install redis service

Mymaster:172.18.74.59myslave:172.18.74.119myslave02:172.18.74.87

Configure one master and two slaves according to master-slave replication

Copy the sentinel file to / etc/redis/ in the installation package

[root@mymaster redis-5.0.5] # cp sentinel.conf / etc/redis/ [root@mymaster redis-5.0.5] # cd / etc/redis/ [root@mymaster redis] # ls6379.conf sentinel.conf

Turn off protection mode

[root@mymaster redis] # vim sentinel.conf

The configuration instructs Sentinel to monitor a master server named mymaster, which has an IP address of 172.18.74.59 and a port number of 6379, and at least 2 Sentinel consents are required to determine that the master server has failed (as long as the number of agreed Sentinel is not up to standard, automatic failover will not be performed)

The down-after-milliseconds option specifies the number of milliseconds that Sentinel thinks the server is offline. If the server does not return a reply to the PING command sent by Sentinel within a given number of milliseconds, or returns an error, Sentinel marks the server as subjectively offline

When finished, send the sentinel configuration file to two slave nodes

Scp / etc/redis/sentinel.conf 172.18.74.119:/etc/redis/scp / etc/redis/sentinel.conf 172.18.74.87:/etc/redis/

Add redis command to environment variable

Export PATH=$PATH:/usr/local/redis/bin

Refresh profile

Source / etc/profile

Three servers start reids in master-slave order

Redis-server / etc/reids/6379.confredis-sentinel / etc/redis/sentinel.conf &

Mymaster

Myslave

Myslave02

Then check the mymaster.

You'll find a few more messages.

+ slave indicates that a new slave server has been identified and associated by Sentinel. You can see that master is 172.18.74.59 at this time. 172.18.74.119 and 172.18.74.87 are slave.

Test: master info and slave info view replication information:

Mymaster

Drop-down

Myslave

Myslave02

Then shut down the services related to mymaster's redis.

See a prompt on myslave, change yourself into master, change the original master into slave.

+ switch-master: the configuration has changed, and the IP and address of the primary server have changed.

+ sdown: the given instance mymaster is now subjectively offline.

Check out myslave's info.

You can see that the Lord has switched, thus achieving high availability

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report