创建用户并安装依赖包
# selinux会影响haproxy启动,会有'cannot bind UNIX socket (Permission denied)'的报错,请关闭它除非你知道设置selinux规则
sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config
setenforce 0
groupadd -r haproxy
useradd -r -g haproxy -s /sbin/nologin -d /var/lib/haproxy -c "haproxy" haproxy
mkdir /var/lib/haproxy
chown -Rf haproxy:haproxy /var/lib/haproxy
yum install make gcc gcc-c++ openssl openssl-devel pcre pcre-devel systemd systemd-devel -y
下载安装包并安装
curl -LO https://www.haproxy.org/download/2.6/src/haproxy-2.6.6.tar.gz
tar -zxf haproxy-2.6.6.tar.gz && cd haproxy-2.6.6
# 编译以及安装参数请查看'haproxy-2.6.6/Makefile'
make TARGET=linux-glic USE_OPENSSL=1 USE_EPOLL=1 USE_PROMEX=1 USE_SYSTEMD=1
make install PREFIX=/etc/haproxy SBINDIR=/sbin MANDIR=/usr/share/man DOCDIR=/usr/share/doc
- USE_PROMEX=1: 开启 Prometheus exporter
- USE_SYSTEMD=1: 使用systemd管理服务
设置systemd启动
vi /usr/lib/systemd/system/haproxy.service
添加systemd启动文件,内容如下
[Unit]
Description=HAProxy Load Balancer
After=network.target
[Service]
Environment="CONFIG=/etc/haproxy/haproxy.cfg" "PIDFILE=/run/haproxy.pid" "EXTRAOPTS=-S /run/haproxy-master.sock"
ExecStartPre=/usr/sbin/haproxy -f $CONFIG -c -q $EXTRAOPTS
ExecStart=/usr/sbin/haproxy -Ws -f $CONFIG -p $PIDFILE $EXTRAOPTS
ExecReload=/usr/sbin/haproxy -f $CONFIG -c -q $EXTRAOPTS
ExecReload=/bin/kill -USR2 $MAINPID
SuccessExitStatus=143
KillMode=mixed
Restart=always
Type=notify
LimitNOFILE=65535
StandardOutput=null
StandardError=null
[Install]
WantedBy=multi-user.target
systemctl enable haproxy.service
将haproxy加入到开机自启
haproxy配置
请浏览官方文档 http://docs.haproxy.org/2.6/configuration.html
标签:haproxy,systemd,USE,CentOS7,usr,sbin,2.6 From: https://www.cnblogs.com/zhaojli/p/16862671.html