HAproxy + keepalived + mysql High Availability Linux System Cluster

Source: Internet
Author: User
Tags 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 TCP (Layer 4) and HTTP (Layer 7) applications, 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:

Haproxy server and its IP address planning:

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 inset (0.00 sec) 4) Create a database blog on the slave end and export the bl on the master end Og library, import it 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 formaster to send eventMaster_Host: 192.168.1.243Master _ User: repMaster_Port: 3306Connect_Retry: 60Master_Log_File: binlog1_22343read _ Master_Log_Pos: shard _ Log_Pos: shard _ IO_Running: shard: mysql, test, shard: shard: last_Errno: 0Last_Error: Skip_Counter: counters: Until_Log_Pos: counters: Keys: Master_SSL_Cert: Keys: Master_SSL_Key: Keys: 01 row inset (0.00 sec)


2. install and configure Haproxy. The master and backup installation configurations are identical.

Wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.24.tar.gztar-zxvf Haproxy-1.4.24.tar.gzcdhaproxy-1.4.24makeTARGET = linux26 PREFIX =/usr/local/haproxymakeinstallPREFIX =/usr/local/haproxycd/usr/local/haproxymkdirconf logs // create conf under this directory, the logs directory stores the HAproxy configuration files, PID files, and log files respectively. Vim conf/haproxy. confglobalmaxconn 50000 chroot/usr/local/haproxyuid 99gid 99 daemonquietnbproc 2 pidfile/usr/local/haproxy/logs/haproxy. pid # define globalmode httpoption httplog # proactively disable the http channel option dontlognull after each request is complete # do not record the Health Check log information option comment redispatchoption comment 3log 127.0.0.1 local3maxconn 20000 contimeout 5000 clitimeout 50000 srvtimeout 192.168.1.236bi Nd *: 80 mode httpstats 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 httpcloseoption 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 # haproxy 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/syslogrestart


4. install and configure Keepalived.

Wget http://www.keepalived.org/software/keepalived-1.2.5.tar.gzln-s /Usr/src/kernels/2.6.18-164. el5-x86_64 // usr/src/linuxtar-zxvf keepalived-1.2.5.tar.gzcdkeepalived-1.2.5. /configure -- prefix =/usr/local/keepalivedmake & makeinstallcpkeepalived/etc/init. d/keepalived. rh. init/etc/init. d/keepalivedchmod + x/etc/init. d/keepalivedcpkeepalived/etc/init. d/keepalived. sysconfig/etc/sysconfig/keepalivedchkconfig -- add keepalivedchkconfig -- level 35 keepalived oncp/usr/local/ Keepalived/sbin/keepalived/bin/mkdir-p/etc/keepalivedcp/usr/local/keepalived/etc/keepalived. conf/etc/keepalived/# Haproxy master configuration file vim/etc/keepalived. conf! Configuration File detail {icationication_email {shifeng_zhang88@163.com} notification_email_from shifeng_zhang88@163.comsmtp _ server detail _ connect_timeout 30router_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 MASTERinterface limit 50 priority 100advert_int 1 authentication {auth_type authentication} track_script {signature} virtual_ipaddress {192.168.1.236 # haproxy virtual IP} # Haproxy backup the configuration file only needs to change the value of state and priority state BACKUPpriority 50 # Start the keepalived service/etc/init. d/keepalivedstart


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 -eq0 ];then/etc/init.d/haproxyrestartecho"Start haproxy"&> /dev/nullsleep3if[ `ps-C haproxy --no-header |wc-l` -eq0 ];then/etc/init.d/keepalivedstopecho"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.

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.

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.