如何在CentOS 6中安装和配置HAProxy 1.4.24

时间:2019-08-20 17:58:15  来源:igfitidea点击:

HAproxy是广泛使用的基于tcp/http的开放源代码负载均衡软件。

在本教程中,我们将在CentOS 6.4中安装最新的稳定的HAProxy版本1.4.24.

我们建议安装最新的稳定版本1.4.24,请阅读下面给出的发行说明。

配置信息

  • 负载均衡服务器名称: HAProxy
  • 服务器数量: 2(webserver1和webserver2)
  • HAProxy IP地址: 192.241.190.251
  • webserver1 IP地址: 192.168.122.101
  • webserver2 IP地址: 192.168.122.102
  • 服务器运行状况检查文件:healthcheck.txt

以root用户身份登录,在服务器上下载HAProxy

yum install wget gcc gcc-c++ autoconf automake make
wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.24.tar.gz

解压下载包

tar -xvzf haproxy-1.4.24.tar.gz

编译HAProxy

cd haproxy-1.4.24

make TARGET=linux26

make install

在/etc中创建haproxy目录及其配置文件

mkdir -p /etc/haproxy

在/etc/haproxy中创建一个文件aproxy.cfg

在配置文件中,我们添加了两个web服务器 webserver1和webserver2

vi /etc/haproxy/haproxy.cfg

global
        log 127.0.0.1   local0
        log 127.0.0.1   local1 notice
        maxconn 5000
        chroot /var/lib/haproxy
        user haproxy
        group haproxy
        daemon

defaults
        log     global
        mode    http
        option  httplog
        option  dontlognull
        option redispatch
        retries 3
        maxconn 4000
        contimeout      8000
        clitimeout      80000
        srvtimeout      80000

listen LOADBAL  192.241.190.251:80
      mode http
      cookie LOADBAL  insert
      balance roundrobin
      option httpclose
      option forwardfor
      option httpchk HEAD /healthcheck.txt HTTP/1.0
      server webserver1 192.168.122.101:80 cookie webserver1 check
      server webserver2 192.168.122.102:80 cookie webserver2 check
      capture response header Location len 40

listen stats :4997
    mode http
    stats enable
    stats hide-version
    stats realm Haproxy Statistics
    stats uri /
    stats auth haloaduser:your_password

登录到服务器webserver1和webserver2,在工作目录中创建一个healthcheck.txt空白文件。

在这个例子中,我们有两台服务器 webserver1和webserver2

(a)用root登录两台服务器

(b)我们的web应用程序路径位于/var/www/html/application_name

touch /var/www/html/application_name/healthcheck.txt

在负载均衡服务器中启动haproxy服务

haproxy -f /etc/haproxy/haproxy.conf

要停止haproxy服务,首先获取haproxy的pid然后终止它

ps -ef|grep haproxy

kill -9 pidof_haproxy

在配置文件中获取haproxy后台面板

listen stats 192.241.190.251:4997
    mode http
    stats enable
    stats hide-version
    stats realm Haproxy Statistics
    stats uri /
    stats auth haloaduser:your_password

在浏览器中,使用下面链接打开后台面板
http://192.241.190.251:4997

配置中的haloaduser:your_password 是用户名和密码。