Haproxy Installation/Startup/simple Configuration

Source: Internet
Author: User
Tags mysql client mysql load balancing haproxy
Haproxy although with Ha, but it does is load balanced. Haproxy is working on the four floor, which means that it can represent a wider range of services than Nginx, with a higher performance than nginx. Compared to LVS, configurable and relatively good, the need for the machine is not more than LVS. So haproxy is good to use under certain demand. For example, use Haproxy to load balance from MySQL's nodes. Here we will simply use Haproxy for MySQL load balancing.
Environmental Preparedness
IP Address Functions
192.168.111.100 Load Balancing Server
192.168.111.101 MySQL server
192.168.111.102 MySQL server

Here we assume that the MySQL server for both nodes has been installed. It is also configured as follows
1.MySQL ports are 3306 2. The server turned on 3306 Port 3. Create MySQL user test, password test, which can be accessed by the host under 192.168.111.* 4. To display the load to two different machines, create a different machine in one machine. Database to differentiate. One is MySQL1, one is MySQL2
The following is mainly for load balancing node installation and configuration, which is the core of this note.
Download Haproxy First, download the address
http://www.haproxy.org/
Extract
TAR-ZXVF haproxy-1.7.8.tar.gz

Install (you need to specify the kernel version at compile time and specify the path to install)
Make target=linux26 make
install Prefix=/usr/local/haproxy

To specify the kernel version of Linux during make, you can view the kernel version by using the following command
Cat/proc/version

Installation Success information (the following installation information is not specified in the case of prefix)
[root@m007 haproxy-1.7.8]# make install
install-d "/usr/local/sbin" Install Haproxy "/usr/local/sbin"
install-d "/usr/local/share/man"/man1
install-m 644 doc/haproxy.1 "/usr/local/share/man"/man1
Install -D "/usr/local/doc/haproxy" for
x in 51degrees-device-detection architecture close-options configuration Cookie-options deviceatlas-device-detection intro linux-syn-cookies LUA Management Netscaler-client-ip-insertion-protocol network-namespaces proxy-protocol Spoe wurfl-device-detection; Do \
        install-m 644 doc/$x. txt "/usr/local/doc/haproxy";
    
Here, I first talk about the layout of my directory structure, I learned in the process, feel the directory structure encountered a lot of holes. I'm listing my directory structure here, and my haproxy is under Usr/local/haproxy.
/usr/local/haproxy Sbin Here is an executable file, which is Haproxy (created during installation) Haproxy logs here is the log (created by yourself). Haproxy.log conf Here is the configuration file (created by yourself) haproxy.cnf haproxy.pid doc I haven't used it yet, see the name should be stored document (created during installation) Haproxy share I didn't use it either. The man created during the installation process that is, we have to create two folders ourselves, one is logs, the other is Conf. Under these two folders, you create a log file, a CNF master configuration file, and a PID file.
After the success of the installation is to be configured, the process of configuration is still more troublesome. First 1.7.8 This version I did not find the configuration file it provided. I did find the configuration file under examples and I didn't find it. So I found some of the configuration on the Internet to be pieced together. The simplest configuration information for Haproxy is listed below. The contents of this configuration are placed in the/usr/local/haproxy/conf/haproxy.cnf file. Where the PID file is used, I saved a 1 in the PID.
Global Daemon # Background run Nbproc 1 pidfile/usr/local/haproxy/conf/haproxy.pid defaul               TS mode tcp #默认的模式mode {tcp|http|health},tcp is 4 layers, HTTP is 7 layers, health will only return OK Retries 2 #两次连接失败就认为是服务器不可用, you can also force directed to other healthy server option Abortonclo by setting option Redispatch #当serverId对应的服务器挂掉后 later
        Se #当服务器负载很高的时候, automatic end of the current queue processing more long links maxconn 4096 #默认的最大连接数 timeout connect 5000ms #连接超时
        Timeout client 30000ms #客户端超时 Timeout server 30000ms #服务器超时 #timeout Check 2000 #= Heartbeat timeout                         Log 127.0.0.1 local0 err #[err warning Info Debug] ####### #test1配置 ################# Listen test1                     #这里是配置负载均衡, Test1 is the name, can be any bind 0.0.0.0:3306 #这里是监听的IP地址和端口, the port number can be between 0-65535, to avoid port conflicts mode TCP #连接的协议, this is the TCP protocol #maxconn 4086 #log 127.0.0.1 local0 Debug Server S1 192.168.111
       .101:3306 #负载的机器 Server S2 192.168.111.102:3306 #负载的机器, the load of the machine can have more than one, down can be arranged 
So it's ready to be configured. below to start Haproxy
./usr/local/haproxy/sbin/haproxy-f/USR/LOCAL/HAPROXY/CONF/HAPROXY.CNF
-F is the path that specifies our configuration file.
No log information is printed during the boot process, quiet enough to make you suspect did not start successfully, and then you go to check the log, found that there is no information in the log, do not know if your log is not configured correctly or system startup no problem, blankly feeling, I do not know where to start. But as a programmer, your mind is still agile, I first test can not load balance ah, so the use of MySQL client tools, input the local machine IP, plus the listening port number, found connected, and then withdrew again, found that even another machine, here, we found that the experiment has been successful. But as a programmer you are still unwilling, we have to monitor, we also want to view the log. Here we go.
Configuration MonitoringHaproxy with the monitoring function, and simple configuration, directly in the main configuration file to add a configuration, the format is so indented, not a copy of the problem. This configuration can be seen, the name of the project is used Haproxy, access to the browser in the local IP address plus the port plus the URI can be. For example: Http://192.168.111.100:8888/haproxy
Listen  admin_stats
        bind 0.0.0.0:8888 
        mode  http 
        stats uri     /haproxy Stats Realm Global\ Statistics 
        Stats auth  admin:admin


Configure the log below
The log is not yet done (follow the steps on the Internet to do, but always do not give me a record, I can not, ah, study and write a separate log of the bar)
Port DetectionHaproxy can detect whether our service is down, and the way to detect it is by specifying a detection port. Configuration is simple
Listen test1
        bind 0.0.0.0:3306
        mode tcp
        #maxconn 4086
        #log 127.0.0.1 local0 Debug
        server S1 192.168.111.101:3306 Check Port 3306
        server s2 192.168.111.102:3306 Check Port 3306
When we stop a service, the monitoring view is as follows (soon the machine we turned off is marked red)


to view Haproxy process status
Ps-ef | grep haproxy
Kill the Haproxy process .
Kill-9 PID

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.