HAProxy的安装与应用

HAProxy是一个使用C语言编写的自由及开放源代码软件[1],其提供高可用性、负载均衡,以及基于TCP和HTTP的应用程序代理。这里贴出yum仓库中对haproxy的介绍,这里主要将如何安装应用,对程序不多过多描述.

  : HAProxy is a TCP/HTTP reverse proxy which is particularly suited for
        : high availability environments. Indeed, it can:
        :  - route HTTP requests depending on statically assigned cookies
        :  - spread load among several servers while assuring server persistence
        :    through the use of HTTP cookies
        :  - switch to backup servers in the event a main server fails
        :  - accept connections to special ports dedicated to service monitoring
        :  - stop accepting connections without breaking existing ones
        :  - add, modify, and delete HTTP headers in both directions
        :  - block requests matching particular patterns
        :  - report detailed status to authenticated users from a URI
        :    intercepted by the application

说明一下实验环境配置:
haproxy server: 192.168.1.106
server1: 192.168.1.105
server2: 192.168.1.109
在配置之前要用ntpdate命令对3台机子进行时间同步.

[root@localhost ~]# ntpdate 192.168.1.0
31 May 20:59:49 ntpdate[11432]: no servers can be used, exiting

1.在server上安装web服务:

在两台真实的web服务机器上安装httpd或者nginx,前面的博客有提到了,这里选择安装httpd服务,在两台机器上直接yum安装即可.

[root@localhost ~]# yum -y install httpd

2.配置web服务的主页:
一般后端的主页应该是一致的,这里为了演示效果两台web服务的主页都不一样.

109主机上:

[root@localhost ~]# vim /var/www/html/index.html 

<109@com>
~         

:wq

105主机上:

[root@localhost ~]# vim /var/www/html/index.html 

<105@com>
~                                                                                                
~                                                                                                
~             

:wq

3.关闭防火墙:
防止iptable对实验有影响可以先将两个主机的iptable清空,有必要也可以将firewalld停掉.在105和109主机上运行如下两个命令..

[root@localhost ~]# iptables -F
[root@localhost ~]# systemctl stop firewalld.service 

4.启动服务并检验:
启动httpd服务并在客户端验证配置,在105和109上运行如下命令

[root@localhost ~]# systemctl start httpd
[root@localhost ~]# systemctl enable httpd

在客户端验证,出现如下信息则说明配置成功,提示没有curl命令就自己yum安装一个.

[root@localhost ~]# curl http://192.168.1.109/index.html
<109@com>
[root@localhost ~]# curl http://192.168.1.105/index.html
<105@com>

5.在haproxy服务器端安装:

haproxy在centos6.7之后就默认在base的yum仓库中直接yum安装就可以了,这里我已经安装好了.

[root@localhost ~]# yum -y install haproxy
Loaded plugins: fastestmirror, langpacks
Repository epel is listed more than once in the configuration
Repository epel-debuginfo is listed more than once in the configuration
Repository epel-source is listed more than once in the configuration
base                                                                      | 3.6 kB  00:00:00     
epel                                                                      | 4.7 kB  00:00:00     
extras                                                                    | 3.4 kB  00:00:00     
updates                                                                   | 3.4 kB  00:00:00     
(1/2): epel/x86_64/updateinfo                                             | 930 kB  00:00:02     
(2/2): epel/x86_64/primary_db                                             | 6.4 MB  00:00:02     
Loading mirror speeds from cached hostfile
 * base: mirror.bit.edu.cn
 * epel: mirrors.aliyun.com
 * extras: mirrors.nju.edu.cn
 * updates: mirrors.nju.edu.cn
Package haproxy-1.5.18-7.el7.x86_64 already installed and latest version
Nothing to do

6.查询并配置:

对于刚安装的服务不知道配置文件路径的可以使用rpm命令查询.如下是haproxy服务安装的所有所有文件路径.

[root@localhost ~]# rpm -ql haproxy
/etc/haproxy
/etc/haproxy/haproxy.cfg
/etc/logrotate.d/haproxy
/etc/sysconfig/haproxy
/usr/bin/halog
/usr/bin/iprange
/usr/lib/systemd/system/haproxy.service
/usr/sbin/haproxy
/usr/sbin/haproxy-systemd-wrapper
/usr/share/doc/haproxy-1.5.18
/usr/share/doc/haproxy-1.5.18/CHANGELOG
/usr/share/doc/haproxy-1.5.18/LICENSE
/usr/share/doc/haproxy-1.5.18/README
/usr/share/doc/haproxy-1.5.18/ROADMAP
/usr/share/doc/haproxy-1.5.18/VERSION
/usr/share/doc/haproxy-1.5.18/acl.fig
/usr/share/doc/haproxy-1.5.18/architecture.txt
/usr/share/doc/haproxy-1.5.18/close-options.txt
/usr/share/doc/haproxy-1.5.18/coding-style.txt
/usr/share/doc/haproxy-1.5.18/configuration.txt
/usr/share/doc/haproxy-1.5.18/cookie-options.txt
/usr/share/doc/haproxy-1.5.18/design-thoughts
/usr/share/doc/haproxy-1.5.18/design-thoughts/backends-v0.txt
/usr/share/doc/haproxy-1.5.18/design-thoughts/backends.txt
/usr/share/doc/haproxy-1.5.18/design-thoughts/be-fe-changes.txt
/usr/share/doc/haproxy-1.5.18/design-thoughts/binding-possibilities.txt
/usr/share/doc/haproxy-1.5.18/design-thoughts/buffer-redesign.txt
/usr/share/doc/haproxy-1.5.18/design-thoughts/buffers.fig
/usr/share/doc/haproxy-1.5.18/design-thoughts/config-language.txt
/usr/share/doc/haproxy-1.5.18/design-thoughts/connection-reuse.txt
/usr/share/doc/haproxy-1.5.18/design-thoughts/cttproxy-changes.txt
/usr/share/doc/haproxy-1.5.18/design-thoughts/entities-v2.txt
/usr/share/doc/haproxy-1.5.18/design-thoughts/how-it-works.txt
/usr/share/doc/haproxy-1.5.18/design-thoughts/http_load_time.url
/usr/share/doc/haproxy-1.5.18/design-thoughts/rate-shaping.txt
/usr/share/doc/haproxy-1.5.18/design-thoughts/sess_par_sec.txt
/usr/share/doc/haproxy-1.5.18/examples
/usr/share/doc/haproxy-1.5.18/examples/acl-content-sw.cfg
/usr/share/doc/haproxy-1.5.18/examples/auth.cfg
/usr/share/doc/haproxy-1.5.18/examples/build.cfg
/usr/share/doc/haproxy-1.5.18/examples/content-sw-sample.cfg
/usr/share/doc/haproxy-1.5.18/examples/cttproxy-src.cfg
/usr/share/doc/haproxy-1.5.18/examples/examples.cfg
/usr/share/doc/haproxy-1.5.18/examples/haproxy.cfg
/usr/share/doc/haproxy-1.5.18/examples/option-http_proxy.cfg
/usr/share/doc/haproxy-1.5.18/examples/ssl.cfg
/usr/share/doc/haproxy-1.5.18/examples/tarpit.cfg
/usr/share/doc/haproxy-1.5.18/examples/test-section-kw.cfg
/usr/share/doc/haproxy-1.5.18/examples/transparent_proxy.cfg
/usr/share/doc/haproxy-1.5.18/examples/url-switching.cfg
/usr/share/doc/haproxy-1.5.18/gpl.txt
/usr/share/doc/haproxy-1.5.18/haproxy-en.txt
/usr/share/doc/haproxy-1.5.18/haproxy-fr.txt
/usr/share/doc/haproxy-1.5.18/haproxy.1
/usr/share/doc/haproxy-1.5.18/internals
/usr/share/doc/haproxy-1.5.18/internals/acl.txt
/usr/share/doc/haproxy-1.5.18/internals/body-parsing.txt
/usr/share/doc/haproxy-1.5.18/internals/buffer-operations.txt
/usr/share/doc/haproxy-1.5.18/internals/buffer-ops.fig
/usr/share/doc/haproxy-1.5.18/internals/connect-status.txt
/usr/share/doc/haproxy-1.5.18/internals/connection-header.txt
/usr/share/doc/haproxy-1.5.18/internals/connection-scale.txt
/usr/share/doc/haproxy-1.5.18/internals/entities.fig
/usr/share/doc/haproxy-1.5.18/internals/entities.pdf
/usr/share/doc/haproxy-1.5.18/internals/entities.svg
/usr/share/doc/haproxy-1.5.18/internals/entities.txt
/usr/share/doc/haproxy-1.5.18/internals/hashing.txt
/usr/share/doc/haproxy-1.5.18/internals/header-parser-speed.txt
/usr/share/doc/haproxy-1.5.18/internals/header-tree.txt
/usr/share/doc/haproxy-1.5.18/internals/http-cookies.txt
/usr/share/doc/haproxy-1.5.18/internals/http-docs.txt
/usr/share/doc/haproxy-1.5.18/internals/http-parsing.txt
/usr/share/doc/haproxy-1.5.18/internals/naming.txt
/usr/share/doc/haproxy-1.5.18/internals/pattern.dia
/usr/share/doc/haproxy-1.5.18/internals/pattern.pdf
/usr/share/doc/haproxy-1.5.18/internals/polling-states.fig
/usr/share/doc/haproxy-1.5.18/internals/repartition-be-fe-fi.txt
/usr/share/doc/haproxy-1.5.18/internals/sequence.fig
/usr/share/doc/haproxy-1.5.18/internals/stats-v2.txt
/usr/share/doc/haproxy-1.5.18/internals/stream-sock-states.fig
/usr/share/doc/haproxy-1.5.18/internals/todo.cttproxy
/usr/share/doc/haproxy-1.5.18/lgpl.txt
/usr/share/doc/haproxy-1.5.18/proxy-protocol.txt
/usr/share/doc/haproxy-1.5.18/queuing.fig
/usr/share/haproxy
/usr/share/haproxy/400.http
/usr/share/haproxy/403.http
/usr/share/haproxy/408.http
/usr/share/haproxy/500.http
/usr/share/haproxy/502.http
/usr/share/haproxy/503.http
/usr/share/haproxy/504.http
/usr/share/haproxy/README
/usr/share/man/man1/halog.1.gz
/usr/share/man/man1/haproxy.1.gz
/var/lib/haproxy

看到有一个/etc/haproxy/haproxy.cfg可以猜测是配置文件了,打开看看

[root@localhost ~]# vim /etc/haproxy/haproxy.cfg 

#---------------------------------------------------------------------
# Example configuration for a possible web application.  See the
# full configuration options online.
#
#   http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    # to have these messages end up in /var/log/haproxy.log you will
    # need to:
    #
    # 1) configure syslog to accept network log events.  This is done
    #    by adding the '-r' option to the SYSLOGD_OPTIONS in
    #    /etc/sysconfig/syslog
    #
    # 2) configure local2 events to go to the /var/log/haproxy.log
    #   file. A line like the following can be added to
    #   /etc/sysconfig/syslog
    #
    #    local2.*                       /var/log/haproxy.log
    #
    log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon

    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend  main *:5000
    acl url_static       path_beg       -i /static /images /javascript /stylesheets
    acl url_static       path_end       -i .jpg .gif .png .css .js

    use_backend static          if url_static
    default_backend             app

#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static
    balance     roundrobin
    server      static 127.0.0.1:4331 check

#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
    balance     roundrobin
    server  app1 127.0.0.1:5001 check
    server  app2 127.0.0.1:5002 check
    server  app3 127.0.0.1:5003 check
    server  app4 127.0.0.1:5004 check

这里先简单说明一下配置参数,之后再详细配置到再进行说明.

global:全局配置段,全局有效

进程及安全配置相关的参数
有chroot, deamon, user,group,uid, gid,log.等等很多参数,详情可以看官方文档,这里简单说说几个.

  log: 定义全局的syslog服务器,最多只能定义两个,使用格式如下:
       log <address> [len <length>] <facility> [max level [min level]]
  facility:收集日志流并统一存放在统一位置,
  
  nbproc <number>:要启动的haproxy的进程数量

  ulimit-n <number>:每个haproxy进程可打开的最大文件数量
因为的配置文件中的log是这样的配置(haproxy没有日志功能)
log         127.0.0.1 local2
所以要在本地本机上 vim /etc/rsyslog.conf  添加如下的配置,监听udp并指定local2的位置,

$ModLoad imudp
$UDPServerRun 514
local2.*                                                /var/log/haproxy.log

然后重启
[root@localhost ~]# systemctl start rsyslog.service 
[root@localhost ~]# ss -uln
State       Recv-Q Send-Q   Local Address:Port                  Peer Address:Port              
UNCONN      0      0                    *:54689                            *:*                  
UNCONN      0      0                    *:59841                            *:*                  
UNCONN      0      0                    *:514                              *:*                  
UNCONN      0      0                    *:57283                            *:*                  
UNCONN      0      0        192.168.122.1:53                               *:*                  
UNCONN      0      0             *%virbr0:67                               *:*                  
UNCONN      0      0                    *:68                               *:*                  
UNCONN      0      0                    *:5353                             *:*                  
UNCONN      0      0            127.0.0.1:323                              *:*                  
UNCONN      0      0                   :::514              #监听了514端口了               :::*                  
UNCONN      0      0                   :::24181                           :::*                  
UNCONN      0      0                  ::1:323  

性能调整相关参数

  maxconn <number>:每个haproxy进程所能接受的最大并发连接数
  maxconnrate <number>:每个haproxy进程每秒种所能创建的最大连接数量
  maxsessrate <number>:在SSL安全套接字下每个haproxy进程每秒种所能创建的最大连接数量
  maxsslconn <number>: 在SSL安全套接字下每个haproxy进程所能接受的最大并发连接数

proxies:代理配置段,近在代理服务器从生效.

defaults:
为frontend, listen, backend提供默认配置

fronted:
前端代理服务器,相当于nginx, server {}

backend:
后端真实服务器,相当于nginx, upstream {}

listen:
同时拥前端和后端

7.配置haproxy服务:
在 /etc/haproxy/haproxy.cfg注释掉默认的代理配置并添加如下的配置.

[root@localhost ~]# vim /etc/haproxy/haproxy.cfg

...省略...

#frontend  main *:5000
#    acl url_static       path_beg       -i /static /images /javascript /stylesheets
#    acl url_static       path_end       -i .jpg .gif .png .css .js
#
#    use_backend static          if url_static
#    default_backend             app

#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
#backend static
#    balance     roundrobin
#    server      static 127.0.0.1:4331 check

#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
#backend app
#    balance     roundrobin
#    server  app1 127.0.0.1:5001 check
#    server  app2 127.0.0.1:5002 check
#    server  app3 127.0.0.1:5003 check
#    server  app4 127.0.0.1:5004 check

frontend testweb  #定义名称为 testweb  的frontend
        bind *:80    #监听所有地址的web服务80端口
        default_backend mywebs  #定义名称为mywebs的backend后端web服务

backend mywebs    #定义名称为mywebs的backend
        balance roundrobin   #定义负载均衡算法
        server srv1 192.168.1.105:80 check
        server srv2 192.168.1.109:80 check
#定义server
# srv1: server的id名称
#check:进行健康检测

8.启动haproxy并检验:

[root@localhost ~]# systemctl enable haproxy
Created symlink from /etc/systemd/system/multi-user.target.wants/haproxy.service to /usr/lib/systemd/system/haproxy.service.


--------------分割线--------------

在客户端上实验
[root@localhost ~]# for i in {1..10}; do curl http://192.168.1.106/; done
<105@com>
<109@com>
<105@com>
<109@com>
<105@com>
<109@com>
<105@com>
<109@com>
<105@com>
<109@com>

到这里就完成了一个简单的haproxy服务的配置.

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,547评论 6 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,399评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,428评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,599评论 1 274
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,612评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,577评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,941评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,603评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,852评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,605评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,693评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,375评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,955评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,936评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,172评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,970评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,414评论 2 342

推荐阅读更多精彩内容

  • 目录: HAProxy是什么 HAProxy的核心能力和关键特性 HAProxy的安装和运行 使用HAProxy搭...
    kelgon阅读 79,748评论 9 159
  • 本文描述的是haproxy做代理,负载均衡,haproxy状态查看,后端主机的上下线,keepalived高可用 ...
    think_lonely阅读 5,606评论 0 7
  • 一.HAProxy介绍 HAProxy: 是法国人Willy Tarreau开发的一个开源软件,是 一款应对客户端...
    楠人帮阅读 1,089评论 0 2
  • 一个假期就这么又没了,也不知道自己干了这什么,看了哪些书,只是依稀记着来时我妈妈在车站接我的背影,走时爸爸在车站送...
    视界_ac37阅读 194评论 0 0
  • 曾几何时,为自己取了个笔名叫做故逝秋,本意秋天终究会过去。那时的我喜欢用文字描述自己的世界,喜欢用诗句勾勒所有的情...
    白运参阅读 260评论 0 0