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

Redis Master / Slave + keepalived High availability automatic switch

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

Introduction of the environment

Master: 172.168.1.69

Slave: 172.168.1.57

VIP 172.168.1.253 Virtual IP

two. Design ideas:

When both Master and Slave are working properly, Master is responsible for the service and Slave is responsible for Standby

When Master hangs up and Slave is timed, Slave takes over the service and turns off master-slave replication

When the Master returns to normal, the data is synchronized from the Slave. After synchronizing the data, the master-slave replication function is turned off and the Master identity is restored. At the same time, Slave waits for the completion of the Master synchronization data to restore the Slave identity.

And then cycle in turn.

It should be noted that to do so, localization policy needs to be enabled on both Master and Slave, otherwise in the process of automatic switching, the party that does not enable localization will empty the data of the other party, resulting in complete data loss. (configuration mode, open the configuration file of redis. Find appendonly. The default is appendonly no. Change to appendonly yes.)

Redis profile cannot be set in read-only mode

three. Preparation work before installation

1. Do the following on the main server 172.168.1.69

Echo "172.168.1.69 test01" > > / etc/hosts

Echo "172.168.1.57 test" > > / etc/hosts

two。 Do the following on the slave server 172.168.1.57

Echo "172.168.1.69 test01" > > / etc/hosts

Echo "172.168.1.57 test" > > / etc/hosts

Four: configure redis master and slave (abbreviated)

Port number: P: 6379

Password: 123456

Start the service:

# / mnt/tools/redis/bin/redis-server / mnt/tools/redis/etc/redis.conf

Execute on the primary server:

# / mnt/tools/redis/bin/redis-cli-h 172.168.1.69-a 123456 set test 123456

Execute from the server:

# / mnt/tools/redis/bin/redis-cli-h 172.168.1.57 get test

Whether the test has been started

# / mnt/tools/redis/bin/redis-cli ping

Performance testing

# / mnt/tools/redis/bin/redis-benchmark

Shut down the service

# / mnt/tools/redis/bin/redis-cli-p 6379 shutdown

Fifth: install and configure keepalived (direct yum installation can also be convenient, simple and rough, without having to install your own dependency packages)

1.mkdir / root/tools-p

Cd / root/tools

Wget http://www.keepalived.org/software/keepalived-1.1.17.tar.gz

Tar-zxf keepalived*

two。 Make a kernel connection

[root@zjx tools] # uname-r

3.10.0-123.9.3.el7.x86_64

[root@zjx tools] # ln-s / usr/src/kernels/3.10.0-123.9.3.el7.x86_64 / usr/src/linux

Cd keepalived-1.1.17

. / configure error (yum install-y openssl openssl-devel yum install popt-devel)

Make & & make install

Move startup scripts and configuration files

[root@zjx /] # cp / usr/local/etc/rc.d/init.d/keepalived / etc/init.d/

[root@zjx /] # cp / usr/local/etc/sysconfig/keepalived / etc/sysconfig/

[root@zjx /] # configuration file path specified by default in mkdir / etc/keepalived startup script

[root@zjx /] # / bin/cp / usr/local/etc/keepalived/keepalived.conf / etc/keepalived/

[root@zjx /] # / bin/cp / usr/local/sbin/keepalived / usr/sbin/

After performing the above operation, you can start it in the form of / etc/init.d/keepalived

/ etc/init.d/keepalived start

/ etc/init.d/keepalived stop

/ etc/init.d/keepalived restart

3.master keepalived.cnf profile

! Configuration File for keepalived

Vrrp_script chk_redis {

Script "/ etc/keepalived/scripts/redis_check.sh"

Interval 2

}

Vrrp_instance VI_1 {

State MASTER

Interface eth0

Virtual_router_id 51

Mcast_src_ip 172.168.1.253

Priority 100

Advert_int 1

Authentication {

Auth_type PASS

Auth_pass 1111

}

Track_script {

Chk_redis

}

Virtual_ipaddress {

172.168.1.253

}

Notify_master / etc/keepalived/scripts/redis_master.sh

Notify_backup / etc/keepalived/scripts/redis_backup.sh

Notify_fault / etc/keepalived/scripts/redis_fault.sh

Notify_stop / etc/keepalived/scripts/redis_stop.sh

}

Slave:keepalived.cnf profile

! Configuration File for keepalived

Vrrp_script chk_redis {

Script "/ etc/keepalived/scripts/redis_check.sh"

Interval 2

}

Vrrp_instance VI_1 {

State BACKUP

Interface eth0

Virtual_router_id 51

Mcast_src_ip 172.168.1.253

Priority 10

Advert_int 1

Authentication {

Auth_type PASS

Auth_pass 1111

}

Track_script {

Chk_redis

}

Virtual_ipaddress {

172.168.1.253

}

Notify_master / etc/keepalived/scripts/redis_master.sh

Notify_backup / etc/keepalived/scripts/redis_backup.sh

Notify_fault / etc/keepalived/scripts/redis_fault.sh

Notify_stop / etc/keepalived/scripts/redis_stop.sh

}

4. Create scripts to monitor Redis on Master and Slave

$mkdir / etc/keepalived/scripts

$vim / etc/keepalived/scripts/redis_check.sh

#! / bin/bash

ALIVE= `/ mnt/tools/redis/bin/redis-cli-a 123456 PING`

If ["$ALIVE" = = "PONG"]; then

Echo $ALIVE

Exit 0

Else

Echo $ALIVE

Exit 1

Fi

5. Write the following key scripts responsible for operation:

Notify_master / etc/keepalived/scripts/redis_master.sh

Notify_backup / etc/keepalived/scripts/redis_backup.sh

Notify_fault / etc/keepalived/scripts/redis_fault.sh

Notify_stop / etc/keepalived/scripts/redis_stop.sh

Because Keepalived calls according to the status when it changes the state:

Notify_master will be called when entering the Master state

Notify_backup will be called when entering the Backup state

Enter the Fault state and call notify_fault when an abnormal situation is found.

Call notify_stop when the Keepalived program terminates

1) first, create notity_master and notify_backup scripts on Redis Master:

$vim / etc/keepalived/scripts/redis_master.sh

#! / bin/bash

REDISCLI= "/ usr/local/bin/redis-cli-a 123456"

LOGFILE= "/ var/log/keepalived-redis-state.log"

Echo "[master]" > > $LOGFILE

Date > > $LOGFILE

Echo "Being master...." > > $LOGFILE 2 > & 1

Echo "Run SLAVEOF cmd..." > > $LOGFILE

$REDISCLI SLAVEOF 172.168.1.57 6379 > > $LOGFILE 2 > & 1

Sleep 10 # delay 10 seconds before canceling synchronization status after data synchronization is completed

Echo "Run SLAVEOF NO ONE cmd..." > > $LOGFILE

$REDISCLI SLAVEOF NO ONE > > $LOGFILE 2 > & 1

$vim / etc/keepalived/scripts/redis_backup.sh

#! / bin/bash

REDISCLI= "/ usr/local/bin/redis-cli-a 123456"

LOGFILE= "/ var/log/keepalived-redis-state.log"

Echo "[backup]" > > $LOGFILE

Date > > $LOGFILE

Echo "Being slave...." > > $LOGFILE 2 > & 1

Sleep 15 # delay 15 seconds before switching between master and slave roles after the data is synchronized by the other party

Echo "Run SLAVEOF cmd..." > > $LOGFILE

$REDISCLI SLAVEOF 172.168.1.57 6379 > > $LOGFILE 2 > & 1

2) create notity_master and notify_backup scripts on Redis Slave:

$vim / etc/keepalived/scripts/redis_master.sh

#! / bin/bash

REDISCLI= "/ usr/local/bin/redis-cli-a 123456"

LOGFILE= "/ var/log/keepalived-redis-state.log"

Echo "[master]" > > $LOGFILE

Date > > $LOGFILE

Echo "Being master...." > > $LOGFILE 2 > & 1

Echo "Run SLAVEOF cmd..." > > $LOGFILE

$REDISCLI SLAVEOF 172.168.1.69 6379 > > $LOGFILE 2 > & 1

Sleep 10 # delay 10 seconds before canceling synchronization status after data synchronization is completed

Echo "Run SLAVEOF NO ONE cmd..." > > $LOGFILE

$REDISCLI SLAVEOF NO ONE > > $LOGFILE 2 > & 1

$vim / etc/keepalived/scripts/redis_backup.sh

#! / bin/bash

REDISCLI= "/ usr/local/bin/redis-cli-a 123456"

LOGFILE= "/ var/log/keepalived-redis-state.log"

Echo "[backup]" > > $LOGFILE

Date > > $LOGFILE

Echo "Being slave...." > > $LOGFILE 2 > & 1

Sleep 15 # delay 15 seconds before switching between master and slave roles after the data is synchronized by the other party

Echo "Run SLAVEOF cmd..." > > $LOGFILE

$REDISCLI SLAVEOF 172.168.1.69 6379 > > $LOGFILE 2 > & 1

3) then create the same script in Master and Slave as follows:

$vim / etc/keepalived/scripts/redis_fault.sh

#! / bin/bash

LOGFILE=/var/log/keepalived-redis-state.log

Echo "[fault]" > > $LOGFILE

Date > > $LOGFILE

$vim / etc/keepalived/scripts/redis_stop.sh

#! / bin/bash

LOGFILE=/var/log/keepalived-redis-state.log

Echo "[stop]" > > $LOGFILE

Date > > $LOGFILE

6. Add executable permissions to both master and slave servers:

$chmod + x / etc/keepalived/scripts/*.sh

7. Conduct related functional tests

Start Redis on Master and slave

$/ mnt/tools/redis/bin/redis-server / mnt/tools/redis/etc/redis.conf

Start Keepalived on Master and slave

$

Try to connect to Redis through VIP:

$redis-cli-h 172.168.1.253 INFO

The connection is successful and the Slave is also connected.

Role:master

Slave0:172.168.1.57,6379,online

Try to insert some data:

$redis-cli-h 172.168.1.69 SET Hello Redis

OK

Read data from VIP

$redis-cli-h 172.168.1.253 GET Hello

"Redis"

Read data from Master

$redis-cli-h 172.168.1.69 GET Hello

"Redis"

Read data from Slave

$redis-cli-h 172.168.1.57 GET Hello

"Redis"

8. Functional testing is carried out by simulating related faults ()

Kill the Redis process on Master:

Killall-9 redis-server

View the Keepalived log on Master

View the Keepalived log on Master

$tail-f / var/log/keepalived-redis-state.log

[fault]

Thu Sep 27 08:29:01 CST 2012

At the same time, the log on Slave shows:

$tail-f / var/log/keepalived-redis-state.log

[master]

Being master....

Run SLAVEOF cmd...

OK Already connected to specified master

Run SLAVEOF NO ONE cmd...

OK

Then we can see that Slave has taken over the service and assumed the role of Master.

$redis-cli-h 172.168.1.253 INFO

$redis-cli-h 172.168.1.57 INFO

Role:master

Then we resume the Redis process of Master (after restoring the master redis, we need to restart the keepalived and the redis of the slave library can return to normal)

$/ etc/init.d/redis start

View the Keepalived log on Master

$tail-f / var/log/keepalived-redis-state.log

[master]

Being master....

Run SLAVEOF cmd...

OK

Run SLAVEOF NO ONE cmd...

OK

At the same time, the log on Slave shows:

$tail-f / var/log/keepalived-redis-state.log

[backup]

Being slave....

Run SLAVEOF cmd...

OK

It is found that the current Master has restored the role of Master again, and the failover and automatic recovery have been successful.

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