Configure high availability for Redis Based on Keepalived

Source: Internet
Author: User
Tags failover haproxy install redis

Configure high availability for Redis Based on Keepalived

Configure high availability for Redis Based on Keepalived

For more information about Keepalived, see my related article:

Function IP address Install software
Primary redis 10.13.6.13 Redis, keepalived
From redis 10.13.6.16 Redis, keepalived
VIP 10.13.6.17  

I. redis master-slave Construction
1. Install redis

This article uses the yum source installation method (you need to configure the epel source), you can also use the source code compilation method to install

[Root @ P2Pp_Red01 ~] # Yum install-y redis

[Root @ P2Pp_Red02 ~] # Yum install-y redis

2. modify the configuration file

① Files and content to be modified by the primary redis

[Root @ P2Pp_Red01 ~] # Vi/etc/redis. conf

Bind 0.0.0.0

Others can install the default status

② Files and content to be modified from redis

[Root @ P2Pp_Red02 ~] # Vi/etc/redis. conf

Bind 0.0.0.0

Slaveof 10.13.6.13 6379 // specify the address and port of the primary redis

3. view the Master/Slave status

[Root @ P2Pp_Red01 ~] # Redis-cli INFO | grep role-A 3

Role: master

Slave0: 10.13.6.16, 45270, online

Db0: keys = 6, expires = 0

[Root @ P2Pp_Red02 ~] # Redis-cli INFO | grep role-A 3

Role: slave

Master_host: 10.13.6.13

Master_port: 6379

Master_link_status: up

Ii. Use keepalived for high availability for redis

1. Install keepalived on both servers.

[Root @ P2Pp_Red01 ~] # Yum install-y keepalived

[Root @ P2Pp_Red02 ~] # Yum install-y keepalived

2. Prepare the configuration file

① Main redis configuration file. For more information about the meaning of each line, see my other document.

[Root @ P2Pp_Red01 scripts] # vi/etc/keepalived. conf

! Configuration File for keepalived

 

Global_defs {

Notification_email {

Acassen@firewall.loc

Failover@firewall.loc

Sysadmin@firewall.loc

}

Notification_email_from Alexandre.Cassen@firewall.loc

Smtp_server 10.13.4.17

Smtp_connect_timeout 30

Router_id LVS_DEVEL

}

Vrrp_script chk_redis {

Script "killall-0 redis-server"

Interval 2

}

Vrrp_instance VI_1 {

State MASTER

Interface eth0

Virtual_router_id 100

Priority100

Advert_int 1

Authentication {

Auth_type PASS

Auth_pass 1111

}

Virtual_ipaddress {

10.13.6.17

}

Track_script {

Chk_redis

}

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

 

}

~ [Root @ P2Pp_Red01 ~] # Vi/etc/keepalived/scripts/redis_master.sh

 

#! /Bin/bash

REDISCLI = "/usr/bin/redis-cli"

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 10.13.4.13 6379 >>$ LOGFILE 2> & 1

Sleep 10

# Cancel the synchronization after a delay of 10 seconds.

 

Echo "Run slaveof no one cmd..."> $ LOGFILE

$ Rediscli slaveof no one >>$ LOGFILE 2> & 1

~

[Root @ P2Pp_Red01 ~] # Vi/etc/keepalived/scripts/redis_backup.sh

 

#! /Bin/bash

REDISCLI = "/usr/bin/redis-cli"

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

 

Echo "[backup]" >>$ LOGFILE

Date> $ LOGFILE

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

 

Sleep 15

# A delay of 15 seconds before switching the Master/Slave role after the data is synchronized by the other party

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

$ Rediscli slaveof 10.13.4.13 6379 >>$ LOGFILE 2> & 1

[Root @ P2Pp_Red01 scripts] # vi/etc/keepalived/scripts/redis_stop.sh

 

#! /Bin/bash

 

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

 

Echo "[stop]" >>$ LOGFILE

Date> $ LOGFILE

[Root @ P2Pp_Red01 scripts] # vi/etc/keepalived/scripts/redis_fault.sh

 

#! /Bin/bash

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

 

Echo "[fault]" >>$ LOGFILE

Date> $ LOGFILE

~

② Prepare the configuration file from redis

[Root @ P2Pp_Red02 ~] # Vi/etc/keepalived. conf

! Configuration File for keepalived

Global_defs {

Notification_email {

Acassen@firewall.loc

Failover@firewall.loc

Sysadmin@firewall.loc

}

Notification_email_from Alexandre.Cassen@firewall.loc

Smtp_server 10.13.4.13

Smtp_connect_timeout 30

Router_id LVS_DEVEL

}

Vrrp_script chk_redis {

Script "killall-0 redis-server"

Interval 1

}

Vrrp_instance VI_1 {

State BACKUP

Interface eth0

Virtual_router_id 100

Priority 99

Advert_int 1

Authentication {

Auth_type PASS

Auth_pass 1111

}

Virtual_ipaddress {

10.13.6.17

}

# Track_script {

# Chk_redis

#}

# Notify_master/etc/keepalived/scripts/redis_master.sh

# Notify_backup/etc/keepalived/scripts/redis_backup.sh

# Policy_fault/etc/keepalived/scripts/redis_fault.sh

# Notify_stop/etc/keepalived/scripts/redis_stop.sh

 

[Root @ P2Pp_Red02 ~] # Vi/etc/keepalived/scripts/redis_master.sh

 

#! /Bin/bash

REDISCLI = "/usr/bin/redis-cli"

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 10.13.6.13 6379 >>$ LOGFILE 2> & 1

Sleep 10

# Cancel the synchronization after a delay of 10 seconds.

 

Echo "Run slaveof no one cmd..."> $ LOGFILE

$ Rediscli slaveof no one >>$ LOGFILE 2> & 1

~

[Root @ P2Pp_Red02 ~] # Vi/etc/keepalived/scripts/redis_backup.sh

 

#! /Bin/bash

REDISCLI = "/usr/bin/redis-cli"

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

 

Echo "[backup]" >>$ LOGFILE

Date> $ LOGFILE

Echo "Being slave ....

Sleep 15

# A delay of 15 seconds before switching the Master/Slave role after the data is synchronized by the other party

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

$ Rediscli slaveof 10.13.6.13 6379 >>$ LOGFILE 2> & 1

The remaining two files are the same as those of the primary redis.

This section will no longer simulate faults, but it was passed during testing and is now used in the production environment

Haproxy + Keepalived build Weblogic high-availability server Load balancer Cluster

Keepalived + HAProxy configure high-availability Load Balancing

Haproxy + Keepalived + Apache configuration notes in CentOS 6.3

Haproxy + KeepAlived WEB Cluster on CentOS 6

Haproxy + Keepalived build high-availability Load Balancing

Haproxy implements fully transparent Exchange proxy service

This article permanently updates the link address:

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.