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

Posted Luckyzd

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RHEL 6.5----haproxy实现负载均衡相关的知识,希望对你有一定的参考价值。

 

主机名 IP  所需软件 
master 192.168.30.130  
node-1 192.168.30.131  
node-2 192.168.30.132  

 

 

 

 

 

在master上安装

[[email protected] ~]# tar -xf haproxy-1.4.22.tar.gz -C /usr/local/src/
[[email protected] ~]# cd /usr/local/src/
[[email protected] src]# cd haproxy-1.4.22/
[[email protected] 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
[[email protected] 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
[[email protected] 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

[[email protected] haproxy-1.4.22]# ls /usr/local/haproxy/  #可以看出目录下面没有配置文件,需要手动创建
doc  sbin  share
[[email protected] haproxy-1.4.22]# cd /usr/local/haproxy/
[[email protected] haproxy]# mkdir etc
[[email protected] haproxy]# cd etc/
[[email protected] 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

[[email protected] ~]# /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/etc/haproxy.cfg 
[[email protected] ~]# echo $?
0
[[email protected] ~]# 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
[[email protected] ~]# /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/etc/haproxy.cfg -st `cat /usr/local/haproxy/haproxy.pid`
[[email protected] ~]# echo $?
0
[[email protected] ~]# killall haproxy
[[email protected] ~]# ps -aux | grep haproxy | grep -v grep 
Warning: bad syntax, perhaps a bogus -? See /usr/share/doc/procps-3.2.8/FAQ

查看帮助

[[email protected] ~]# /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 : dont 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.

开启日志收集

[[email protected] ~]# 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服务
[[email protected] ~]# service rsyslog restart 
Shutting down system logger:                               [  OK  ]
Starting system logger:                                    [  OK  ]

在node-1和node-2上

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

浏览器测试

技术分享图片

按F5刷新

技术分享图片

查看haproxy的工作状态

技术分享图片

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

[[email protected] ~]# cd /usr/local/src/haproxy-1.4.22/examples/
[[email protected] 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
比如创建开机启动
[[email protected] examples]# cp haproxy.init /etc/init.d/haproxy
[[email protected] examples]# chkconfig --add haproxy
[[email protected] examples]# chkconfig haproxy on
[[email protected] examples]# chkconfig haproxy --list 
haproxy            0:off    1:off    2:on    3:on    4:on    5:on    6:off

 

以上是关于RHEL 6.5----haproxy实现负载均衡的主要内容,如果未能解决你的问题,请参考以下文章

RHEL服务器配置BIND以及实现DNS负载均衡

RHEL 5.4下部署LVS(DR)+keepalived实现高性能高可用负载均衡

RHEL7下Nginx负载均衡配置

RHEL 6.5----Nginx负载均衡

RHEL6 搭建LVS/DR 负载均衡集群 案例

RHEL6 搭建LVS/NAT 负载均衡集群 案例