haproxy 安装及主配参数简介

haproxy 入门与安装

haproxy 是一款开源的负载均衡软件,他提供 L4 和 L7 层负载功能,全称为 high availability proxy。

CentOS7.6 中使用 yum 安装使用 haproxy

yum 安装haproxy 就不在赘述

CentOS7.6 中源码编译安装 haproxy

1.yum install wget -y
2.由于 haproxy 高版本依赖 lua5.3 以上的版本,因此编译安装 haproxy 之前,需要提前编译安装 lua
	wget http://www.lua.org/ftp/lua-5.3.5.tar.gz
	#安装编译 lua 所依赖的软件包
	[root@haproxy ~]# yum install libtermcap-devel ncurses-devel libevent-devel readline-devel gcc -y
    [root@haproxy ~]# tar xf lua-5.3.5.tar.gz 
    [root@haproxy ~]# 
    [root@haproxy ~]# cd lua-5.3.5
    [root@haproxy lua-5.3.5]# make linux test
3.	#安装haproxy所需开发包组
	[root@haproxy ~]# yum install gcc gcc-c++ glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel net-tools iotop bc zip unzip zlib zlib-devel lsof ntpdate -y
	#下载一下 haproxy 源码包,地址 
	wget http://www.haproxy.org/download/2.0/src/haproxy-2.0.4.tar.gz  
	# 编译安装 haproxy
    root@haproxy ~]# tar xf haproxy-2.0.4.tar.gz 
    [root@haproxy ~]# 
    [root@haproxy ~]# cd haproxy-2.0.4
    
    root@haproxy haproxy-2.0.4]# make ARCH=x86_64 TARGET=linux-glibc USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 USE_CPU_AFFINITY=1 USE_LUA=1 LUA_INC=/root/lua-5.3.5/src LUA_LIB=/root/lua-5.3.5/src PREFIX=/usr/local/haproxy 
    
	[root@haproxy haproxy-2.0.4]# make install PREFIX=/usr/local/haproxy
	
	[root@haproxy haproxy-2.0.4]# cp haproxy /usr/sbin/
	
4. 	#创建 haproxy 的 systemd 配置文件
    [root@haproxy ~]# vim /usr/lib/systemd/system/haproxy.service
    
    [unit]
    Description=HAProxy Load Balancer
    After=syslog.target network.target
    
    [Service]
    ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q
    ExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/lib/haproxy/haproxy.pid
    ExecReload=/bin/kill -USR2 $MAINPID

    [Install]
    WantedBy=multi-user.target
    [root@haproxy ~]# 
    [root@haproxy ~]# systemctl daemon-reload
    [root@haproxy ~]#
5. #创建 haproxy 服务的配置文件
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg

#---------------------------------------------------------------------
global
    log         127.0.0.1 local2
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy  #编译的haproxy可能没有user/group的id启动时会报错  需自己创建
    group       haproxy  #  groupadd haproxy
  						 #  useradd -g haproxy haproxy
    daemon
    stats socket /var/lib/haproxy/stats

#---------------------------------------------------------------------
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


#---------------------------------------------------------------------
listen webapps
    bind 192.168.66.7:80
    mode tcp
    log global
    balance static-rr
   # hash-type consistent
    server web1 172.20.21.17:80  weight 2 check inter 3000 fall 2 rise 5
    server web2 172.20.21.27:80 check inter 3000 fall 2 rise 5

# web1/web2的根据自己服务器IP定

6. [root@haproxy ~]# systemctl restart haproxy.service 
[root@haproxy ~]# ss -ntl
State       Recv-Q Send-Q                                    Local Address:Port                                                   Peer Address:Port              
LISTEN      0      50                                                    *:3306                                                              *:*                  
LISTEN      0      128                                                   *:111                                                               *:*                  
LISTEN      0      128                                        192.168.66.7:80                                                                *:*                  
LISTEN      0      128                                                   *:6000                                                              *:*                  
LISTEN      0      128                                                   *:22                                                                *:*                  
LISTEN      0      128                                           127.0.0.1:631                                                               *:*                  
LISTEN      0      100                                           127.0.0.1:25                                                                *:*                  
LISTEN      0      128                                           127.0.0.1:6010                                                              *:*                  
LISTEN      0      128                                           127.0.0.1:6011                                                              *:*                  
LISTEN      0      128                                                  :::111                                                              :::*                  
LISTEN      0      128                                                  :::6000                                                             :::*                  
LISTEN      0      128                                                  :::22                                                               :::*                  
LISTEN      0      128                                                 ::1:631                                                              :::*                  
LISTEN      0      100                                                 ::1:25                                                               :::*                  
LISTEN      0      128                                                 ::1:6010                                                             :::*                  
LISTEN      0      128                                                 ::1:6011                                                             :::*                  
[root@haproxy ~]# 

haproxy 主要配置参数介绍

1. haproxy 的全局配置内容

global  :             表示全局配置内容开始
log     :             定义 rsyslog 配置
chroot  :             锁定 haproxy 进程的运行目录
pidfile :             指定 pid 文件存放路径
maxconn :             指定进程的最大并发连接数
user    :             指定进程的运行者身份
group   :             指定进程的运行组身份
daemon  :             指定进程以守护进程运行 
nbproc  :             指定 haproxy 开启的进程数
cpu-map :             1.8 版本以上的 haproxy 指定进程号和指定cpu绑定
nbthread:             指定每个进程所开启的线程数,默认为 1 个
spread-checks:        分散 tcp 健康监测,防止后台服务器数量过多,压力过大,建议 2-5

2. proxies 配置 haproxy 所代理的主机配置
defaults                    :表示代理配置功能开始
mode       http/tcp         :表示默认采用的代理模式, L4 或者 L7
log        global           :日志配置
option     dontlognull      :是否记录空连接日志
option     http-server-close:开启 http 服务端主动关闭连接功能
option     forwardfor       :开启 http 转发的 X-Forwarded-For 报文头功能
option     redispatch       :当已建立的连接服务器宕机后,强制将用户请求调度到其他健康的服务器
retries                     :连接失败后的重复尝试连接次数
timeout http-request        :设置 http 请求最大请求时长
timeout queue               :连接在等待队列中的最大时长
timeout connect             :设置等待连接成功的最长时间
timeout client              :设置客户端不活跃时间
timeout server              :设置服务器端最大不活跃时间
timeout http-keep-alive     :设置连接的 keepalive 时间
timeout check               :设置已连接的连接检查
maxconn                     :设置前端最大连接数量

3. listen 方式定义代理服务功能
listen   :后面配置一个代理名称,配置区分大小写
bind     :定义本个代理中 haproxy 对外监听的 ip 地址和端口
server   :指定后端被代理的地址和健康监测配置

4. frontend 和 backend
通过前后端分别定义的方式完成代理配置,建议使用上面的 listen 方式,比如:
frontend web
        bind 172.20.21.7:80
        use_backend web_server
backend web_server
        server 172.20.21.17:80
        server 172.20.21.27:80
发布了39 篇原创文章 · 获赞 2 · 访问量 1024

猜你喜欢

转载自blog.csdn.net/weixin_45341507/article/details/103910437