Load balancing for TCP layer using Haproxy

Source: Internet
Author: User
Tags haproxy rsyslog

First download Haproxy package: Source Installation

wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.13.tar.gz

wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.24.tar.gz

Installation: Tar zxvf haproxy-1.4.13.tar.gz

Make Target=linux26 Prefix=/usr/local/haproxy # #我的系统内核为2.6, so target=linux26

Make install Prefix=/usr/local/haproxy #

I am the direct yum installation

Yum Install Haproxy

Add non-native IP bonding support

Reference vi/etc/sysctl.conf

Net.ipv4.ip_nonlocal_bind = 1 sysctl–p

If you are using an extranet, you do not need to use this

If the intranet is used and the backend service has a bound IP address, this step must be used #

Then configure:

Vi/etc/haproxy/haproxy.cfg

Global
Daemon
Nbproc 1
Pidfile/var/run/haproxy.pid # #默认配置

Defaults
Mode http #默认的模式mode {tcp|http|health},tcp is Layer 4, HTTP is 7 layer, health will only return OK
Retries 2 #两次连接失败就认为是服务器不可用, can also be set by the following
Option Redispatch #当serverId对应的服务器挂掉后, Force-directed to other healthy servers
Option Abortonclose #当服务器负载很高的时候 to automatically end the link that is being processed for the current queue
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] # #统计页面配置

Listen Admin_stats
Bind 0.0.0.0:1080 #监听端口
Mode HTTP #http的7层模式
Option Httplog #采用http日志格式
#log 127.0.0.1 Local0 Err
Maxconn 10
Stats Refresh 30s #统计页面自动刷新时间
Stats Uri/stats #统计页面url
Stats Realm xingcloud\ Haproxy #统计页面密码框上提示文本
Stats Auth Admin:admin #统计页面用户名和密码设置
Stats hide-version #隐藏统计页面上HAProxy的版本信息# #test1配置

Listen Test1
Bind 0.0.0.0:90
Mode TCP
#maxconn 4086
#log 127.0.0.1 local0 Debug
Server S1 10.18.138.201:80
Server S2 10.18.102.190:80
Server S3 10.18.102.189:80
Server S4 10.18.102.188:80
Server S5 10.18.102.187:80 # #frontend配置 # #test2配置

Listen Test2
Bind 0.0.0.0:91
Mode TCP
#maxconn 4086
#log 127.0.0.1 local0 Debug
Server S1 10.18.138.130:80 weight 1
Server s2 10.18.138.201:8080 weight 6 # #frontend配置

Ok. Start Haproxy.

Haproxy/etc/haproxy/haproxy.cfg

Try it now and find that the load has been successful.

Configure keepalived

Yum Install keepalived

After the installation is complete, configure the Rsyslog

Vi/etc/rsyslog.d/haproxy.conf

Add the following content:

MODLOADIMUDP modload imudp Udpserverrun 514
local0.*/var/log/haproxy.log

Save, restart Rsyslogd.

Restart RSYSLOGD

OK. Now you can see the log.

Try.

Tail-f/var/log/haproxy.log

Finally, add a smooth restart command:

Haproxy-f/ETC/HAPROXY/HAPROXY.CFG-SF Cat/var/run/haproxy.pid

Related Article

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.