keepalived + haproxy + MySQL to build a highly available database

Source: Internet
Author: User
Tags haproxy

keepalived + haproxy + MySQL build high availability
1, the high availability of keepalived is the main preparation, there is a standby
2, keepalived + haproxy built high-availability is can be scheduled for two high-availability

Topology diagram:

Keepalived: Responsible for preemption of virtual IP, using VRRP protocol
Haproxy: Responsible for the access scheduling, reduce the single point of pressure, listen to a port alone, here with 23306

1. Install MySQL
Build MySQL on two machines, and master from the configuration, here do not introduce
2. Build Haproxy
1, download source package,: http://www.haproxy.org/#down
2, in 81.128 and 81.129 decompression installation

Tar XF haproxy-1.8.4.tar.gz
CD haproxy-1.8.4
Yum install-y gcc
Make target=linux310 arch=x86_64 # uname-a View host information fill in
Make install sbindir=/usr/sbin/mandir=/usr/share/man/docdir=/usr/share/doc/
3. Provide startup script

#!/bin/sh## haproxy## chkconfig:-15# Description:haproxy is a free, very fast and reliable solution # Offering high availability, load balancing, and # proxying for TCP and http-based applications# processn ame:haproxy# config:/etc/haproxy/haproxy.cfg# pidfile:/var/run/haproxy.pid# Source function Library. /etc/rc.d/init.d/functions# Source Networking configuration: /etc/sysconfig/network# Check that networking are up. ["$NETWORKING" = "no"] && exit 0exec= "/usr/sbin/haproxy" prog=$ (basename $exec) [-e/etc/sysconfig/$prog] && amp;. /etc/sysconfig/$progcfgfile =/etc/haproxy/haproxy.cfgpidfile=/var/run/haproxy.pidlockfile=/var/lock/subsys/ Haproxycheck () {$exec-c-v-f $cfgfile $OPTIONS}start () {$exec-c-q-f $cfgfile $OPTIONS if [$?-ne 0]; the        n echo "Errors in the configuration file, check with $prog check." Return 1 fi echo-n $ "Starting $prog:" # start it up here, usually something like "DAemon $exec "Daemon $exec-D-F $cfgfile-P $pidfile $OPTIONS retval=$? echo [$retval-eq 0] && touch $lockfile return $retval}stop () {echo-n $ "stopping $prog:" # Stop I    T here, often "Killproc $prog" Killproc $prog retval=$?    echo [$retval-eq 0] && rm-f $lockfile return $retval}restart () {$exec-c-q-f $cfgfile $OPTIONS If [$?-ne 0];        Then echo "Errors in the configuration file, check with $prog check." Return 1 fi Stop start}reload () {$exec-c-q-f $cfgfile $OPTIONS if [$?-ne 0]; then echo "Error        s in the configuration file, check with $prog check. " Return 1 fi echo-n $ "reloading $prog:" $exec-D-F $cfgfile-P $pidfile $OPTIONS-SF $ (cat $pidfile) retval=    $? echo return $retval}force_reload () {restart}fdr_status () {status $prog}case "$" in Start|stop|restart|reloa    d) $;    Force-reload) force_reload;;     Check   check;;    status) Fdr_status;; Condrestart|try-restart) [!-F $lockfile] | |    restart;; *) echo $ "Usage: $ start|stop|status|restart|try-restart|reload|force-reload}" Exit 2esac

4. Provide configuration files
Mkdir/etc/haproxy
Mkdir/var/lib/haproxy
Useradd-r Haproxy

Vim/etc/haproxy/haproxy.cfg

Global log 127.0.0.1 local2 chroot/var/lib/haproxy pidfile/var/run/haproxy.pid maxconn                    4000 User haproxy Group Haproxy daemon Stats socket/var/lib/haproxy/statsdefaults mode TCP Log Global option Dontlognull option Redis         Patch Retries 3 Timeout Http-request 10s Timeout queue 1m timeout connect           10s Timeout client 1m timeout server 1m timeout http-keep-alive 10s timeout check                  10s maxconn 600listen stats mode http bind:6677 stats enable stats hide-version Stats uri/haproxyadmin?stats Stats Realm haproxy\ Statistics stats auth admin:admin stats admi N if TRUE frontend main bind *:23306 default_backend mysqlbackend mysql balance leastconn s Erver M1 192.168.81.128:3306 Check Port 3306 maxconn server m2 192.168.81.129:3306 Check port 3306 maxconn 300 

5. Modify the log system

# # #Provides UDP syslog Reception//Remove the following two lines of comments to turn on UDP snooping

$ModLoad IMUDP
$UDPServerRun 514

local2.*/var/log/haproxy.log//Add this line
Service Rsyslog Restart

6. Start Test Haproxy
Service Haproxy Start
Chkconfig--add Haproxy
Chkconfig Haproxy on
Netstat-tnlp
mysql-p23306-uroot-p123456-h192.168.81.129 # View server_id to determine if successful

3. Build keepalived
1, download source package,: http://www.keepalived.org/download.html
2, in 81.128 and 81.129 decompression installation
Tar XF keepalived-1.2.7.tar.gz
CD keepalived-1.2.7
./configure--prefix=/usr/local/keepalived--sbindir=/usr/sbin/--sysconfdir=/etc/--mandir=/usr/local/share/man/
Make && make install
Chkconfig--add keepalived
Chkconfig keepalived on

3. Provide configuration files
VIM/ETC/KEEPALIVED/KEEPALIVED.CONF # Two machine configuration files are different

!     Configuration File for Keepalivedglobal_defs {notification_email {# ignore [email protected]     [Email protected]   [Email protected]} Notification_email_from [email protected] smtp_server 192.168.200.1 smtp_connect_timeout-router_id LVS_DEVEL }vrrp_script chk_haproxy {Script "/etc/keepalived/chk.sh" # Check Haproxy scripts Interval 2 #  Check once every two seconds}vrrp_instance vi_1 {State backup # is defined as a BACKUP node Nopreempt # Open does not preempt, the other does not write interface Ens33 virtual_router_id the Priority 100 # Open No preemption, so the precedence must be higher than the other, and the other Write Advert_int 1 Authentication {auth_type PASS auth_pass abcd} virtual_ipaddress {192 .168.81.150 # Configure VIP} track_script {Chk_haproxy # invoke check script} notify  _backup "/etc/init.d/haproxy restart" Notify_fault "/etc/init.d/haproxy Stop"}

4. Create a check file
vim/etc/keepalived/chk.sh

#!/bin/bashif [ $(ps -C haproxy --no-header | wc -l) -eq 0 ]; then       /etc/init.d/keepalived stopfi

chmod +x/etc/keepalived/chk.sh
Service keepalived Start

5. Testing
IP Addr # To see if a virtual IP is bound
Tcpdump-nn-i Ens33 VRRP # Grab a bag view
Http://192.168.81.128:6677/haproxyadmin?stats # viewing status through Haproxy

keepalived + haproxy + MySQL to build a highly available database

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.