Installation and deployment of haproxy

Source: Internet
Author: User
Tags syslog haproxy

Http://hi.baidu.com/coolda/blog/item/b9d9e8f87b2be51bd8f9fd7d.html

Installation and deployment of haproxy

 

Considering the concurrency of the company's current server, we finally chose haproxy to achieve load balancing. Compared with other Server Load balancer systems, the configuration and use of haproxy is relatively simple.
The following is a self-installed and deployed haproxy record. It is important to solve the log output Problem of haproxy + syslog-ng.
PS: I am very sorry for this problem:-|

Platform: SuSE Linux Enterprise Server 11 (x86_64)

1. haproxy compilation and Installation

Obtain the source code of haproxy
Official Address: http://haproxy.1wt.eu/
Latest: http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.8.tar.gz

Command:

Wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.8.tar.gz

Considering version updates, the specific installation properties may slightly change. We recommend that you browse the README and makefile files in the haproxy directory before installation.

Compile haproxy
Enter the decompressed haproxy directory and compile it with the following command:

Maketarget = OS arch = archcpu = CPU use_xxx = 1...

Several common options are highlighted here.

Target
To optimize the target operating system during compilation, you can select one of the following values:
Generic, linux22, linux24, linux24e, linux26, Solaris, FreeBSD, OpenBSD,
Cygwin, custom.
If you are not sure about the target system, you can retain the default value of generic.

CPU
To optimize a specific CPU, you can select one of the following values: Generic, i586, i686, ultrasparc, and custom. The default value is generic.

Arch
The specified arch value can be forcibly compiled to generate a program for a specific system architecture. It is usually used to generate programs for different architecture platforms on a system architecture platform. For example, compile a 32-bit program on the target system of a 64-bit architecture.

Currently, the optional values include x86_64, i386, i8.0, i586, and i686.
Note: If you select one of the above values, "-M32" or "-M64" will be added to cflags and ldflags.
.

Debug
You can specify DEBUG =-ddebug_xxx to generate debugging information for specific code.
Currently, the defined debug macros are: debug_full, debug_memory, debug_fsm, debug_hash, and
Debug_auth.
Use_pcre, use_static_pcre options can be used by specifying "use_xxx = 1", specifying "use_xxx =" indicates disabled.

Specify whether to use the regular expression implementation in the libpcre library to replace the implementation in the system's libc library. This option is recommended because libpcre library is more efficient than libc.

Note:
Because libpcre is not widely used, it is necessary to ensure that the dynamic library of libpcre is installed in the target system during dynamic connection. Because libpcre is not widely used, it is necessary to ensure that the dynamic library of libpcre is installed in the target system during dynamic connection. For the above problem, you can use use_static_pcre to specify the static link libpcre.
When compiling with the libpcre library, make sure that the libpcre library is installed in the system. If libpcre is not used, the standard regular expression in the system libc library is used by default. Static_pcre is strongly recommended.

Prefix
Specify the installation directory. The default value is/usr/local. Other files such as documents will be in the corresponding directory specified by prefix.

Destdir
Specifies the installation directory, which is not set by default. You can specify this attribute if you want to install haproxy in a sandbox.
For Linux, kernel 2.6 +, and CPU x86_64, run the following command to compile:

Maketarget = linux26 use_static_pcre = 1

Install haproxy
You can directly copy the generated haproxy program to the directory you want to install. Or run the following command to install it to the specified or default directory:
Sudo make install

2. Configure haproxy

A few documents are provided in the DOC/directory. Specific configuration instructions can be found in the configuration.txt file.
The following describes the log output problems during the configuration process and some configuration options that need attention.

1). log output problems.

According to the instructions, the haproxy log output uses the system log program, such as syslogd and syslog-ng. The main difference between these two log systems is that their configuration file formats are different. Considering that syslog-ng is an upgraded version of syslog, it is superior to syslogd in terms of performance and functionality, therefore, we will mainly introduce how to configure syslog-ng to output haproxy logs.
My machine environment, as always: SuSE Linux Enterprise Server 11 (x86_64 ),
The default log program is syslog-ng.

I. edit the System Log service configuration file/etc/sysconfig/syslog To view the syslog_daemon value. This value specifies the Log service program to be used. The default value is null, indicating automatic detection, if you are not sure, you can explicitly specify syslog_daemon = "syslog-ng ".

Ii. Edit the syslog-ng configuration file/etc/syslog-ng/syslog-ng.conf and add the following code at the end:

# For supporting the logging of "haproxy"
 
Source my_src {
#
# Process log messages from network:
#
UDP (IP ("0.0.0.0") Port (514 ));
};
 
Filter f_local0 {facility (local0 );};
Filter custom {Program ("haproxy ");};
 
Destination d_haproxy {file ("/var/log/haproxy. log ");};
 
Log {source (my_src); filter (f_local0); destination (d_haproxy );};
# Log {source (my_src); filter (custom); destination (d_haproxy) ;};# OK, work too

Note: logs are output to the/var/log directory, which works normally. If logs are output to other sub-directories such as/home, they fail. The specific cause is unknown. In addition, haproxy logs can also be sent to a log server for unified processing. For details, see syslog-ng documentation.

Iii. Restart the syslog service by using the following command:

Service syslog restart

Iv. Edit the configuration file of haproxy. Assume the name is haproxy. cfg.
Add the following code to the logging module (such as global and default:

# Use the system's syslog To record logs (through UDP, the default port number is 514)
Log 127.0.0.1 local0

Note: local0 is consistent in the two configuration files.

This will check the log and it will be OK.

Tail-F/var/log/haproxy. Log

Note: Other log-related issues to be resolved
A. All the current haproxy logs are recorded in one file. Consider storing the logs into different files by date;
B. Logs cannot be output to other directories except/var/log /.

2). Descriptions of some common configuration options

The following is a configuration file that I use. The common descriptions are all explained. For more information, see the configuration.txt file.

######################################## #################################
# Haproxy configuration file
######################################## #################################
 
Global
# Use the system's syslog To record logs (through UDP, the default port number is 514)
Log 127.0.0.1 local0 # info [err warning info debug]
Chroot/home/user/haproxy
 
# Limit the maximum number of connections of a single process
Maxconn 65535
 
# Enable the process to run in the background, that is, run as a daemon. Enable the daemon when the process is officially run. This is prohibited first. It is equivalent to adding the parameter-D to the command line.
# Daemon
# Specify the number of processes to be created when running as a daemon. By default, only one process is created and the daemon mode is required.
# Nbproc 1
 
# Set the debug mode to run, which is mutually exclusive with the daemon mode. It is equivalent to adding the parameter-D to the command line.
# Debug
Pidfile/home/user/haproxy/logs/haproxy. pid # not work
 
Ults
# Allow the current session to be redistributed if the connection fails or is disconnected
Option redispatch
# Set the number of reconnections after a failed connection to a server
Retries 2
# Setting server allocation algorithms
Balance roundrobin
 
# Do not record NULL connections
Option dontlognull
 
# Set the maximum waiting time for successful connection to the server
Timeout connect 5000 Ms
# Set the maximum timeout value for the client
Timeout client 1800000 Ms
# Set the maximum timeout time on the server
Timeout server 1800000 Ms
 
# Enable the sending of TCP keepalive packets on both sides, clients and servers
# Note: when the server's CPU usage is strong, it is best not to enable TTL to reduce resource consumption.
# Option tcpka
 
############################# Statistics page configuration ####### ###########################
 
Listen admin_stat
# Listening port
Bind *: 8011
# Layer-7 http mode
Mode HTTP
Option httplog
Log global
# Automatic refresh time on the statistics page
Stats refresh 30 s
# Statistics page url
Stats uri/admin? Stats
# Prompt text in the password box on the statistics page
Stats realm haproxy \ statistics
# User name and password settings on the statistics page
Stats auth admin: Admin
# Hide the haproxy version on the statistics page
Stats hide-version
 
########################### TCP connection monitoring configuration ######## ########################
 
Listen TCP-in
Bind *: 2211
Mode TCP
# Logging options
Option tcplog
Log global
 
# Backend servers
# Weight -- Adjust the load on the server
# Check -- allow health check on this server
# Inter -- set the time between two consecutive health checks, in milliseconds (MS), default 2000 (MS)
# Rise -- after specifying the number of consecutive successful health checks, you can determine that the server is operable. The default value is 2.
# Fall -- after specifying the number of failed health checks, the server is considered to be in the failed status. The default value is 3.
# Maxconn -- specify the maximum number of concurrent connections that can be sent to the server
Server localhost 0.0.0.0: 2233 weight 3 check inter 2000 rise 2 fall 3
Server 192.168.1.100 192.168.1.100: 2233 weight 3 check inter 2000 rise 2 fall 3
Server 192.168.1.101 192.168.1.101: 2233 weight 3 check inter 2000 rise 2 fall 3
 
######################### HTTP connection monitoring configuration ########## ######################
 
Listen http-in
Bind *: 2212
Mode HTTP
Option httplog
Log global
 
# Set the Health Check Mode
# Option httpchk options * HTTP/1.1 \ r \ nhost: \ www
# Option smtpchk
 
# Backend servers
Server localhost 0.0.0.0: 2234 weight 3 check inter 2000 rise 2 fall 3
Server 192.168.1.100 192.168.1.100: 2234 weight 3 check inter 2000 rise 2 fall 3
Server 192.168.1.101 192.168.1.101: 2234 weight 3 check inter 2000 rise 2 fall 3

In this configuration file, the listen block is used instead of frontend and backend. The listen block defines a complete proxy process and also contains the frontend listening socket and backend server description. At this level, it seems clear to use frontend and backend. However, the test shows that when the listen block is used, the output log records the health status of the backend server, but the frontend and backend logs do not.
We recommend enabling health check. Otherwise, haproxy will not redirect the connection to the next available server when the backend server fails.

Based on the URL of the statistics page configured above, you can access: http: // host_ip: 2212/admin? The stats page is displayed. Note: The port number is the preconfigured http port.

About attributes
Pidfile/home/user/haproxy/logs/haproxy. PID
The job is not working yet. The reason is pending.

 

From: http://blog.ehomy.net/archives/153

 

======================================== 360buy haproxy configuration example = ======================================

Frontend http_80
Bind 0.0.0.0: 80
Log global
ACL index url_end/index.html index.htm index. asp index. aspx index. php index. jsp
Use_backend index_rr if Index
Acl img url_end. gif. jpg. JPEG
Use_backend img_hash if img
ACL statis url_reg \. (CSS | JS | SWF | PNG | CSS ?. * | JS ?. *) $
Use_backend www_any if statis
Default_backend www_any
Backend index_rr
Balance roundrobin
Log global
Cookie serverid
Option httpchk get/robots.txt HTTP/1.1 \ r \ nhost: www.360buy.com
Server www-80-21 192.168.71.21: 80 check inter 3000 rise 1 fall 1 maxconn 65535
Backend img_hash
Balance URI Len 15
Log global
Cookie serverid
Option httpchk get/robots.txt HTTP/1.1 \ r \ nhost: www.360buy.com
Server img-80-51 192.168.71.51: 80 check inter 3000 rise 1 fall 1 maxconn 65535
Backend www_any
Balance URI Len 15
Log global
Cookie serverid
Option httpchk get/robots.txt HTTP/1.1 \ r \ nhost: www.360buy.com
Server www-80-21 192.168.71.21: 80 check inter 3000 rise 1 fall 1 maxconn 65535
[BEIJING] Jimin (497907352) 10:34:24

Round-Robin is performed on index pages with relatively large access volumes, and other pages are hashed to one server to increase the hit rate.

URL Len 15 uses the first 15 digits of the URI as the hash

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.