RHEL 6.5----haproxy实现负载均衡

主机名 IP
master 192.168.30.130
node-1 192.168.30.131
node-2 192.168.30.132

在master上安装

[root@master ~]# tar -xf haproxy-1.4.22.tar.gz -C /usr/local/src/
[root@master ~]# cd /usr/local/src/
[root@master src]# cd haproxy-1.4.22/
[root@master haproxy-1.4.22]# ls
CHANGELOG  doc     examples  LICENSE   Makefile.bsd  README   src      tests  VERDATE
contrib    ebtree  include   Makefile  Makefile.osx  ROADMAP  SUBVERS  TODO   VERSION
[root@master haproxy-1.4.22]# vim Makefile
PREFIX = /usr/local/haproxy
SBINDIR = $(PREFIX)/sbin
MANDIR = $(PREFIX)/share/man
DOCDIR = $(PREFIX)/doc/haproxy

#### TARGET system
# Use TARGET=<target_name> to optimize for a specifc target OS among the
# following list (use the default "generic" if uncertain) :
#    generic, linux22, linux24, linux24e, linux26, solaris,
#    freebsd, openbsd, cygwin, custom, aix52
TARGET = Linux26
[root@master haproxy-1.4.22]# make install 
.........................
install -d /usr/local/haproxy/sbin
install haproxy /usr/local/haproxy/sbin
install -d /usr/local/haproxy/share/man/man1
install -m 644 doc/haproxy.1 /usr/local/haproxy/share/man/man1
install -d /usr/local/haproxy/doc/haproxy
...

配置haproxy

[root@master haproxy-1.4.22]# ls /usr/local/haproxy/  #可以看出目录下面没有配置文件,需要手动创建
doc  sbin  share
[root@master haproxy-1.4.22]# cd /usr/local/haproxy/
[root@master haproxy]# mkdir etc
[root@master haproxy]# cd etc/
[root@master etc]# touch haproxy.cfg
global
log 127.0.0.1  local0
#log 127.0.0.1  local1 notice
#log loghost    local0 info
maxconn 4096
chroot /usr/local/haproxy
uid 99                          #所属运行的用户uid
gid 99                          #所属运行的用户组
daemon                        #以后台形式运行haproxy
nbproc 1                        #启动1个haproxy实例
pidfile /usr/local/haproxy/haproxy.pid  #将所有进程写入pid文件
#debug
#quiet
defaults
log    global
log    127.0.0.1      local3        #日志文件的输出定向
mode    http                      #所处理的类别,默认采用http模式,可配置成tcp作4层消息转发
option  httplog                        #日志类别
option  httpclose      #每次请求完毕后主动关闭http通道,haproxy不支持keep-alive,只能模拟这>
种模式的实现
option  dontlognull
option  forwardfor      #如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Head
er中获得客户端ip
option  redispatch            #当serverid对应的服务器挂掉后,强制定向到其他健康服务器
retries 2                    #2次连接失败就认为服务器不可用,主要通过后面的check检查
maxconn 2000
balance roundrobin                    #负载均衡算法
stats  uri    /haproxy-stats          #haproxy 监控页面的访问地址
# 可通过 http://localhost:80/haproxy-stats 访问
contimeout      5000              #连接超时时间
clitimeout      50000              #客户端连接超时时间
srvtimeout      50000              #服务器端连接超时时间
listen  localhost 0.0.0.0:80                  #运行的端口及主机名
mode    http
option  httpchk GET /index.html          #健康检测
server  s1 192.168.30.131:80  weight 3 check  #后端的主机 IP &权衡
server  s2 192.168.30.132:80 weight 3 check  #后端的主机 IP &权衡
#server  s3 192.168.148.110:8081 weight 3 check  #后端的主机 IP &权衡

启动和停止haproxy

[root@master ~]# /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/etc/haproxy.cfg 
[root@master ~]# echo $?
0
[root@master ~]# ps -aux | grep haproxy | grep -v grep 
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
nobody     2699  0.0  0.0  12260  1204 ?        Ss   17:42   0:00 /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/etc/haproxy.cfg
重启haproxy
[root@master ~]# /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/etc/haproxy.cfg -st `cat /usr/local/haproxy/haproxy.pid`
[root@master ~]# echo $?
0
[root@master ~]# killall haproxy
[root@master ~]# ps -aux | grep haproxy | grep -v grep 
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ

查看帮助

[root@master ~]# /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/etc/haproxy.cfg  --help
HA-Proxy version 1.4.22 2012/08/09
Copyright 2000-2012 Willy Tarreau <w@1wt.eu>

Usage : haproxy [-f <cfgfile>]* [ -vdVD ] [ -n <maxconn> ] [ -N <maxpconn> ]
        [ -p <pidfile> ] [ -m <max megs> ]
        -v displays version ; -vv shows known build options. 
        -d enters debug mode ; -db only disables background mode.
        -V enters verbose mode (disables quiet mode)
        -D goes daemon
        -q quiet mode : don't display messages
        -c check mode : only check config files and exit
        -n sets the maximum total # of connections (2000)
        -m limits the usable amount of memory (in MB)
        -N sets the default, per-proxy maximum # of connections (2000)
        -p writes pids of all children to this file
        -de disables epoll() usage even when available
        -ds disables speculative epoll() usage even when available
        -dp disables poll() usage even when available
        -sf/-st [pid ]* finishes/terminates old pids. Must be last arguments.

开启日志收集

[root@master ~]# vim /etc/rsyslog.conf 
.........大约在13,14行,开启UDPServerRun 
13 $ModLoad imudp
14 $UDPServerRun 514
..........大约在60行左右
61 local7.*                                                /var/log/boot.log   #在此行下面添加如下两行内容
62 local3.*                                                /var/log/haproxy.log
63 local0.*                                                /var/log/haproxy.log
重启rsyslog服务
[root@master ~]# service rsyslog restart 
Shutting down system logger:                               [  OK  ]
Starting system logger:                                    [  OK  ]

在node-1和node-2上

安装httpd和php
[root@node-1 ~]# yum install -y httpd php 
创建测试页面
[root@node-1 ~]# echo "<h1>Web-Server:192.168.30.131</h1>" > /var/www/html/index.html
[root@node-1 ~]# service httpd restart 
在node-2上安装httpd和php
[root@node-2 ~]# yum install -y httpd php
[root@node-2 ~]# echo "<h1>Web-Server:192.168.30.132</h1>" > /var/www/html/index.html
[root@node-2 ~]# service httpd restart

浏览器测试

按F5刷新

查看haproxy的工作状态

配置文件模板和启动脚本的模板可以在这个目录下查看

[root@master ~]# cd /usr/local/src/haproxy-1.4.22/examples/
[root@master examples]# ls
acl-content-sw.cfg     debug2html                init.haproxy
auth.cfg               debugfind                 init.haproxy.flx0
build.cfg              errorfiles                linux-2.4.21-40.EL-custom.diff
check                  examples.cfg              option-http_proxy.cfg
check.conf             haproxy-1.1.21-flx.1.pkg  rc.highsock
config.rc.haproxy      haproxy.cfg               stats_haproxy.sh
content-sw-sample.cfg  haproxy.init              tarpit.cfg
cttproxy-src.cfg       haproxy.spec              test-section-kw.cfg
debug2ansi             haproxy.vim               url-switching.cfg
比如创建开机启动
[root@master examples]# cp haproxy.init /etc/init.d/haproxy
[root@master examples]# chkconfig --add haproxy
[root@master examples]# chkconfig haproxy on
[root@master examples]# chkconfig haproxy --list 
haproxy            0:off    1:off    2:on    3:on    4:on    5:on    6:off
原文地址:https://www.cnblogs.com/zd520pyx1314/p/9130270.html