admin管理员组

文章数量:1642474

本文主要介绍Haproxy负载均衡的安装配置以及结合keepalived保证高可用,概要如下:
-安装haproxy
-配置haporxy
-单台haproxy负载均衡配置
-安装keepalived
-配置keepalived
-haproxy主从配置
-haproxy主从切换测试
-haproxy配置文件详解

安装haproxy

环境说明:

软件下载目录:/opt/soft
安装目录:/opt/haproxy
日志目录:/data/logs/haproxy/haproxy.log
所属用户:haproxy.haproxy

获取haproxy

Haproxy 官网下载地址:http://www.haproxy/

cd /opt/soft
Wget http://www.haproxy/download/1.5/src/haproxy-1.5.15.tar.gz

编译haproxy

编译时指定内核版本target=linux26,你可以# uname –r查看一下本地的系统内核版本。PREFIX=/opt/haproxy是指定haproxy安装路径。

tar -zxvf haproxy-1.5.9.tar.gz
cd haproxy-1.5.9
make TARGET=linux26 PREFIX=/opt/haproxy
make install PREFIX=/opt/haproxy

修改配置文件

Haproxy安装完后默认是没有配置文件的,需要手动vi haproxy.cfg编译一个,或者从源码包目录examples/haproxy.cfg拷贝过来。

cd /opt/haproxy
cp /opt/soft/haproxy-1.5.9/examples/haproxy.cfg /opt/haproxy/

修改配置文件:

vi haproxy.cfg
修改前 修改后
chroot /usr/share/haproxy chroot /usr/share/haproxy
uid 99 uid haproxy
gid 99 gid haproxy
redispatch redispatch
contimeout 5000 timeout connect 5000
clitimeout 50000 timeout client 50000
srvtimeout 50000 timeout server 50000

在daemon下面增加一行:
pidfile /opt/haproxy/haproxy.pid
如图:

可以将文件中的几个listen段删除,并在timeout server 下面增加这一段:

listen  admin_stats 0.0.0.0:8888
       option httplog
        stats refresh 30s
        stats uri /stats
        stats realm Haproxy Manager
        stats auth admin:admin

listen  webserver1 0.0.0.0:8090
        cookie  SERVERID rewrite
        balance roundrobin
        server  app1_1 192.168.253.129:80 cookie app1inst1 check inter 2000 rise
 2 fall 5
        server  app1_2 192.168.253.129:8081 cookie app1inst2 check inter 2000 ri
se 2 fall 5
        server  app1_3 192.168.253.130:8080 cookie app1inst3 check inter 2000 ri
se 2 fall 5
        server  app1_4 192.168.253.131:80 cookie app1inst4 check inter 2000 rise
 2 fall 5

如果不删除的话,则需要将文件71行左右的
srvtimeout 20000 改为 timeout server 20000,
将最后一行(79行左右)
errorfile 503 /etc/haproxy/errors/503.http
改为
errorfile 503 /opt/haproxy/errorfiles/503.http,
并将解压后的源码包目录下的/opt/soft/haproxy-1.5.9/examples/errorfiles拷贝到/opt/haproxy下,即

cp –r /opt/soft/haproxy-1.5.9/examples/errorfiles  /opt/haproxy
vi /opt/haproxy/haproxy.cfg

最后配置文件如下:

# this config needs haproxy-1.1.28 or haproxy-1.2.1
 global
        log 127.0.0.1   local0
        log 127.0.0.1   local1 notice
        #log loghost    local0 info
        maxconn 4096
        #chroot /usr/share/haproxy
        uid haproxy
        gid haproxy
        daemon
        pidfile /opt/haproxy/haproxy.pid
        #debug
        #quiet

defaults
        log     global
        mode    http
        option  httplog
        option  dontlognull
        retries 3
        #redispatch
        maxconn 2000
        timeout connect         5000
        timeout client            50000
                   timeout server                     50000

listen  appli1-rewrite 0.0.0.0:10001
        cookie  SERVERID rewrite
        balance roundrobin

listen  appli2-insert 0.0.0.0:10002
        option  httpchk
        balance roundrobin
        cookie  SERVERID insert indirect nocache
        server  inst1 192.168.114.56:80 cookie server01 check inter 2000 fall 3
        server  inst2 192.168.114.56:81 cookie server02 check inter 2000 fall 3
        capture cookie vgnvisitor= len 32

        option  httpclose               # disable keep-alive

listen  appli3-relais 0.0.0.0:10003
        dispatch 192.168.135.17:80

        option  persist
        server  inst2 192.168.114.56:81 check inter 2000 fall 3 backup

listen  ssl-relay 0.0.0.0:8443
        option  ssl-hello-chk
        balance source
        server  inst1 192.168.110.56:443 check inter 2000 fall 3
        server  inst2 192.168.110.57:443 check inter 2000 fall 3
        server  back1 192.168.120.58:443 backup

listen  appli5-backup 0.0.0.0:10005
        option  httpchk *
        balance roundrobin
        cookie  SERVERID insert indirect nocache
        server  inst1 192.168.114.56:80 cookie server01 check inter 2000 fall 3
        server  inst2 192.168.114.56:81 cookie server02 check inter 2000 fall 3
        server  inst3 192.168.114.57:80 backup check inter 2000 fall 3
        capture cookie ASPSESSION len 32
        timeout server  20000

        option  httpclose               # disable keep-alive
        option  checkcache              # block response if set-cookie & cacheab
le

        rspidel ^Set-cookie:\ IP=       # do not let this cookie tell our intern
al IP address

        errorloc        502     

本文标签: 负载均衡haproxyKeepalived