HAProxy安装已经tcp流量转发

高可用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
在这里插入图片描述

HAProxy TCP 负载均衡配置

配置:

global
maxconn 51200
chroot /usr/local/haproxy
uid 99
gid 99
daemon
#quiet
nbproc 1 #进程数
pidfile /usr/local/haproxy/haproxy.pid

defaults
mode http #默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK
#retries 2 #两次连接失败就认为是服务器不可用,也可以通过后面设置
option redispatch #当serverId对应的服务器挂掉后,强制定向到其他健康的服务器
option abortonclose #当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接
timeout connect 20000ms #连接超时
timeout client 30000ms #客户端超时
timeout server 30000ms #服务器超时
#timeout check 2000 #=心跳检测超时
log 127.0.0.1 local0 err #[err warning info debug]
balance roundrobin #负载均衡算法
#option  httplog#日志类别,采用httplog
#option  httpclose   #每次请求完毕后主动关闭http通道,ha-proxy不支持keep-alive,只能模拟这种模式的实现
#option  dontlognull
#option  forwardfor  #如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip

listen admin_stats
bind 0.0.0.0:8888 #监听端口
option httplog #采用http日志格式
stats refresh 30s #统计页面自动刷新时间
stats uri /stats #统计页面url
stats realm Haproxy Manager #统计页面密码框上提示文本
stats auth admin:admin #统计页面用户名和密码设置
#stats hide-version #隐藏统计页面上HAProxy的版本信息a

frontend tcp_front
bind *:25600
mode tcp
log global
option tcplog
timeout client 300s
backlog 4096
maxconn 65000
default_backend tcp_behind

backend tcp_behind
mode  tcp
option tcplog
option log-health-checks
balance roundrobin
server s1 172.168.1.100:25601
timeout connect 1s
timeout queue 5s
timeout server 300s
global     # 全局参数的设置
log  127.0.0.1 local2      
 # log语法:log <address_1>[max_level_1] 
 # 全局的日志配置,使用log关键字,
    # 指定使用127.0.0.1
    # 上的syslog服务中的local0日志设备,记录日志等级为info的日志
    chroot  /usr/local/haproxy          #改变当前工作目录
    pidfile  /usr/local/haproxy/logs/haproxy.pid       #当前进程id文件
    maxconn  4000     #最大连接数
    user  haproxy      #所属用户
    group  haproxy     #所属组
    daemon     #以守护进程方式运行haproxy
    #stats socket /usr/local/haproxy/stats
defaults
mode   tcp   
 #默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK
    log   global       #应用全局的日志配置
option    tcplog                                                             
option   dontlognull  
option   redispatch  
retries  3 
    timeout connect         10s                #连接超时
    timeout client          1m                   #客户端超时
    timeout server          1m                   #服务器端超时
    maxconn                 3000                 #每个进程可用的最大连接数   
listen rabbitmq_local_cluster 10.100.100.171:8082 
mode tcp
    balance     roundrobin                               #负载均衡算法
server  web001 10.100.100.171:7000  check inter 2000 fall 3 weight 30 
 #定义的多个后端
server  web002 10.100.100.171:7001  check inter 2000 fall 3 weight 30 
 #定义的多个后端
listen private_monitoring :8101
    mode http
    option httplog
    stats enable
    stats uri /stats
stats refresh 1s
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值