高可用HAProxy——yum安装部署配置使用

HAProxy介绍

  HAProxy是高性能TCP(第四层)/HTTP(第七层)反向代理负载均衡服务器。(The Reliable, High Performance TCP/HTTP Load Balancer)

HAProxy安装部署

查看列表

$ yum list | grep haproxy
在这里插入图片描述

yum安装

$ yum -y install haproxy
在这里插入图片描述

查看详细信息

$ rpm -qi haproxy
在这里插入图片描述

查看帮助

[root@testHaproxy ~]# haproxy --help
HA-Proxy version 1.5.18 2016/05/10
Copyright 2000-2016 Willy Tarreau <willy@haproxy.org>

Usage : haproxy [-f <cfgfile>]* [ -vdVD ] [ -n <maxconn> ] [ -N <maxpconn> ]
        [ -p <pidfile> ] [ -m <max megs> ] [ -C <dir> ]
        -v displays version ; -vv shows known build options.
        -d enters debug mode ; -db only disables background mode.
        -dM[<byte>] poisons memory with <byte> (defaults to 0x50)
        -V enters verbose mode (disables quiet mode)
        -D goes daemon ; -C changes to <dir> before loading files.
        -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)
        -L set local peer name (default to hostname)
        -p writes pids of all children to this file
        -de disables epoll() usage even when available
        -dp disables poll() usage even when available
        -dS disables splice usage (broken on old kernels)
        -dG disables getaddrinfo() usage
        -dV disables SSL verify on servers side
        -sf/-st [pid ]* finishes/terminates old pids. Must be last arguments.

修改配置文件

$ vim /etc/haproxy/haproxy.cfg

指定配置文件

$ haproxy -f /etc/haproxy/haproxy.cfg -c

启动haproxy

$ service haproxy start

查看状态

$ service haproxy status
在这里插入图片描述
$ /bin/systemctl status haproxy.service
在这里插入图片描述

HAProxy配置文件

  HAProxy配置文件主要由全局设定和代理设定两部分组成,包含5个域:global、default、frontend、backend、listen。

global

# 全局配置,定义haproxy进程的工作特性和全局配置
global
    log         127.0.0.1 local2
    chroot      /var/lib/haproxy #chroot运行的路径
    pidfile     /var/run/haproxy.pid #haproxy pid的存放位置
    maxconn     65536 #最大连接数
    nbproc      10
    ulimit-n    200000 
    user        haproxy #haproxy的运行用户 
    group       haproxy #haproxy的运行用户的所属组
    daemon #守护进程的方式在后台工作
    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats

  全局配置,通常是一些进程级别的配置,与操作系统相关。

default

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
    mode                    http #默认使用的七层协议,也可以是tcp四层协议,如果配置为health,则表示健康检查,返回ok
    log                     global
    option                  tcplog #详细记录tcp日志
    option                  redispatch
    option                  dontlognull #不记录健康检查的日志信息
    option                  forwardfor       
    retries                 3 #重试次数为3次,失败3次以后则表示服务不可用
    timeout http-request    5s #http请求超时时间,客户端建立连接5s但不请求数据的时候,关闭客户端连接
    timeout queue			10s #等待最大时间,表示等待最大时长为10s
    timeout connect         10s #连接超时时间,表示客户端请求转发至服务器所等待的时长为10s
    timeout client          30m #客户端超时时间,表示客户端非活跃状态的时间为30min
    timeout server          30m #服务器超时时间,表示客户端与服务器建立连接后,等待服务器的超时时间为30min
    timeout http-keep-alive 10s #持久连接超时时间,表示保持连接的超时时长为10s
    timeout check           10s #心跳检测超时时间,表示健康状态监测时的超时时间为10s

  默认参数配置,主要是涉及的公共配置,在defaults中一次性添加。frontendbackendlisten未配置时,都可以默认defaults中的参数配置。若配置了,会覆盖。

frontend & backend

frontend  test
    bind *:8082
    default_backend   test
    option httplog

    acl user-core path_beg /test/v1/user/
    use_backend user-core_server if  user-core
# test
backend test
    mode http
    balance   roundrobin
    server node1 10.xxx.xxx.1:7000 check port 7000 inter 5000 rise 5 fall 5
    server node2 10.xxx.xxx.2:7000 check port 7000 inter 5000 rise 5 fall 5

# user-core_server
backend user-core_server
    mode http
    balance  roundrobin
    server   node1  10.xxx.xxx.1:7001 check port 7001 inter 5000 rise 5 fall 5
    server   node2  10.xxx.xxx.2:7001 check port 7001 inter 5000 rise 5 fall 5 backup

frontend haproxy_statis_front
     bind                     *:8081
     mode                     http
     default_backend          statis_haproxy

backend statis_haproxy
     mode                     http
     balance                  roundrobin
     stats                    uri           /haproxy/stats
     stats                    auth          haproxy:zkK_HH@zz
     stats                    refresh       30s
     stats                    show-node
     stats                    show-legends
     stats                    hide-version

    

  frontend可以看作是前端接收请求的部分,内部指定后端;
  backend可以看作是后端服务接收请求的部分;

frontend中acl规则详解

1)语法:acl 自定义的acl名称 acl方法 -i [匹配的路径或文件]
其中:

  • acl:是一个关键字,表示定义ACL规则的开始,后面跟自定义acl名称;
  • -i:忽略大小写,后面跟匹配的路径或者文件的正则表达式;

2)语法:use_backend backend实例名称 if acl规则

  • use_backend:后面跟backend实例名;
  • if:后面跟acl规则名称,表示如果满足acl规则,则请求backend实例;

3)示例:

   acl user-core path_beg /test/v1/user/
   use_backend user-core_server if  user-core

表示符合http://ip:port/test/v1/user会转发user-core_server实例服务;

listen

listen admin_stats
        bind    *:8080  #监听端口
        mode    http
        option  httplog
        log     global
        stats   enable  #统计接口启用开关
        maxconn 10
        stats   refresh 30s  #页面刷新时长
        stats   uri /haproxy?stats  #haproxy ui访问后缀
        stats   realm haproxy  #认证时的realm,作为提示用的
        stats   auth admin:admin  #认证用户名和密码
        stats   hide-version  #隐藏HAProxy版本号
        stats   admin if TRUE  #管理界面只有认证通过后才能在ui上进行管理

  listen是`frontend和backend的组合,haproxy的监控ui可以通过这个进行配置。

haproxy页面

通过上述的listen配置后,重启haproxy。
$ service haproxy restart
通过`http://ip:8080/haproxy?stats访问
在这里插入图片描述
输入认证信息:用户名:admin,密码:admin
在这里插入图片描述

原文地址:https://www.cnblogs.com/Andya/p/12626218.html