Load Balancer-haproxy Installation configuration

Source: Internet
Author: User
Tags auth bind create directory syslog custom name haproxy
Haproxy provides high availability, load balancing, and proxies based on TCP and HTTP applications, supporting virtual hosting, which is a free, fast, and reliable solution. Haproxy is especially useful for Web sites that are heavily loaded, and often require session-hold or seven-tier processing. The haproxy runs on the current hardware and can support tens of thousands of concurrent connections. and its operating mode makes it easy and safe to integrate into your current architecture, while protecting your Web server from being exposed to the web.
The Haproxy configuration is divided into five sections, as follows:
1. Global: The parameters are process-level, usually related to the operating system. These parameters are usually set only once, and if configured correctly, you do not need to modify them again
2, defaults: Configure default parameters, these parameters can be used to frontend,backend,listen components
3, Frontend: Receive the front-end virtual node of the request, frontend can more rules directly specify the use of the backend backend
4, Backend: Backend service cluster configuration, is a real server, a backend corresponding to one or more entity servers

5. Combination of Listen fronted and backend

First, install the Haproxy

1. Download the latest Haproxy installation package, official website: http://www.haproxy.org, if you cannot access it, you can use the online proxy to access the download. Download: haproxy-1.5.8.tar.gz

2. Upload to the Linux haproxy user root directory and unzip:

TAR-ZXVF haproxy-1.5.8.tar.gz

Create Directory/home/haproxy/haproxy

3. Installation

CD haproxy-1.5.8

Make target=linux26 arch=x86_64 prefix=/home/haproxy/haproxy #将haproxy安装到/home/haproxy/haproxy, TARGET is the specified kernel version

Make install Prefix=/home/haproxy/haproxy

Enter the/home/haproxy/haproxy directory to create the/home/haproxy/haproxy/conf directory, and copy the configuration examples

cp/home/haproxy/haproxy-1.5.8/examples/haproxy.cfg/home/haproxy/haproxy/conf/

4. Modify the configuration

The configuration instructions are as follows: (ref.: http://freehat.blog.51cto.com/1239536/1347882)

########## #全局配置 ######### Global log 127.0.0.1 local0 #[log output configuration, all logs are recorded on this machine via local0 output] log 127.0.0.1 local1 notice #定 Semantic haproxy log level [error warringinfo debug] Daemon #以后台形式运行harpoxy nbproc 1 #设置进程数量 pidfile/home/ha
        Proxy/haproxy/conf/haproxy.pid #haproxy Process pid file ulimit-n 819200 #ulimit number limit maxconn 4096 #默认最大连接数, consider ulimit-n restrictions                    #chroot/usr/share/haproxy #chroot运行路径 uid #运行haproxy user uid GID 99 #运行haproxy user group GID #debug #haproxy debug level, it is recommended to debug #quiet ####### #默认配置 ############ defaults log GL when opening a single process			Obal mode http #默认的模式mode {tcp|http|health},tcp is Layer 4, HTTP is level 7, health will only return OK option Httplog #日志类别, use httplog option Dontlognull #不记录健康检查日志信息 retries 2 #两次连接失败就认为是服务器不可用, or you can set option FORWARDFO by following R #如果后端服务器需要获得客户端真实ip需要配置的参数, you can obtain the client IP option httpclose #每次请求完毕后主动关闭http通道 from the HTTP header, Haproxy does not support keep-alive, only simulates this Implementation of the pattern #option reDispatch #当serverId对应的服务器挂掉后, Force-directed to other healthy servers, will not support option Abortonclose #当服务器负载很高的时候, automatically end the current queue processing more long links
        Maxconn 4096 #默认的最大连接数 Timeout connect 5000ms #连接超时 timeout client 30000ms #客户端超时 Timeout server 30000ms #服务器超时 #timeout check #心跳检测超时 #timeout http-keep-alive10s #默认持久连接超时时间 #ti Meout Http-request 10s #默认http请求超时时间 #timeoutqueue 1m #默认队列超时时间 balance Roundrobin #设置默认负载均衡方 , polling mode #balance Source # Sets the default load-balancing method, similar to Nginx ip_hash #balnace leastconn #设置默认负载均衡方式, minimum number of Connections # # # #
        # # # #统计页面配置 ######## Listen admin_stats bind 0.0.0.0:1080 #设置Frontend和Backend的组合体, name of monitoring Group, custom name as required Mode HTTP #http的7层模式 option Httplog #采用http日志格式 #log 127.0.0.1 Local0 err #错误日志记录 maxconn #默认的最大连接数 stats refresh 30s #统计页面自动刷新时间 stats ur                I/stats#统计页面url Stats Realm xingcloud\ Haproxy #统计页面密码框上提示文本 stats auth admin:admin #设置监控页面的用户和密码: admin, you can set
	Multiple user names stats auth frank:frank #设置监控页面的用户和密码: Frank stats hide-version #隐藏统计页面上HAProxy的版本信息 Stats Admin If TRUE #设置手工启动/disabled, backend server (haproxy-1.4.9 later version) ####### #设置haproxy error page ##### errorfile 403/home/haproxy /haproxy/errorfiles/403.http errorfile 500/home/haproxy/haproxy/errorfiles/500.http errorfile 502/home/haproxy/ Haproxy/errorfiles/502.http errorfile 503/home/haproxy/haproxy/errorfiles/503.http errorfile 504/home/haproxy/ Haproxy/errorfiles/504.http ####### #frontend前端配置 ############## bind *:80 #这里建议使用bind *:80 Way, otherwise do cluster high availability when there is a problem,
    VIP switch to other machines will not be accessible. ACL Web HDR (host)-I www.abc.com #acl后面是规则名称,-i is the domain name to access, ACL img HDR (HOST)-I img.abc.com if access to www.abc.com this domain name is divided
    The scope of the webserver that is sent to the following.
    #如果访问img. abc.com.cn is distributed to imgserver this scope. Use_backend webserver if Web use_backend imgserver if img ####### #backend后端配置############## backend webserver #webserver作用域 mode http balance roundrobin #banlance Roun
    Drobin Polling, balance source Save session value, support Static-rr,leastconn,first,uri and other parameters option httpchk/index.html http/1.0 #健康检查 #检测文件, if distributed to the background index.html access is no longer distributed to it server Web1 10.16.0.9:8085 Cookie 1 weight 5 check Inter rise 2 Fall 3 S erver web2 10.16.0.10:8085 Cookie 2 weight 3 check Inter 1 Rise 2 Fall 3 #cookie 1500 = ServerID 1,check The heartbeat frequency #rise 2 is 2 times correct to think that the server is available, Fall 3 is 3 times the failure considers the server unavailable, weight represents the weight backend imgserver mode http option Httpchk/index. 
    PHP balance roundrobin Server img01 192.168.137.101:80 check Inter 3 fall Server img02 192.168.137.102:80 Check Inter Fall 3 ####### #tcp配置 ################# listen test1 b
        IND 0.0.0.0:90 mode TCP option Tcplog #日志类别 with Tcplog maxconn 4086 #log 127.0.0.1 local0 Debug Server S1 10.18.138.201:80 weight 1 server s2 10.18.102.190:80 weight 1 


5. Plus log Support

# vim/etc/syslog.conf
At the bottom of the increase
local3.*/home/haproxy/haproxy/logs/haproxy.log
local0.*/home/haproxy/haproxy/logs/haproxy.log

#vim/etc/sysconfig/syslog
Modified: syslogd_options= "-r-m 0"
Restart the Log Services service syslog restart

6. Start the service

Start the service:
#/home/haproxy/haproxy/sbin/haproxy-f/home/haproxy/haproxy/conf/haproxy.cfg
Restart Service:
#/home/haproxy/haproxy/sbin/haproxy-f/home/haproxy/haproxy/conf/haproxy.cfg-st ' cat/home/haproxy/haproxy/conf/ Haproxy.pid '
Stop service:
# Killall Haproxy

7. Monitoring

Visit: Http://192.168.101.125:1080/stats


Configuration reference:

Global log 127.0.0.1 local0 log 127.0.0.1 local1 notice #log loghost local0 info ma
        Xconn #uid #gid daemon #debug #quiet defaults log Global
        Mode HTTP option httplog option dontlognull retries 3 Timeout Connect 5000 Timeout client 50000 Timeout server 50000 listen admin_stats bind 0.0.0.0:1080 #设置Fro                  Ntend and backend combination, the name of the Monitoring group, custom name as required mode HTTP #http的7层模式 option Httplog                                              #采用http日志格式 #log 127.0.0.1 local0 err #错误日志记录 maxconn 10 #默认的最大连接数 Stats Refresh 30s #统计页面自动刷新时间 stats uri/stats #统计页面ur
        L Stats Realm xingcloud\ Haproxy #统计页面密码框上提示文本 stats auth admin:admin #设置监控页面的用户和密码: admin, you can set multiple usernames Stats Auth Frank: Frank #设置监控页面的用户和密码: Frank Stats hide-version #隐藏统计页面上HAProxy的版本信息 Stats admin if TRUE
        #设置手工启动/Disable, backend server (haproxy-1.4.9 later version) listen Web 0.0.0.0:8080 cookie ServerID Insert Indirect nocache Balance Roundrobin server app1_1 192.168.100.204:8081 cookie app1inst1 check Inter rise 2 Fall 5 Ser 
        Ver app1_2 192.168.100.208:8081 Cookie app1inst2 check Inter rise 2 Fall 5 Listen tcptest bind 0.0.0.0:5222 Mode TCP option Tcplog #采用tcp日志格式 balance source #log 127.0.0.1 local0 D Ebug server S1 192.168.100.204:7222 weight 1 server s2 192.168.100.208:7222 weight 1

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.