问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

HAProxy安装和常用命令

创作时间:
作者:
@小白创作中心

HAProxy安装和常用命令

引用
1
来源
1.
http://www.cdweb.net/article/gjedpe.html

HAProxy是一个高性能的TCP/HTTP负载均衡器及代理服务器,广泛应用于Web应用的流量分发和高可用性架构中。本文将详细介绍HAProxy的两种安装方式(YUM和编译)及其配置步骤,帮助读者快速掌握HAProxy的部署和使用。

YUM安装HAProxy

CentOS自带了HAProxy,但可能版本比较老。可以在IUS源上找到稳定版的HAProxy。

配置YUM源

cat > /etc/yum.repos.d/ius-7.repo << EOF
[ius]
name = IUS for Enterprise Linux 7 - $basearch
baseurl = https://repo.ius.io/7/$basearch/
enabled = 1
repo_gpgcheck = 0
gpgcheck = 1
gpgkey = https://repo.ius.io/RPM-GPG-KEY-IUS-7
[ius-debuginfo]
name = IUS for Enterprise Linux 7 - $basearch - Debug
baseurl = https://repo.ius.io/7/$basearch/debug/
enabled = 0
repo_gpgcheck = 0
gpgcheck = 1
gpgkey = https://repo.ius.io/RPM-GPG-KEY-IUS-7
[ius-source]
name = IUS for Enterprise Linux 7 - Source
baseurl = https://repo.ius.io/7/src/
enabled = 0
repo_gpgcheck = 0
gpgcheck = 1
gpgkey = https://repo.ius.io/RPM-GPG-KEY-IUS-7
EOF

清理、更新和快速生成YUM缓存

yum clean all
yum update
yum makecache fast

查看系统中HAProxy版本

yum list | grep haproxy

安装HAProxy 18u

yum install haproxy18u -y

编译安装HAProxy

编译安装HAProxy时,可以借助于PCRE环境,该环境下编译时借助正则表达式分析编译速度会快很多,但是没有该环境也可以安装。

安装PCRE和PCRE开发库

yum -y install pcre pcre-devel

创建HAProxy运行的用户和组

groupadd -r haproxy
useradd -g haproxy -M -s /sbin/nologin haproxy

编译安装HAProxy

# 下载地址: http://www.haproxy.org/
# 解压:
tar zxf haproxy-1.8.23.tar.gz -C /usr/local
# 编译安装:
cd /usr/local/haproxy-1.8.23
make TARGET=linux3100 ARCH=x86_64 PREFIX=/usr/local/haproxy USE_PCRE=1
make install PREFIX=/usr/local/haproxy

说明:

  • make时需要使用TARGET指定内核及版本,可以通过uname -r来查看,版本如下:
- linux22     for Linux 2.2
- linux24     for Linux 2.4 and above (default)
- linux24e    for Linux 2.4 with support for a working epoll (> 0.21)
- linux26     for Linux 2.6 and above
- linux2628   for Linux 2.6.28, 3.x, and above (enables splice and tproxy)
- solaris     for Solaris 8 or 10 (others untested)
- freebsd     for FreeBSD 5 to 10 (others untested)
- netbsd      for NetBSD
- osx         for Mac OS/X
- openbsd     for OpenBSD 5.7 and above
- aix51       for AIX 5.1
- aix52       for AIX 5.2
- cygwin      for Cygwin
- haiku       for Haiku
- generic     for any other OS or version.
- custom      to manually adjust every setting
  • 使用ARCH指定架构,不过ARCH选项可省。使用USE_PCRE=1表示使用PCRE环境编译,加快编译速度。

编译安装完成后,只有3个目录:doc、share和sbin,sbin里面只有一个HAProxy的主程序HAProxy。

管理HAProxy服务

ln -s /usr/local/haproxy/sbin/haproxy /usr/sbin/haproxy
cd /usr/local/haproxy-1.8.23/examples
cp haproxy.init /etc/rc.d/init.d/haproxy
cd /etc/rc.d/init.d
chmod +x haproxy

设置HAProxy相关的配置文件

# 创建配置目录
mkdir /usr/local/haproxy/conf
# 创建主配置文件
touch /usr/local/haproxy/conf/haproxy.cfg
# 创建启动脚本配置目录
mkdir /etc/haproxy/
# 添加配置文件的软连接
ln -s /usr/local/haproxy/conf/haproxy.cfg /etc/haproxy/haproxy.cfg
# 创建日志目录
mkdir /usr/local/haproxy/log
# 创建日志文件
touch /usr/local/haproxy/log/haproxy.log
# 添加软连接
ln -s /usr/local/haproxy/log/haproxy.log /var/log/haproxy.log
# 拷贝错误页面
cp -r /usr/local/haproxy-1.8.23/examples/errorfiles/ /usr/local/haproxy/
# 给错误页面添加软连接
ln -s /usr/local/haproxy/errorfiles /etc/haproxy/errorfiles

配置HAProxy.cfg参数

vim /usr/local/haproxy/conf/haproxy.cfg

#---
# Global settings
#---
global
 log 127.0.0.1 local2   ###[err warning info debug]
 chroot /usr/local/haproxy
 pidfile /var/run/haproxy.pid ###haproxy的pid存放路径,启动进程的用户必须有权限访问此文件
 maxconn 4000     ###最大连接数,默认4000
 user haproxy
 group haproxy
 daemon       ###创建1个进程进入deamon模式运行。此参数要求将运行模式设置为"daemon"

#---
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---
defaults
 mode http    ###默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK
 log global   ###采用全局定义的日志
 option dontlognull  ###不记录健康检查的日志信息
 option httpclose  ###每次请求完毕后主动关闭http通道
 option httplog   ###日志类别http日志格式
 option forwardfor  ###如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip
 option redispatch  ###serverId对应的服务器挂掉后,强制定向到其他健康的服务器
 timeout connect 10000 #default 10 second timeout if a backend is not found
 timeout client 300000 ###客户端连接超时
 timeout server 300000 ###服务器连接超时
 maxconn  60000  ###最大连接数
 retries  3   ###3次连接失败就认为服务不可用,也可以通过后面设置

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

#---
# main frontend which proxys to the backends
#---
frontend main
 bind 0.0.0.0:80
 acl url_static path_beg -i /static /p_w_picpaths /javascript /stylesheets
 acl url_static path_end -i .jpg .gif .png .css .js

 use_backend static if url_static  ###满足策略要求,则响应策略定义的backend页面
 default_backend dynamic   ###不满足则响应backend的默认页面

#---
# static backend for serving up p_w_picpaths, stylesheets and such
#---

backend static
 balance  roundrobin     ###负载均衡模式轮询
 server  static 127.0.0.1:80 check ###后端服务器定义

backend dynamic
 balance roundrobin
 server   websrv1 192.168.180.9:80 check maxconn 2000
 server   websrv2 192.168.180.4:80 check maxconn 2000

#---
# round robin balancing between the various backends
#---

errorfile 403 /etc/haproxy/errorfiles/403.http
errorfile 500 /etc/haproxy/errorfiles/500.http
errorfile 502 /etc/haproxy/errorfiles/502.http
errorfile 503 /etc/haproxy/errorfiles/503.http

启动HAProxy服务

# HAProxy配置文件语法检查
haproxy -c -f /usr/local/haproxy/conf/haproxy.cfg
Configuration file is valid
# HAProxy服务启动
systemctl start haproxy
# HAProxy服务开机自启动
chkconfig --add haproxy
chkconfig haproxy on
chkconfig --list | grep haproxy

设置HAProxy日志

$ModLoad imudp
$UDPServerRun 514
$ModLoad imtcp
$InputTCPServerRun 514
# 添加:
local2.*                                                /var/log/haproxy.log

说明:
以上修改HAProxy日志会同时写入到/var/log/haproxy.log/var/log/messages两个文件里,由于HAProxy日志量很多所以只让其写入到/var/log/haproxy.log文件需要修改/etc/rsyslog.conf文件这个配置:

*.info;mail.none;authpriv.none;cron.none                /var/log/messages
更改为:
*.info;mail.none;authpriv.none;cron.none;local2.none               /var/log/messages      #这样只会写入到/var/log/haproxy.log

修改/etc/sysconfig/rsyslog

# 修改”SYSLOGD_OPTIONS”参数
-c 2 使用兼容模式,默认是 -c 5;-r 开启远程日志;-m 0 标记时间戳,单位是分钟,0表示禁用该功能。
# 完整内容如下
# Options for rsyslogd
# Syslogd options are deprecated since rsyslog v3.
# If you want to use them, switch to compatibility mode 2 by "-c 2"
# See rsyslogd(8) for more details
#SYSLOGD_OPTIONS=""
SYSLOGD_OPTIONS="-c 2 -r -m 0"
#SYSLOGD_OPTIONS="-c 2 -r -m 0"
&~
# 重新启动rsyslog服务
systemctl restart rsyslog.service

测试

打开浏览器打开HAProxy监控页面,192.168.246.168:1080/stats,账号:admin,密码:admin

查看HAProxy的log:tail -f /var/log/haproxy.log

HAProxy常用命令

# 检查配置文件语法
haproxy -c -f /etc/haproxy/haproxy.cfg

# 以daemon模式启动,以systemd管理的daemon模式启动
haproxy -D -f /etc/haproxy/haproxy.cfg [-p /var/run/haproxy.pid]
haproxy -Ds -f /etc/haproxy/haproxy.cfg [-p /var/run/haproxy.pid]

# 启动调试功能,将显示所有连接和处理信息在屏幕
haproxy -d -f /etc/haproxy/haproxy.cfg

# restart。需要使用st选项指定pid列表
haproxy -f /etc/haproxy.cfg [-p /var/run/haproxy.pid] -st `cat /var/run/haproxy.pid`

# graceful restart,即reload。需要使用sf选项指定pid列表
haproxy -f /etc/haproxy.cfg [-p /var/run/haproxy.pid] -sf `cat /var/run/haproxy.pid`

# 显示HAProxy编译和启动信息
haproxy -vv

参考文档

  • HAProxy(一)安装和配置
  • 安装HAProxy和HAProxy命令
© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号