Build enterprise-level Load Balancing Based on HAproxy + keepalived + mysql Replication

Source: Internet
Author: User
Tags failover syslog haproxy

Recently, HAproxy + keepalived + mysql was used to test the high-availability Linux system cluster.
HAProxy is a free proxy software that provides high availability, load balancing, and layer-7 applications based on TCP) and HTTP, with HAProxy, you can quickly and reliably provide Proxy solutions based on TCP and HTTP applications.
Keepalived is mainly used for health checks between LoadBalance master and LoadBalance backup to achieve failover.
Mysql Replication is mainly used to improve mysql's ability to process data and implement disaster recovery and backup.

Project topology:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131227/1J91CH0-0.jpg "title =" 1.jpg"/>

Haproxy server and its IP address planning:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131227/1J91C212-1.jpg "title =" 0.jpg"/>

Project implementation:

1. Mysql master-slave replication configuration.

1) create a mysql user authorized copy account on the master server ). Grant replication slave on *. * to 'rep '@ '192. 168.1.244 'identified by 'rep123'; 2) edit the mysql configuration file my. cnf of the master server. Server-id = 1 // specify the server's IDlog-bin = mysql-bin // enable binary log binlog-ignore = mysql // ignore mysql and information_schema database binlog-ignore = information_schemabinlog-do-db = blog // synchronize the database, synchronize all databases by default. 3) view the master status. Mysql> show master status; + --------------- + ---------- + ------------ + bytes + | File | Position | Binlog_Do_DB | bytes | + ----------------- + ---------- + ------------ + bytes + | binlog1_22343 | 339244 | blog | mysql, information_schema | + --------------- + ---------- + -------------- + ------------------------ + 1 row in set (0.00 sec) 4) Create a database blog on the slave end and export the master end Blog library, import to this library, and modify the mysql main configuration file my. cnf server-id = 2 to restart the mysql database. Mysql> change master to-> master_host = '2017. 168.1.243 ',-> master_port = 3306,-> master_user = 'rep',-> master_password = 'rep123',-> master_log_file = 'binlog. 022343 ',-> master_log_pos = 339244; 5) mysql master-slave synchronization test, show slave status \ G; you can see that Slave_IO_Running and Slave_ SQL _Running are both YES. Mysql> show slave status \ G; ***************************** 1. row ************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.243 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: binlog000022343 runtime: 339110 Relay_Log_File: Invalid Relay_Log_Pos: 339244 runtime: Required runtime: Yes runtime: mysql, test, information_schema failed: Failed: 0 Last_Error: Skip_Counter: 0 unknown: 339110 Relay_Log_Space: 339244 Until_Condition: None Until_Log_File: Failed: 0 unknown: No condition: Master_SSL_Cert: master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 01 row in set (0.00 sec)

2. the installation and configuration of Haproxy are the same for master and backup.

Wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.24.tar.gztar-zxvf haproxy-1.4.24.tar.gzcd haproxy-1.4.24make TARGET = linux26 PREFIX =/usr/local/haproxymake install PREFIX =/usr/local/haproxycd/usr/local/haproxymkdir conf logs // created under this directory conf, the logs directory stores the HAproxy configuration files, PID files, and log files respectively. Vim conf/haproxy. confglobal maxconn 50000 chroot/usr/local/haproxy uid 99 gid 99 daemon quiet nbproc 2 pidfile/usr/local/haproxy/logs/haproxy. pid # debugdefaults log global mode http option httplog # proactively disable the http channel option dontlognull after each request is complete # do not record the Health Check log information option forwardfor option redispatch option abortonclose retries 3 log 127.0.0.1 local3 maxconn 20000 contimeout 5000 clitimeout 50000 srvtimeout 50000 Listen 192.168.1.236 bind *: 80 mode http stats uri/admin # backend server status view Address stats auth admin: admin # Status view page login account password balance source # scheduling algorithm, source is the same as nginx ip_hash. To solve the session problem, option httpclose option forwardforserver web1 192.168.1.248: 80 weight 5 check inter 2000 rise 2 fall 5 server web2 192.168.1.249: 8080 weight 5 check inter 2000 rise 2 fall 5 # Start the Haproxy service/usr/local/haproxy/sbin/haproxy-f haproxy. conf # hap Roxy STARTUP script #! /Bin/bashBASE_DIR = "/usr/local/haproxy" ARGV = "$ @" start () {echo "START HAPoxy SERVERS" $ BASE_DIR/sbin/haproxy-f $ BASE_DIR/conf/haproxy. conf} stop () {echo "STOP HAPoxy Listen" kill-TTOU $ (cat $ BASE_DIR/logs/haproxy. pid) echo "STOP HAPoxy process" kill-USR1 $ (cat $ BASE_DIR/logs/haproxy. pid)} case $ ARGV instart) startERROR = $ ?;; Stop) stopERROR =$ ?;; Restart) stopstartERROR =$ ?;; *) Echo "hactl. sh [start | restart | stop]" esacexit $ ERROR

3. Enable System Log support for Haproxy.

Vim/etc/syslog. conf # Add: local3. */var/log/haproxy. loglocal0. */var/log/haproxy. logvim/etc/sysconfig/syslog # modify: SYSLOGD_OPTIONS = "-r-m 0" # restart the syslog service/etc/init. d/syslog restart

4. install and configure Keepalived.

Wget http://www.keepalived.org/software/keepalived-1.2.5.tar.gzln-s/usr/src/kernels/BRL 18-164. el5-x86_64 // usr/src/linuxtar-zxvf keepalived-1.2.5.tar.gzcd keepalived-1.2.5. /configure -- prefix =/usr/local/keepalivedmake & make installcp keepalived/etc/init. d/keepalived. rh. init/etc/init. d/keepalivedchmod + x/etc/init. d/keepalivedcp keepalived/etc/init. d/keepalived. sysconfig/etc/sysconfig/keepal Ivedchkconfig -- add partition -- level 35 keepalived oncp/usr/local/keepalived/sbin/keepalived/bin/mkdir-p/etc/keepalivedcp/usr/local/keepalived/etc/keepalived/ keepalived. conf/etc/keepalived/# Haproxy master configuration file vim/etc/keepalived. conf! Configuration File for login {icationication_email {shifeng_zhang88@163.com} login shifeng_zhang88@163.com smtp_server smtp.163.com route 30 router_id LVS_Master} vrrp_script chk_http_port {script "/etc/keepalived/login" interval 5 # script execution interval weight-5 # after the script is executed, the priority is changed: 5 indicates priority + 5; -5 indicates priority-5} vrrp_instance VI_A {state MASTER interface eth0 limit 50 priority 100 advert_int 1 authentication {auth_type PASS auth_pass authorization} track_script {signature} virtual_ipaddress {192.168.1.236 # haproxy virtual IP}} # In the Haproxy backup configuration file, you only need to change the state and priority values to state BACKUPpriority 50 # Start the keepalived service/etc/init. d/keepalived start

5.Editcheck_haproxy.shScript. Put the haproxy STARTUP script in/etc/init. d.

vim /etc/keepalived/check_haproxy.sh#!/bin/bashA=`ps -C haproxy --no-header |wc -l`if [ $A -eq 0 ];then/etc/init.d/haproxy restartecho "Start haproxy" &> /dev/nullsleep 3if [ `ps -C haproxy --no-header |wc -l` -eq 0 ];then/etc/init.d/keepalived stopecho "Stop keepalived" &> /dev/nullfifi#chomd +x /etc/keepalived/check_haproxy.sh

6. Haproxy + Keepalived high availability test.

1) Haproxy + Keepalived high availability test.

Test method: Stop the keepalived Service of the master node, view the backup keepalived log, and find that when the master node is down, the slave node role will immediately become the master node, enabling the VIPS protocol, and immediately bind the VIP address to the eth0 Nic. When the master node recovers, the opposite is done.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131227/1J91631C-2.jpg "title =" 3.jpg"/>

2) Haproxy + Keepalived load balancing test

Test method: when the client accesses the VIP, haproxy distributes the access request to the backend WEB Server Based on the configured scheduling algorithm and weight, thus implementing the load balancing function.

3) Haproxy + Keepalived failover Test

Test method: When the backend WEB server goes down, haproxy automatically detects and sends the request to the normal server, and uses the haproxy monitoring page http: // 192.168.1.235/admin) you can view it.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131227/1J91BX7-3.jpg "title =" 00.jpg"/>

Note: I hope you can provide valuable comments)

1) the topology map is just a prototype of the project. You need to add monitoring servers and email servers later.

2) the Mysql master database is responsible for writing user data, and the slave is responsible for reading user data and backing up data. Besides monitoring the mysql master and slave databasesSeconds_Behind_Master option to avoid synchronization or delay of Master/Slave Data.

3) when the WEB load is very high, you can add multiple physical machines in haproxy, and consider separating the project images to form a separate image server.







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.